Log insert, update, delete for a table : Audit Log Table « Trigger « Oracle PL / SQL






Log insert, update, delete for a table

  

SQL>
SQL> CREATE TABLE employees
  2  ( employee_id          number(10)      not null,
  3    last_name            varchar2(50)      not null,
  4    email                varchar2(30),
  5    hire_date            date,
  6    job_id               varchar2(30),
  7    department_id        number(10),
  8    salary               number(6),
  9    manager_id           number(6)
 10  );

Table created.

SQL>
SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary,department_id ,manager_id)
  2                values ( 1001, 'Lawson', 'lawson@g.com', '01-JAN-2002','MGR', 30000,1 ,1004);

1 row created.

SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)
  2                values ( 1002, 'Wells', 'wells@g.com', '01-JAN-2002', 'DBA', 20000,2, 1005 );

1 row created.

SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id ,manager_id)
  2                 values( 1003, 'Bliss', 'bliss@g.com', '01-JAN-2002', 'PROG', 24000,3 ,1004);

1 row created.

SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
  2                 values( 1004,  'Kyte', 'tkyte@a.com', SYSDATE-3650, 'MGR',25000 ,4, 1005);

1 row created.

SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
  2                 values( 1005, 'Viper', 'sdillon@a .com', SYSDATE, 'PROG', 20000, 1, 1006);

1 row created.

SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id,manager_id)
  2                 values( 1006, 'Beck', 'clbeck@g.com', SYSDATE, 'PROG', 20000, 2, null);

1 row created.

SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
  2                 values( 1007, 'Java', 'java01@g.com', SYSDATE, 'PROG', 20000, 3, 1006);

1 row created.

SQL>
SQL> insert into employees( employee_id, last_name, email, hire_date, job_id, salary, department_id, manager_id)
  2                 values( 1008, 'Oracle', 'wvelasq@g.com', SYSDATE, 'DBA', 20000, 4, 1006);

1 row created.

SQL>
SQL> create table employees_log(
  2    who varchar2(30),
  3    when date );

Table created.

SQL>
SQL> create or replace trigger biud_employees_copy
  2    before insert or update or delete
  3       on employees
  4  begin
  5    insert into employees_log(who, when )values(user, sysdate );
  6  end;
  7  /

Trigger created.

SQL>
SQL> update employees set salary = salary * 1.1;

8 rows updated.

SQL>
SQL> select * from employees_log;

WHO                            WHEN
------------------------------ ---------
JAVA2S                         13-JUN-08

SQL>
SQL> delete from employees where department_id = 10;

0 rows deleted.

SQL>
SQL> select * from employees_log;

WHO                            WHEN
------------------------------ ---------
JAVA2S                         13-JUN-08
JAVA2S                         13-JUN-08

SQL>
SQL>
SQL> drop table employees;

Table dropped.

SQL> drop table employees_log;


SQL>

   
  








Related examples in the same category

1.Log and audit data change
2.Log user name and system time in a trigger
3.Logging All Operatins Using Autonumbering
4.Logging All Operations
5.Logging INSERT Operations
6.Logging INSERT Operations With WHEN Conditions
7.Trigger for auditing