Hello,
I would like to know what suggestions anyone has for this, I expect pretty common, problem.
I'm creating a number of tables which need to store the history of particular objects. For example, lets say a people table like this:
PersonId (PK) | Name | PhoneNumber | DateFrom (PK) | DateTo
Every time a person changes their name or phone number, a new row is inserted and the relevent date put in. This means a persons details are available for any point in time.
The issue I have is how to deal with the PersonId in relation to foreign ids and the creation of new personids. I know of three solutions:
1. Make another almost identical table, except without the date information. This table will store the most recent entry and there will be one row for each person.
2. Have another table that has just one column of the PersonIds.
3. Have no extra tables.
1 and 2 allow for easy foreign key constraints and also easy creation of new personids. 3 is the most simple structurely, but I'm worried there wont be any foreign key constraints or at least it will be harder to create them. Also, to create a new id for 3 I'd guess I'd have to do a query something like this:
INSERT INTO Persons(PersonId,Name,PhoneNumber,DateFrom,DateTo)
(SELECT Max(PersonId) FROM Persons)+1,
'Joe Bloggs',
07777777777,
getdate(),
'1/31/9999'
I think I'm leaning towards 3 at the moment, perhaps looking into how to make constraints that will stop the submission of wrong data. Otherwise I'll just to reply on my front end manage that.
As a further note, history is just as important to me as present details.
I have discussed part of this problem also here:
http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=103468
Please let me know your thoughts on this. I really appreciate the help and time people put in on this forum, so thanks!