![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
For a database design, I'm looking for the best way to model history. For example (but it can of course get more complex): We have a PERSON table and one of the attributes is status which can be [baby, toddler, child, adolescent, adult] or something like that. I want to keep a history and know from every person on what date he changed from one status to another, e.g. James Smith went adolescent on 2005-02-07. (I know the example is stupid )My thought is to make a separate table with that. Something like PERSONSTATUSHISTORY and add a record to that whenever a status changes in the PERSON table. The advantage is that the PERSON table has one record per person and it's all easily understandable I think. Another way is to put changes immediately in the PERSON table. But of course then my person_id primary key for PERSON-table is not enough anymore and it's more difficult to just select a person and see what his status is now. |
#3
| |||
| |||
|
|
Hello, For a database design, I'm looking for the best way to model history. For example (but it can of course get more complex): We have a PERSON table and one of the attributes is status which can be [baby, toddler, child, adolescent, adult] or something like that. I want to keep a history and know from every person on what date he changed from one status to another, e.g. James Smith went adolescent on 2005-02-07. (I know the example is stupid )My thought is to make a separate table with that. Something like PERSONSTATUSHISTORY and add a record to that whenever a status changes in the PERSON table. The advantage is that the PERSON table has one record per person and it's all easily understandable I think. Another way is to put changes immediately in the PERSON table. But of course then my person_id primary key for PERSON-table is not enough anymore and it's more difficult to just select a person and see what his status is now. Can anybody advice me on how to model history the best way? Or show me places to read about it. There are probably things I don't think about and I want a model that I won't regret about later on .Greetz, Bart |
#4
| |||
| |||
|
#5
| |||
| |||
|
#6
| |||
| |||
|
|
Here is an old posting of mine. Let me use a history table for price changes. The fact to store is that a price had a duration: CREATE TABLE PriceHistory (upc CHAR(13) NOT NULL REFERENCES Inventory(upc), start_date DATE NOT NULL, end_date DATE, -- null means current CHECK(start_date < end_date), PRIMARY KEY (upc, start_date), item_price DECIMAL (12,4) NOT NULL CHECK (item_price > 0.0000), etc.); You actually needs more checks to assure that the start date is at 00:00 and the end dates is at 23:59:59.999 Hrs. You then use a BETWEEN predicate to get the appropriate price. |
|
SELECT .. FROM PriceHistory AS H, Orders AS O WHERE O.sales_date BETWEEN H.start_date AND COALESCE (end_date, CURRENT_TIMESTAMP); It is also a good idea to have a VIEW with the current data: CREATE VIEW CurrentPrices (..) AS SELECT .. FROM PriceHistory WHERE end_date IS NULL; |
#7
| |||
| |||
|
|
Here is an old posting of mine. Let me use a history table for price changes. The fact to store is that a price had a duration: CREATE TABLE PriceHistory (upc CHAR(13) NOT NULL REFERENCES Inventory(upc), start_date DATE NOT NULL, end_date DATE, -- null means current CHECK(start_date < end_date), PRIMARY KEY (upc, start_date), item_price DECIMAL (12,4) NOT NULL CHECK (item_price > 0.0000), etc.); |
#8
| |||
| |||
|
|
What are the advantages and disadvantages to keeping history in the table with the current data vs, having a separate history table? |
#9
| |||
| |||
|
|
Hello, For a database design, I'm looking for the best way to model history. For example (but it can of course get more complex): We have a PERSON table and one of the attributes is status which can be [baby, toddler, child, adolescent, adult] or something like that. I want to keep a history and know from every person on what date he changed from one status to another, e.g. James Smith went adolescent on 2005-02-07. (I know the example is stupid )My thought is to make a separate table with that. Something like PERSONSTATUSHISTORY and add a record to that whenever a status changes in the PERSON table. The advantage is that the PERSON table has one record per person and it's all easily understandable I think. Another way is to put changes immediately in the PERSON table. But of course then my person_id primary key for PERSON-table is not enough anymore and it's more difficult to just select a person and see what his status is now. Can anybody advice me on how to model history the best way? Or show me places to read about it. There are probably things I don't think about and I want a model that I won't regret about later on .Greetz, Bart You could have one table you call Person and then have another table you |
#10
| |||
| |||
|
![]() |
| Thread Tools | |
| Display Modes | |
| |