你的位置:首页 > 数据库

[数据库]MySQL触发器 Update触发Insert失败


今天工作需要,想要实现将仅对状态更新的表进行历史记录显示,于是考虑在原表中建立触发器,将更新的内容同时写入另一张表

于是进行测试

--建立测试表
CREATE TABLE `triggletest_triggle` ( `id` INT(11) NOT NULL, `name` VARCHAR(5) NULL DEFAULT NULL, PRIMARY KEY (`id`))COLLATE='latin1_swedish_ci'ENGINE=InnoDB

--建立目标表CREATE TABLE `triggletest` (  `seq` INT(11) NOT NULL,  `id` INT(11) NOT NULL,  `name` VARCHAR(5) NULL DEFAULT NULL,  PRIMARY KEY (`seq`),  INDEX `id` (`id`))COLLATE='latin1_swedish_ci'ENGINE=InnoDB;

---写入测试数据INSERT INTO `triggletest_triggle` VALUES(1,'A');

--建立触发器DROP TRIGGER if EXISTS test1CREATE TRIGGER test1 AFTER UPDATE ON triggletest_triggleFOR EACH ROWBEGIN  INSERT INTO triggletest(id) values (new.id);END

执行触发器语句,报错,报错内容如下:

/* SQL错误(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 'CREATE TRIGGER test1
AFTER UPDATE ON triggletest_triggle
FOR EACH ROW
BEGIN' at line 2 */

 

问题待查