外键是否总是引用另一个表中的唯一键? [英] does foreign key always reference to a unique key in another table?
问题描述
子表中的外键(单列)是否不可能引用具有某些重复值的父键?
Is it not possible that foreign key(single column) in a child table references to a parent key which has some duplicate values?
推荐答案
通过SQL标准,外键必须引用父表的主键或唯一键.如果主键具有多个列,则外键必须具有相同的列数和顺序.因此,外键引用父表中的唯一行;不能重复.
By the SQL standard, a foreign key must reference either the primary key or a unique key of the parent table. If the primary key has multiple columns, the foreign key must have the same number and order of columns. Therefore the foreign key references a unique row in the parent table; there can be no duplicates.
发表您的评论
如果T.A
是主键,则不能,您不能有任何重复项.任何主键都必须是唯一的并且不能为空.因此,如果子表具有引用父级主键的外键,则它必须与一个非空的唯一值匹配,因此在父表中恰好引用了一行.在这种情况下,您不能创建引用多个父行的子行.
If T.A
is a primary key, then no you can't have any duplicates. Any primary key must be unique and non-null. Therefore if the child table has a foreign key referencing the parent's primary key, it must match a non-null, unique value, and therefore references exactly one row in the parent table. In this case you can't make a child row that references multiple parent rows.
您可以创建一个子行,其外键列为NULL,在这种情况下,它不引用父表中的任何行.
You can create a child row whose foreign key column is NULL, in which case it references no row in the parent table.
这篇关于外键是否总是引用另一个表中的唯一键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!