dkretz
dkretz

Reputation: 37645

Last Updated Date: Antipattern?

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?

Upvotes: 4

Views: 654

Answers (6)

HLGEM
HLGEM

Reputation: 96542

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.

Upvotes: 0

SQLMenace
SQLMenace

Reputation: 134923

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

Upvotes: 0

frankodwyer
frankodwyer

Reputation: 14048

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

Upvotes: 0

cletus
cletus

Reputation: 625007

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.

Upvotes: 0

coppro
coppro

Reputation: 14506

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.

Upvotes: 2

Greg Hewgill
Greg Hewgill

Reputation: 992707

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.

Upvotes: 7

Related Questions