Although the years of this question have passed, I would like to clarify for Spanish speakers, the tests have been done in Postgres:
The following constraint was added to a table of 1337 records, where the kit is the primary key:
**Bloque 1**
ALTER TABLE ele_kitscompletos
ADD CONSTRAINT unique_div_nkit
PRIMARY KEY (div_nkit)
This creates a default primary key NOT DEFERRED for the table so when trying the next UPDATE we get error:
update ele_kitscompletos
set div_nkit = div_nkit + 1;
ERROR: duplicate key violates uniqueness restriction
«unique_div_nkit»
In Postgres, executing an UPDATE for each ROW verifies that the RESTRICTION or CONSTRAINT is met.
The CONSTRAINT IMMEDIATE is now created and each statement is executed separately:
ALTER TABLE ele_kitscompletos
ADD CONSTRAINT unique_div_nkit
PRIMARY KEY (div_nkit)
DEFERRABLE INITIALLY IMMEDIATE
**Bloque 2**
BEGIN;
UPDATE ele_kitscompletos set div_nkit = div_nkit + 1;
INSERT INTO public.ele_kitscompletos(div_nkit, otro_campo)
VALUES
(1338, '888150502');
COMMIT;
Query OK, 0 rows affected (execution time: 0 ms; total time: 0 ms)
Query OK, 1328 rows affected (execution time: 858 ms; total time: 858
ms) ERROR: llave duplicada viola restricción de unicidad
«unique_div_nkit» DETAIL: Ya existe la llave (div_nkit)=(1338).
Here SI allows changing the primary key since it executes the entire first complete sentence (1328 rows); but although it is in transaction (BEGIN), the CONSTRAINT is validated immediately upon finishing each sentence without having made COMMIT, therefore generates the error when executing the INSERT. Finally we created the CONSTRAINT DEFERRED do the following:
**Bloque 3**
ALTER TABLE public.ele_edivipol
DROP CONSTRAINT unique_div_nkit RESTRICT;
ALTER TABLE ele_edivipol
ADD CONSTRAINT unique_div_nkit
PRIMARY KEY (div_nkit)
DEFERRABLE INITIALLY DEFERRED
If we execute each statement of ** Block 2 **, each sentence separately, no error is generated to the INSERT since it does not validate but the final COMMIT is executed where it finds an inconsistency.
For complete information in English I suggest you check the links:
Deferrable SQL Constraints in Depth
NOT DEFERRABLE versus DEFERRABLE INITIALLY IMMEDIATE