Hello,I'm creating a table to track real changes of an object and also our knowledge of an object against time. For example, it could be some statistics about a town. The kind of design I've come up with so far is:--These will be constants that will never change since the town is created in the databaseCREATE TABLE TownSeeds( Id int Identity(1,1) CONSTRAINT PK_TownSeeds Primary Key, EntryDate DateTime NOT NULL DEFAULT Current_TimeStamp)--stores changing attributes of the townCREATE TABLE Towns( Id int CONSTRAINT FK_TownsChanges_TownSeeds FOREIGN KEY REFERENCES dbo.TownSeeds(Id), TownName varchar(255) NOT NULL, Population float, --other attributes... --actual history of the town ChangeDate DateTime NOT NULL, NextChangeDate DateTime NOT NULL, --created to make queries easier --stores our history of knowledge of the town UpdateDate datetime NOT NULL, NextUpdateDate datetime NOT NULL, CONSTRAINT PK_Towns Primary Key(Id,UpdateDate))
Here is some example data being inserted:--Create town with two points in history where population changesINSERT INTO TownSeeds VALUES(DEFAULT,DEFAULT)DECLARE @Town1Id intSET @Town1Id=Scope_Identity()DECLARE @EntryDate datetimeSET @EntryDate=(SELECT EntryDate FROM TownSeeds WHERE Id=@Town1Id)INSERT INTO TownsSELECT Id=@Town1Id, TownName='test town', Population=5000, ChangeDate='19050101', NextChangeDate='19100101', UpdateDate=@EntryDate, NextUpdateDate='99991231' INSERT INTO TownsSELECT Id=@Town1Id, TownName='test town', Population=7000, ChangeDate='19100101', NextChangeDate='99991231', UpdateDate=@EntryDate, NextUpdateDate='99991231' --Better research shows that the actual population was 5500 in 1905DECLARE @UpdateDate datetimeSET @UpdateDate=CURRENT_TIMESTAMPUPDATE TownsSET NextUpdateDate=@UpdateDateWHERE Id=@Town1Id AND UpdateDate=@EntryDateINSERT INTO TownsSELECT Id=@Town1Id, TownName='test town', Population=5500, ChangeDate='19050101', NextChangeDate='19100101', UpdateDate=@UpdateDate, NextUpdateDate='99991231'
I'm a little concerned it might be hard to apply constraints to this data with this added complexity.I'd really appreciate some feedback on this design. I'm guessing this is a fairly common requirement so I'm hoping someone here will have some helpful feedback from their experience implementing something similar.Thanks,Michael