Firebird trigger before uninstall

I have two tables AUTHORand BOOKrelated AUTHORID.

I need to create a trigger that, in the case of an author’s deletion, first deletes the books of that author and then deletes the author. And if someone just tries to update the field AUTHORIDin AUTHOR, he will automatically update AUTHORIDin BOOK.
Is this even doable? :)

set term # ;  
create trigger del for author  
before delete or update as  
  declare variable aut int;  
  declare variable bok int;  
begin  
  if(deleting) then  
  begin  
    delete from book where authorid=:aut;  
    delete from author where authorid=:aut;  
  end  
  if (updating) then  
  begin  
    update book set authorid=new.authorid;  
  end end#  
set term ; # 
+4
source share
1 answer

You do not need a trigger for this. You can use a foreign key to delete ON DELETE CASCADE. This will automatically cascade the deletion to the dependent row if the foreign key target is deleted.

, , , ON UPDATE CASCADE. , .

, , (/ Interbase 6):

CREATE TABLE T1 (P1 INTEGER NOT NULL PRIMARY KEY);
CREATE TABLE T2 (F2 INTEGER REFERENCES T1(P1)
  ON UPDATE CASCADE
  ON DELETE CASCADE);
+5

All Articles