一个表中的多个外键到另一个表中的单个主键是否可以?

发布于 2024-12-04 23:32:34 字数 416 浏览 2 评论 0原文

假设我有两个表

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 技术交流群。

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

发布评论

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

评论(2

笨笨の傻瓜 2024-12-11 23:32:34

这样可以吗?

是的,这是正确的做法。

或者我可以用某种关系合并/规则来表示
所有这些列都是productID 的外键

不,它们都是不同的关系。

没有建立一对一的关系?

请注意,这些是多对一关系,而不是一对一关系,许多PrebuiltSystems将具有相同的处理器< /代码>

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

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

苯莒 2024-12-11 23:32:34

是的,有多个外键是可以的。然而,在你的例子中,我仍然会采取不同的做法;如果每种组件类型都只有一列,那么您就会受到很大的限制。你用多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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文