3、基于值的审核:使用了数据库触发器。在插入、更新或删除记录时,就会运行一个包括记录事件全部细节的pl/sql代码块。
前面所描述的数据库审核可以捕获到针对某个表执行一条命令的事实,但是没有跟踪受影响的行。例如,如果执行AUDIT INSERT ON GYJ.T10命令,只要在指定表中插入一行就会生成一条审核记录,但是该审核记录不会包含插入行的实际值。有些时侯,我们希望捕获这些实际值地,此时就需要使用数据库触发器。
gyj@OCM> create table t1 (id int,name varchar2(100));
Table created.
gyj@OCM> insert into t1 values(1,'Joe');
1 row created.
gyj@OCM> insert into t1 values(2,'Tom');
1 row created.
gyj@OCM> insert into t1 values(3,'Rose');
1 row created.
gyj@OCM> insert into t1 values(4,'Jack');
1 row created.
gyj@OCM> insert into t1 values(5,'jemmy');
1 row created.
gyj@OCM> commit;
Commit complete.
gyj@OCM> create table t_audit (os_user varchar2(20),ip_address varchar2(20),change varchar2(100));
Table created.
创建触发器语句,操作如下:
gyj@OCM> CREATE OR REPLACE TRIGGER gyj.name_audit
2 AFTER UPDATE OF name
3 ON gyj.t1
4 REFERENCING NEW AS NEW OLD AS OLD
5 FOR EACH ROW
6 BEGIN
7 IF :old.name != :new.name THEN
8 INSERT INTO gyj.t_audit
9 VALUES(sys_context('userenv','os_user'),sys_context('userenv','ip_address'),:new.id||' name changed from '||:old.name||' to '||:new.name);
10 END IF;
11 END;
12 /
gyj@OCM> update t1 set name='guoyJoe' where name='Joe';
1 row updated.
gyj@OCM> update t1 set name='Tony' where id=2;
1 row updated.
gyj@OCM> update t1 set name='coco' where id=3;
1 row updated.
gyj@OCM> select * from t_audit;
OS_USER IP_ADDRESS CHANGE
---------- ------------------ ---------------------------------------
Oracle 1 name changed from Joe to guoyJoe
oracle 2 name changed from Tom to Tony
oracle 3 name changed from Rose to coco
