Beginner

134. Referential Integrity

Understand how databases maintain valid relationships between related tables.

📘 Lesson 144 of 728 ⏱ 9 min 🎯 Intermediate

Referential integrity is a database rule that ensures relationships between related tables remain valid. A foreign key generally prevents a child record from referencing a parent record that does not exist, subject to the database's configured rules.

Without referential integrity, databases can contain orphan records and inconsistent relationships.

An order should normally reference a customer that actually exists in the Customers table.

SQL
CREATE TABLE Orders (
    OrderID INT PRIMARY KEY,
    CustomerID INT,
    FOREIGN KEY (CustomerID)
        REFERENCES Customers(CustomerID)
);

Referential integrity is important in customer-order systems, banking transactions, employee departments, product orders, and financial databases.

  • Referential integrity protects table relationships.
  • Foreign keys are commonly used to enforce it.
  • It helps prevent invalid references.
  • It reduces orphan records.
  • Delete and update rules can affect relationship behavior.
What is referential integrity?

Referential integrity ensures that relationships between related tables remain valid.

What database feature commonly enforces referential integrity?

Foreign key constraints are commonly used to enforce referential integrity.

Have you finished this lesson?

Introduction to SQL – All Lessons