从复合主键只有一个键作为外键 [英] Only one key from composite primary key as foreign key

查看:216
本文介绍了从复合主键只有一个键作为外键的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

$ b

在这个数据库中, key1 & key2 构成 table4 的组合主键,但是我可以添加一个外键给 table3 ,其中包含 key1



为什么MySQL允许这样做?上面的数据库设计是没有意义的吗?

解决方案

TL; DR这不是标准的SQL。 MySQL文档本身建议不要这样做。

在SQL中,FOREIGN KEY声明必须引用声明为PRIMARY KEY或UNIQUE NOT NULL的列表。 (PRIMARY KEY创建一个UNIQUE NOT NULL约束。)(约束必须是显式的,即使任何包含UNIQUE集合的NOT NULL列都必须是唯一的。)

MySQL文档说:


处理包含NULL值的非唯一键或键的外键引用不是为UPDATE或DELETE CASCADE等操作定义良好。建议您使用仅引用UNIQUE(或PRIMARY)和NOT NULL键的外键。


(您可以思考只是什么是和不是明确定义的操作,因为文档没有真正的澄清。)
$ b

1.8.2.3外键差异

13.1.18 CREATE TABLE语法

13.1.18.6使用FOREIGN KEY约束

a>



在关系模型中,FK引用CK(候选键)。 superkey是一个独特的列集。 CK是一个超级密钥,不包含更小的超级密钥。一个CK可以被称为PK(主键)。当一个列集的值必须出现在其他地方,我们说有一个IND(包含依赖)。 FK是CK的IND。当一个IND是一个超级键时,我们可以称之为外键超级键。

SQL PRIMARY KEY声明一个超级键。这是一个PK(因此CK),当它不包含一个较小的UNIQUE NOT NULL列集。 SQL UNIQUE NOT NULL声明一个超级键。当它不包含较小的PRIMARY KEY或UNIQUE NOT NULL列集时,它是一个CK。 SQL FOREIGN KEY声明了一个外键。一个PRIMARY KEY实际上可能是一个PK(因此是CK),而一个UNIQUE NOT NULL实际上可能是一个CK。然后一个SQL FOREIGN KEY实际上是是一个FK。


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.

Why MySQL allows this? Does the above database design make no sense at all?

解决方案

TL;DR It's not standard SQL. MySQL documentation itself advises not doing it.

In SQL a FOREIGN KEY declaration must reference a column list declared PRIMARY KEY or UNIQUE NOT NULL. (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.)

The MySQL documentation says:

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. You are advised to use foreign keys that reference only keys that are both UNIQUE (or PRIMARY) and 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
13.1.18 CREATE TABLE Syntax
13.1.18.6 Using FOREIGN KEY Constraints

In the relational model a FK references a CK (candidate key). A superkey is a unique column set. A CK is a superkey containing no smaller superkey. One CK can be called the PK (primary key). When a column set's values must appear elsewhere we say there is an IND (inclusion dependency). A FK is an IND to a CK. When an IND is to a superkey we could call that a "foreign superkey".

SQL PRIMARY KEY declares a superkey. It is a PK (hence CK) when it does not contain a smaller UNIQUE NOT NULL column set. SQL UNIQUE NOT NULL declares a superkey. It is a CK when it does not contain a smaller PRIMARY KEY or UNIQUE NOT NULL column set. SQL FOREIGN KEY declares a foreign superkey. A PRIMARY KEY might actually be a PK (hence CK) and a UNIQUE NOT NULL might actually be a CK. Then an SQL FOREIGN KEY to these actually is a FK.

这篇关于从复合主键只有一个键作为外键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆