Referential Integrity in DBMS With Example
Referential integrity is a database rule that ensures relationships between tables remain consistent — specifically, that a foreign key value in one table always corresponds to an existing primary key value in another table.
Why Referential Integrity Matters
Without referential integrity, you could end up with "orphaned" records — for example, an order in an orders table pointing to a customer ID that no longer exists in the customers table. This breaks data reliability and can crash applications relying on that data.
How It's Enforced
Referential integrity is enforced using foreign key constraints. When you define a foreign key, the database automatically checks that any value inserted matches an existing primary key in the referenced table.
Example
CREATE TABLE customers (customer_id INT PRIMARY KEY, name VARCHAR(100));
CREATE TABLE orders (order_id INT PRIMARY KEY, customer_id INT, FOREIGN KEY (customer_id) REFERENCES customers(customer_id));
With this setup, trying to insert an order with a customer_id that doesn't exist in the customers table will be rejected by the database.
Referential Actions
| Action | Behavior |
|---|---|
| CASCADE | Automatically updates or deletes matching rows in the child table |
| SET NULL | Sets the foreign key value to NULL when the parent row is deleted |
| RESTRICT | Prevents deletion or update if related rows exist |
| NO ACTION | Similar to RESTRICT, checked at the end of the transaction |
Referential integrity is a core part of maintaining a reliable relational database, and understanding it is essential before moving on to broader concepts like ACID properties and transactions.
Ready to master DBMS & SQL Training Course?
Join Uncodemy's hands-on training with mentor support and placement assistance.
What Happens During Insert, Update and Delete
A foreign key constraint is checked at three moments. On insert or update of the child row, the database verifies the new foreign key value exists in the parent; if not, the statement is rejected. On update or delete of the parent row, the database must decide what to do about any child rows that point to it — and that decision is controlled by the referential action you declared.
INSERT INTO orders VALUES (101, 7); -- fails if customer 7 does not exist
DELETE FROM customers WHERE customer_id = 7; -- behaviour depends on the action
Choosing Between CASCADE, SET NULL and RESTRICT
The right action depends on what the relationship means in the business, not just on syntax.
| Action | Good fit when |
|---|---|
| CASCADE | Child rows have no meaning without the parent — delete an invoice and its line items should go too. |
| SET NULL | The child can survive alone — an employee whose department is removed can temporarily have no department. |
| RESTRICT / NO ACTION | Deleting the parent should be blocked while children exist — stop removing a customer who still has orders. |
CASCADE is powerful but easy to misuse: a single delete can quietly remove a large chain of related rows. Use it only where the child truly cannot exist on its own.
Design Tips That Prevent Orphans
- Index foreign key columns — the database checks them constantly, and an index keeps deletes and joins fast.
- Define the parent table before the child, or the constraint cannot be created.
- Decide the action deliberately for every foreign key rather than accepting the default, which is usually RESTRICT-like.
- Be careful loading data: inserting children before parents violates the constraint, so either order the loads or defer checks where supported.
- If a column may legitimately have "no parent yet", allow NULL in it and remember that a NULL foreign key is not checked against the parent.
It also helps to see where referential integrity sits among the broader integrity rules. Entity integrity says every row has a unique, non-null primary key. Domain integrity says each column only holds values of the right type and range. Referential integrity is the one that links tables together, keeping foreign keys pointing at rows that really exist. The three work as a set: a foreign key can only be trusted because the primary key it references is itself guaranteed unique and present.
Takeaway: referential integrity is enforced at insert, update and delete, and the referential action is where you encode what the relationship really means. Choose it per foreign key, index those columns, and you will keep the data consistent without manual cleanup.