Home » SQL & PL/SQL » SQL & PL/SQL » Urgent: "on delete cascade" prevents delete trigger
Urgent: "on delete cascade" prevents delete trigger [message #25881] Thu, 22 May 2003 10:09 Go to next message
Marcus Rudke
Messages: 4
Registered: May 2003
Junior Member
run into a problem concerning triggers and the 'on delete cascade' option.
Problem, looks simple, but...
-----------------------
I am using Oracle 8.1.5.
2 tables: TabA TabB

TabA is parent of TabB (foreign key in TabB references
field in TabA with 'on delete cascade').
Both Tables have an 'after delete' Trigger. These Triggers do _not_ change one
of the both tables (they can even be empty) in any way.
Problem: If I delete records in TabA only the Trigger of TabB is fired, 2 times!
It's possible to do this with 3 tables. Then the most inner trigger is fired 3 times.

What is wrong? Thank you very much in advance.
Re: Urgent: "on delete cascade" prevents delete trigger [message #25886 is a reply to message #25881] Thu, 22 May 2003 12:34 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
Marcus, it would be helpful if you could post the triggers on both tables. If these are row triggers, the table A trigger should fire once and the table B trigger will fire for each child row. Post the triggers and we'll take a look at it...
Re: Urgent: "on delete cascade" prevents delete trigger [message #25894 is a reply to message #25886] Fri, 23 May 2003 02:45 Go to previous messageGo to next message
Marcus Rudke
Messages: 4
Registered: May 2003
Junior Member
>Marcus, it would be helpful if you could post the
>triggers on both tables. If these are row triggers,
>the table A trigger should fire once and the table B
>trigger will fire for each child row. Post the
>triggers and we'll take a look at it...

OK, here they are. They are no row triggers and contain just dummy instructions (to debug). I post
the tables as well (at the end of this message,
consider the 'on cascade delete' option).

Triggers:
++++++++
Trigger for Table "A":
create or replace trigger AFTER_A AFTER DELETE ON A
BEGIN
dbms_output.put_line('AFTER A');
END;

Trigger for Table "B":
create or replace trigger AFTER_B AFTER DELETE ON B
BEGIN
dbms_output.put_line('AFTER B');
END;

Tables:
+++++++
create table A
(
A_ID NUMBER(9) PRIMARY KEY
);

create table B
(
B_ID NUMBER(9) PRIMARY KEY
);
alter table B
add constraint FK_B_A foreign key (B_ID)
references A (A_ID) on delete cascade;

--> on delete on Table "A" the output is:
'AFTER B'
'AFTER B'
Trigger A is NOT fired. Why?
Re: Urgent: "on delete cascade" prevents delete trigger [message #25897 is a reply to message #25894] Fri, 23 May 2003 11:51 Go to previous messageGo to next message
Todd Barry
Messages: 4819
Registered: August 2001
Senior Member
Here are my results with your exact code:

sql>create table A
  2  (
  3  A_ID NUMBER(9) PRIMARY KEY
  4  );
 
Table created.
 
sql>create table B
  2  (
  3  B_ID NUMBER(9) PRIMARY KEY
  4  );
 
Table created.
 
sql>alter table B
  2  add constraint FK_B_A foreign key (B_ID)
  3  references A (A_ID) on delete cascade;
 
Table altered.
 
sql>create or replace trigger AFTER_A AFTER DELETE ON A
  2  BEGIN
  3  dbms_output.put_line('AFTER A');
  4  END;
  5  /
 
Trigger created.
 
sql>create or replace trigger AFTER_B AFTER DELETE ON B
  2  BEGIN
  3  dbms_output.put_line('AFTER B');
  4  END;
  5  /
 
Trigger created.
 
sql>insert into a values (1);
 
1 row created.
 
sql>insert into b values (1);
 
1 row created.
 
sql>commit;
 
Commit complete.
 
sql>set serveroutput on
 
sql>delete from a where a_id = 1;
<b>AFTER B
AFTER A</b>
 
1 row deleted.
Re: Urgent: "on delete cascade" prevents delete trigger [message #25910 is a reply to message #25897] Mon, 26 May 2003 03:13 Go to previous messageGo to next message
Marcus Rudke
Messages: 4
Registered: May 2003
Junior Member
Nice. It's obvious that my result is not the expected
one but it is:
---
AFTER B
AFTER B

1 row deleted.
---
And, please, again: why? What version of oracle are you
using. Mine is 8.1.5 (same result with 8.1.6).
Re: Urgent: "on delete cascade" prevents delete trigger [message #25915 is a reply to message #25897] Mon, 26 May 2003 06:16 Go to previous messageGo to next message
Marcus Rudke
Messages: 4
Registered: May 2003
Junior Member
Seems to be a bug in 8.1.5 and 8.1.6. In Oracle 8.1.7
we have the expected result.
Pls give me a better solution or example for On Delete Cascade feature of Oracle [message #33497 is a reply to message #25894] Thu, 14 October 2004 22:23 Go to previous message
Anil
Messages: 80
Registered: September 2001
Member
Pls give me a better solution or example for On Delete Cascade feature of Oracle
Previous Topic: Real Life SQL queries needed
Next Topic: Update and differens with in Oracle 8.05 and 8i
Goto Forum:
  


Current Time: Tue Aug 12 16:05:00 CDT 2025