Oracle中審計刪除(DELETE)操作的觸發器

rongshiyuan發表於2012-08-11

Oracle中審計刪除(DELETE)操作的觸發器

轉載連結:

 
幫朋友寫的一個簡單審計刪除操作的觸發器,供參考。
首先是授權:

SQL> connect / as sysdba
Connected.

SQL> grant select on v_$sql to eygle;

Grant succeeded.

SQL> grant select on v_$session to eygle;

Grant succeeded.
建立測試表及審計表:

SQL> connect eygle/eygle
Connected.
SQL> create table eygle as select user_id,username from dba_users;

Table created.

SQL> desc eygle
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 USER_ID                                   NOT NULL NUMBER
 USERNAME                                  NOT NULL VARCHAR2(30)

SQL> create table eygle_delete_audit
  2  (user_id number,username varchar2(30),deleteby varchar2(30),deldate date,ipaddress varchar2(50),sqltext clob);

Table created.
建立如下觸發器:
CREATE OR REPLACE TRIGGER eygle_after_delete
    AFTER DELETE
        ON eygle
        FOR EACH ROW
BEGIN

INSERT INTO eygle_delete_audit
SELECT :old.user_id,:old.username,user,sysdate,SYS_CONTEXT ('userenv', 'ip_address')
,q.sql_text
FROM v$sql q, v$session s
WHERE s.audsid = (SELECT USERENV ('SESSIONID') FROM DUAL)
AND s.sql_id = q.sql_id;

END;
/
測試刪除操作:

SQL> connect eygle/eygle@rac1
Connected.
SQL> delete from eygle where user_id=46;

1 row deleted.

SQL> commit;

Commit complete.

SQL> truncate table eygle_delete_audit;

Table truncated.

SQL> delete from eygle where user_id=0;

1 row deleted.

SQL> commit;

Commit complete.

SQL> select * from eygle_delete_audit;

   USER_ID USERNAME                       DELETEBY
---------- ------------------------------ ------------------------------
DELDATE      IPADDRESS
------------ --------------------------------------------------
SQLTEXT
--------------------------------------------------------------------------------
         0 SYS                            EYGLE
12-JAN-11    172.16.3.248
delete from eygle where user_id=0

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/17252115/viewspace-740740/,如需轉載,請註明出處,否則將追究法律責任。

相關文章