Referential Integrity
Referential integrity is a relational model constraint that ensures every Foreign Key value in a child table either matches an existing Primary Key value in the parent table or is NULL. It prevents orphan records — rows that reference data that doesn’t exist.
Enforcement
The DBMS checks referential integrity on every:
- INSERT into child table — FK value must exist in parent
- UPDATE of FK in child — New value must exist in parent
- DELETE from parent — Must not leave orphan children (unless cascade/set null)
- UPDATE of PK in parent — Must not break existing references
Referential Actions
What happens when a referenced parent row changes. See Database Constraints for syntax:
| Action | Effect on child rows |
|---|---|
RESTRICT | Block the operation |
CASCADE | Delete/update child rows to match |
SET NULL | Set FK to NULL |
SET DEFAULT | Set FK to default value |
Violations and Orphans
Without referential integrity:
-- Parent deleted, orphan remains
DELETE FROM customers WHERE id = 1;
-- orders.customer_id = 1 still exists → orphan record
-- Joins return no customer info, aggregations are wrongWith referential integrity, the DELETE is either blocked (RESTRICT) or handled (CASCADE, SET NULL).