多个主键作为单个外键 [英] Multiple Primary Keys as single foreign key

查看:244
本文介绍了多个主键作为单个外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有要求设计2个或更多的表格。
1. SubParts表
2. MainParts表。



一个MainPart可以有多个子部分。我正在考虑做一些类似于

的SubPart表:
ID和名称

MainPart表:
ID和名称

SubPart_MainPart关系表:
MainPart_ID SubPart_Ids(数组或逗号分隔)

有没有办法把多个subpart_id放在关系表的单列中?
或者我应该使用MainPart_D和SUbPart_ID作为关系ID中的组合主键?第二种方法会增加关系表中的大量记录。
当我尝试迭代逗号分隔列(SubPart_Ids)时,第一种方法会增加循环代码。



您是否有其他方法?



感谢您的帮助

解决方案

SubPart是否有一个或多个主要部分?如果只有一个主要部分,那么你所需要的只是一个主要部分和主要部分;其中MainPart ID在SubPart表中。



如果有多个MainParts,那么你需要一个SubPart_MainPart表。这不应该是一个逗号分隔列表。每行应该是MainPart和SubPart之间的一个链接。


I have a requirement to design 2 or more tables. 1. SubParts table 2. MainParts table.

A single MainPart can have multiple subparts. I am thinking of doing something like

SubPart table: Id and Name

MainPart table: Id and Name

SubPart_MainPart relationship table: MainPart_ID SubPart_Ids(array or comma seperated)

Is there a way to put multiple subpart_id in single column in the relationship table? or should I use MainPart_D and SUbPart_ID as combined primary key in the relationship id?

the second approach will increase lot of records in the relationship table. where as the first approach will increase looping the code when I try to iterate the comma separated column(SubPart_Ids).

Do you have any other approach for this?

thanks for your help

解决方案

Does the SubPart have one or multiple mainParts?

If only one MainPart, then all you need is the SubPart and MainPart table; which the MainPart ID in the SubPart table.

If multiple MainParts, then you need a SubPart_MainPart table. This should NOT be a comma seperated list. Each row should be one link between a MainPart and SubPart.

这篇关于多个主键作为单个外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆