相关记录的最佳sql实践
您好,我需要能够链接相关应用程序,并正在尝试找出用于保存、更新和删除的最佳实践表结构。
我有下表:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
使用关系类型来指定应用程序如何相关,并使用日期来指定该关系何时有效
编辑:
也许您对“链接”一词的使用存在集体误解。
如果您的意思是“分组”,那么您可能会考虑如下结构:
在这里您可以将应用程序放入同一个“组”中,然后当它们位于同一组中时将它们全部查询回来。
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:
here you can just place applications into the same 'group' and then query them all back when they are in the same group.
还有两张桌子。
一个用于关联类型:
一个用于关联本身:
[编辑] 为了澄清,您需要为每个单独的链接添加一条记录。将与指定应用程序之间的关系相关的任何字段添加到 ApplicationAssoc。
Two more tables.
One for association type:
And one for the association itself:
[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.
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.
应用程序实体与其自身具有多对多关系,因此您需要另一个表来存储该映射:
Application entity has a many to many relationship with itself, so you need another table to store that mapping:
您需要一个链接表,它基本上允许您在
Application
与其自身之间建立多对多关系。You need a link table which basically allows you to have a many-to-many relationship between
Application
and itself.