How to create a Foreign key in MySql. What’s the use of Foreign key constraint in a MySql. In this tutorial, You’ll learn about Foreign key constraint and it’s advantages in MySql.
In this tutorial, You’ll learn about Foreign key constraint and it’s advantages.
What is Foreign Key in MySql
In simple words, A Foreign key is a reference to a primary key in another table.
Advantage of Foreign Key
i) Foreign key helps in maintaining referential integrity. It means if a value is in the one table then it must also exist in the other table. Any attempt to break this constraint will give an error.
ii) It helps in understanding the relationship between tables just by looking at the table definitions.
iii) When you create a Foreign key it automatically creates an index on that column.
NOTE : Foreign is only supported by InnoDB storage engine. So if you are using MyISAM then you can’t use Foreign key constraint in your tables.
Source: Foreign Key in Mysql : How to Create a Foreign Key in Mysql