What is the prefered way to represent status changes over time in a relational database?
September 4, 2007 12:53 PM Subscribe
What is the prefered way to represent status changes over time in a relational database?
I've got this neato idea for a mysql-based inventory database for my work that'll be awesome once I clear this hurdle, so I'll be much obliged for any enlightenment you can provide!
Let's say you have a database acting as a national registry of cars: make, model,
owner, owner's state, and VIN could be the relevant entities. In addition to this, you want to record any change of owner or owner's state such that you could later call up the entire history of an individual vehicle, or what vehicles an owner has owned, or what states a car has been registered in. What's the best way to model this scenario for a relational database system? (All I can think of is to have an additional table that acts like a spreadsheet or flat file db, recording each relationship as it changes, plus the timestamp of the change. This isn't how the pros from Dover do it, is it?)
Finally, what is the proper terminology for the concept of being able to record and search on the history of status changes?
posted by kimota to computers & internet (17 answers total) 2 users marked this as a favorite
You've got your car table:
tblCar
====
CarID (autogenerated)
CarName
CarMake
CarModel
etc.
One row per car. You've also got another table to track its history:
tblCarHistory
========
CarHistoryID (autogenerated)
CarID
CarHistoryDate
CarHistoryType
CarHistoryDescription
etc.
The "CarID" column, which is unique to tblCar, is what links each car to tblCarHistory. The simplest way to put them together via SQL is thus:
SELECT c.*,ch.*
FROM tblCar c
LEFT JOIN tblCarHistory hc ON c.CarID = hc.CarID
ORDER BY hc.CarHistoryDate
posted by mkultra at 12:59 PM on September 4, 2007