• 触发器实现源表操作(增,删,改)自动补录操作日志


    1:源表数据修改时将修改的数据插入临时表,标记字段为修改
    2:源表数据删除时将删除数据插入临时表,编辑字段为删除
    3:源表数据新增时将新增数据插入临时表,标记字段为新增
    一:定义临时表字段
    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
    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 nameC=@point 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 ageC=@point02 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;
     
     
  • 相关阅读:
    在C#中使用SQL存储过程说明
    asp.net后台获取html控件值
    SQL字符串操作汇总[记住这个]select substring(quantityperunit,charindex('',quantityperunit)+1,100) as 结果 from products
    分页控件AspnetPager的用法,巩固用
    摆脱Access在.net中中模糊查询,老错的困扰
    基于黑金开发板的秒表verilog hdl程序
    2808 sci 接收中断
    黑金开发板状态机实现的可用按键控制的流水灯
    基于黑金开发板的键盘边沿检测程序
    可以使用键盘实现加减数的数码管的verilog hdl程序(基于黑金开发板)
  • 原文地址:https://www.cnblogs.com/zja001/p/10095436.html
Copyright © 2020-2023  润新知