Composite vs Single Foreign Keys
Posted by: Mike Ferris
Date: March 11, 2016 09:46AM

Given:

CREATE TABLE `person` (
`ID` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45)DEFAULT NULL,
`born_in` int(11) DEFAULT NULL,
`lives_in` int(11) DEFAULT NULL,
PRIMARY KEY (`ID`));

CREATE TABLE `country` (
`id` int(11) NOT NULL,
`name` varchar(45) DEFAULT NULL,
PRIMARY KEY (`ID`));

What is the advantage or disadvantage of using a composite FK vs two single FKs? Keeping two single indexes?

ALTER TABLE `person`
ADD INDEX `person_born_in_country_id_idx` (`born_in`),
ADD INDEX `person_lives_in_country_id_idx` (`lives_in`),
ADD CONSTRAINT `person_born_in_country_id` FOREIGN KEY (`born_in`) REFERENCES `country` (`ID`),
ADD CONSTRAINT `person_lives_in_country_id` FOREIGN KEY (`lives_in`) REFERENCES `country` (`ID`);

vs.

ALTER TABLE `person`
ADD INDEX `person_born_in_country_id_idx` (`born_in`),
ADD INDEX `person_lives_in_country_id_idx` (`lives_in`),
ADD CONSTRAINT `person_born_in_lives_in_country_id` FOREIGN KEY (`born_in`,`lives_in`) REFERENCES `country` (`ID`,'ID');

Options: ReplyQuote


Subject
Written By
Posted
Composite vs Single Foreign Keys
March 11, 2016 09:46AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.