简体   繁体   中英

Database Design to keep history of changes?

I have a database table that stores pupils at a school and their athletic performance. So each child will have a competitive status (Youth, U15 and Open) along with their records for certain events (long jump, high jump and 100m).

I would like to pull a report for a specific child showing the timeline of the child, movement from Youth -> U15 to Open. As well as the various records (personal best's) for the duration of the pupil being at the school.

However the database table stores oly the current status of the child as well as their current personal best's.

How can we structure the database so that the history of the child is recorded?

I would add two columns, START_DATE and END_DATE . The most recent record will have an end date of infinity "31-Dec-9999" (or whatever the max date is in mysql) . Inserting a new row does become a bit involved though:

  • If there is no row for the person, insert a new record where the START_DATE is the current date, and the END_DATE is infinity.
  • If a row already exists, "end" the current record by setting END_DATE to the current date, and insert a new record where the START_DATE is the current date, and the END_DATE is infinity.

So you might have:

PUPIL, STATUS, LONG_JUMP, START_DATE, END_DATE
Mike, Open, 2 meters, 01-Jan-2012, 31-Dec-9999
Mike, U15, 2 meters, 01-Jan-2011, 01-Jan-2012

Mike was in U15 for all of 2011, and is currently in Open as of Jan 2012.

Your queries for the "current" information will look for rows where the current date is between START_DATE and END_DATE , and you can query the full set by a given person to see the timeline.

You can also add a trigger to your table. a before update trigger.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM