How to add foreign keys in MySQL?

The foreign key is used to link one or more tables together. It matches the primary key field of another table to link the two tables. It allows us to create a parent-child relationship with the tables. We can add a foreign key to a table in two ways:

  • Using the CREATE TABLE Statement
  • Using the ALTER TABLE Statement

Following is the syntax to define a foreign key using CREATE TABLE OR ALTER TABLE statement:

  1. [CONSTRAINT constraint_name]    
  2.     FOREIGN KEY [foreign_key_name] (col_name, …)    
  3.     REFERENCES parent_tbl_name (col_name,…)