I have posted old post about
Log DDL Changes in Your Schema, Today I decided to post new post about Log Data Changes in Database(DML Operations)
I decided to create generic solution that can be used in any database.
The Idea
I will create two tables only for storing every data changes in application.
I will create generic
GENERATE_TRIGGER function (pass table name as parameter )to return script of trigger which I can use to log data changes in table.
I will store Data Logging in two separate tables(Master and detail Table) as below
Master table is for storing details about every transaction (DML) in database table
Detail table is for storing data changes in table data.
LOGGING_DATA_HDR Master Table
Contains main data about every DML applied to any table
1-
LOG_ID sequence column that based on
LOG_ID_SEQ sequence.
2-
TABLE_NAME refers to table which DML applied on it.
2-
PK_DATA contains primary key value of table.
If primary key is composite key, it separated columns by "-" string.
3-
ROW_ID refers to
ROWID of table..
4-
LOG_DATE refers to Time stamp execution of DML in table.
5-
OPERATION_TYPE refers to DML type.
INSERT ==> "I"
DELete ==> "D"
UPDATE==> "U"
6-
DB_USER refers to database user which executed the DML statement.
7-
CLIENT_IP refers to IP of machine which from it DML statement is executed.
8-
CLIENT_HOST refers to host name of machine which from it DML statement is executed.
9-
CLIENT_OS_USER refers to operating system user of machine which from it DML statement is executed.
10-
APP_USER refers to application user if I used application user, I get it from
GC$APP_USER variable in
MAHMOUD_LOGGING package.
LOGGING_DATA_DTL Detail Table
1-
LOG_ID is foreign key to
LOGGING_DATA_HDR table.
2-
COLUMN_NAME refers to column name in table.
3-
OLD_VALUE refers to old value before execution of DML statement.
4-
NEW_VALUE refers to new value after execution of DML Statement.