Versioning in SQL Tables - how to handle it?

前端 未结 7 1228
囚心锁ツ
囚心锁ツ 2021-01-30 08:50

Here\'s a fictional scenario with some populated data. For tax purposes, my fictional company must retain records of historical data. For this reason, I\'ve included a version c

7条回答
  •  迷失自我
    2021-01-30 09:22

    Here is my suggested approach, which has worked very well for me in the past:

    • Forget the version number. Instead, use StartDate and EndDate columns
    • Write a trigger to ensure that there are no overlapping date ranges for the same ID, and that there is only ever one record with a NULL EndDate for the same ID (this is your currently effective record)
    • Put indexes on StartDate and EndDate; this should give you reasonable performance

    This will easily let you report by date:

    select *
    from MyTable 
    where MyReportDate between StartDate and EndDate
    

    or get the current info:

    select *
    from MyTable 
    where EndDate is null
    

提交回复
热议问题