Database structure for storing historical data

后端 未结 7 972
夕颜
夕颜 2020-12-13 05:48

Preface: I was thinking the other day about a new database structure for a new application and realized that we needed a way to store historical data in an efficient way. I

7条回答
  •  抹茶落季
    2020-12-13 05:58

    I myself like to keep it simple. I would use two tables, a customer table and a customer history table. If you have the key (eg customerId) in the history table there is no reason to make a joining table, a select on that key will give you all records.

    You also don't have audit information (eg date modified, who modified etc) in the history table as you show it, I expect you want this.

    So mine would look something like this:

    CustomerTable  (this contains current customer information)
    CustID (distinct non null)
    ...all customer information fields
    
    CustomerHistoryTable
    CustId (not distinct non null)
    ...all customer information fields
    DateOfChange 
    WhoChanged
    

    The DataOfChagne field is the date the customer table was changed (from the values in this record) to the values in a more recent record of the values in the CustomerTable

    You orders table just needs a CustomerID if you need to find the customer information at the time of the order it is a simple select.

提交回复
热议问题