MySQL Add constraint foreign key
MySQL Add constraint foreign key
Add foreign key to MySQL table using alter table syntax and add constraint keyword.
MySQL Add constraint foreign key syntax
ALTER TABLE table_nameB ADD CONSTRAINT constraint_name FOREIGN KEY (column_name) REFERENCES table_nameA (column_name);
Add constraint foreign key example
ALTER TABLE `books` ADD CONSTRAINT fk_product_id FOREIGN KEY (product_id) REFERENCES `products` (id);
Output
table `books` altered.