Referential Integrity

Referential integrity ensures that relationships between tables remain consistent by enforcing foreign key constraints.

Explanation:

  • Prevents orphan records by ensuring that referenced data exists.
  • If a foreign key references a primary key, changes in the primary key are restricted.

Example SQL Enforcing Referential Integrity:

sqlCopyEditCREATE TABLE Orders (
    order_id INT PRIMARY KEY,
    customer_id INT,
    FOREIGN KEY (customer_id) REFERENCES Customers(customer_id) ON DELETE CASCADE
);

Here, if a customer is deleted, all their orders are automatically deleted (ON DELETE CASCADE).