Saturday, 1 October 2011

auditing by using of triggers

CREATE TABLE DEPT12
  ( DEPTNO NUMBER(2), DNAME VARCHAR2(14), LOC VARCHAR2(13)
  );

 --------------------------------------------

 INSERT INTO DEPT12 VALUES
  (20, 'RESEARCH', 'DALLAS' );
 INSERT INTO DEPT12 VALUES
  (30, 'SALES', 'CHICAGO' );
 INSERT INTO DEPT12 VALUES
  (40, 'OPERATIONS', 'BOSTON' );
 CREATE TABLE DEPT$AUDIT
  (
    DEPTNO      NUMBER           ,
    DNAME       VARCHAR2(14 byte),
    LOC         VARCHAR2(13 byte),
    CHANGE_TYPE VARCHAR2(1 byte) ,
    CHANGED_BY  VARCHAR2(30 byte),
    CHANGED_TIME DATE
  );
 
select *From dept$audit



 ---------------------------------------------------
 CREATE OR REPLACE TRIGGER auditDEPTAR AFTER
 INSERT OR
 UPDATE OR
 DELETE ON DEPT12 FOR EACH ROW DECLARE my DEPT$audit%ROWTYPE;
BEGIN
  IF inserting THEN
    my.change_type := 'I';
  elsif updating THEN
    my.change_type :='U';
  ELSE
    my.change_type := 'D';
  END IF;
  my.changed_by   := USER;
  my.changed_time := sysdate;
  CASE my.change_type
  WHEN 'I' THEN
    my.DEPTNO := :new.DEPTNO;
    my.DNAME  := :new.DNAME;
    my.LOC    := :new.LOC;
  ELSE
    my.DEPTNO := :old.DEPTNO;
    my.DNAME  := :old.DNAME;
    my.LOC    := :old.LOC;
  END CASE;
   INSERT INTO DEPT$AUDIT VALUES my ;
END;
-------------------------------------
INSERT INTO DEPT12 VALUES
  (70, 'ACCOUNTING', 'NEW YORK'
  );
  -------------------------
  TRIGGER auditDEPTAR Compiled.
1 rows inserted
DEPTNO                 DNAME          LOC           CHANGE_TYPE CHANGED_BY                     CHANGED_TIME             
---------------------- -------------- ------------- ----------- ------------------------------ -------------------------
70                     ACCOUNTING     NEW YORK      I           APPS                           01-OCT-11                

No comments:

Post a Comment