Oracle触发器来记录表删除操作简单方法

最近项目中Oracle库中一个表log_gpackage有数据丢失现象,但因为没有启用归档,所以CDC和Dataguard都无法使用。google一下,最简单的方法,增加触发器处理逻辑:

 1 ---创建触发器
 2 --创建记录表,该表分别记录了,删除的用户、机器名、ip及删除时间等
 3 create table log_gpackage_history_1120 as 
 4 (select sid,username,program,machine,'000.000.000.000' ipaddress,sysdate moditime from v$session where 0=1);
 5 
 6 --创建目标表的触发器,只记录删除操作
 7 create or replace trigger log_gpackage_history_tr
 8 before delete on dbusrdms.log_gpackage
 9 for each row
10 begin
11 insert into log_gpackage_history_1120 select sid,username,program,machine,sys_context('userenv','ip_address'),sysdate from v$session 
12 where audsid = userenv('sessionid');
13 end;
14 
15 
16 --测试数据
17 SELECT * FROM log_gpackage;
18 delete from log_gpackage where LOGSEQNO='172886';
19 
20 
21 --检查测试结果
22 SELECT * FROM  log_gpackage_history_1120
23 
24 SID USERNAME  PROGRAM MACHINE IPADDRESS MODITIME
25 1 484 DBUSRDMS  plsqldev.exe  WORKGROUP\AP5 192.168.127.8   2018-11-20 23:00:18

 

posted @ 2018-11-20 23:02  邀月  阅读(2774)  评论(1编辑  收藏  举报