People buy stuff in a web shop.
I take their orders and save them to a database. For each ordered item, I need the quantity and the information about the item. At the very least, I’m going to need to save the current price and name of each item but I would really prefer to save everything.
And here comes the question… What’s the best way to keep all the information about each ordered item as it was at the time of purchase?
- Copy into a separate table with the same columns as the product table
- Make copies of products in the same table, marked as non-editable, non-viewable copies
- Some sort of copy on write schema that saves some space until the “live” product information is actually changed?
- ????
For simplicity, let’s assume all product information is kept in a single table.
If the products in your shop are going to change frequently, then it sounds like what you need is a product version history.
Basically, you should try to identify all the properties/fields of your shop items that are going to frequently change, and put them into a separate
product_properties(or something) table, with a timestamp for that particular version of those properties. Perhaps all your properties will change all the time, in which case you might get away with a single table. In any event, you can assume that the newest timestamp is the latest version of the product.When the shop owner updates a product, you create a new record in
product_propertieswith all the changed properties, and update the main product table record to point to this new record (or simply just pull out the newest timestamp at display time).When someone makes a purchase, you record the product id and the history/version id of the product in the purchase table.
With this type of history setup, you’ve normalized the product data, so it’s not stored over and over again with each purchase record, and you can still get the specific details of each purchase.