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: