SCOTT@ bys001>conn bys/bys BYS@ bys001>create table emp_audit(oldempno number(4),newempno number(4),oldsal number(7,2),newsal number(7,2),luser varchar2(100),sdate date); 创建触发器: create or replace trigger trg_emp_audit after insert or delete or update of sal,empno on scott.emp for each row declare pragma autonomous_transaction; begin insert into emp_audit values (old.empno,:new.empno,old.sal,:new.sal,user,sysdate); commit; end; / Trigger created. BYS@ bys001>select * from emp_audit; no rows selected会话二:新建SCOTT用户的会话,更新一条记录
SCOTT@ bys001>update emp set sal=9999,empno=9998 where empno=7369; 1 row updated.会话一:查询审计信息已经生成--和会话1 的操作是否提交无关
BYS@ bys001>col luser for a10 BYS@ bys001>select * from emp_audit; OLDEMPNO NEWEMPNO OLDSAL NEWSAL LUSER SDATE ---------- ---------- ---------- ---------- ---------- ------------------- 7369 9998 800 9999 SCOTT 2013/07/25 20:21:21
BYS@ bys001>drop trigger trg_emp_audit; Trigger dropped. BYS@ bys001>drop table emp_audit purge; Table dropped.####################################################################
SCOTT@ bys001>create table emp_audit(oldempno number(4),newempno number(4),oldsal number(7,2),newsal number(7,2),luser varchar2(100),sdate date); create or replace trigger trg_emp_audit after insert or delete or update of sal,empno on scott.emp for each row declare pragma autonomous_transaction; begin insert into emp_audit values (old.empno,:new.empno,old.sal,:new.sal,user,sysdate); commit; end; / Trigger created. SCOTT@ bys001>col luser for a10 SCOTT@ bys001>select * from emp_audit; no rows selected 只更新sal字段 SCOTT@ bys001>update emp set sal=9999 where empno=7369; 1 row updated. 查询审计表里的信息 SCOTT@ bys001>select * from emp_audit; OLDEMPNO NEWEMPNO OLDSAL NEWSAL LUSER SDATE ---------- ---------- ---------- ---------- ---------- ------------------- 7369 7369 800 9999 SCOTT 2013/07/25 20:05:48#####################################################################
20:37:13 SCOTT@ bys001>grant select on emp to test; Grant succeeded.会话二:
BYS@ bys001>conn test/test Connected. TEST@ bys001>create table emp_audit(oldempno number(4),newempno number(4),oldsal number(7,2),newsal number(7,2),luser varchar2(100),sdate date); Table created. create or replace trigger trg_emp_audit after insert or delete or update of sal,empno on scott.emp for each row declare pragma autonomous_transaction; begin insert into emp_audit values (:old.empno,:new.empno,:old.sal,:new.sal,user,sysdate); commit; end; / after insert or delete or update of sal,empno on scott.emp for each row * ERROR at line 2: ORA-00942: table or view does not exist
会话一:给TEST用户在EMP上的增删改权限,查询权限上一步已经给过。
20:37:26 SCOTT@ bys001>grant insert,update,delete on emp to test; Grant succeeded.会话二:
create or replace trigger trg_emp_audit after insert or delete or update of sal,empno on scott.emp for each row declare pragma autonomous_transaction; begin insert into emp_audit values (:old.empno,:new.empno,:old.sal,:new.sal,user,sysdate); commit; end; / after insert or delete or update of sal,empno on scott.emp for each row * ERROR at line 2: ORA-01031: insufficient privileges