What is the use of SQL FOREIGN KEY Constraint in the database?
What is the use of SQL FOREIGN KEY Constraint in the database?
40521-Sep-2021
Updated on 21-Sep-2021
Home / DeveloperSection / Forums / What is the use of SQL FOREIGN KEY Constraint in the database?
Ashutosh Kumar Verma
27-Sep-2021SQL Foreign 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 table, parent table is 'Customer' and child table is 'Salesman'
Customer table :
Salesman table :
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,