MySQL: refresh a table in its own trigger

MySQL seems to have a really annoying limitation on the inability to update the table inside the trigger defined for the same table.
I am using MySQL version 5.1 and I get the error message: "The table in the stored function / trigger cannot be updated because it is already in use by the operator that called this function / trigger."

What I have:

create table folder( id int unsigned not null auto_increment PRIMARY KEY , name varchar(100) not null , parentId int unsigned not null ) ; 

This is a hierarchical folder structure. The folder has a name and possibly a parent folder (if not then parentId is zero).
When the folder is deleted, I need to change the parentId all subfolders that were inside it so that they do not become children of a nonexistent folder.

This is pretty simple (almost trivial):

 CREATE DEFINER=root@l ocalhost TRIGGER onFolderDelete after delete ON folder FOR EACH ROW update folder set parentId=0 where parentId=old.id ; 

However, such a simple trigger is not allowed by MySQL, because, as I said above, you cannot update the table inside your own trigger.

Is there any way to implement such a trigger, somehow imitating its effects?

PS: Please do not offer to send both statements (DELETE and UPDATE) sequentially. This is obviously the last solution, if nothing else is possible.

Edit:
I use the MyISAM engine (performance reasons), so I cannot use foreign keys.

+4
source share
1 answer

Can you add a foreign key with ON DELETE SET NULL (or DEFAULT) ?
UPDATE ( DEFAULT is still not implemented, SET NULL is the only option ...)
So you will have something like

 create table folder( id int unsigned not null auto_increment PRIMARY KEY , name varchar(100) not null , parentId int unsigned null , FOREIGN KEY(parentId) REFERENCES folder(id) ON UPDATE CASCADE ON DELETE SET NULL ) ; 
+4
source

All Articles