In Oracle, you can handle this with triggers, in particular compound triggers
alter table CarDrivers add CONSTRAINT CarFK FOREIGN KEY (CarID) REFERENCES Cars (CarID) on delete cascade enable / create or replace TRIGGER "CarDrivers_delete" for delete ON CarDrivers compound trigger type driver_ids is table of Drivers.DriverID%type INDEX BY PLS_INTEGER; ids driver_ids; AFTER EACH ROW IS BEGIN ids (ids.COUNT + 1) := :NEW.Driver; END AFTER EACH ROW; AFTER STATEMENT IS BEGIN FOR i IN 1 .. ids.COUNT LOOP delete from Drivers WHERE DriverID = ids (i); END LOOP; END AFTER STATEMENT; END; /
Thus, it is enough to issue
delete from Cars where CarID = 666
and deletion will be cascaded to the CarDrivers table by restriction and to the Drivers table by trigger.
The use of compound triggers is necessary to avoid ORA-04091, i.e. changing the error table. Complex triggers are available starting with Oracle11g. Look here
user1593165
source share