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 test1
CREATE TRIGGER test1
AFTER UPDATE ON triggletest_triggle
FOR EACH ROW
BEGIN
INSERT INTO triggletest(id) values (new.id);
ENDmysql下载odbc失败
执⾏触发器语句,报错,报错内容如下:
/* 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 */
---最终实现代码
DROP TRIGGER if EXISTS test1;
CREATE TRIGGER test1 AFTER UPDATE iggletest_triggle FOR EACH ROW
BEGIN
INSERT INTO triggletest(id,name) values (new.id,new.name);
END;
分析,由于访问⼯具HediSQL,导致⽆法正常创建触发器,相同语句,在HediSQL中执⾏,报错,使⽤shell调⽤mysql,直接执⾏程序,成功。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论