views:

229

answers:

6

I keep seeing questions floating through that make reference to a column in a database table named something like DateLastUpdated. I don't get it.

The only companion field I've ever seen is LastUpdateUserId or such. There's never an indicator about why the update took place; or even what the update was.

On top of that, this field is sometimes written from within a trigger, where even less context is available.

It certainly doesn't even come close to being an audit trail; so that can't be the justification. And if there is and audit trail somewhere in a log or whatever, this field would be redundant.

What am I missing? Why is this pattern so popular?

+5  A: 

Such a field can be used to detect whether there are conflicting edits made by different processes. When you retrieve a record from the database, you get the previous DateLastUpdated field. After making changes to other fields, you submit the record back to the database layer. The database layer checks that the DateLastUpdated you submit matches the one still in the database. If it matches, then the update is performed (and DateLastUpdated is updated to the current time). However, if it does not match, then some other process has changed the record in the meantime and the current update can be aborted.

Greg Hewgill
So in these cases it's kind of a cheap way to detect dirty updates. Which would in my experience be better handled by a sequential version number; but that doesn't invalidate your answer. Hmmm.
le dorfier
Oracle has builtin seqency generators which provides an atomic nextvalue, but other database don't have such a feature, in which case a timestamp is natural occuring sequence number
Scott Weinstein
I'm not familiar with a dbms that doesn't have an identity column generator. And if timestamp were the natural option, then you'd think Oracle would use it.
le dorfier
+2  A: 

It depends on the exact circumstance, but a timestamp like that can be very useful for autogenerated data - you can figure out if something needs to be recalculated if a depedency has changed later on (this is how build systems calculate which files need to be recompiled).

Also, many websites will have data marking "Last changed" on a page, particularly news sites that may edit content. The exact reason isn't necessary (and there likely exist backups in case an audit trail is really necessary), but this data needs to be visible to the end user.

coppro
Build systems I'm familiar with use filedates, not a column in a table. News sites usually seem to have several generations of versions - and they *do* have the version info, not just the timestamp when they occurred - which would make this redundant.
le dorfier
+1  A: 

These sorts of things are typically used for business applications where user action is required to initiate the update. Typically, there will be some kind of business app (eg a CRM desktop application) and for most updates there tends to be only one way of making the update.

If you're looking at address data, that was done through the "Maintain Address" screen, etc.

Such database auditing is there to augment business-level auditing, not to replace it. Call centres will sometimes (or always in the case of financial services providers in Australia, as one example) record phone calls. That's part of the audit trail too but doesn't tend to be part of the IT solution as far as the desktop application (and related infrastructure) goes, although that is by no means a hard and fast rule.

Call centre staff will also typically have some sort of "Notes" or "Log" functionality where they can type freeform text as to why the customer called and what action was taken so the next operator can pick up where they left off when the customer rings back.

Triggers will often be used to record exactly what was changed (eg writing the old record to an audit table). The purpose of all this is that with all the information (the notes, recorded call, database audit trail and logs) the previous state of the data can be reconstructed as can the resulting action. This may be to find/resolve bugs in the system or simply as a conflict resolution process with the customer.

cletus
Then you're just reiterating the need for a real log (at least in the last two paragraphs). And the field is especially deficient unless all you care about is the single most recent change.
le dorfier
A: 

It is certainly popular - rails for example has a shorthand for it, as well as a creation timestamp (:timestamps).

At the application level it's very useful, as the same pattern is very common in views - look at the questions here for example (answered 56 secs ago, etc).

It can also be used retrospectively in reporting to generate stats (e.g. what is the growth curve of the number of records in the DB).

frankodwyer
I *think* SO uses a journaling pattern, where answers aren't altered, they are superseded. So you'd have a created timestamp; but no need for a modified timestamp. But I might be wrong. As for stats, you'd be screwed if a record has been modified more than once - you can only store one date.
le dorfier
@le dorfier that is where the history table comes into place. Also how would you answer a request like this" show me all customers who did note update their profile in the last month, we want to send them an email" you cannot tell me that you want to go through a log file
SQLMenace
Sure. "SELECT * FROM Addresses WHERE NOT EXISTS (SELECT 1 FROM updates WHERE AddressId = adddresses.AddressId AND (timestamp less than 30 days old)". Piece of cake.
le dorfier
A: 

there are a couple of scenarios

Let's say you have an address table for your customers you have your CRM app, the customer calls that his address has changed a month ago, with the LastUpdate column you can see that this row for this customer hasn't been touched in 4 months

usually you use triggers to populate a history table so that you can see all the other history, if you see that the creationdate and updated date are the same there is no point hitting the history table since you won't find anything

you calculate indexes (stock market), you can easily see that it was recalculated just by looking at this column

there are 2 DB servers, by comparing the date column you can find out if all the changes have been replicated or not etc etc ect

SQLMenace
But there are more direct ways of dealing with all those situations. An outer join will tell you if there is a previous version record. Index calculations should have an IndexCalculatedTimestamp column specific to the column; not to indicate that the record overall has had some delta.
le dorfier
No replication scheme I've ever seen would depend on this user field.
le dorfier
>>An outer join will tell you if there is a previous version recordsure, I have a billion row history table, don't you think in terms of performance LastUpdated makes a lot more sense. You see I work with TB size DBs and cannot afford to do some big outer joins just to see if someone has updated
SQLMenace
>>No replication scheme I've ever seen would depend on this user fieldtrue they use GUIDs but it is easier to compare dates
SQLMenace
A: 

This is also very useful if you have to send feeds out to clients that are delta feeds, that is only the records that have been changed or inserted since the data of the last feed are sent.

HLGEM