TGD/practicas/prac2/09_mod_constraints.sql

25 lines
1.2 KiB
MySQL
Raw Permalink Normal View History

2018-10-14 12:52:33 +02:00
INSERT INTO PROFESOR(cod_pro, nombre) VALUES('JR', 'Juan Ruiz');
-- Fails here, constraint is INITIALLY IMMEDIATE
INSERT INTO PROFESOR(cod_pro, nombre) VALUES('JR', 'Javier Pérez');
ROLLBACK; -- Don't save the first insertion
-- Change constraint to be INITIALLY DEFERRED
ALTER TABLE profesor DROP PRIMARY KEY CASCADE KEEP INDEX;
ALTER TABLE profesor ADD CONSTRAINT cp_prof PRIMARY KEY(cod_pro) INITIALLY DEFERRED DEFERRABLE;
-- Because we used CASCADE to DROP the primary key constraint,
-- we need to restore the FOREIGN KEY in docencia
ALTER TABLE docencia
ADD CONSTRAINT ca_doc_prof
FOREIGN KEY(cod_pro) REFERENCES profesor(cod_pro) INITIALLY IMMEDIATE DEFERRABLE;
INSERT INTO PROFESOR(cod_pro, nombre) VALUES('JR', 'Juan Ruiz');
INSERT INTO PROFESOR(cod_pro, nombre) VALUES('JR', 'Javier Pérez');
-- Fails here, constraint is INITIALLY DEFERRED
COMMIT;
-- Restore constraint to initial setting
ALTER TABLE profesor DROP PRIMARY KEY CASCADE KEEP INDEX;
ALTER TABLE profesor ADD CONSTRAINT cp_prof PRIMARY KEY(cod_pro) INITIALLY IMMEDIATE DEFERRABLE;
ALTER TABLE docencia ADD CONSTRAINT ca_doc_prof FOREIGN KEY(cod_pro) REFERENCES profesor(cod_pro) INITIALLY IMMEDIATE DEFERRABLE;