Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ways to Maintain Data History in SQL Server 2008 Database

For a long time, we've wanted to create a case management system where no history is ever lost. When a change is made, we want to record that change, but have the ability to go back to any point in time and see what the record looked like. I wanted to pose this question to the Stack Overflow community to see what are some ways of doing this, is there technology already in place to achieve this?

like image 390
Dylan Vester Avatar asked Oct 06 '10 17:10

Dylan Vester


People also ask

How do you maintain history in SQL Server?

SQL Server 2016 introduced a new feature, Temporal Tables, which allow you to keep a historical record of all of the versions of each row in a table. As rows get introduced, changed, and deleted over time, you can always see what the table looked like during a certain time period or at a specific point in time.

How can we track history of data changes in SQL?

SQL Server provides two features that track changes to data in a database: change data capture and change tracking. These features enable applications to determine the DML changes (insert, update, and delete operations) that were made to user tables in a database.

How do I find the history of a SQL Server database?

Using SQL Server Management StudioIn Object Explorer, connect to an instance of the SQL Server Database Engine, and then expand that instance. Expand SQL Server Agent, and then expand Jobs. Right-click a job, and then click View History. In the Log File Viewer, view the job history.

How do you View and maintain database backup history?

In SSMS object explorer panel, right-click the database. From the right-click menu select Reports >> Standard Reports >> Backup and Restore Events. In the report, you can expand the Successful Backup Operations section, to see the backup history.


2 Answers

Yes, that technology definitely exists - it's a bit of an effort to implement it and do so correctly.

What you're looking for is called temporal databases - see some resources:

  • Temporal Database on Wikipedia
  • Database Design: A Point in Time Architecture
  • Temporal Database as a Ph.D. thesis
  • StackOverflow post: why do we need temporal databases?
like image 146
marc_s Avatar answered Sep 28 '22 20:09

marc_s


I'm not sure how a temporal database like marc_s mentioned works, but if you're using SQL Server 2008 or later, you can take advantage of its built-in Change Data Capture (CDC) functionality:

  • Change Data Capture (MSDN)
  • Basics of Change Data Capture (MSDN)
  • Using Change Data Capture
  • Sql Server 2008: Change Data Capture (CDC)

Enabling CDC uses the replication transaction log to store the inserts, updates, and deletes for a table and creates table-valued functions which allow you to retrieve the rows as of a given date/time, or to retrieve just the changes.

You can't rely on CDC alone, though, because your transaction log will become unmanageably big and slow. So what you do is:

  • enable CDC,
  • create a history table using the same schema as the original table, but adding a couple more columns for storing row version information (much like a slowly-changing dimension in a relational OLAP database), and
  • create a job which will periodically polls the CDC functions for changes since its last load and pushes them to the history table

Then you can then use the history table in your queries, joining to it as you normally would, but with an additional predicate(s) to get the record "as-of" whatever date you want.

like image 28
utexaspunk Avatar answered Sep 28 '22 21:09

utexaspunk