DROP TRIGGER IF EXISTS tt1;
CREATE TRIGGER tt2
AFTER INSERT ON t1
FOR EACH ROW
BEGIN
INSERT INTO `t2` (`t2_id`) VALUES (new.t1_id);
END;
上面这段代码在SQLyog中执行有错,错误信息如下:
查询:create trigger tt1 after insert on t1 for each row begin insert into `t2` (`t2_id`) values (new.t1_id)
错误代码: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 5
执行耗时 : 0 sec
传送时间 : 0 sec
总耗时 : 0.001 sec
--------------------------------------------------
查询:end
错误代码: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'end' at line 1
执行耗时 : 0 sec
传送时间 : 0 sec
总耗时 : 0 sec
然后我尝试着将 触发器中的insert into...这条语句注释掉,然后再执行就成功了;
再去改变触发器,将注释去掉,同样成功了;
请问为什么直接写就会报错呢?
好像要把; 转一下 这个关键字 DELIMITER
这个我之前也加了,但是还是没用啊 报错一样的
@OSManager: 可能是已经有after insert 试下这个
DELIMITER $
DROP TRIGGER IF EXISTS tt2$
CREATE TRIGGER tt2
AFTER INSERT ON t1
FOR EACH ROW
BEGIN
INSERT INTO `t2` (`t2_id`) VALUES (new.t1_id);
END$
DELIMITER ;