Skip to content

DEFERRABLE

CREATE TABLE Employees (
ID INT PRIMARY KEY,
supervisor_ID INT REFERENCES Employees(ID) DEFERRABLE INITIALLY DEFERRED
);
INSERT INTO Employees (ID, supervisor_ID)
VALUES (1, 2), (2, 1);
INSERT 0 2

In the above code, a circular self-referential constraint is implemented on the Employees table, where supervisor_ID references the ID of the same table. The DEFERRABLE keyword makes it possible to delay the checking of the constraint until the end of the transaction. By further specifying INITIALLY DEFERRED, it enforces that the constraint checking will always be deferred till the transaction’s end. The data insertion doesn’t violate the foreign key constraint as the check is performed after both insertions are executed.