8

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?

1

4 Answers 4

8

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

3
  • 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
    May 5, 2010 at 4:15
  • Added links to some examples that might (hopefully) make the process clearer. :-)
    – Ron Savage
    May 5, 2010 at 17:27
  • 1
    @Ron Savage, the links you provided are now broken.
    – sdlins
    Oct 7, 2018 at 17:23
4

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. :-)

2
  • 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
    May 5, 2010 at 4:02
  • Well, that's why you use it for slowly changing dimensions.
    – Ken
    May 10, 2010 at 16:03
1

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).

1
  • 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
    May 5, 2010 at 4:01
-1

Maybe a tool can help you to do that for you. Have a look at nextep designer :

https://github.com/christophefondacci/nextep-designer

With this IDE you will be able to take snapshots of your database structure and data and put it under version control. After this you can compute the differences between any 2 versions and generate the appropriate SQL that can insert / update / delete your data.

Maybe this is an alternative way to achieve what you wanted.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.