幽灵资源网 Design By www.bzswh.com

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

于是进行测试

--建立测试表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);
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 */
---最终实现代码
DROP TRIGGER if EXISTS test1;
CREATE TRIGGER test1 AFTER UPDATE ON test.triggletest_triggle FOR EACH ROW
BEGIN
INSERT INTO triggletest(id,name) values (new.id,new.name);
END; 

分析,由于访问工具HediSQL,导致无法正常创建触发器,相同语句,在HediSQL中执行,报错,使用shell调用mysql,直接执行程序,成功。

标签:
mysql触发器insert,update

幽灵资源网 Design By www.bzswh.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
幽灵资源网 Design By www.bzswh.com