TGD/practicas/prac2/10_cascade_trigger.sql
Carlos Galindo 7e26615aa7
Practica 2
2018-10-14 12:52:33 +02:00

23 lines
541 B
SQL

-- In WHEN, NEW and OLD don't need the colon
-- Update docencia.cod_pro when profesor.cod_pro changes
CREATE OR REPLACE TRIGGER cascade_profesor_update
AFTER UPDATE ON profesor
FOR EACH ROW
WHEN (OLD.cod_pro <> NEW.cod_pro)
BEGIN
UPDATE docencia
SET cod_pro = :new.cod_pro
WHERE cod_pro = :old.cod_pro;
END;
/
-- Delete entries from docencia when profesor is deleted
CREATE OR REPLACE TRIGGER cascade_profesor_delete
AFTER DELETE ON profesor
FOR EACH ROW
BEGIN
DELETE FROM docencia
WHERE cod_pro = :old.cod_pro;
END;
/