A Foreign Key is a very important concept in relational databases. It is used to create a relationship between two tables.
What is a Foreign Key?
A Foreign Key is a column or a set of columns in one table that refers to the Primary Key in another table.
In simple words:
A Foreign Key in Table A points to a Primary Key in Table B.
It ensures that the value in Table A must exist in Table B.
Key Characteristics of a Foreign Key
- Referential Integrity: Ensures that a record in the child table must refer to an existing record in the parent table.
- Duplicates Allowed: A foreign key column can have duplicate values.
- NULL Values: Foreign key columns can contain NULLs (if allowed).
- Multiple Foreign Keys: A table can have multiple foreign keys.
- No Automatic Indexing: Unlike a primary key, a foreign key does not create an index automatically (though you can create one manually).
Example of Foreign Key
Suppose we have two tables:
1. Departments
Table (Parent Table)
dept_id | dept_name |
---|---|
1 | HR |
2 | Finance |
3 | IT |
(dept_id
is the Primary Key)
2. Employees
Table (Child Table)
emp_id | emp_name | dept_id |
---|---|---|
101 | Rahul Sharma | 1 |
102 | Neha Verma | 3 |
103 | Amit Singh | 2 |
In this table:
dept_id
inEmployees
is a Foreign Key.- It references
dept_id
in theDepartments
table. - It ensures that any department assigned to an employee must exist in the
Departments
table.
SQL Syntax to Define a Foreign Key
While creating a table:
CREATE TABLE Employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(100),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES Departments(dept_id)
);
Or, separately after table creation:
ALTER TABLE Employees
ADD CONSTRAINT fk_department
FOREIGN KEY (dept_id) REFERENCES Departments(dept_id);
Important Rules About Foreign Keys
- You cannot insert a value into the child table that doesn’t exist in the parent table.
- If you delete a record from the parent table that is referenced by the child table, it will cause an error unless handled properly using ON DELETE CASCADE or ON DELETE SET NULL.
ON DELETE and ON UPDATE Options
You can define actions to take when the referenced primary key changes or is deleted:
Option | Description |
---|---|
ON DELETE CASCADE | Deletes child records automatically when parent is deleted. |
ON DELETE SET NULL | Sets the foreign key to NULL when parent is deleted. |
ON UPDATE CASCADE | Updates foreign keys in the child table automatically if primary key value changes. |
Example:
CREATE TABLE Employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(100),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES Departments(dept_id)
ON DELETE SET NULL
ON UPDATE CASCADE
);
Primary Key vs Foreign Key
Feature | Primary Key | Foreign Key |
---|---|---|
Uniqueness | Must be unique | Can have duplicates |
NULL Values | Not allowed | Allowed (if business rules allow) |
Purpose | Uniquely identify a record | Maintain relationship between tables |
Table existence | In the same table | Points to another table |
Real-Life Example
Imagine:
- Departments: HR, Finance, IT
- Employees: Assigned to these departments
If a new employee is entered into the Employees
table with a non-existing department ID like 99, the database will reject the insertion because no such department exists — this protects data consistency.
Summary
- A Foreign Key is used to link two tables.
- It enforces referential integrity between records.
- The foreign key value must either match a value in the referenced table’s primary key or be NULL.
- Helps prevent orphan records (records that reference non-existing entities).