In addition to the other (correct) answers, speaking of PostgreSQL , it is necessary to indicate that:
with NOT DEFERRABLE each row is checked during insert / update
with DEFERRABLE (currently IMMEDIATE ) all rows are checked at the end of the insert / update
with DEFERRABLE (currently DEFERRED ) all rows are checked at the end of the transaction
Therefore, it is wrong to say that the DEFERRABLE constraint acts like NOT DEFERRABLE when it is set to IMMEDIATE.
Let's clarify this difference:
CREATE TABLE example( row integer NOT NULL, col integer NOT NULL, UNIQUE (row, col) DEFERRABLE INITIALLY IMMEDIATE ); INSERT INTO example (row, col) VALUES (1,1),(2,2),(3,3); UPDATE example SET row = row + 1, col = col + 1; SELECT * FROM example;
This correctly outputs:

But if we delete the DEFERRABLE INITIALLY IMMEDIATE statement,
ERROR: the value of the duplicated key violates the unique restriction "example_row_col_key". MORE: Key ("row", column) = (2, 2) already exists. ********** Mistake **********
ERROR: the value of the duplicated key violates the unique constraint of "example_row_col_key" SQL state: 23505 Details: the key ("row", column) = (2, 2) already exists.
APPENDIX (October 12, 2017)
This behavior is really documented here , the Compatibility section:
In addition, PostgreSQL checks the immediate limitations of uniqueness immediately, and not at the end of the statement, as the standard suggests.
Teejay Jun 23 '17 at 13:47 on 2017-06-23 13:47
source share