-- 2Insert data insert into test_update_trigger values(1,10),(2,20);
--3Set triggers
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) ,'Modify to:',cast(OLD.age as char))); end
-- 4 modifications 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 'Test trigger';
CREATE TABLE
test_log
test_log
(id
int(10) NOT NULL AUTO_INCREMENT,log_text
log_text
varchar(100) DEFAULT NULL,PRIMARY KEY (
id
),) ENGINE=InnoDB DEFAULT CHARSET=utf8 comment 'Log';
-- 2Insert data
insert into test_update_trigger values(1,10),(2,20);
--3Set triggers
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) ,'Modify to:',cast(OLD.age as char)));
end
-- 4 modifications
update test_update_trigger set age=30 where id=2;
-- 5 query results
select * from test_log;
id update_id log_text
1 2 30 is modified to: 20