Dung (Donny) Nguyen

Senior Software Engineer

Referential Integrity Constraints in Databases

Referential integrity is a fundamental concept in relational database design that ensures relationships between tables remain consistent. These constraints help maintain data accuracy and reliability by preventing orphaned records.

Core Concepts

Referential integrity constraints ensure that relationships between tables remain valid by enforcing that references to data in one table must exist in another table. This is implemented primarily through foreign keys.

A foreign key is a field (or collection of fields) in one table that refers to the primary key in another table. The table containing the foreign key is called the child table, while the table containing the primary key is called the parent table.

Types of Referential Integrity Actions

When you define a foreign key relationship, you can specify what happens when a referenced record is updated or deleted:

  1. CASCADE: Changes in the parent table automatically update or delete corresponding records in the child table
  2. SET NULL: Sets the foreign key fields to NULL when the referenced record is deleted or updated
  3. SET DEFAULT: Sets the foreign key fields to their default values
  4. RESTRICT/NO ACTION: Prevents deletion or update of the parent record if child records exist

Implementation Example

CREATE TABLE customers (
    customer_id INT PRIMARY KEY,
    name VARCHAR(100),
    email VARCHAR(100)
);

CREATE TABLE orders (
    order_id INT PRIMARY KEY,
    customer_id INT,
    order_date DATE,
    total_amount DECIMAL(10,2),
    FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
    ON DELETE RESTRICT
    ON UPDATE CASCADE
);

Benefits of Referential Integrity

Common Challenges