Sub topic 17
Deferrable Constraints and Circular References
Prerequisites
This chapter assumes that you can create tables, define primary and foreign keys, and use transactions with BEGIN, COMMIT, and ROLLBACK. The examples use PostgreSQL syntax.
Why circular references are difficult
A circular reference exists when two tables depend on each other. For example, an employee belongs to a department, while a department has one employee as its head.
If both foreign keys are checked immediately, inserting the first row fails. The department cannot reference an employee that does not exist yet. The employee cannot reference a department that does not exist yet.
A deferrable constraint is a constraint whose check can be postponed until the transaction commits. PostgreSQL supports this behavior for foreign keys declared with DEFERRABLE.
Declaring deferred foreign keys
Create both tables first, then add the foreign keys:
CREATE TABLE departments (
department_id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
head_employee_id INTEGER
);
CREATE TABLE employees (
employee_id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
department_id INTEGER NOT NULL
);
ALTER TABLE departments
ADD CONSTRAINT fk_department_head
FOREIGN KEY (head_employee_id)
REFERENCES employees (employee_id)
DEFERRABLE INITIALLY DEFERRED;
ALTER TABLE employees
ADD CONSTRAINT fk_employee_department
FOREIGN KEY (department_id)
REFERENCES departments (department_id)
DEFERRABLE INITIALLY DEFERRED;DEFERRABLE means the constraint may be postponed. INITIALLY DEFERRED makes PostgreSQL check it at transaction commit by default.
Now both rows can be inserted in one transaction:
BEGIN;
INSERT INTO departments (department_id, name, head_employee_id)
VALUES (10, 'Operations', 100);
INSERT INTO employees (employee_id, name, department_id)
VALUES (100, 'Rina Patel', 10);
COMMIT;At COMMIT, both referenced rows exist, so the transaction succeeds. If either row were missing, the commit would fail and the transaction could not complete.
Immediate and deferred checking
A constraint declared as DEFERRABLE INITIALLY IMMEDIATE is checked after each statement unless you defer it:
BEGIN;
SET CONSTRAINTS fk_department_head, fk_employee_department DEFERRED;
-- Statements that temporarily violate the relationship can run here.
COMMIT;SET CONSTRAINTS affects the current transaction only. It does not change the table definition.
A deferred constraint still protects the database. It only permits temporary inconsistency inside a transaction. Other transactions do not see uncommitted changes.
Design choices and limitations
Circular references often indicate that one relationship should be optional. Making head_employee_id nullable lets you create the department first, assign its head later, and avoid a cycle.
You can also redesign the model. For example, store a department's head assignment in a separate table. This may simplify deletion and employee transfers.
Deferrable behavior is database-specific. PostgreSQL supports deferrable foreign keys. MySQL commonly ignores or does not support this feature, and SQL Server uses different mechanisms. Check your database documentation before relying on it.
Exercise
Why can two INITIALLY DEFERRED foreign keys permit a department and its head employee to be inserted in either order?
Show answer
The database postpones both foreign-key checks until the transaction commits, when both referenced rows can exist.