Search code examples
mysqlrevision-history

How to expand this tables to support revision history


I have 3 tables:

  • pricelists (pricelist_id, name)
  • prices (price_id, pricelist_id, price, note)
  • tickets (ticket_id, price_id, name, time)

So, the main reason for versioning prices is because prices can be changed in future and I want to keep information about past prices for statistics, and I want to tickets has real price, not future changed price.

Can you please give me some example of queries?


Solution

  • I suppose one possible approach is making two price tables instead of two: the first one will store some generic price-related data (like 'note' and 'pricelist_id' link, as these won't change with time), and the second one will store the actual prices (along with, probably, timestamps of their activation - but that's not necessary):

    prices         (price_id, pricelist_id, note)
    price_versions (price_ver_id, price_id, price, started_at, ended_at)
    tickets        (ticket_id, price_ver_id, name, issued_at) 
    

    As you see, you refer to price_versions in your tickets to get the specific price. But you can easily collect the generic price-related information as well (by joining the prices table from there).

    This approach lets you construct an additional constraint, checking that issued_at is not before started_at and not after ended_at (it that NOT NULL in the corresponding row). But that's an addition, not a requirement, I suppose.