Beginner

514. Referential Integrity

Learn how databases maintain valid relationships between related tables.

📘 Lesson 514 of 728 ⏱ 13 min 🎯 Advanced

Referential integrity ensures that relationships between related tables remain valid, typically by requiring foreign key values to reference existing primary or unique key values.

Maintaining valid relationships prevents orphan records and protects the consistency of business data.

An Order.CustomerID should reference an existing Customer.CustomerID.

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

Important in banking, financial systems, customer databases, inventory systems, and enterprise applications.

  • Referential integrity protects table relationships.
  • Foreign keys commonly enforce relationships.
  • Parent keys must satisfy the referenced constraint.
  • It helps prevent orphan records.
  • Rules can differ between database systems.
What is referential integrity?

It ensures that relationships between related tables remain valid.

Which database feature commonly enforces referential integrity?

Foreign key constraints commonly enforce referential integrity.

Have you finished this lesson?

Introduction to SQL – All Lessons