I have following table
Person(PID, Name, Dob, AddressId) PID being surrogate key(auto incremented)
Address(AddressId, Line1, Line2, City, State) AddressId being surrogate key(auto incremented)
PersonID (PID) is used in my lot of transaction tables. Since address of a person may change over the period of time, I need to store record version in database so that I can refer correct version according to time slice. How should I handle it.
I am thinking of having solution something like this
- Using same table to store history e.g.
- Person(PID, Name, Dob, AddressId)
- Address(AddressId, Line1, Line2, City, State, StartDate, EndDate, LastUpdatedBy, LastUpdatedDate)
Let's assume initial record of person as below
Person(100, X, Y, 110)
Address(110, A, B, C, D, 2011-01-01, 9999-12-31, Y, 2011-01-01)
Now address was changed in 2012 for PID 100. I am thinking to do
Address(320, A, B, C, D, 2011-01-01, 2012-06-01, Y, 2011-01-01) - INSERT
Address(110, A, B, Q, D, 2012-06-01, 9999-12-31, Z, 2012-06-01) - UPDATE
if address changes again in 2014 for PID 100. It would look like
Address(740, A, B, Q, D, 2012-06-01, 2014-06-01, Y, 2012-01-01) - INSERT
Address(320, A, B, C, D, 2011-01-01, 2012-06-01, Y, 2011-01-01) - NO CHANGE
Address(110, A, B, N, D, 2014-06-01, 9999-12-31, R, 2012-06-01) - UPDATE
Now I can query historical data as well as current data from same table without changing surrogate key. I won't have to refer historical tables hence no bombarding of logic and tables.
This approach won't let me enforce unique key constraint on tables assuming I have Address Code being unique. How shall I handle this?
Please suggest different approach and feedback on this.