Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I store both birthday and age?

Tags:

sql

database

Im making an app that uses both birthday and age to make some deductions, but as the age can be obtain through the birthday and current date, Im questioning if I should be storing them both and not just the date, for one part I could use the age attribute to simplify some querys without converting dates, what would be the right thing to do following conventions?

like image 294
Andrew Avatar asked Dec 02 '22 10:12

Andrew


1 Answers

Calculations based on data should be always... calculated, not stored. Well, not always, usually, but it depends on situation. Below are couple of pros and cons:

Cons

  • calculation logic might change, so stored values will be no loner valid.
  • or invalid data could be entered (and you will receive invalid data when querying).
  • or the result changes with time, as age does, eg. today you have 20 years, but in one year you will have 21.

Pros

  • however, as @RonenAriely mentioned, storing calculated data in order to gain performance is one of pros of such approach.

So, to sum up, you should make calculations, like DATEDIFF(NOW(), DateOfBirth) to get an age, as the result changes in time and the function don't influence performance much.

like image 139
Michał Turczyn Avatar answered Dec 04 '22 01:12

Michał Turczyn