触发器实现源表操作(增,删,改)自动补录操作日志
Posted zja001
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了触发器实现源表操作(增,删,改)自动补录操作日志相关的知识,希望对你有一定的参考价值。
1:源表数据修改时将修改的数据插入临时表,标记字段为修改
2:源表数据删除时将删除数据插入临时表,编辑字段为删除
3:源表数据新增时将新增数据插入临时表,标记字段为新增
2:源表数据删除时将删除数据插入临时表,编辑字段为删除
3:源表数据新增时将新增数据插入临时表,标记字段为新增
一:定义临时表字段
1:查看源数据testb表
-------------如何修改mysql已经建好表的编码------------------------------
ALTER TABLE 表名 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
1:查看源数据testb表
-------------如何修改mysql已经建好表的编码------------------------------
ALTER TABLE 表名 DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;
2:创建第一个触发器---插入触发器
create trigger tri_userC_insert AFTER insert
on `usera` FOR EACH ROW
begin
insert into userc(idC,nameC,ageC,flogS,flog_time) select id,name,age,‘插入‘,now() from usera where id not in (select idC from `userc`);
end
create trigger tri_userC_insert AFTER insert
on `usera` FOR EACH ROW
begin
insert into userc(idC,nameC,ageC,flogS,flog_time) select id,name,age,‘插入‘,now() from usera where id not in (select idC from `userc`);
end
3:创建第二个触发器---更新触发器
create trigger tri_userC AFTER update
on `usera` FOR EACH ROW
begin
set @point=(select name from `usera` where id = OLD.id );
set @point02=(select age from `usera` where id = OLD.id );
if @point != old.name then
update `userc` set [email protected] where idC=OLD.id;
update `userc` set flogS=‘更新‘ where idC=OLD.id;
update `userc` set flog_time=now() where idC=OLD.id;
end if;
if @point02 != old.age then
update `userc` set [email protected] where idC=OLD.id;
update `userc` set flogS=‘更新‘ where idC=OLD.id;
update `userc` set flog_time=now() where idC=OLD.id;
end if;
end
4:创建第三个触发器---删除触发器
create trigger tri_userC_delete AFTER delete
on `usera` FOR EACH ROW
begin
update userc set flogs = ‘删除‘ where idC not in (select id from `usera`);
update userc set flog_time = now() where idC not in (select id from `usera`);
end
5:建表语句(A表)
CREATE TABLE `usera` (
`id` int(10) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`age` int(3) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
create trigger tri_userC AFTER update
on `usera` FOR EACH ROW
begin
set @point=(select name from `usera` where id = OLD.id );
set @point02=(select age from `usera` where id = OLD.id );
if @point != old.name then
update `userc` set [email protected] where idC=OLD.id;
update `userc` set flogS=‘更新‘ where idC=OLD.id;
update `userc` set flog_time=now() where idC=OLD.id;
end if;
if @point02 != old.age then
update `userc` set [email protected] where idC=OLD.id;
update `userc` set flogS=‘更新‘ where idC=OLD.id;
update `userc` set flog_time=now() where idC=OLD.id;
end if;
end
4:创建第三个触发器---删除触发器
create trigger tri_userC_delete AFTER delete
on `usera` FOR EACH ROW
begin
update userc set flogs = ‘删除‘ where idC not in (select id from `usera`);
update userc set flog_time = now() where idC not in (select id from `usera`);
end
5:建表语句(A表)
CREATE TABLE `usera` (
`id` int(10) NOT NULL,
`name` varchar(50) DEFAULT NULL,
`age` int(3) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
6:建表语句(C表)
CREATE TABLE `userc` (
`idC` int(255) NOT NULL,
`nameC` varchar(255) DEFAULT NULL,
`ageC` int(255) DEFAULT NULL,
`flogs` varchar(255) DEFAULT NULL,
`flog_time` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE TABLE `userc` (
`idC` int(255) NOT NULL,
`nameC` varchar(255) DEFAULT NULL,
`ageC` int(255) DEFAULT NULL,
`flogs` varchar(255) DEFAULT NULL,
`flog_time` datetime DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
以上是关于触发器实现源表操作(增,删,改)自动补录操作日志的主要内容,如果未能解决你的问题,请参考以下文章