简体   繁体   English

使用外键作为主键的设计注意事项

[英]Design considerations to using Foreign Key as Primary Key

Are there any general design considerations (good/bad/neutral) for using a foreign key of one table as the primary key in another table? 使用一个表的外键作为另一个表的主键时,是否有一般的设计考虑(好/坏/中立)?

For example, assume the following tables as part of a film catalogue: 例如,假设以下表格是电影目录的一部分:

titles
------
 id


episodes
--------
 title_id (PK/FK)

Episodes could obviously be done with both an id and a title_id, where id would be the PK and title_id would be UNIQUE, but since title_id is already unique, and, technically, identifies the episode, would there be anything to consider in just using it as the PK? 显然可以使用id和title_id来完成情节,其中id是PK,title_id是UNIQUE,但是由于title_id已经是唯一的,并且从技术上来说,可以识别情节,因此在使用它时是否需要考虑任何因素作为PK? What about in general? 一般情况如何? What design considerations can you see to this? 您可以看到哪些设计注意事项?

Thanks for your thoughts! 感谢您的想法!

The answer to your question is basically the description of the technique known as "shared primary key". 您问题的答案基本上是对称为“共享主键”的技术的描述。 Accordingly, I've replaced the two tags about primary-key and foreign-key with the single tag shared-primary-key. 因此,我用单个标签shared-primary-key替换了关于主键和外键的两个标签。

Shared primary key is a design where the PK of one table is also an FK that references the PK of another table. 共享主键是一种设计,其中一个表的PK也是引用另一个表的PK的FK。 As the tag wiki for shared-primary-key indicates, this is useful for one-to-one relationships, whether they are mandatory or optional. 正如共享主键的标记Wiki所指示的那样,这对于一对一关系非常有用,无论它们是强制性还是可选的。 These relationships are sometimes called IS-A relationships, as in "an automobile is a vehicle". 这些关系有时称为IS-A关系,例如“汽车就是汽车”。 The relationship between vehicles and autos is also known as a class/subclass or type/subtype relationship. 车辆与汽车之间的关系也称为类/子类或类型/子类型关系。

Like any design technique, it has its benefits and its costs. 像任何设计技术一样,它也有其优点和成本。

Edit in reply to comment: 编辑以回复评论:

The biggest benefit to shared primary key is that it enforces the 1-to-1 nature of the relationship. 共享主键的最大好处是,它强制执行关系的一对一性质。 Having this rule enforced in the database is generally more productive than trying to make sure that all the application code follows the rule. 在数据库中强制执行此规则通常比尝试确保所有应用程序代码都遵循该规则更有效率。

A secondary benefit is that is makes the join between the two tables simple and fast. 第二个好处是使两个表之间的连接简单快速。 It's fast (for some database systems) because of the indexes built to support PKs are used by the optimizer to speed up the join. (对于某些数据库系统而言)它的速度很快,因为优化器使用了为支持PK而建立的索引来加快连接速度。

A third benefit is that a third table can reference both of these two tables with the same FK. 第三个好处是,第三个表可以使用相同的FK引用这两个表。

The cost is that there is some programming involved in adding a new entry to both tables. 代价是在两个表中都添加了新的条目需要一些程序。 The PK from the primary table has to be copied into the secondary table, and the system typically won't do this for you. 来自主表的PK必须复制到辅助表中,并且系统通常不会为您执行此操作。 Also, the join, while fast, is not free. 另外,加入虽然很快,但并非免费。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM