插入前如何终止MySQL触发器?
为此,语法如下-
delimiter // create trigger yourTriggerName before insert on yourTableName FOR EACH ROW BEGIN yourStatement1 . . N END // delimiter ;
要了解上述语法,让我们创建一个表-
mysql> create table DemoTable1919 ( Value int );
这是在插入之前创建MySQL触发器的查询-
mysql> delimiter // mysql> create trigger sumis60 before insert on DemoTable1919 FOR EACH ROW BEGIN declare custom_message varchar(256); IF (SELECT sum(Value) from DemoTable1919) = 60 THEN set custom_message= "cannot insert because sum(Value) is greater than 60"; signal sqlstate '13500' set message_text= custom_message; END IF; END // mysql> delimiter ;
现在您可以使用insert命令在表中插入一些记录,并检查触发器的工作情况-
mysql> insert into DemoTable1919 values(10); mysql> insert into DemoTable1919 values(20); mysql> insert into DemoTable1919 values(30); mysql> insert into DemoTable1919 values(40); ERROR 1644 (13500): cannot insert because sum(Value) is greater than 60