Are DELETE TRIGGERS recursive or not?

classic Classic list List threaded Threaded
7 messages Options
Reply | Threaded
Open this post in threaded view
|

Are DELETE TRIGGERS recursive or not?

Ralf Junker
I wonder if a DELETE TRIGGER should trigger itself recursively in the following scenario:

  create table f (i integer primary key, p integer, n text);
 
  create trigger f_delete after delete on f
    begin
      delete from f where p = old.i;
    end;  
   
  insert into f values (1, null, 'folder 1');
  insert into f values (3, 1, 'file 1 in folder 1');
  insert into f values (4, 1, 'file 2 in folder 1');

  insert into f values (2, null, 'folder 2');
  insert into f values (5, 2, 'file 1 in folder 2');
  insert into f values (6, 2, 'file 2 in folder 2');
  insert into f values (7, 2, 'folder1 in folder 2');
  insert into f values (8, 2, 'folder2 in folder 2');

  insert into f values (9, 7, 'file 1 in folder 1 in folder 2');
  insert into f values (10, 7, 'file 2 in folder 1 in folder 2');

Then I call:

  delete from f where i = 2;

I would like this statement to delete (via the trigger) all rows except for the first three (1, 3, 4) inserts. However, the last two inserts (9, 10) stay undeleted.

My question (SQLite 3.2.7): Are delete triggers recursive? Should a delete caused by a trigger fire another trigger? Or is this a bug?

Unfortunately, I did not find any details in the help so any comments are very welcome.

Regards,

Ralf

Reply | Threaded
Open this post in threaded view
|

Re: Are DELETE TRIGGERS recursive or not?

Kurt Welgehausen
If the trigger were recursive, it would invoke
itself forever, even if it's not doing anything
because the where clause fails.

Regards
Reply | Threaded
Open this post in threaded view
|

Re: Are DELETE TRIGGERS recursive or not?

D. Richard Hipp
In reply to this post by Ralf Junker
Ralf Junker <[hidden email]> wrote:
> I wonder if a DELETE TRIGGER should trigger itself recursively ....

Not at this time.  Though work is underway to change this.
We need recusive delete triggers in order to implement
cascadinig deletes for referential integrity.
--
D. Richard Hipp <[hidden email]>

Reply | Threaded
Open this post in threaded view
|

Re: Are DELETE TRIGGERS recursive or not?

Ralf Junker
Hello [hidden email],

Thank you! I am very much looking forward to recursive delete triggers for just the very purpose you mentioned!

Regards,

Ralf

>Not at this time.  Though work is underway to change this.
>We need recusive delete triggers in order to implement
>cascadinig deletes for referential integrity.

Reply | Threaded
Open this post in threaded view
|

RE: Are DELETE TRIGGERS recursive or not?

Allan, Mark
In reply to this post by Ralf Junker
Hi Ralf,

Have you had a look at the following web-page:-

http://www.justatheory.com/computers/databases/sqlite

This is a way of enforcing referential integrity in SQlite with triggers although evidently not with recursive triggers.
We have implemented this in our system. Would you mind explaining to me the benefit of recursive triggers for this purpose? I ask as we are trying hard to optimise the performance of our deletes, do you think using recursive triggers (when implemented) will be more efficient at cascading deletes?

Thanks

Mark

> -----Original Message-----
> From: Ralf Junker [mailto:[hidden email]]
> Sent: 26 October 2005 16:58
> To: [hidden email]
> Subject: Re: [sqlite] Are DELETE TRIGGERS recursive or not?
>
>
> Hello [hidden email],
>
> Thank you! I am very much looking forward to recursive delete
> triggers for just the very purpose you mentioned!
>
> Regards,
>
> Ralf
>
> >Not at this time.  Though work is underway to change this.
> >We need recusive delete triggers in order to implement
> >cascadinig deletes for referential integrity.
>
>
>


DISCLAIMER:
This information and any attachments contained in this email message is intended only for the use of the individual or entity to which it is addressed and may contain information that is privileged, confidential, and exempt from disclosure under applicable law.  If the reader of this message is not the intended recipient, or the employee or agent responsible for delivering the message to the intended recipient, you are hereby notified that any dissemination, distribution, forwarding, or copying of this communication is strictly prohibited.  If you have received this communication in error, please notify the sender immediately by return email, and delete the original message immediately.

Reply | Threaded
Open this post in threaded view
|

RE: Are DELETE TRIGGERS recursive or not?

Ralf Junker
Hello Allan, Mark,

I am not sure, but I don't think recursive delete triggers apply to your web-page example.

I was looking for recursive delete triggers because the information I need to store in a single table is hierarchical in a sense that a record can refer to another record in the same table (like when storing a tree structure). With recursive delete triggers, a single delete would automatically delete all children AND GRANDCHILDREN of that tree's node. Deleting children with triggers is possible without recursion, but you need recursive delete triggers to delete the grandchildren also.

I don't know if recursive delete triggers will improve performance, but they will certainly make it easier to maintain referential integrity for hierarchical, tree-like data.

Regards,

Ralf

>Have you had a look at the following web-page:-
>
>http://www.justatheory.com/computers/databases/sqlite
>
>This is a way of enforcing referential integrity in SQlite with triggers although evidently not with recursive triggers.
>We have implemented this in our system. Would you mind explaining to me the benefit of recursive triggers for this purpose? I ask as we are trying hard to optimise the performance of our deletes, do you think using recursive triggers (when implemented) will be more efficient at cascading deletes?

Reply | Threaded
Open this post in threaded view
|

Re: Are DELETE TRIGGERS recursive or not?

Igor Tandetnik
Ralf Junker <[hidden email]> wrote:
> I was looking for recursive delete triggers because the information I
> need to store in a single table is hierarchical in a sense that a
> record can refer to another record in the same table (like when
> storing a tree structure). With recursive delete triggers, a single
> delete would automatically delete all children AND GRANDCHILDREN of
> that tree's node. Deleting children with triggers is possible without
> recursion, but you need recursive delete triggers to delete the
> grandchildren also.

Well, you don't strictly _need_ recursive triggers (as in, can't
implement the same functionality without them), they are just more
convenient. You can always write

create trigger del_parents delete on parents
begin
    delete from grandchildren
    where parentId in
        (select id from children where parentId = old.id);
    delete from children where parentId = old.id;
end;

create trigger del_children delete on children
begin
    delete from grandchildren where parentId = old.id;
end;

Igor Tandetnik