Cascade delete of self referencing tbl using triggers
I'd like to implement a trigger on a self referencing table that'll cascade delete entries in that table. Consider the following example...
create table person
(name varcher2(40), id number, parent_id number);
A sample set of table records might look like...
name id parent_id
-----------------------
joe 1 NULL
mary 2 1
betty 3 9
bill 4 1
sally 5 2
If/when I go to delete 'joe' I'd like the trigger to delete mary and bill (because thier parent_id = joe's id) as well as sally (because her parent_id = mary's id and mary's parent_id is joe's id).
The closest I've come is....
create trigger cascade_del_person
before delete on person for each row
begin
delete from person where parent_id = :old.id;
end;
If errors out with an ORA_04091 telling me that the table is mutating.
Any suggestions?
Thanks
-dave