I want to store last 5 days stats for a user in database. I have two types of db table design in my mind.
Design 1:
id user_name stats
1 Peter 54 //Day 1 stats for Peter
2 Peter 50 //Day 2 stats for Peter
3 Abc 10 //Day 1 stats for Abc
4 Peter 55 //Day 3 stats for Peter
5 Abc 14 //Day 2 stats for Abc.
Design 2:
id user_name day1 day2 day3 day4 day5
1 Peter 54 50 55 - -
2 Abc 10 14 - - -
Which of the above two is better ?
Since I want stats for only last 5 days, thats why I want to overwrite the oldest day stats when adding new day so that at any time there are maximum 5 days stats for the user.
Please tell me how that should be done.
Design 1 is far superior. If you decide you want to record more days in future, you don’t want to have to write another set of queries to get to this data. I presume you meant to have a ‘day’ column in Design 1? Why not have a DateTime field to indicate the day like so:
Then you can purge the data at your own convenience. The id primary key field is simply to simplify row identification when used in queries / updates.