MySQL Forums
Forum List  »  Triggers

Problem deleting record on the same table that the trigger fired
Posted by: Chan Nan
Date: March 15, 2007 10:50AM

Hi,

My scenario is to delete old duplicate records before insert the new ones (in another word reuse the name here). However, I got an error when running it.

Here's the info (please bear with me if I don't explain it clearly)

table mytable:
user_id, proj_id, item_id, data1, data2, data3
(note - user_id, proj_id, and item_id can have duplicate values
item_id shoud be reusable as a group)
ie.
user_id proj_id item_id data1 data2 data3
1 100 1234 hello Paris Food
1 100 1234 bye Chicago fruit
1 100 1234 hey New York beef
2 100 2222 A B C
2 100 2222 B B C

I'd like to write a trigger so that when three new records come in
like:

1 100 1234 bonjour Paris Food
1 100 1234 hola Chicago fruit
1 100 1234 whatup New York beef

the old ones should be replaced and I should have:
1 100 1234 bonjour Paris Food
1 100 1234 hola Chicago fruit
1 100 1234 whatup New York beef
2 100 2222 A B C
2 100 2222 B B C

Here's the trigger:
....
delete from mytable where user_id = new.user_id and proj_id=new.proj_id and item_id=new.item_id (delete old data if duplicate before insert new ones)
....
and the error:
ERROR 1442 (HY000): Can't update table 'mytable' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.

Does the error mean "I can't modify the table while the trigger is fired?".
I'd like to know if this is the way trigger was designed or I missed something. I know this can be handled fairly easy in the application side but it would be nice to do it within the trigger.

Thanks a lot in advanced.

Options: ReplyQuote


Subject
Views
Written By
Posted
Problem deleting record on the same table that the trigger fired
4848
March 15, 2007 10:50AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.