Referential integrity is the property of a relational database in which every foreign key value either matches an existing key in the referenced table or is NULL where the design allows it. Among database fundamentals, it is the guarantee that relationships between tables never point to rows that do not exist, so an order cannot reference a deleted customer and an invoice line cannot belong to a missing invoice.
The rule is usually enforced by the database management system through declared foreign key constraints, although some systems leave it to application code. When the engine enforces it, every insert, update and delete that could break a relationship is checked, and the statement is rejected or propagated according to the actions defined on the constraint.
How referential integrity is enforced
Enforcement works by checking two sides of a relationship: the child table that holds the foreign key and the parent table that holds the referenced primary key or unique key. Any change on either side that would create an orphaned row triggers a check before the change becomes durable.
On the child side, an INSERT or UPDATE of a foreign key column is accepted only when a matching parent row exists. On the parent side, a DELETE or an update of the referenced key is evaluated against the referential action declared on the constraint, which decides whether the operation fails or whether dependent rows are changed as well.
The following table shows which operations the engine must check and what it verifies in each case.
| Operation | Table affected | What the engine verifies |
|---|---|---|
| INSERT child row | Child | Foreign key value exists in the parent key |
| UPDATE child key | Child | New foreign key value exists in the parent key |
| DELETE parent row | Parent | No child rows depend on it, or an action applies |
| UPDATE parent key | Parent | Dependent child rows are handled by the declared action |
| INSERT parent row | Parent | No referential check is needed |
| DELETE child row | Child | No referential check is needed |
To make parent-side checks fast, engines require the referenced columns to carry a primary key or unique constraint, which always has an index. The foreign key columns themselves are not always indexed automatically, and a missing index on the child column is one of the most common causes of slow deletes and lock contention.
Referential actions on delete and update
The SQL standard defines five referential actions that describe what happens to child rows when a parent row is deleted or its key is updated. The choice expresses a business rule, so it should follow from what the relationship means rather than from convenience.
| Action | Effect on child rows | Typical use |
|---|---|---|
| NO ACTION | Statement fails if orphans would remain at check time | Default in most systems; allows deferral |
| RESTRICT | Statement fails immediately if children exist | Master data that must never vanish silently |
| CASCADE | Child rows are deleted or updated with the parent | Composition, such as order lines of an order |
| SET NULL | Foreign key in child rows becomes NULL | Optional links, such as an assigned agent |
| SET DEFAULT | Foreign key in child rows takes its default value | Reassignment to a placeholder parent |
The difference between NO ACTION and RESTRICT is subtle. With NO ACTION, the check can run at the end of the statement or the transaction, so a statement that temporarily breaks and then repairs the relationship can still succeed. RESTRICT checks immediately and does not allow deferral.
CASCADE deserves particular caution. A single delete on a top-level parent can remove thousands of rows across several tables, and chains of cascades can be hard to predict. Many teams therefore reserve cascading deletes for true part-of relationships and use RESTRICT elsewhere.
Referential integrity compared with related constraints
Referential integrity is one of several integrity types that a relational database protects, and it depends on the others to work correctly. The table below places it next to its closest neighbors.
| Integrity type | What it guarantees | Main mechanism |
|---|---|---|
| Entity integrity | Each row is uniquely identifiable | Primary key, NOT NULL |
| Referential integrity | Relationships point to existing rows | Foreign key constraint |
| Domain integrity | Values fit the allowed type and range | Data types, CHECK constraints |
| Business rule integrity | Complex cross-row rules hold | Triggers, stored procedures, application logic |
A foreign key is the declarative tool, and referential integrity is the property it maintains. Triggers can also enforce relationships, but they are procedural, easier to bypass during bulk loads and harder to reason about, so a declared constraint is preferred whenever the engine supports it.
Composite keys add one more detail. When a foreign key spans several columns, the MATCH SIMPLE rule, the default in most systems, skips the check if any column is NULL, while MATCH FULL requires either all columns to be NULL or all to match a parent row.
Designing and operating foreign keys in practice
Good practice is to declare foreign keys in the schema for every relationship that the data model contains, and to let the database enforce them. Application code can be bypassed by scripts, imports, other services and manual fixes, while a constraint applies to every client.
- Index the foreign key columns in child tables to keep parent deletes and joins efficient.
- Choose actions deliberately: CASCADE for composition, RESTRICT for reference data, SET NULL for optional links.
- Use deferrable constraints when a transaction must insert mutually dependent rows in any order.
- Load parents before children during migrations, or disable and then revalidate constraints explicitly.
- Validate after re-enabling constraints, because some engines can mark them as trusted without checking existing rows.
The following decision table summarizes common situations and the usual design response.
| Situation | Recommended approach | Reason |
|---|---|---|
| Order lines belong to one order | ON DELETE CASCADE | Lines have no meaning without the order |
| Orders reference customers | ON DELETE RESTRICT | Deleting a customer must be a deliberate decision |
| Ticket assigned to an employee | ON DELETE SET NULL | The ticket survives when the employee leaves |
| Bulk load of millions of rows | Disable, load, revalidate | Faster load with integrity restored afterward |
| Tables split across services | Application checks and reconciliation | No single engine can enforce the link |
Limitations, trade-offs and common mistakes
Enforcement has a measurable write cost: every child insert performs a lookup in the parent index, and parent deletes scan or probe child tables. In most OLTP workloads this overhead is small compared with the cost of cleaning corrupted data later, but at very high write rates it is noticeable.
Distributed architectures weaken the guarantee. When related data lives in different databases, shards or microservices, no single engine can enforce the relationship, and teams fall back on eventual consistency, compensating actions and periodic reconciliation jobs.
- Omitting constraints for speed and relying on the application, which leads to orphans after the first manual fix.
- Forgetting child-side indexes, which turns a parent delete into a full table scan with long locks.
- Overusing CASCADE, which makes accidental mass deletion possible from a single statement.
- Disabling checks during imports and never revalidating, which leaves the constraint marked but untrusted.
- Referencing non-unique columns, which most engines reject and which signals a modeling error.
Explained without jargon
Think of a library card file. Every loan card says which reader borrowed a book, using a reader number. Referential integrity means every reader number on a loan card belongs to a real reader who is still on file.
The database acts like a careful librarian. It refuses a loan card for a reader who does not exist, and before a reader is removed it either stops the removal or deals with that reader’s loans first. The result is data you can trust, because no record points to something that is missing.
A typical use case
An online retailer stores customers, orders and order lines in three related tables. The orders table has a foreign key to customers with ON DELETE RESTRICT, and the order lines table has a foreign key to orders with ON DELETE CASCADE, with an index on each foreign key column.
When a support agent tries to delete a customer account that still has 14 historical orders, the database rejects the statement, and the agent must anonymize the customer instead, which keeps the accounting history intact. When a draft order is canceled before payment, deleting the order row automatically removes its 6 order lines.
Months later, a data import script with a bug tries to insert 2,000 order lines for an order number that was never created. The foreign key check rejects the whole batch, the error is visible immediately, and the reporting team never sees revenue attributed to a nonexistent order.