一个表中的多个外键到另一个表中的单个主键是否可以?
假设我有两个表
Products 表:
|ProductID|ProductName|ListPrice|
PrebuiltSystems 表:
|BuldID|Processor|Motherboard|RAM|
处理器、主板等的值都是现有的ProductID
。我现在正在创建 从每个部件名称列到一个 ProductID
的外键关系,并为每个部件创建一堆导航属性和关系行。这样可以吗?
或者是否有某种关系合并/规则,我可以用它来表示所有这些列都是 productID
的外键,而无需创建一对一的关系?
Say I have two tables
Products table:
|ProductID|ProductName|ListPrice|
PrebuiltSystems table:
|BuldID|Processor|Motherboard|RAM|
The values for the Processor, Motherboard etc. are all existing ProductID
's. 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. 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 ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,这是正确的做法。
不,它们都是不同的关系。
请注意,这些是多对一关系,而不是一对一关系,许多
PrebuiltSystems
将具有相同的处理器< /代码>
Yes, this is the right way to do it.
No, they are all different relationships.
Note that these are many-to-one, not one-to-one relationships, many
PrebuiltSystems
will have the sameProcessor
是的,有多个外键是可以的。然而,在你的例子中,我仍然会采取不同的做法;如果每种组件类型都只有一列,那么您就会受到很大的限制。你用多CPU系统做什么?多个硬盘等怎么办?
您应该规范化 PrebuiltSystems 表,以便拥有一个链接表,该链接表创建与产品的 n 对 n 关系(例如,每个产品可以是任意数量的预构建系统的一部分,并且每个预构建系统可以有任意数量的产品在其中)。
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? 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 (e.g. each product can be part of any number of prebuilt systems, and each prebuilt system can have any number of products in it).