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,
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
SQL 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,