
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
How can we assign FOREIGN KEY constraint on multiple columns?
MySQL allows us to add a FOREIGN KEY constraint on multiple columns in a table. The condition is that each Foreign Key in the child table must refer to the different parent table.
Example
Suppose we have a table ‘customer2’ which have a Primary Key constraint on the field ‘cust_unq_id’ as follows −
mysql> describe customer2; +-------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------+------+-----+---------+-------+ | cust_id | int(11) | YES | | NULL | | | First_name | varchar(20) | YES | | NULL | | | Last_name | varchar(20) | YES | | NULL | | | City | varchar(10) | YES | | NULL | | | cust_unq_id | int(11) | NO | PRI | 0 | | +-------------+-------------+------+-----+---------+-------+ 5 rows in set (0.06 sec)
And we have a table orders1 which is already having a Foreign Key constraint on field ‘Cust_id’ referencing to the parent table ‘customer’.
mysql> describe orders1; +--------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+-------------+------+-----+---------+-------+ | order_id | int(11) | NO | PRI | NULL | | | Product_name | varchar(25) | YES | | NULL | | | orderdate | date | YES | | NULL | | | Cust_id | int(11) | YES | MUL | NULL | | | cust_unq_id | int(11) | YES | | NULL | | +--------------+-------------+------+-----+---------+-------+ 5 rows in set (0.04 sec)
Now, with the help of following ALTER TABLE query we can add another foreign key constraint on the field ‘cust_unq_id’ referencing to the parent table ‘customer2’
mysql> Alter table orders1 add FOREIGN KEY(cust_unq_id) REFERENCES Customer2(Cust_unq_id); Query OK, 0 rows affected (0.25 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> describe orders1; +--------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +--------------+-------------+------+-----+---------+-------+ | order_id | int(11) | NO | PRI | NULL | | | Product_name | varchar(25) | YES | | NULL | | | orderdate | date | YES | | NULL | | | Cust_id | int(11) | YES | MUL | NULL | | | cust_unq_id | int(11) | YES | MUL | NULL | | +--------------+-------------+------+-----+---------+-------+ 5 rows in set (0.06 sec)
From the above result set, it can be observed that ‘orders1’ table is having two, one on ‘cust_id’ and other on ‘cust_unq_id’ foreign key constraints.
- Related Articles
- How can we remove composite PRIMARY KEY constraint applied on multiple columns of an existing MySQL table?
- How can we remove FOREIGN KEY constraint from a column of an existing MySQL table?
- How can we set PRIMARY KEY on multiple columns of a MySQL table?
- How can we add a FOREIGN KEY constraint to the field of an existing MySQL table?
- How can we set PRIMARY KEY on multiple columns of an existing MySQL table?
- How can we get sorted output based on multiple columns?
- How can we sort multiple columns in a single query?
- How can we remove PRIMARY KEY constraint from a column of an existing MySQL table?
- How can we update columns values on multiple rows with a single MySQL UPDATE statement?
- MySQL error 1452 - Cannot add or a child row: a foreign key constraint fails
- What do you mean by FOREIGN KEY and how can we use it in MySQL table?
- How can we add FOREIGN KEY constraints to more than one fields of a MySQL table?
- How can we apply the PRIMARY KEY constraint to the field of an existing MySQL table?
- MySQL error 1452 - Cannot add or update a child row: a foreign key constraint fails?
- How can we emulate CHECK CONSTRAINT by using triggers?

Advertisements