看到mysql的触发器,随手写了一个:
mysql> create trigger t_ai_test
-> after insert on test
-> for each row
-> begin
-> update test set name='test_1';
-> end;$
Query OK, 0 rows affected (0.00 sec)
OK 没问题!
mysql> insert into test values(2,'test');$
ERROR 1442 (HY000): Can't update table 'test' in stored function/trigger because it is already used by statement which invoked this stored function/trigge
执行插入就报这个错。
查官网是这么回答的:
Old question, but in case anyone finds this: MySQL triggers can't manipulate the table they are assigned to. All other major DBMS support this feature so hopefully MySQL will add this support soon.
answered Feb 22 '10 at 21:16
“MySQL will add this support soon”,看来我还没有等到。那好吧,找解决办法先~
原因:MYSQL的限制触发器中不能对本表进行操作,否则没完没了的递归
解决办法:用set
mysql> create trigger t_ai_test
-> after insert on test
-> for each row
-> begin
-> set New.name=concat(concat(New.id,'_'),New.name);
-> end;$