Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should a value always be stored in a database using a specific unit (e.g. meters) or should the unit be settable in an additional field?

Should a value always be stored in a database using a specific unit (e.g. meters) or should the unit be settable using an additional field or table?

Example 1: Unit defined as part of the field

buildings
-----------------------------
building_id       INT
date_built_utc    DATE
reported_area_m   DOUBLE

Example 2: Unit defined in another field

buildings
-----------------------------
building_id          INT
date_built           DATE
date_built_unit      VARCHAR(50)
reported_area        DOUBLE
reported_area_unit   VARCHAR(50)

I am strongly leaning towards Example 1 because reporting on the values would be easier ( and less error prone ) if there is one standard unit for storing the data. The application layer can easily handle any conversions between units if the need arises.

Which method would be preferred and why?

like image 858
Ryan Taylor Avatar asked Dec 02 '22 07:12

Ryan Taylor


1 Answers

I think allowing more than one unit would introduce a lot of confusion and maintenance headaches. Can you imagine someone reading 100 and thinking meters rather than feet? That's what caused the loss of a NASA orbiter, after all.

EDIT: What I mean is, by all means, if you can force a unit, do it. This will prevent any possible confusion over what the unit is.

like image 134
Matthew Jones Avatar answered Dec 04 '22 07:12

Matthew Jones