[英]Is it fine to have multiple foreign keys from one table to single primary key in another table?
Say I have two tables 说我有两个桌子
Products table: 产品表:
|ProductID|ProductName|ListPrice|
PrebuiltSystems table: PrebuiltSystems表:
|BuldID|Processor|Motherboard|RAM|
The values for the Processor, Motherboard etc. are all existing ProductID
's. 处理器,主板等的值都是现有的ProductID
。 I am now creating foreign key relationships from each of the part name columns to the one ProductID
and have a bunch of navigation properties and relationships lines being created for each part. 现在,我正在从每个零件名称列到一个ProductID
创建外键关系,并为每个零件创建了一堆导航属性和关系线。 Is this ok? 这个可以吗?
Or is there some kind of relationship merger/rule that I can use to say all those columns are foreign keys to productID
without creating one to one relationships ? 还是可以使用某种关系合并/规则来说所有这些列都是productID
外键,而无需创建一对一的关系?
Is this ok? 这个可以吗?
Yes, this is the right way to do it. 是的,这是正确的方法。
Or is there somekind of relationship merger/rule that I can use to say all those columns are foreign keys to productID 还是可以使用某种关系合并/规则来表示所有这些列都是productID的外键
No, they are all different relationships. 不,他们都是不同的关系。
without creating one to one relationships ? 没有建立一对一的关系?
Note that these are many-to-one , not one-to-one relationships, many PrebuiltSystems
will have the same Processor
请注意,这些是多对一的关系,而不是一对一的关系,许多PrebuiltSystems
将具有相同的Processor
Yes it is fine to have multiple foreign keys. 是的,可以有多个外键。 However, in your example, I'd still do it differently; 但是,在您的示例中,我仍然会以不同的方式进行操作; if you have one column for each component type you limit yourself very much. 如果每种组件类型都有一列,则会限制您自己。 What do you do with a multi-CPU-system? 您如何使用多CPU系统? What with multiple harddisks etc.? 多个硬盘等如何处理?
You should rather normalize the PrebuiltSystems table so that you have a link table which creates a n-to-n relationship to the products (eg each product can be part of any number of prebuilt systems, and each prebuilt system can have any number of products in it). 您应该规范化PrebuiltSystems表,以便您有一个链接表,该表创建与产品的n对n关系(例如,每个产品可以是任意数量的预构建系统的一部分,并且每个预构建系统可以具有任意数量的产品在里面)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.