Database Design for Revisions?

I think the key question to ask here is ‘Who / What is going to be using the history’?

If it’s going to be mostly for reporting / human readable history, we’ve implemented this scheme in the past…

Create a table called ‘AuditTrail’ or something that has the following fields…

[ID] [int] IDENTITY(1,1) NOT NULL,
[UserID] [int] NULL,
[EventDate] [datetime] NOT NULL,
[TableName] [varchar](50) NOT NULL,
[RecordID] [varchar](20) NOT NULL,
[FieldName] [varchar](50) NULL,
[OldValue] [varchar](5000) NULL,
[NewValue] [varchar](5000) NULL

You can then add a ‘LastUpdatedByUserID’ column to all of your tables which should be set every time you do an update / insert on the table.

You can then add a trigger to every table to catch any insert / update that happens and creates an entry in this table for each field that’s changed. Because the table is also being supplied with the ‘LastUpdateByUserID’ for each update / insert, you can access this value in the trigger and use it when adding to the audit table.

We use the RecordID field to store the value of the key field of the table being updated. If it’s a combined key, we just do a string concatenation with a ‘~’ between the fields.

I’m sure this system may have drawbacks – for heavily updated databases the performance may be hit, but for my web-app, we get many more reads than writes and it seems to be performing pretty well. We even wrote a little VB.NET utility to automatically write the triggers based on the table definitions.

Just a thought!

Leave a Comment