相关记录的最佳sql实践

发布于 2024-12-07 04:50:24 字数 372 浏览 0 评论 0原文

您好,我需要能够链接相关应用程序,并正在尝试找出用于保存、更新和删除的最佳实践表结构。

我有下表:

APPLICATION{ApplicationId, Name, Description}

我需要能够说应用程序 1 链接到 2 和 3。因此,如果您打开应用程序 2,您会看到它链接到应用程序 1 和 3。然后应用程序 3 链接到 1 和2.

链接表的最佳表结构是什么?

编辑

我的主要查询是我是否需要为每个连接添加一条记录,即对于应用程序 1、2 和 3,我是否需要 6 条记录? 1→2、1→3、2→1、2→3、3→1、3→2 ??如果不是,返回给定 ID 的所有链接应用程序的最佳查询是什么?

Hi I need be able to link related applications and am trying to work out the best practice table structure for saving, updating and deleting.

I have the following table:

APPLICATION{ApplicationId, Name, Description}

I need to be able to say Application 1 is linked to 2 and 3. Therefore if you open application 2 you'd see that it is linked to application 1 and 3. Then application 3 is linked to 1 and 2.

What is the best table structure for a linked table?

EDIT

My main query is will I need a record for each join ie for applications 1, 2 and 3 would I need 6 records? 1->2, 1->3, 2->1, 2->3, 3->1, 3->2 ?? If not what is the best query to return all linked apps for a given id?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

鸠魁 2024-12-14 04:50:25
application_association
-------------------------
application_1_id
application_2_id
relationship_type
begin_dt
end_dt

使用关系类型来指定应用程序如何相关,并使用日期来指定该关系何时有效

编辑

也许您对“链接”一词的使用存在集体误解。

如果您的意思是“分组”,那么您可能会考虑如下结构:

group
------------------
group_id
name

application_group
-------------------
application_id
group_id

在这里您可以将应用程序放入同一个“组”中,然后当它们位于同一组中时将它们全部查询回来。

application_association
-------------------------
application_1_id
application_2_id
relationship_type
begin_dt
end_dt

use relationship_type to specify how the applications are related, and use the dates to specify when that relationship was valid

edit:

maybe there is a collective misinterpretation of your use of the word 'linked'.

If you instead mean 'grouped' then you might consider a structure like the following:

group
------------------
group_id
name

application_group
-------------------
application_id
group_id

here you can just place applications into the same 'group' and then query them all back when they are in the same group.

夜未央樱花落 2024-12-14 04:50:25

还有两张桌子。

一个用于关联类型:

create table ApplicationAssocType
(
    Id int identity(1,1)
   ,[Description] varchar(128) not null
)

一个用于关联本身:

create table ApplicationAssoc
(
    Id int identity(1,1)
   ,ApplicationId1 int not null references Appliation(ApplicationId)
   ,ApplicationId2 int not null references Appliation(ApplicationId)
   ,ApplicationAssocTypeId int not null references ApplicationAssocType(Id)
)

[编辑] 为了澄清,您需要为每个单独的链接添加一条记录。将与指定应用程序之间的关系相关的任何字段添加到 ApplicationAssoc。

Two more tables.

One for association type:

create table ApplicationAssocType
(
    Id int identity(1,1)
   ,[Description] varchar(128) not null
)

And one for the association itself:

create table ApplicationAssoc
(
    Id int identity(1,1)
   ,ApplicationId1 int not null references Appliation(ApplicationId)
   ,ApplicationId2 int not null references Appliation(ApplicationId)
   ,ApplicationAssocTypeId int not null references ApplicationAssocType(Id)
)

[Edit] To clarify, you'd add a record for each individual link. Add any fields to ApplicationAssoc which pertain to the relationship between the applications specified.

丶视觉 2024-12-14 04:50:25

CREATE TABLE AppLink (App1 int, App2 int)

这可以无限扩展,满足您需要的任意数量的关系。

CREATE TABLE AppLink (App1 int, App2 int)

This is endlessly extendable for as many relations as you need.

孤芳又自赏 2024-12-14 04:50:25

应用程序实体与其自身具有多对多关系,因此您需要另一个表来存储该映射:

APP_Relationship (ApplicationId, RelatedApplicationId)

Application entity has a many to many relationship with itself, so you need another table to store that mapping:

APP_Relationship (ApplicationId, RelatedApplicationId)
断肠人 2024-12-14 04:50:25
LinkedApplication{ID, ApplicationId, LinkedApplicationId}
LinkedApplication{ID, ApplicationId, LinkedApplicationId}
秋风の叶未落 2024-12-14 04:50:25

您需要一个链接表,它基本上允许您在 Application 与其自身之间建立多对多关系。

CREATE TABLE lnkApplication
(ApplicationID1 int, ApplicationID2 int)

GO
ALTER TABLE [dbo].[lnkApplication]  WITH NOCHECK 
ADD  CONSTRAINT [FK_ApplicationLink1] FOREIGN KEY([ApplicationID1])
REFERENCES [dbo].[tblApplication] ([ApplicationID])
GO
ALTER TABLE [dbo].[lnkApplication]  WITH NOCHECK 
ADD  CONSTRAINT [FK_ApplicationLink2] FOREIGN KEY([ApplicationID2])
REFERENCES [dbo].[tblApplication] ([ApplicationID])
GO

You need a link table which basically allows you to have a many-to-many relationship between Application and itself.

CREATE TABLE lnkApplication
(ApplicationID1 int, ApplicationID2 int)

GO
ALTER TABLE [dbo].[lnkApplication]  WITH NOCHECK 
ADD  CONSTRAINT [FK_ApplicationLink1] FOREIGN KEY([ApplicationID1])
REFERENCES [dbo].[tblApplication] ([ApplicationID])
GO
ALTER TABLE [dbo].[lnkApplication]  WITH NOCHECK 
ADD  CONSTRAINT [FK_ApplicationLink2] FOREIGN KEY([ApplicationID2])
REFERENCES [dbo].[tblApplication] ([ApplicationID])
GO
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文