简体   繁体   English

在外键表中使用主键作为外键

[英]Use of Primary Key as Foreign Key in Foreign Key Table

This may sound confusing and or simple but.. 这听起来可能令人困惑和/或简单,但是..

If I use a foreign key from Table B in Table A, which has a separate primary key. 如果我使用表A中表B的外键,该键具有单独的主键。 Do I need to include Table A's primary key as a foreign key in Table B? 是否需要在表B中包括表A的主键作为外键?

Thanks! 谢谢!

======================================================================== ================================================== ======================

EDIT: 编辑:

Okay, let me try and clarify my question a bit. 好吧,让我尝试澄清一下我的问题。

例

In the case above, should I use Taco_ID as a FK in Table 2? 在上述情况下,我应该在表2中将Taco_ID用作FK吗? Or is does it completely unnecessary? 还是完全没有必要?

In general, you don't usually make foreign keys bidirectionally like that. 通常,您通常不会像这样双向创建外键。 If you do, it means that the two tables exist in a 1-to-1 relationship: Each taco has a type, and each taco type can only be used by one taco. 如果这样做,则意味着两个表以1对1的关系存在:每个炸玉米饼都有一个类型,每种炸玉米饼类型只能由一个炸玉米饼使用。 If you have a relationship like this, there's not really any reason to have them in separate tables, they could just be additional columns in the same table. 如果您具有这样的关系,则实际上没有任何理由将它们放在单独的表中,它们可能只是同一表中的其他列。

Normally foreign keys are used for 1-to-many or many-to-many relationships. 通常,外键用于一对多或多对多关系。 A 1-to-many relationship would be if many different tacos can be of the same type. 如果许多不同的玉米饼可以是同一类型,则一对多关系是。 They each have Taco_Type_ID foreign key. 它们每个都有Taco_Type_ID外键。

For a many-to-many relationship, you typically use a separate relation table. 对于多对多关系,通常使用单独的关系表。

CREATE TABLE Taco_Types (
    Taco_ID INT, -- FK to Table1.Taco_ID
    Taco_Type_ID INT, -- FK to Table2.Taco_Type_ID
    PRIMARY KEY (Taco_ID, Taco_Type_ID)
);

foreign keys and primary keys are SOMETIMES related, but not always. 外键和主键有时是相关的,但并非总是如此。 A foreign key in a table literally just means "whatever value is in this field MUST exist in this other table over ---> here". 表中的外键实际上只是意味着“此字段中的任何值都必须在此其他表中--->在这里”。 Whether that value is a PK or not in that other table is irrelevant - it just has to exist, and it has to be unique. 在另一个表中该值是否为PK无关紧要-它必须存在并且必须唯一。 That may fit the bill of being a primary key, but being primary is NOT required. 这可能适合作为主键的账单,但是不需要成为主键。

You can have composite foreign keys, eg in a (silly) address book, the child table you list a person's phone numbers in COULD be keyed with (firstname, lastname) , but that runs into the problem of "ok, which John Smith does this number belong to?". 您可以使用复合外键,例如在(愚蠢的)通讯录中,您可以使用(firstname, lastname)键入您在某个人的电话号码中列出的子表,但这会遇到“确定”的问题,John Smith这样做这个数字属于?”。

In most databases, foreign key references must be to primary keys or unique keys (and NULL s are allowed). 在大多数数据库中,外键引用必须是主键或唯一键(并且允许NULL )。 MySQL recommends this but does not require it : MySQL建议这样做,但不需要

However, the system does not enforce a requirement that the referenced columns be UNIQUE or be declared NOT NULL. 但是,系统不强制要求引用的列为UNIQUE或声明为NOT NULL。 The handling of foreign key references to nonunique keys or keys that contain NULL values is not well defined for operations such as UPDATE or DELETE CASCADE. 对于诸如UPDATE或DELETE CASCADE之类的操作,未很好地定义对非唯一键或包含NULL值的键的外键引用的处理。 You are advised to use foreign keys that reference only UNIQUE (including PRIMARY) and NOT NULL keys. 建议您使用仅引用UNIQUE(包括PRIMARY)和NOT NULL键的外键。

Do you need to reference primary keys for a foreign key relationship? 您需要参考主键来建立外键关系吗? First, you don't even need to declare foreign key relationships. 首先,您甚至不需要声明外键关系。 I think they are important because they allow the database to maintain referential integrity. 我认为它们很重要,因为它们允许数据库保持参照完整性。 But, they are not required. 但是,它们不是必需的。 Nor is there any semantic difference in queries, based on the presence of foreign keys (for instance, NATURAL JOIN does not use them). 基于外键的存在,查询中的语义也没有任何区别(例如, NATURAL JOIN不使用它们)。 The optimize can make use of the declared relationship. 优化可以利用声明的关系。

Second, if you are going to declare the foreign key, I would recommend using the primary key of the referenced table. 其次,如果要声明外键,我建议使用引用表的主键。 That is, after all, one of the major reasons for having primary keys. 毕竟,这就是拥有主键的主要原因之一。

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

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