Add Foreign Key to Existing Table in MySQL


To add a foreign key constraint to an existing table in MySQL use the following query:

ALTER TABLE `myTable` ADD CONSTRAINT fk_myTable_otherTableID FOREIGN KEY (`otherTableID`) REFERENCES `otherTable`(`ID`);



Published:
Last modification: