Add Foreign Key Mysql
Query ok 0 rows affected 2 82 sec records.
Add foreign key mysql. A foreign key is a column or group of columns in a table that links to a column or group of columns in another table. In simple words a foreign key is a reference to a primary key in another table. When you add a foreign key constraint to a table using alter table remember to first create an index on the column s referenced by the foreign key. The table containing the foreign key is called the child table and the table containing the candidate key is called the referenced or parent table.
In this tutorial you will learn about mysql foreign key and how to create drop and disable a foreign key constraint. A foreign key is a field or collection of fields in one table that refers to the primary key in another table. Mysql has the ability to enforce a record that exists on a parent table when you are adding modifying data or validate that a record doesn t exist when you are deleting data from your child table leaving your database inconsistent. How to create a foreign key in mysql.
What s the use of foreign key constraint in a mysql. The foreign key places constraints on data in the related tables. Mysql alter table employee table add constraint fk department id foreign key department id references department table department id. What is foreign key in mysql.
Advantage of foreign key. Sql foreign key constraint. Dropping foreign key. In this tutorial you ll learn about foreign key constraint and it s advantages.
Alter table child add foreign key my fk parent id references parent id. A foreign key is a key used to link two tables together.