StackOverflowNewbie
StackOverflowNewbie

Reputation: 40653

MySQL: how to store height/weight information?

I need to capture user's height and weight in my database. In the UI, I need to provide an option to specify height in centimeters and inches -- and for weight, kilograms and pounds. Later on, when I display the users, I need to display their height and weight in only 1 unit of measurement (e.g. either in cms or ins for all of them).

What's the best way to store this data? Do I store 2 pieces of data for height (e.g. height value, height unit) and 2 pieces of data for weight (e.g. weight value, weight unit), then store user's data as inputted? When I need to display the data in uniform units, then I do conversion in the SQL.

Or should I convert the data before being stored that way I don't need to do conversation later when I need to display the user data?

By the way, when I display the data back to the actual user, it has to be in the same units the user picked.

Upvotes: 1

Views: 3239

Answers (2)

Eugen Rieck
Eugen Rieck

Reputation: 65304

You definitly want to save only one height and only one width - use a unit of your choice. Unit conversion is a single multiplication, which is in essence a zero cost.

You can also have a units table, and a simple inner join with a calculated field will give you the correct numerical value in a single query (SELECT as well as INSERT/UPDATE)

Upvotes: 0

GDP
GDP

Reputation: 8178

Think normalization - unless there is a compelling reason to do otherwise, store it in one place, in one format, and always use the standard conversion in your code later on. The database should store your data, not the business rules.

Upvotes: 7

Related Questions