简体   繁体   English

复合主键中只有一个键作为外键

[英]Only one key from composite primary key as foreign key

In this database, key1 & key2 make up the composite primary key of table4 , but i'm able to add a foreign key to table3 that comprise just key1 .在这个数据库中, key1key2组成了table4的复合主键,但我可以向table4添加一个外键, table3包含key1

Why MySQL allows this?为什么 MySQL 允许这样做? Does the above database design make no sense at all?上面的数据库设计完全没有意义吗?

This answer takes the question's "add a foreign key to table3 " to mean that a FK (foreign key) was added in table3 referencing one of the columns of the composite PK (primary key) of table4 .此答案采用问题的“向table3添加外键”表示在table3中添加了 FK(外键),引用table4的复合 PK(主键)的列之一。 In standard SQL a FK can reference a proper/smaller subset of a PK.在标准 SQL 中,FK 可以引用 PK 的适当/较小子集。

This other answer presumably takes "add a foreign key to table3 " to mean that a FK was added in table4 with one of the columns of the PK referencing table3 .这个另一个答案大概采用“向table3添加外键”表示在table4中添加了 FK,其中 PK 的列之一引用table3 A FK column set in a table is independent of any PK or UNIQUE declarations in it.表中设置的 FK 列独立于其中的任何 PK 或 UNIQUE 声明。


In standard SQL a FK can reference a proper/smaller subset of a PK.在标准 SQL 中,FK 可以引用 PK 的适当/较小子集。

The referenced column list must be declared PRIMARY KEY or UNIQUE.引用的列列表必须声明为 PRIMARY KEY 或 UNIQUE。 (PRIMARY KEY creates a UNIQUE NOT NULL constraint.) (The constraint has to be explicit, even though any set of NOT NULL columns containing a set that is UNIQUE has to be unique.) (PRIMARY KEY 创建一个 UNIQUE NOT NULL 约束。)(该约束必须是显式的,即使任何包含 UNIQUE 集合的 NOT NULL 列集合必须是唯一的。)

Unfortunately MySQL lets you declare a FK referencing a column list that is not UNIQUE.不幸的是,MySQL 允许您声明 FK 引用非 UNIQUE 的列列表。 Even though such a FK or one referencing non-NULL columns (OK in standard SQL) is not implemented properly, and the documentation itself advises not doing it:即使这样的 FK 或一个引用非 NULL 列(在标准 SQL 中正常)没有正确实现,并且文档本身建议不要这样做:

The handling of foreign key references to non-unique 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 keys that are both UNIQUE (or PRIMARY) and NOT NULL.建议您使用仅引用 UNIQUE(或 PRIMARY)和 NOT NULL 键的外键。

(You can ponder just what are and are not the well-defined operations, since the documentation doesn't actually clarify.) (您可以考虑哪些是明确定义的操作,哪些不是,因为文档实际上并没有澄清。)

1.8.2.3 Foreign Key Differences 1.8.2.3 外键差异
13.1.18 CREATE TABLE Syntax 13.1.18 创建表语法
13.1.18.6 Using FOREIGN KEY Constraints 13.1.18.6 使用外键约束

PS Re relational vs SQL PS Re 关系 vs SQL

In the relational model a FK references a CK (candidate key).在关系模型中,FK 引用 CK(候选键)。 A superkey is a unique column set.超级键是唯一的列集。 A CK is a superkey containing no smaller superkey. CK 是不包含更小的超级密钥的超级密钥。 One CK can be called the PK (primary key).一个 CK 可以称为 PK(主键)。 When a column set's values must appear elsewhere we say there is an IND (inclusion dependency).当列集的值必须出现在其他地方时,我们说存在 IND(包含依赖)。 A FK is an IND to a CK. FK 是 CK 的 IND。 When an IND is to a superkey we could call that a "foreign superkey".当 IND 与超级键相关时,我们可以称其为“外来超级键”。

An SQL PK or UNIQUE NOT NULL declares a superkey. SQL PKUNIQUE NOT NULL声明一个超级键。 It is a CK when it does not contain a smaller column set declared as SQL PK or UNIQUE NOT NULL .当它不包含声明为 SQL PKUNIQUE NOT NULL的较小列集时,它是 CK。 SQL FK declares a foreign superkey. SQL FK声明了一个外超键。 So an SQL PK might actually be a relational PK (hence CK) and a UNIQUE NOT NULL might actually be a CK.因此,SQL PK 实际上可能是关系 PK(因此是 CK),而 UNIQUE NOT NULL 实际上可能是 CK。 An SQL FK to one of these actually is a relational FK.其中之一的 SQL FK 实际上关系 FK。

Foreign keys are not dependent of primary keys at all.外键根本不依赖于主键。 In fact, they have nothing to do with primary keys.事实上,它们与主键无关。

A primary keys single purpose is to identify a row uniquely.主键的单一用途是唯一标识一行。

A foreign keys purpose is to make sure that for each entry in the referencing table (the child table) there must be an entry in the referenced table (the parent table).外键的目的是确保对于引用表(子表)中的每个条目,被引用表(父表)中必须有一个条目。 It's only a technical requirement in MySQL that there must be an index (not necessarily a primary key or a unique key, a simple index suffices) on the referenced column.在 MySQL 中只是一个技术要求,在被引用的列上必须有一个索引(不一定是主键或唯一键,一个简单的索引就足够了)。

Therefore your design makes sense, yes.因此,您的设计是有道理的,是的。

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

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