在mysql数据库中触发之前

时间:2016-11-18 18:10:43

标签: mysql

我正在尝试创建一个触发器,如果​​没有票据小于成人号,那么值1将被插入到trig表中。但是,我收到一个错误。代码是:

CREATE TRIGGER ch 
BEFORE INSERT 
ON packagebooking
IF(tick.tickets<packagebooking.Adult
     AND tick.Package_id=packagebooking.Package_id)
Begin atomic
insert into trig values(1)

MySQL说:文档

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'IF(tick.tickets<packagebooking.Adult
     AND tick.Package_id=packagebooking.Pa' at line 4.

我应该做些什么改变?

1 个答案:

答案 0 :(得分:1)

您的触发器创建语法有错误,将IF语句放在FOR EACH ROW中,如下所示:

CREATE TRIGGER ch BEFORE INSERT
   ON `database`.`table`
   FOR EACH ROW BEGIN
      IF(tick.tickets<packagebooking.Adult
          AND tick.Package_id=packagebooking.Package_id)
      THEN Begin atomic
           insert into trig values(1)
      END IF;
   END;