How to create history for n: m relationships
Problem:
I have a: m relationship between a table A
and another table B
and this requires the complete history of that relationship, in other words, I should be able to reproduce the status of any point in the past.
Ideas:
My first attempt is to parse the relationship in a 1: m and m: n ratio by inserting a dummy table to preserve the status of the relationship at any point in time when some change occurs. Like this:
TABLE A TABLE B
-------- ---------
ID INT ID INT
TABLE HIST TABLE CROSS_REF
---------- -----------------
ID INT REF_B_ID INT
REF_A_ID INT REF_HIST_ID INT
VERSION TIMESTAMP
Pro: Easily request the information you require. Con: This attempt will create a lot of redundant data, especially when the "B" -Side is large and there are only minor changes. (for example, in the "Company - Employee" use case)
A second approach would be to maintain two n: m relationships, one for the current status and one for the changes that have occurred.
TABLE A TABLE B
-------- ---------
ID INT ID INT
TABLE CHANGES TABLE CROSS_REF
---------- -----------------
REF_A_ID INT REF_A_ID INT
REF_B_ID INT REF_B_ID INT
ACTION_TIME TIMESTAMP
ACTION_TYPE VARCHAR
Pro: no redundant data. Con: It is difficult to request status at a certain point in time in the past.
Questions
- Do you have any other suggestion how can I create a data model for this application?
- If there isn't really a better way to solve this: which direction should I go? Or does it mostly depend on the given use case like the example above?
- If I go to the second approach, what might a status request look like at a certain point in time in the past? I can solve this problem on the client side, but I would also like to know a solution on the database side. (PL / SQL would be doable as well)
source to share