Free tutorials & notes in Hindi & English · Clean code examples 🧰 Free PDF & Image Tools →
MySQL + SQL · Lesson 21

Referential Integrity in DBMS

In this lesson: Learn referential integrity with foreign keys, ON DELETE CASCADE, SET NULL and RESTRICT — MySQL examples with output.

What is referential integrity

Referential integrity is a rule that keeps linked tables consistent: a foreign key value must match an existing value in the table it references, or be null. This stops "orphan" rows that point to records which do not exist, so the relationships in your database always make sense.

💡 Referential integrity = a foreign key must point to a real parent row (or be null).

The foreign key link

CREATE TABLE customers (
    id INT PRIMARY KEY,
    name VARCHAR(50)
);
CREATE TABLE orders (
    id INT PRIMARY KEY,
    customer_id INT,
    FOREIGN KEY (customer_id) REFERENCES customers(id)
);

The foreign key orders.customer_id must match a real customers.id. The DBMS enforces this automatically.

What it prevents

-- customers has ids 1 and 2 only
INSERT INTO orders VALUES (10, 99);   -- ERROR: customer 99 does not exist

The DBMS rejects this insert because customer 99 has no matching row. Without referential integrity, you could create an order for a non-existent customer — an orphan row that corrupts the data.

On delete and update

What if you delete a customer who has orders? By default, the DBMS blocks the delete to protect integrity:

DELETE FROM customers WHERE id = 1;   -- ERROR if orders reference id 1

You can control this behaviour with referential actions defined on the foreign key.

ON DELETE CASCADE

FOREIGN KEY (customer_id) REFERENCES customers(id)
    ON DELETE CASCADE

With ON DELETE CASCADE, deleting a customer automatically deletes all their orders too. This keeps things consistent — no orphaned orders are left behind. Use it when child rows have no meaning without their parent.

Other referential actions

ActionOn deleting the parent
RESTRICT / NO ACTIONBlock the delete (default)
CASCADEDelete the child rows too
SET NULLSet the child's foreign key to NULL

Choose based on what should happen to child rows when a parent goes away.

Common mistakes

  • Inserting a child before its parent. The parent row must exist first.
  • Deleting a referenced parent without a plan. Decide CASCADE, SET NULL or RESTRICT.
  • Mismatched column types. Foreign and referenced keys must share a type.
  • Forgetting foreign keys entirely. Then nothing enforces integrity.

Summary

  • Referential integrity keeps foreign keys pointing to real rows.
  • The DBMS rejects inserts that reference non-existent parents.
  • By default, deleting a referenced parent is blocked.
  • ON DELETE CASCADE deletes child rows with the parent.
  • SET NULL and RESTRICT are other referential actions.

Next, learn the keys behind it in primary key vs foreign key, or add rules with constraints.

Frequently Asked Questions

What is referential integrity?
Referential integrity is a rule that a foreign key value must match an existing value in the referenced table (or be null). It stops you from creating "orphan" rows that point to records which do not exist, keeping the data consistent.
How does a foreign key enforce referential integrity?
A foreign key links a column in one table to the primary key of another. The DBMS then rejects any insert or update that would set the foreign key to a value not present in the parent table, so relationships stay valid.
What is an orphan row?
An orphan row is a row whose foreign key points to a parent record that no longer exists — like an order for a customer who was deleted. Referential integrity prevents orphan rows from being created.
What does ON DELETE CASCADE do?
ON DELETE CASCADE means that when a parent row is deleted, all child rows referencing it are automatically deleted too. For example, deleting a customer also deletes all their orders, keeping the data consistent without orphans.
What happens if I try to delete a referenced row?
By default, the DBMS blocks the delete to protect integrity, giving an error. You can change this with referential actions like CASCADE (delete children too), SET NULL (clear the link), or RESTRICT (block it explicitly).
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 Live Code Editor

This page's programs are ready here — run them, edit them, and learn. No installation needed.
Powered by OneCompiler. The code loads into the editor automatically — press Run to see the output. If the editor does not open, open it in a new tab.
Next topicER Model and ER Diagram Your progress: 21 / 151 (14%) · Lesson 22 / 151 Continue →

How was this lesson?

Your feedback helps us make the content better.

Write a review or suggestion (optional)

After approval, your name and city may appear publicly with your review. Please do not write your email or phone number.

▶ CodingEasily on YouTube

Learn with video lessons in easy Hindi

Featured video: RDBMS Part 3 – Class XI Commerce IT

▶ Subscribe – it’s free