
- 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
What do you mean by FOREIGN KEY and how can we use it in MySQL table?
Actually FOREIGN KEY is a column or combination of columns that can be used to set a link between the data in two tables. In other words, we can say that FOREIGN KEY constraint relates to two tables. It is also used to enhance data integrity because PRIMARY KEY of a table is linked to the FOREIGN KEY of other tables.
Syntax
FOREIGN KEY [column_name] REFERENCES [table having Primary Key] ([column_name]);
Here REFERENCES is a keyword; column_name is the list of columns on which FOREIGN KEY is to be set; a table having Primary Key is the name of the table which contains PRIMARY KEY; column_name is the list of columns on which PRIMARY KEY has set.
Example
Suppose we have two tables ‘Customer’ and ‘Orders’. The relationship between both the tables can be established by a foreign key in the table ‘orders’ specified by field ‘Cust_Id’. The query to create both the tables is as follows −
mysql> Create Table Customer(Cust_ID int Primary Key, First_name Varchar(20), Last_name Varchar(20), City Varchar(10)); Query OK, 0 rows affected (0.13 sec) mysql> Create Table Orders(Order_Id Int Primary Key, Product_Name Varchar(25), Orderdate DATE, Cust_ID Int, FOREIGN KEY(Cust_ID) REFERENCES Customer(Cust_id)); Query OK, 0 rows affected (0.13 sec) mysql> Describe Orders; +--------------+-------------+------+-----+---------+-------+ | 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 | | +--------------+-------------+------+-----+---------+-------+ 4 rows in set (0.04 sec)
Here, the table ‘Customer’ is called the Parent table or referenced table and the table ‘Orders’ is called child table or referencing table. A row in the child table must contain values that exist in the parent table. For example, each row in table ‘orders’ must have a ‘Cust_Id’ that exists in the ‘customer’ table.
- Related Articles
- What do you mean by PRIMARY KEY and how can we use it in MySQL table?
- How can we remove FOREIGN KEY constraint from a column of an existing MySQL table?
- How can we add a FOREIGN KEY constraint to the field of an existing MySQL table?
- How can we add FOREIGN KEY constraints to more than one fields of a MySQL table?
- What do you mean MySQL user variables and how can we assign values to\nthem?
- What do you mean by database view and how do MySQL views work?
- How can we assign FOREIGN KEY constraint on multiple columns?
- How to refer primary key as Foreign to various table in MySQL?
- What does it mean by select 1 from MySQL table?
- What do you mean by threshing? Where is it used?
- How to disable MySQL foreign key checks and what are the benefits of\ndisabling it?
- Can we remove a primary key from MySQL table?
- What do you mean by interfaces and services?
- What do you mean by measurement and motion?
- What do you mean by wear and tear?
