Database
  Home arrow Database arrow Page 5 - Building an Audit Trail for Your Data
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
DATABASE

Building an Audit Trail for Your Data
By: Michael Kleane
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 38
    2004-02-25

    Table of Contents:
  • Building an Audit Trail for Your Data
  • Creating Audit Tables
  • Passing Table Names
  • Creating the Audit Triggers
  • Making the Trigger Code Readable
  • Auditing Tips/Techniques

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building an Audit Trail for Your Data - Making the Trigger Code Readable


    (Page 5 of 6 )

    To make the resulting trigger code somewhat readable, I’ve liberally used carriage returns CHR(10) in the print statements(a handy tool whenever dynamically generating code that might need to be debugged later). 

    In Oracle, the keywords ‘:new’ and ‘:old’ allow you to reference the new value and old value, respectively, of a table column.  Some logic is required in the trigger itself to differentiate between insert, updates, and deletes.  Two variables, v_prefix and v_condition, and a simple Oracle LOOP, help us build an IF/ELSE statement.

    Going back to our previous example, the build_audit_triggers script would look like the following when processing the audit_test table:


    CREATE OR REPLACE TRIGGER AUDIT_TEST$AUDTRG 
     AFTER INSERT 
    OR DELETE OR UPDATE ON AUDIT_TEST FOR EACH ROW 
    DECLARE 
    v_operation VARCHAR2
    (10) := NULL;   
    BEGIN    
    IF INSERTING THEN 
       v_operation 
    := 'INS'
    ELSIF UPDATING THEN 
       v_operation 
    := 'UPD'
    ELSE 
       v_operation 
    := 'DEL'
    END 
    IF;     
    IF INSERTING 
    OR UPDATING THEN
      INSERT INTO AUDIT_TEST$AUD 
    (  
      TEST_VARCHAR2
    ,     
      TEST_CHAR
    ,  
      TEST_LONG

      TEST_NUMBER

      TEST_NUMBER2
    ,  
      TEST_DATE
    ,   
      aud_action
    ,aud_timestamp,aud_user)  VALUES 

      
    :new.TEST_VARCHAR2,             
      
    :new.TEST_CHAR,       
      
    :new.TEST_LONG,     
      
    :new.TEST_NUMBER
      
    :new.TEST_NUMBER2,    
      
    :new.TEST_DATE,
      v_operation
    ,SYSDATE,USER);
    ELSE 
      INSERT INTO AUDIT_TEST$AUD 

      TEST_VARCHAR2
    ,   
      TEST_CHAR

      TEST_LONG
    ,
      TEST_NUMBER
    ,    
      TEST_NUMBER2
    ,   
      TEST_DATE
    ,    
      aud_action
    ,aud_timestamp,aud_user)  VALUES 
    (  
      
    :old.TEST_VARCHAR2,            
      
    :old.TEST_CHAR,        
      
    :old.TEST_LONG,    
      
    :old.TEST_NUMBER,             
      
    :old.TEST_NUMBER2,        
      
    :old.TEST_DATE,   
      v_operation
    ,SYSDATE,USER);

    END 
    IF; 
    END
    ;
    /


    Any inserts, updates, or deletes will be captured in audit_test$aud (actual DML statements omitted):


    SELECT test_varchar2,aud_action,TO_CHAR(aud_timestamp,’DD-MON-YY HH24:MI:SS’aud_timestamp,aud_user FROM audit_test$aud;

    TEST_VARCHAR2AUD_ACTIONAUD_TIMESTAMPAUD_USER
    varchar2_dataINS15-JAN-04 09:15:00 SCOTT
    updated_dataUPD15-JAN-04 09:16:12 SCOTT
    updated_data    DEL15-JAN-04 09:16:18SCOTT

    When displaying Oracle’s SYSDATE, you’ll need to use TO_CHAR to get the full date and time.

    More Database Articles
    More By Michael Kleane


     

    DATABASE ARTICLES

    - Excel Reference
    - Database Programming in C# with MySQL : Usin...
    - Formatting Techniques for Data Access from E...
    - Data Access from Excel VBA
    - Generating a Multiple Table Crystal Report u...
    - ADO and the Command Object
    - On Wiring Up an ADO Data Control
    - Reading and Writing to Files on the Intranet
    - Using ADO Record to Create and Navigate Intr...
    - Using Data Access Pages to Access Data on a ...
    - Using ADO with the SQL Native Client
    - ADO`s Stream Object
    - Opening a Record Object Referencing an Open ...
    - Introducing Jasper (SQL Anywhere 10 Beta)
    - Creating a Database Project in VS 2005

     
    Application Delivery: Everything You Wanted to Know, but Didn`t Know You Needed to Ask
    A comprehensive guide to examining the topics of Wide-area Data Services and app....

     
    Best Practices: Safe and Secure Hardware Asset Recovery
    Companies increasingly must meet EPA and local requirements for the disposal of ....

     
    Managing SSL Security in Multi-Server Environments
    Read this white paper to learn how to simplify management of your organization's....

     
    Open Source Security Myths
    Open Source Software (OSS) is computer software whose source code is available t....

     
    Power and Cooling Capacity Management for Data Centers
    This paper describes the principles for achieving power and cooling capacity man....

     




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT