Quantcast
Channel: dBforums – Everything on Databases, Design, Developers and Administrators
Viewing all articles
Browse latest Browse all 13329

How to write an automatic journal?

$
0
0
I would like to write an automatic journal for my table 'booking', which logs every insert, update and delete.

The (simplified) table 'booking' looks like this:

Code:

CREATE TABLE "BOOKING"
(
  ID bigint PRIMARY KEY NOT NULL,
  VERSION bigint NOT NULL,
  CUSTOMER_ID bigint NOT NULL,
  TEXT varchar(255)
)

and the 'bookingjournal' I would like to create looks like this:

Code:

CREATE TABLE "BOOKINGJOURNAL"
(
  ID bigint PRIMARY KEY NOT NULL,
  VERSION bigint NOT NULL,
  CUSTOMER_ID bigint NOT NULL,
  TEXT varchar(255),
  OPERATION varchar(255)
)

Let's say my application makes the following operations on the table 'booking':

Code:

INSERT INTO BOOKING VALUES(1, 0, 42, 'Booking of customer 42.');
INSERT INTO BOOKING VALUES(2, 0, 43, 'Booking of customer 43.');
UPDATE BOOKING SET (TEXT, VERSION) = ('Changed booking of customer 42.', 1) WHERE ID = 1;
UPDATE BOOKING SET (TEXT, VERSION) = ('Changed booking of customer 43.', 1) WHERE ID = 2;
DELETE FROM BOOKING WHERE ID = 2;

I would like the 'bookingjournal' to have the following entries:

Code:

ID, VERSION, CUSTOMER_ID, TEXT, OPERATION
1, 0, 42, 'Booking of customer 42.', 'INSERT'
2, 0, 43, 'Booking of customer 43.', 'INSERT'
1, 1, 42, 'Changed booking of customer 42.', 'UPDATE'
2, 1, 43, 'Changed booking of customer 43.', 'UPDATE'
2, 1, 43, 'Changed booking of customer 43.', 'DELETE'

What would be the simplest solution for achieving this? Trigger? PL/SQL-Script? And could someone possibly show me the pieces of code I need?

Any help is greatly appreciated.

Olel

Viewing all articles
Browse latest Browse all 13329