i am experimenting with triggers using the sample HR schema provided by oracle. I am trying to delete or update all the employees whenever their respective department id is updated or deleted in the DEPARTMENTS TABLE using a trigger.
This is the code:
create or replace trigger UPDATE_EMPLOYEES_DEPT_ID
before update or delete of DEPARTMENT_ID on DEPARTMENTS
for each row
begin
if UPDATING then
update EMPLOYEES set DEPARTMENT_ID = :new.DEPARTMENT_ID where DEPARTMENT_ID = :OLD.DEPARTMENT_ID;
ELSIF DELETING then
update employees set department_id = null where DEPARTMENT_ID = :OLD.DEPARTMENT_ID;
end if;
end;
When i execute :
UPDATE departments SET department_ID = 112 WHERE department_ID = 110;
it gives me constraint violation error.
Error report:
SQL Error: ORA-02292: integrity constraint (HR.JHIST_DEPT_FK) violated - child record found
02292. 00000 - "integrity constraint (%s.%s) violated - child record found"
*Cause: attempted to delete a parent key value that had a foreign
dependency.
*Action: delete dependencies first then parent or disable constraint.
Where am i going wrong? Does integrity constraint gets checked before the 'BEFORE TRIGGER'?