Skip to main content

Aunque pasen los años de esta pregunta quisiera hacer unas aclaraciones para los hispano parlantesAlthough the years of this question have passed, las pruebas se han hecho enI would like to clarify for Spanish speakers, the tests have been done in Postgres:

Se añadió la siguienteThe following constraint was added to a una tabla detable of 1337 registrosrecords, donde elwhere the kit es la llave primariais the primary key:

**Bloque 1** ALTER TABLE ele_kitscompletos ADD CONSTRAINT unique_div_nkit PRIMARY KEY (div_nkit) 

Esto crea una llave primaria por defectoThis creates a default primary key NOT DEFERRABLE para la tabla por tanto al intentar el siguienteDEFERRED for the table so when trying the next UPDATE obtenemoswe get error:

update ele_kitscompletos set div_nkit = div_nkit + 1; 

ERROR: llave duplicada viola restricción de unicidad duplicate key violates uniqueness restriction «unique_div_nkit»

EnIn Postgres al ejecutar un, executing an UPDATE por cadafor each ROW se verifica que se cumpla la RESTRICCION óverifies that the RESTRICTION or CONSTRAINT is met.


Ahora se crea elThe CONSTRAINT IMMEDIATE y se ejecuta cada sentencia por separadois 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).

AquiHere SI permite cambiar la llave primaria ya que ejecuta todo la primera sentencia completaallows changing the primary key since it executes the entire first complete sentence (1328 rows); pero aunque se encuentra en transacciónbut although it is in transaction (BEGIN), elthe CONSTRAINT se valida de inmediato al terminar cada sentencia sin haber hechois validated immediately upon finishing each sentence without having made COMMIT, por tanto genera eltherefore generates the error al ejecutar elwhen executing the INSERT. Finalmente creamos elFinally we created the CONSTRAINT DEFERRED hacer lo siguientedo 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 

Si ejecutamos cada sentencia del Bloque 2If we execute each statement of ** Block 2 **, cada sentencia por separadoeach sentence separately, no se genera error alis generated to the INSERT ya que no valida sino que se ejecuta el COMMITsince it does not validate but the final donde encuentra que se presento una inconsistenciaCOMMIT is executed where it finds an inconsistency.


Para info completa en ingles les sugiero consultar losFor complete information in English I suggest you check the links:

Deferrable SQL Constraints in Depth

NOT DEFERRABLE versus DEFERRABLE INITIALLY IMMEDIATE

Aunque pasen los años de esta pregunta quisiera hacer unas aclaraciones para los hispano parlantes, las pruebas se han hecho en Postgres:

Se añadió la siguiente constraint a una tabla de 1337 registros, donde el kit es la llave primaria:

**Bloque 1** ALTER TABLE ele_kitscompletos ADD CONSTRAINT unique_div_nkit PRIMARY KEY (div_nkit) 

Esto crea una llave primaria por defecto NOT DEFERRABLE para la tabla por tanto al intentar el siguiente UPDATE obtenemos error:

update ele_kitscompletos set div_nkit = div_nkit + 1; 

ERROR: llave duplicada viola restricción de unicidad «unique_div_nkit»

En Postgres al ejecutar un UPDATE por cada ROW se verifica que se cumpla la RESTRICCION ó CONSTRAINT.


Ahora se crea el CONSTRAINT IMMEDIATE y se ejecuta cada sentencia por separado:

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).

Aqui SI permite cambiar la llave primaria ya que ejecuta todo la primera sentencia completa (1328 rows); pero aunque se encuentra en transacción (BEGIN), el CONSTRAINT se valida de inmediato al terminar cada sentencia sin haber hecho COMMIT, por tanto genera el error al ejecutar el INSERT. Finalmente creamos el CONSTRAINT DEFERRED hacer lo siguiente:

**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 

Si ejecutamos cada sentencia del Bloque 2, cada sentencia por separado, no se genera error al INSERT ya que no valida sino que se ejecuta el COMMIT final donde encuentra que se presento una inconsistencia.


Para info completa en ingles les sugiero consultar los links:

Deferrable SQL Constraints in Depth

NOT DEFERRABLE versus DEFERRABLE INITIALLY IMMEDIATE

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

Source Link

Aunque pasen los años de esta pregunta quisiera hacer unas aclaraciones para los hispano parlantes, las pruebas se han hecho en Postgres:

Se añadió la siguiente constraint a una tabla de 1337 registros, donde el kit es la llave primaria:

**Bloque 1** ALTER TABLE ele_kitscompletos ADD CONSTRAINT unique_div_nkit PRIMARY KEY (div_nkit) 

Esto crea una llave primaria por defecto NOT DEFERRABLE para la tabla por tanto al intentar el siguiente UPDATE obtenemos error:

update ele_kitscompletos set div_nkit = div_nkit + 1; 

ERROR: llave duplicada viola restricción de unicidad «unique_div_nkit»

En Postgres al ejecutar un UPDATE por cada ROW se verifica que se cumpla la RESTRICCION ó CONSTRAINT.


Ahora se crea el CONSTRAINT IMMEDIATE y se ejecuta cada sentencia por separado:

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).

Aqui SI permite cambiar la llave primaria ya que ejecuta todo la primera sentencia completa (1328 rows); pero aunque se encuentra en transacción (BEGIN), el CONSTRAINT se valida de inmediato al terminar cada sentencia sin haber hecho COMMIT, por tanto genera el error al ejecutar el INSERT. Finalmente creamos el CONSTRAINT DEFERRED hacer lo siguiente:

**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 

Si ejecutamos cada sentencia del Bloque 2, cada sentencia por separado, no se genera error al INSERT ya que no valida sino que se ejecuta el COMMIT final donde encuentra que se presento una inconsistencia.


Para info completa en ingles les sugiero consultar los links:

Deferrable SQL Constraints in Depth

NOT DEFERRABLE versus DEFERRABLE INITIALLY IMMEDIATE