Foreign Key in RDBMS

A Foreign Key is a column (or set of columns) in one table that creates a link to the primary key of another table. It establishes a relationship between two tables and enforces referential integrity − ensuring that values in the foreign key column always correspond to valid values in the referenced primary key.

Example

Consider two tables − Employee and Department. The DeptID in the Employee table is a foreign key that references the DeptID primary key in the Department table ?

Employee
EmpID (PK) EmpName EmpAge DeptID (FK)
1 Alice 30 101
2 Bob 25 102
3 Charlie 28 101
Department
DeptID (PK) DeptName DeptZone
101 Engineering East
102 Marketing West

The DeptID in the Department table is the Primary Key. The DeptID in the Employee table is the Foreign Key that references it.

Employee EmpID (PK) EmpName, EmpAge DeptID (FK) Department DeptID (PK) DeptName DeptZone FK → PK

SQL Example

The following SQL creates both tables with a foreign key constraint ?

CREATE TABLE Department (
    DeptID INT PRIMARY KEY,
    DeptName VARCHAR(50),
    DeptZone VARCHAR(50)
);

CREATE TABLE Employee (
    EmpID INT PRIMARY KEY,
    EmpName VARCHAR(50),
    EmpAge INT,
    DeptID INT,
    FOREIGN KEY (DeptID) REFERENCES Department(DeptID)
);

Conclusion

A foreign key links two tables by referencing the primary key of another table, establishing relationships and enforcing referential integrity. Every value in the foreign key column must either match an existing primary key value in the referenced table or be NULL.

Updated on: 2026-03-14T20:10:00+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements