Click here to Skip to main content
15,861,366 members
Articles / Database Development / SQL Server

Record Versioning with SQL Server

Rate me:
Please Sign up or sign in to vote.
4.71/5 (50 votes)
15 Feb 2007CPOL8 min read 182.3K   498   114   58
Learn how to store data with a version history.

Introduction

Maintaining a version history of SQL Server data has many benefits, but the top three are:

  • Undo history
  • Auditing
  • Soft delete

Record versioning imposes a layer on top of CRUD operations that make them more complex, especially on Update and Delete. No longer can you simply delete a record. Instead, you must flag it as deleted ("soft delete"). No longer can you simply update a record; instead, you must perform a soft delete followed by an insert.

Reporting is also a challenge. Most reporting frameworks do not understand the concept of versioned data. They expect each record to be a distinct data item, not a 'version' of a data item it has already seen.

No versioning solution will circumvent the fundamental challenges to versioned records, but we can greatly improve on the traditional approach to auditing data.

The typical solution

Record versioning is normally accomplished by creating separate audit tables that mirror the schema of the main tables.

Image 1

Does that look right to you? It has a few bad smells to me.

  • I don't like the schema duplication. It adds to the clutter of tables, makes maintenance more difficult, and in general, makes it harder for new developers to digest. I think we can do better.
  • It feels unsafe. The fundamental principal of moving data involves deleting from the old destination. I think we can do better.
  • It's not friendly with ORM. If you use ORM tools or handle your audit trail with business objects, you are forced to copy each field explicitly, from the old business object to the new 'audit' business object. Again, I think we can do better.
  • It doesn't scale. If I want to add a BlogComment table, I have to add another audit table. Now, not only do we have schema duplication, but we have duplicate abstractions of auditing that can grow apart over time. Imagine years later when the BlogComment_Archive table behaves differently than the Blog_Archive table. That makes it even harder to comprehend the schema.
  • I don't want to write reports against this schema. I have to point to separate tables when I want historical drill-down, and that seems unnecessary.

The better plan

As it turns out, we indeed can do much, much better! By using some clever entity inheritance, we can solve the audit problem for all tables in the database instead of just one at a time.

The solution involves a base audit table from which all auditable tables inherit. Study the following diagram and then look at the notes below.

Image 2

  • The Audit table contains all the version information. Date stamp, active state, who updated it. Your audit requirements may include other fields here.
  • The Audit table contains a PermanentRecordId. Because the PK of Blog records will change as you insert new versions, we need a permanent ID to identify a single blog entry and group versions of the same blog entry.
  • The Blog table shares a 1:1 relationship on primary keys. Audit.Id is the PK and Blog.Id is the FK. Notice that Blog.Id is not an identity column.
  • The Blog table looks much cleaner without all the audit garbage distraction. When you look at the Blog table, you immediately understand its purpose. Likewise, the Audit table is immediately obvious.

CRUD Operations

Let's try out a few CRUD operations to see how this new approach feels.

Create (Insert) and Read (Select)

Entity inheritance generally requires two more insertions because you must insert into multiple tables for one complete 'record'.

In this case, insertion now involves two operations. First, you must insert a record into the base Audit table. Then, you must get the PK ID of that inserted record for use with the second insertion into the Blog table.

Multiple insertions per operation is one drawback to the entity inheritance strategy, but it can be encapsulated. For example, the following insertion sample could be converted into a Stored Procedure that takes the Blog table values and the value for Audit.Updated_By.

SQL
/* Setup */
delete from blog
delete from comment
delete from audit

declare @id bigint
/* ------- */

/* Blog insert */
insert into Audit(Updated_By) values ('Ben')
select @id = @@identity
insert into Blog (Id, UserId, Title, Body) values ( @id, 1, 'My first entry', 'My blog body')
select * from Blog join Audit on Blog.Id = Audit.Id where IsActive=1

Image 3

As you can see, the Audit table kicked right in and did its job. We have a PermanentRecordId for this blog entry, and all other information is intact. Notice also that the ID columns are synchronized, and the record is marked as active.

While it's true that you must perform an INNER JOIN with a WHERE to select anything using this strategy, the operation is not costly because it is performed on indexed fields.

Update

In versioned recording, an update is really a soft delete followed by an insert.

Soft deletes are performed directly against the audit table. This is one nice feature: to perform a soft delete, you don't even need to know the record type. This means you could make a single Stored Procedure, spSoftDelete(id), that accepts the ID of the record to soft delete.

SQL
/* Blog update */
declare @BlogRecordId uniqueidentifier
select @BlogRecordId=PermanentRecordId from Audit where Id = @id
update Audit set IsActive=0 where Id=@id
insert into Audit(Updated_By, PermanentRecordId) values ('Ben', @BlogRecordId)
insert into Blog (Id, UserId, Title, Body) 
   values ( @@identity, 1, 'My updated first entry', 'My blog body is different now')
select * from Blog join Audit on Blog.Id = Audit.Id

Image 4

Now we have two entries with the same PermanentRecordId. That means they are different versions of the same logical record. But notice that the old record is no longer active. We could select MAX(Created), but an active flag is faster, and as you will see for Undo operations, necessary.

Delete

Deletes retire all versions of a record. This can be performed on the Audit table alone, making it easy to encapsulate.

SQL
/* Blog delete */
update Audit set IsActive=0 where PermanentRecordId=@BlogRecordId
select * from Blog join Audit on Blog.Id = Audit.Id

Image 5

Alternately, if you have the exact Blog.Id or Audit.Id, you can set IsActive that way. As long as all your update operations are done correctly, there should be only one record where IsActive=1. You could even set a constraint to check this.

CRUD Extensions

Versioning opens the possibility of extended operations, such as undo/redo. To use these CRUD extensions, add two more versions for a total of four blog entries.

SQL
/* Setup for CRUD extensions */
insert into Audit(Updated_By, PermanentRecordId) values ('Ben', @BlogRecordId)
insert into Blog (Id, UserId, Title, Body) 
   values ( @@identity, 1, 'My 3rd updated first entry', 'My blog body is different now')
update Audit set IsActive=0 where PermanentRecordId=@BlogRecordId
insert into Audit(Updated_By, PermanentRecordId) values ('Ben', @BlogRecordId)
insert into Blog (Id, UserId, Title, Body) 
   values ( @@identity, 1, 'My 4th updated first entry', 'My blog body is different now')
/* ------------------------- */

Undo/Rollback/Revert

An undo operation involves a few steps:

  1. Find the version directly preceding the active version, if there is one.
  2. Soft-delete the active version.
  3. Activate the version you found in step 1.

In the sample below, we find the version preceding the active version by relying on primary keys for chronological order. Version #3 will always have a PK ID smaller than version #4 of the same record. Some programmers do not like using indexed primary keys to determine the chronological order. If you are among them, you may want to consider using an alternate indexed column to maintain the chronological order. (Note: the SQL Server 'timestamp' data type will not work because records are updated when their Active status changes, and this changes the timestamp value.)

SQL
/* Rollback/undo to previous (to 3rd) */
select top 1 @id=Id from Audit where PermanentRecordId=@BlogRecordId
    and Id < (select Id from Audit where 
    PermanentRecordId=@BlogRecordId and IsActive=1)  order by Id desc
update Audit set IsActive=0 where PermanentRecordId=@BlogRecordId
update Audit set IsActive=1 where Id = @id
select * from Blog join Audit on Blog.Id = Audit.Id

/* Rollback/undo to previous (to 2nd) */
select top 1 @id=Id from Audit where PermanentRecordId=@BlogRecordId
    and Id < (select Id from Audit where 
    PermanentRecordId=@BlogRecordId and IsActive=1)  order by Id desc
update Audit set IsActive=0 where PermanentRecordId=@BlogRecordId
update Audit set IsActive=1 where Id = @id
select * from Blog join Audit on Blog.Id = Audit.Id

/* Rollback/undo to previous (to 1st) */
select top 1 @id=Id from Audit where PermanentRecordId=@BlogRecordId
    and Id < (select Id from Audit where 
    PermanentRecordId=@BlogRecordId and IsActive=1)  order by Id desc
update Audit set IsActive=0 where PermanentRecordId=@BlogRecordId
update Audit set IsActive=1 where Id = @id
select * from Blog join Audit on Blog.Id = Audit.Id

/* Rollback/undo to previous (still on 1st) */
select top 1 @id=Id from Audit where PermanentRecordId=@BlogRecordId
    and Id < (select Id from Audit where 
    PermanentRecordId=@BlogRecordId and IsActive=1)  order by Id desc
update Audit set IsActive=0 where PermanentRecordId=@BlogRecordId
update Audit set IsActive=1 where Id = @id
select * from Blog join Audit on Blog.Id = Audit.Id

Image 6

Redo

Redo is a simple reversal of the undo operation. Find the first version following the active version, and activate it.

SQL
/* Redo (to 2nd) */
select top 1 @id=Id from Audit where PermanentRecordId=@BlogRecordId
     and Id > (select Id from Audit where 
     PermanentRecordId=@BlogRecordId and IsActive=1)  order by Id asc
update Audit set IsActive=0 where PermanentRecordId=@BlogRecordId
update Audit set IsActive=1 where Id = @id
select * from Blog join Audit on Blog.Id = Audit.Id

Image 7

Adding New Auditable Tables

Adding new tables under our version control system is easy, and the CRUD code you already saw above is easily adapted, especially if you choose to encapsulate in Stored Procedures.

Let's add a table to support versionable, nested blog comments to demonstrate how similar the CRUD is for a new table. Also, notice that the CRUD demonstrated above requires zero changes when a new table is added. Outstanding.

At first, supporting multiple records from multiple tables sounds impossibly difficult, but it works with almost no added effort.

Image 8

  • Comment.Id is an FK to Audit.Id, just like Blog.Id.
  • Comment.PermanentBlogId will store the PermanentId for the blog entry. You could use an FK directly to Blog.Id, but the downside is that you would have to update all FK references when the active version of the Blog record changes. This way, you give up a little referential integrity (that you could add back with constraints if you wanted to), but you gain simplicity through decoupled revision changes.
  • Comment.ParentId points to Comment.Id to allow for nested comments.

So this is getting interesting! Now, comments can have versions just like blog entries, but nothing is ever lost. Let's check our CRUD again to be sure everything works.

Create (Insert)

The only difference here is that we need to reference the PermanentBlogId. That is a requirement only because Comments are owned by Blogs. Fortunately, we have that from earlier in the demo script. If you had the Blog.Id, you could use that to get the PermanentId of the Blog entry.

SQL
/* Comment insert */
insert into Audit(Updated_By) values ('Commenter')
select @id = @@identity
insert into Comment (Id, PermanentBlogId, CommentingUserId, Comment) 
   values ( @id, @BlogRecordId, 2, 'My first comment')
select * from Comment join Audit on Comment.Id = Audit.Id where IsActive=1

Image 9

If you study this, you will see that this comment version has a different PermanentRecordId. That is correct because this is a different set of versioned data. Each comment will get its own PermanentId.

Update

Update is identical excepting different values for the insertion.

SQL
/* Comment update */
declare @CommentRecordId uniqueidentifier
select @CommentRecordId=PermanentRecordId from Audit where Id = @id
update Audit set IsActive=0 where Id=@id
insert into Audit(Updated_By, PermanentRecordId) values ('Commenter', @CommentRecordId)
insert into Comment (Id, PermanentBlogId, CommentingUserId, Comment) 
   values ( @@identity, @BlogRecordId, 1, 'My updated first comment')
select * from Comment join Audit on Comment.Id = Audit.Id

Image 10

Select All Comments for a Blog Entry

Okay, so you're convinced now that the versioning works. You can see that the rollback and restore operations will be performed on the PermanentId values associated with each comment, and you can see each comment gets its own PermanentId. It's confusing to imagine that both Blog entries and Comments have versions!

So, how do you select all the comments for the current version of the blog? Easy.

SQL
/* Insert 2nd comment */
insert into Audit(Updated_By) values ('Commenter #2')
select @id = @@identity
insert into Comment (Id, PermanentBlogId, CommentingUserId, Comment) 
   values ( @id, @BlogRecordId, 2, 'I have a comment too')
select * from Comment join Audit on Comment.Id = Audit.Id where IsActive=1

/* Insert 3rd nested comment */
declare @parentId bigint
select @parentId = @id
insert into Audit(Updated_By) values ('Commenter #3')
select @id = @@identity
insert into Comment (Id, PermanentBlogId, CommentingUserId, Comment, ParentId) 
   values ( @id, @BlogRecordId, 3, 'I have a comment about the comment', @parentId)
select * from Comment join Audit on Comment.Id = Audit.Id where IsActive=1

/* Select all comments for this blog entry */
select * from Comment join Audit on Comment.Id = Audit.Id where IsActive=1 and
   PermanentBlogId = @BlogRecordId order by Created asc

Image 11

Further Study

If you are interested in this approach, I recommend looking into the following advanced topics:

  • Using record versioning with your favorite ORM tool
  • Using record versioning with code generated DALs
  • Entity/table inheritance
  • Hierarchical versions (for example, if you wanted a Blog rollback to also roll Comments back)
  • Writing views for reports
  • Encapsulating and abstracting insert/update operations

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionDoing this in Entity Framework Pin
Tony Valenti21-Feb-16 1:46
Tony Valenti21-Feb-16 1:46 
QuestionPotential snag Pin
Ed Cayce9-May-13 10:50
Ed Cayce9-May-13 10:50 
GeneralMy vote of 3 Pin
uzunyusuf18-Dec-10 9:12
uzunyusuf18-Dec-10 9:12 
GeneralBuy Mobile Phones Pin
jones jackson30-Nov-10 19:58
jones jackson30-Nov-10 19:58 
GeneralORM Mapper Pin
Member 44207608-May-08 8:31
Member 44207608-May-08 8:31 
GeneralJoin overhead Pin
gsundarr27-Apr-07 0:04
gsundarr27-Apr-07 0:04 
GeneralRe: Join overhead Pin
Ben Allfree27-Apr-07 1:17
Ben Allfree27-Apr-07 1:17 
GeneralJust a note about Delete Pin
Julien Berube19-Apr-07 5:34
Julien Berube19-Apr-07 5:34 
GeneralRe: Just a note about Delete Pin
Ben Allfree19-Apr-07 6:09
Ben Allfree19-Apr-07 6:09 
GeneralRe: Just a note about Delete [modified] Pin
Julien Berube2-May-07 8:22
Julien Berube2-May-07 8:22 
GeneralMany-to-Many Pin
Vomityn2-Apr-07 19:13
Vomityn2-Apr-07 19:13 
GeneralRe: Many-to-Many Pin
Ben Allfree2-Apr-07 19:41
Ben Allfree2-Apr-07 19:41 
GeneralRe: Many-to-Many Pin
Tiago Freitas Leal26-Aug-09 21:11
professionalTiago Freitas Leal26-Aug-09 21:11 
GeneralA suggestion for foreign keys Pin
ksalvage7-Mar-07 1:48
ksalvage7-Mar-07 1:48 
GeneralRe: A suggestion for foreign keys Pin
Ben Allfree7-Mar-07 3:35
Ben Allfree7-Mar-07 3:35 
GeneralRe: A suggestion for foreign keys Pin
Marc Greiner12-Mar-07 5:28
Marc Greiner12-Mar-07 5:28 
GeneralRe: A suggestion for foreign keys Pin
Ben Allfree12-Mar-07 6:21
Ben Allfree12-Mar-07 6:21 
QuestionRe: A suggestion for foreign keys Pin
User 397969431-Mar-07 12:48
User 397969431-Mar-07 12:48 
AnswerRe: A suggestion for foreign keys Pin
Ben Allfree31-Mar-07 13:37
Ben Allfree31-Mar-07 13:37 
QuestionRe: A suggestion for foreign keys Pin
joshcdsi17-Aug-07 3:30
joshcdsi17-Aug-07 3:30 
GeneralInteresting, but... don't do it. Pin
baavgai6-Mar-07 6:39
baavgai6-Mar-07 6:39 
GeneralRe: Interesting, but... don't do it. Pin
Ben Allfree6-Mar-07 7:51
Ben Allfree6-Mar-07 7:51 
GeneralRe: Interesting, but... don't do it. Pin
baavgai6-Mar-07 13:01
baavgai6-Mar-07 13:01 
GeneralRe: Interesting, but... don't do it. Pin
Ben Allfree6-Mar-07 13:11
Ben Allfree6-Mar-07 13:11 
GeneralRe: Interesting, but... don't do it. Pin
ksalvage7-Mar-07 1:32
ksalvage7-Mar-07 1:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.