简体   繁体   English

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

[英]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.

相关问题 SQL中的表能否将多列作为仅引用另一表的一个主键的外键? - Can a table in SQL have multiple columns as foreign keys that refer only to one primary key of another table? 一个表中的多个外键链接到第二个表中的单个主键 - Multiple foreign keys from one table linking to single primary key in second table 从表中使用多个外键选择一个主键 - Select one Primary Key using multiple Foreign Keys from a table 在 SQL 中,如何将一个表的多个列中的多个外键 ID 连接到另一个表中的单个主键和唯一的 select 一个? - In SQL, how to join multiple foreign keys IDs in multiple columns of a table to a single primary key in another table and uniquely select one? 将一个表中的外键数组与另一表中的主键匹配? - Match an array of foreign keys in one table with primary key in another table? SQL 两个外键链接到另一个表的一个主键以从该表中提取字段 - SQL Two foreign keys linked to one primary key of another table to pull field from that table Java/Mysql 从一个表中选择主键值并将它们作为外键插入到另一个表中 - Java/Mysql Selecting Primary Key values from one table and Insert them to another table as Foreign Keys 如何创建具有两个主键的表,其中一个是一个表中的外键,另一个是另一个表中的外键? - how to create a table with two primary keys where one is foreign key in one table and the other on another table? SQL-从三个表中选择数据,其中一个表具有多个到同一个主键的外键 - SQL - Select data from three tables where one table has multiple foreign keys to the same primary key 选择另一个表中没有外键的主键 - Selecting primary keys that do not have foreign keys in another table
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM