创建插入后删除的触发器

时间:2014-12-02 17:45:34

标签: mysql sql

我想创建一个触发器,如果​​值大于数字,则删除我插入的任何新行,例如8。

我试过了:

DELIMITER |
CREATE TRIGGER mytrigger AFTER INSERT ON mytab
    FOR EACH ROW 
    BEGIN
        DELETE FROM mytab WHERE myparameter > 8;
    END
|

DELIMITER ;

我试图:

INSERT INTO mytab VALUES (9);

这是消息错误:

#1442 - Can't update table 'mytab' in stored function/trigger because it is already used by statement which invoked this stored function/trigger. 

如果我把:

INSERT INTO mytab VALUES (4);

它运行相同的错误......

谢谢!

1 个答案:

答案 0 :(得分:1)

不要删除!只是在插入上产生错误:

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 ;