简体   繁体   English

使用复合主键作为外键

[英]Using Composite Primary Key As Foreign Key

I've always thought that if I were going to use a foreign key that referenced a composite primary key, I'd need to include all the columns of the composite primary key in both tables.我一直认为,如果我要使用引用复合主键的外键,我需要在两个表中包含复合主键的所有列。

I was confused when I inadvertently didn't do this and received no errors.当我无意中没有这样做并且没有收到错误时,我感到很困惑。 In the example below, RoomId ISN'T unique, and yet it's allowed to be used as a foreign key by itself.在下面的示例中,RoomId 不是唯一的,但它可以单独用作外键。

CREATE TABLE Buildings (
BuildingName VARCHAR(4) PRIMARY KEY,
CampusId TINYINT,
StreetAddress VARCHAR(50),
City VARCHAR(30),
State CHAR(2),
Zip CHAR(5)
);

CREATE TABLE Rooms (
RoomId VARCHAR(5),
BuildingName VARCHAR(20) NOT NULL,
RoomType VARCHAR(15),
Capacity INT,
Notes VARCHAR(100),
CONSTRAINT FK_Buildings_Rooms FOREIGN KEY (BuildingName) REFERENCES Buildings(BuildingName),
PRIMARY KEY (RoomId, BuildingName)
);

CREATE TABLE Instructors(
EmployeeId INT AUTO_INCREMENT PRIMARY KEY,
OfficeId VARCHAR(5),
CONSTRAINT Fk_Instructors_Rooms FOREIGN KEY (OfficeId) REFERENCES Rooms(RoomId));

If I switch the order in the composite primary key to (BuildingName, RoomId), then the foreign key declaration produces the expected error: Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'Fk_Instructors_Rooms' in the referenced table 'rooms'如果我将复合主键中的顺序切换为 (BuildingName, RoomId),则外键声明会产生预期的错误: Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'Fk_Instructors_Rooms' in the referenced table 'rooms' Error Code: 1822. Failed to add the foreign key constraint. Missing index for constraint 'Fk_Instructors_Rooms' in the referenced table 'rooms'

MySQL has extended the traditional definition of foreign keys. MySQL 扩展了外键的传统定义。 In most databases, they are limited to unique or primary keys.在大多数数据库中,它们仅限于唯一键或主键。 As with some other "extensions", the documentation explicitly warns against referring to a non-unique key:与其他一些“扩展”一样, 文档明确警告不要引用非唯一键:

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)而不是 NULL 键的外键。

Of course, not including all the components of a composite key means that you are using a non-unique key.当然,不包括复合键的所有组件意味着您使用的是非唯一键。

Your issue with composite primary keys is an interesting one.您的复合主键问题很有趣。 Personally, I would chalk it up as yet another reason to use auto-incremented primary keys.就个人而言,我认为这是使用自动递增主键的另一个原因。 Single columns are less prone to error in foreign key declarations.单列在外键声明中不太容易出错。

Foreign keys do not need to reference unique or primary key fields.外键不需要引用唯一或主键字段。 They can reference the first field(s) of any index (primary, unique, or plain).它们可以引用任何索引(主索引、唯一索引或普通索引)的第一个字段。

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

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