Best Relational DataBase Representation Of Time Bound Hierarchies

后端 未结 4 1544
温柔的废话
温柔的废话 2021-02-02 04:27

What in everyone\'s opinion is the best representation for a time-bound hierarchy in SQL?

What I mean by this is:
- On any given date you have a normal tree hierarch

4条回答
  •  庸人自扰
    2021-02-02 04:43

    I can think of a couple of reasonable solutions, depending on how your data is being used and how it changes.

    1) Assuming today's hierarchy is the most important. I'd store today's hierarchy with a conventional ParentId column in each record. For previous versions of the hierarchy I'd have a history table of

    ItemId, ParentId, ValidFromDate, ValidToDate
    

    Any time the hierarchy changes, you add a new row to the history table.

    2) If any/all of the hierarchies are of equal importance, I'd store a base line hierarchy and then implement a hierarchy transaction table.

    TransactionId, ItemId, Action (Move/Delete/Add), DateTime, OldParentId, NewParentId
    

提交回复
热议问题