views:

85

answers:

3

I'm trying to use a simple mysql database but tweak it so that every field is backed up up to an indefinite number of versions. The best way I can illustrate this is by replacing each and every field of every table with a stack of all the values this field has ever had (each of these values should be timestamped). I guess it's kind of like having customized version control for all my data..

Any ideas on how to do this?

A: 

A simple solution would be to add a version/revision field to the tables, and whenever a record is updated, instead of updating it in place, insert a copy with the changes applied and the version number incremented. Then when selecting, always choose the record with the latest version. That's roughly how most such schemes are implemented (e.g. Wikimedia does it pretty much this exact way).

Max Shawabkeh
I thought of this but then I have to copy every field in that row instead of only the field that has actually changed values.. Seems a bit wasteful, memory wise.
Shawn
+2  A: 

The usual method for "tracking any changes" to a table is to add insert/update/delete trigger procedures on the table and have those records saved in a history table.

For example, if your main data table is "ItemInfo" then you would also have an ItemInfo_History table that got a copy of the new record every time anything changed (via the triggers).

This keeps the performance of your primary table consistent, yet gives you access to the history of any changes if you need it.

Here are some examples, they are for SQL Server but they demonstrate the logic:

My Repository table My Repository History table My Repository Insert trigger procedure My Repository Update trigger procedure

Ron Savage
I don't exactly follow you yet, but you introduced me to triggers and they are probably what's I'll be using. Thanks
Shawn
Added links to some examples that might (hopefully) make the process clearer. :-)
Ron Savage
A: 

Hmm, what you're talking about sounds similar to Slowly Changing Dimension.

Be aware that version control on arbitrary database structures is officially a rather Hard Problem. :-)

Ken
It seems like slowly changing dimension requires that I copy every field in a row even if only one of the fields was altered.. Seems a bit wasteful, memory wise
Shawn
Well, that's why you use it for *slowly* changing dimensions.
Ken