Categories: PLSQL Tutorials

How to create External Audit table in oracle?

In my previous articles I have given detailed information about triggers, statement triggers and row triggers as well. While working with row level triggers I have explained you that we are using the row level trigger to create audit tables. The audit tables are important tables to check the multiple operations on table. These are really useful tables in oracle. The oracle has its own audit tables but in this article I would like to give you the external audit table to see the multiple operations on oracle table. This is really very important topic as so many programmers missed to create audit table and facing difficulty when get issue. In this article i would like to give you step by step process of creating external audit tables to capture multiple operations on table with its testing.

Create External Audit tables step by step :

In this section i would like to give you step by step way to create the audit table to capture multiple operations on Employee_cp table.

Step 1 : Create Copy of Employee table

Create table Employee_cp as select * from Employee;

Step 2 : Create External Audit table to capture multiple operations

Create Table Emp_Audit
( EMP_ID NUMBER,
OPERATION VARCHAR2(20),
OLD_SAL NUMBER,
NEW_SAL NUMBER,
OP_DATE DATE,
BY_USER VARCHAR2(100)
);

Step 3 : Creating the trigger for External Audit Table

Audit tables

This is most important step where we can create the trigger. We can create a trigger in such way that we can insert the values in Audit records to maintain it.


Create or replace trigger T_Emp_Audit
after insert or update of salary or delete
on Employee_cp
for each row
begin

/*** IF You are inserting the value in Employee_cp table also insert data in audit table***/
if inserting then
insert into Emp_Audit(EMP_ID,OPERATION,OLD_SAL,NEW_SAL,OP_DATE,BY_USER)
values (:new.employee_id,’Insertion’,null,:new.salary,sysdate,user);
end if;

/*** IF You are updating the value in Employee_cp table then insert data in audit table***/

if updating then
insert into Emp_Audit(EMP_ID,OPERATION,OLD_SAL,NEW_SAL,OP_DATE,BY_USER)
values (:old.employee_id,’updating’,:old.salary,:new.salary,sysdate,user);
end if;

/*** IF You are Deleting the value in Employee_cp table then insert data in audit table***/

if deleting then
insert into Emp_Audit(EMP_ID,OPERATION,OLD_SAL,NEW_SAL,OP_DATE,BY_USER)
values (:old.employee_id,’deleting’,:old.salary,null,sysdate,user);
end if;

end;

Kindly make sure that you are not using the commit or rollback in triggers. It is strictly avoided to use commit and rollback statements in trigger.

Step 4 : Testing the trigger and External audit table data.

Insert Operation :

If you are inserting the record in Employee_Cp table as follows,

insert into Emloyee_cp (EMPLOYEE_ID,FIRST_NAME,LAST_NAME,EMAIL,salary,hire_date,job_id)
values (1,’Amit’,’S’,’complexsql@gmail.com’,100000,sysdate,’BI engineer’);

Commit;

You can check the data in audit table using following statement,

Select *
to_char(OP_DATE,’dd-mm-yyyy hh24:mi:ss’)OP_DATE ,BY_USER
from Emp_Audit
order by OP_DATE;

Update Operation :

update Emloyee_cp
set salary=salary +10
where EMPLOYEE_ID=1;

You can check the audit table,

Select *
to_char(OP_DATE,’dd-mm-yyyy hh24:mi:ss’)OP_DATE ,BY_USER
from Emp_Audit
order by OP_DATE;

Delete Operation :

delete from Emloyee_cp
where EMPLOYEE_ID=1;

You can check delete operation in Audit table,

Select *
to_char(OP_DATE,’dd-mm-yyyy hh24:mi:ss’)OP_DATE ,BY_USER
from Emp_Audit
order by OP_DATE;

These audit tables are important to track multiple transactions. When there is million of data and you need to work on the issue mining the data then these audit tables plays an important roles. I hope you like this article is useful to you. If you like this article or if you have any questions about same kindly comment in comments section.

Amit S

Oracle Consultant with vast experience in Oracle BI and PL/SQL Development. Amiet is the admin head of this website who contributes by preparing tutorials and articles related to database technologies. He is responsible to manage the content and front-end of the website.

Share
Published by
Amit S

Recent Posts

The Essential Guide to Cryptocurrency Exchange Platform Development for Beginners and Beyond

Introduction Cryptocurrencies took the world by storm, setting up a new financial system and breaking…

5 months ago

Top 20 System Administrator Interview Questions and answers

In my previous article I have given Top 20 technical support interview questions with its…

8 months ago

Desktop Support Scenario Based Interview Questions

In my previous articles I have given 15 most asked desktop support interview questions with…

8 months ago

How Do Business Analysts Use SQL for Their Needs?

A business analyst is someone who is versed in processes of data analysis used for…

8 months ago

Top 15 Control-M Interview Questions with Answers

In my previous article I have already given top questions and answers for Desktop support…

8 months ago

Top 20 SQL Interview Questions for Production Support

In my previous article I have given unix production support interview questions and answers. In…

8 months ago