• Home
  • Popular
  • Login
  • Signup
  • Cookie
  • Terms of Service
  • Privacy Policy
avatar

Posted by @arijit87


10 Jul, 2024

Updated at 27 Dec, 2024

How to add foreign keys in MySQL?

The foreign key is used to link one or more tables together. We can add a foreign key to a table in two ways:

  1. Using the CREATE TABLE statement
  2. Using the ALTER TABLE statement

?

Following is the syntax to ?create foreign key using CREATE TABLE OR ALTER TABLE statement:

?

[CONSTRAINT?constraint_name]????

??? FOREIGN?KEY?[foreign_key_name]?(col_name,?...)????

??? REFERENCES?parent_tbl_name?(col_name,...)???