-- 2插入数据 insert into test_update_trigger values(1,10),(2,20);
-- 3设置触发器
create trigger tri_test_update after update on test_update_trigger for each row begin insert into test_log(update_id,log_text) values(NEW.id,concat(cast(NEW.age as char),'修改为:',cast(OLD.age as char))); end
-- 4修改 update test_update_trigger set age=30 where id=2;
-- 1建表
CREATE TABLE
test_update_trigger
(id
int(10) NOT NULL AUTO_INCREMENT,age
varchar(10) DEFAULT NULL,PRIMARY KEY (
id
)) ENGINE=InnoDB DEFAULT CHARSET=utf8 comment '测试触发器';
CREATE TABLE
test_log
(id
int(10) NOT NULL AUTO_INCREMENT,log_text
varchar(100) DEFAULT NULL,PRIMARY KEY (
id
),) ENGINE=InnoDB DEFAULT CHARSET=utf8 comment '日志';
-- 2插入数据
insert into test_update_trigger values(1,10),(2,20);
-- 3设置触发器
create trigger tri_test_update
after update on test_update_trigger
for each row
begin
insert into test_log(update_id,log_text) values(NEW.id,concat(cast(NEW.age as char),'修改为:',cast(OLD.age as char)));
end
-- 4修改
update test_update_trigger set age=30 where id=2;
-- 5查询结果
select * from test_log;
id update_id log_text
1 2 30修改为:20