---
title: "What is the use of SQL FOREIGN KEY Constraint in the database?"  
description: "What is the use of SQL FOREIGN KEY Constraint in the database?"  
author: "Ravi Vishwakarma"  
published: 2021-09-21  
updated: 2021-09-21  
canonical: https://www.mindstick.com/forum/156739/what-is-the-use-of-sql-foreign-key-constraint-in-the-database  
category: "database"  
tags: ["database", "mysql", "sql server", "sql"]  
reading_time: 2 minutes  

---

# What is the use of SQL FOREIGN KEY Constraint in the database?

What is the use of [SQL](https://www.mindstick.com/articles/13115/types-of-keys-in-sql-or-oracle-database) [FOREIGN KEY](https://www.mindstick.com/blog/174/foreign-key-self-reference-constraint) [Constraint in the database](https://www.mindstick.com/forum/156741/why-are-use-the-sql-check-constraint-in-the-database-at-table-creation)?

## Replies

### Reply by Ashutosh Kumar Verma

**SQL [Foreign](https://yourviews.mindstick.com/story/2164/here-are-the-best-foreign-universities-for-higher-education) Key :**

In SQL, Foreign key is used for any one table that is related to a column with the PRIMARY KEY of another table.

The table on which the Foreign Key is there is called the 'child table' and the table it is related to is called the 'parent table'.

The Foreign Key is used to retrieve the record of child table from its parent table. The following example is to retrieve the record from two table with the use of primary key and foreign key,

Lets have two [database](https://www.mindstick.com/articles/12226/use-of-database-in-sencha-extjs-and-insert-record-from-user-form-using-ajax) table, parent table is 'Customer' and child table is 'Salesman'

Customer table : ![What is the use of SQL FOREIGN KEY Constraint in the database?](https://www.mindstick.com/mindstickforums/27fd51ba-9e6e-479c-943b-43ea92383920/images/96a77ebe-a11c-45cc-a642-a652a2bffc35.png)

Salesman table : ![What is the use of SQL FOREIGN KEY Constraint in the database?](https://www.mindstick.com/mindstickforums/27fd51ba-9e6e-479c-943b-43ea92383920/images/b588b4f8-d3d2-4996-b61f-b7a56224b7cd.png)

In the above table 'Customer', the Foreign Key is salesman_id which are as Primary Key in Salesman table.

The following SQL statement is used to retrieve the records from both above tables where customer_id is 3007,

![What is the use of SQL FOREIGN KEY Constraint in the database?](https://www.mindstick.com/mindstickforums/27fd51ba-9e6e-479c-943b-43ea92383920/images/bdde37cc-7bcb-4da5-a1af-b51f0de916ac.png)\


---

Original Source: https://www.mindstick.com/forum/156739/what-is-the-use-of-sql-foreign-key-constraint-in-the-database

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
