Learn DBMS and MySQL

Our tutorial provides basic and advance concepts of database management and MySQL commands with detailed examples

DBMS Introduction
MySQL Database
MySQL Tables
MySQL Queries
MySQL Clauses
MySQL Conditions
MySQL Keys

MySQL Foreign Key

The foreign key is used to link one or more than one table together. It is also known as the referencing key. A foreign key matches the primary key field of another table. It means a foreign key field in one table refers to the primary key field of the other table. It identifies each row of another table uniquely that maintains the referential integrity in MySQL.

A foreign key makes it possible to create a parent-child relationship with the tables. In this relationship, the parent table holds the initial column values, and column values of child table reference the parent column values. MySQL allows us to define a foreign key constraint on the child table.

MySQL defines the foreign key in two ways:

Syntax

Following are the basic syntax used for defining a foreign key using CREATE TABLE OR ALTER TABLE statement in the MySQL:

            
[CONSTRAINT constraint_name]  
    FOREIGN KEY [foreign_key_name] (col_name, ...)  
    REFERENCES parent_tbl_name (col_name,...)  
    ON DELETE referenceOption  
    ON UPDATE referenceOption  

For example :

Let's move on to Composite Key in MySQL


Share this page on :