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.
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'