[英]Create trigger that delete after insert
I want to create a trigger that delete any new row I insert if the value is more than a number, for example 8. 我想创建一个触发器,如果该值大于一个数字(例如8),则会删除我插入的任何新行。
I tried this: 我尝试了这个:
DELIMITER |
CREATE TRIGGER mytrigger AFTER INSERT ON mytab
FOR EACH ROW
BEGIN
DELETE FROM mytab WHERE myparameter > 8;
END
|
DELIMITER ;
I tried to: 我试过了:
INSERT INTO mytab VALUES (9);
and this is the message error: 这是消息错误:
#1442 - Can't update table 'mytab' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
also if I put: 另外,如果我把:
INSERT INTO mytab VALUES (4);
it runs the same error... 它运行相同的错误...
Thanks! 谢谢!
Don't delete! 不要删除! Just produce an error on the insert: 只是在插入上产生一个错误:
DELIMITER |
CREATE TRIGGER mytrigger BEFORE INSERT ON mytab
FOR EACH ROW
BEGIN
IF new.myparameter > 8 THEN
signal sqlstate '45000' set message_text = 'Parameter value can be no larger than 8';
END IF;
END
|
DELIMITER ;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.