我有一个名为table
的表:
id int(11) AI PK
name varchar(255)
我将日志存储在table_log
id int(11) AI PK
action enum('insert', 'update', 'delete')
name varchar(255)
ts timestamp current_timestam
目标:我想创建触发器以将日志存储到table_log
以下是TRIGGERS:
CREATE TRIGGER `table_insert` AFTER INSERT ON `table`
FOR EACH ROW
BEGIN
INSERT INTO `table_log` (id, action, name, ts)
VALUES(NEW.id, 'insert', NEW.name, NOW());
END;
CREATE TRIGGER `table_update` AFTER INSERT ON `table`
FOR EACH ROW
BEGIN
INSERT INTO table_log (id, action, name, ts)
VALUES(NEW.id, 'update', NEW.name, NOW());
END;
CREATE TRIGGER `table_delete` AFTER INSERT ON `table`
FOR EACH ROW
BEGIN
INSERT INTO `table_log` (id, action, name, ts)
VALUES(OLD.id, 'delete', OLD.name, NOW());
END;
我尝试创建触发器但语法错误:
错误
SQL query:
CREATE TRIGGER `table_insert` AFTER INSERT ON `table`
FOR EACH
ROW
BEGIN
INSERT INTO `table_log` ( id,
ACTION , name, ts )
VALUES (
NEW.id, 'insert', NEW.name, NOW( )
);
MySQL said: Documentation
#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 :(得分:0)
如果您没有设置除;
之外的其他分隔符,则该语句将在第一个;
结束,并且您的触发器定义将不完整。您需要告诉 MySQL 该语句应该以您定义的分隔符结束。之后,您可以使用delimiter ;
delimiter |
CREATE TRIGGER `table_insert` AFTER INSERT ON `table`
FOR EACH ROW BEGIN
INSERT INTO `table_log` ( id, ACTION , name, ts )
VALUES (NEW.id, 'insert', NEW.name, NOW( ));
END
|
delimiter ;