FOREIGN KEY in SQL
Make relationships real — every child points at a real parent
A FOREIGN KEY is a column that must contain a value from another table’s primary key (or UNIQUE column). It turns “employees.department should match a department” from a hope into a rule the engine enforces on every write. Orphan rows — children pointing at parents that do not exist — simply become impossible.
Point the child at the parent
FOREIGN KEY (department) REFERENCES departments(dept_id) — the child column must hold values that exist in the parent’s key.
Child writes are validated
INSERT/UPDATE on employees looks the department up in departments: absent parent, rejected child. NULL is allowed — “no department yet” is legal.
Parent deletes are guarded
DELETE of a department that still has employees fails by default (RESTRICT/NO ACTION) — referential integrity blocks orphaning.
Referential actions automate the choice
ON DELETE SET NULL keeps the children parentless but alive; ON DELETE CASCADE removes them with the parent. Choose deliberately — CASCADE is a loaded weapon.
Index the child column
Every parent delete/Update must scan children for references; an index on employees.department turns that scan into a lookup.
| result |
|---|
1INSERT INTO employees (id, name, department, salary, age)2VALUES (7, 'Grace', 'Research', 81000, 31);
| error | |
|---|---|
| ERROR: insert or update on table "employees" violates foreign key constraint "employees_department_fkey" | Detail: Key (department)=(Research) is not present in table "departments". |
There is no Research department, so the engine rejects Grace’s row. To hire into Research you first INSERT the department — the constraint forces the data to stay consistent from day one.
Real parent — accepted
A child row whose parent exists passes the check.
1INSERT INTO employees (id, name, department, salary, age)2VALUES (7, 'Grace', 'Engineering', 81000, 31);
| id | name | department | salary | age |
|---|---|---|---|---|
| 7 | Grace | Engineering | 81000 | 31 |
Missing parent — rejected
No Research department exists, so Grace cannot join it.
1INSERT INTO employees (id, name, department, salary, age)2VALUES (8, 'Grace', 'Research', 81000, 31);
| error |
|---|
| ERROR: insert or update on table "employees" violates foreign key constraint "employees_department_fkey" |
Deleting a referenced parent
Three declared outcomes: block, null out, or cascade.
1DELETE FROM departments WHERE dept_id = 'Sales';2-- RESTRICT (default): ERROR, 2 employees reference it3-- ON DELETE SET NULL: employees.department becomes NULL4-- ON DELETE CASCADE: Bob and Elena are deleted too
| error | detail |
|---|---|
| ERROR: update or delete on table "departments" violates foreign key constraint on "employees" | Detail: Key (dept_id)=(Sales) is still referenced from table "employees". |
NULL foreign key is legal
A missing value is not a broken relationship.
1INSERT INTO employees (id, name, department, salary, age)2VALUES (9, 'Hugo', NULL, 49000, 27);3-- pending placement — allowed
| id | name | department | salary | age |
|---|---|---|---|---|
| 9 | Hugo | null | 49000 | 27 |
Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Take the queries you just visualized and see the data structures that power real databases.