views:

29

answers:

2

This is a fairly common problem, it probably has a name, I just don't know what it is.

A.) User sees obscure piece of information in Row B of L_OBSCURE_INFO displayed on some screen at a certain point. It is in table L_Obscure_info.

B.) Under certain circumstances we want to correctly delete data in L_OBSCURE_INFO. Unfortunately, nobody accounted for the fact that the user might want to backtrack and see some random piece of information that was most recently in L_OBSCURE_INFO.

C.) The system is enormous and L_OBSCURE_INFO is used all the time. You have no idea what the ramifications are of implementing some kind of hack and whatever you do, you don't want to introduce more bugs.

I think the best approach would be to create an L_OBSCURE_INFO_HISTORY table and record a record in there every time you change data. But god help your ensuring it's accurate in this system where L_OBSCURE_INFO is being touched everywhere and you don't have time to implement L_OBSCURE_INFO_HISTORY.

Is there a particularly easy, clever design solution for this kind of problem -- basically an elegant database hack? If not, is this kind of design problem under a particular class of problems or have a name?

+2  A: 

Use a database table trigger to maintain your history in a second table.

gmagana
Ahhhh, I live in a world where I must support cross-platform compability between various versions of SQL, Oracle and DB2 so I do not get to play with fun toys like "triggers" except for at home on my private VM, which has SQL 2008 Developer.Curse you!!!! No, just kidding, your answer is fantastic and a perfectly logical solution assuming the trigger does not heavily tax performance (I see no reason it would).
John Sullivan
+2  A: 

If seeing the history of deletes (and I would assume updates) is the requirement, the history table you described is likely your best option.

If, as you say, the table is "being touched everywhere" and you can't control writing of the history via application code, you will have to implement a database trigger that essentially snapshots the row each time a change is made (or only deletes, if that is the limit of your need).

Another option might be to add an "is_active" column in the table, and instead of allowing deletes, allow users to inactivate rows. This is effectively a "soft delete", which is not always a good practice, because filtering out inactive records in every query is a pain.

If you DO have the option of implementing a solution in application code (i.e. no one is touching data except through the application), you could implement a logging mechanism to record the state of a row that is about to get deleted.

Phil Sandler
Soft deletion is a brilliant idea, but implementing during production in a prominently featured table in a large system seems like a dangerous idea.The trigger is also a great suggestion. I'm flagging this as my answer -- both you and gmagana gave me excellent answers. His was simple and you explored more options.Thanks for your time! It's educational. I wasn't aware of the concept of "implementing a logging mechanism" upon row deletion. I should learn more about that.I want to flag both of you as the answerer, but yours raises several unique approaches. Flagged!
John Sullivan