Database Value And Unit as Two Separate Columns?
I wan开发者_开发知识库t to store a computer's processor speed in a database. Should I use one column as a varchar and store the number and unit together e.g. "1.3 ghz"
or
should I create two separate columns, one as a decimal for "1.3" and a second column for the units as a varchar "ghz"?
Consider storing the value in this column with a fixed known unit. For example, make this an integer column store all speeds in this column as raw mega-hertz. This would let you write code that formats and presents the value to the user outside of the database.
At some point you'll want to compare so store it as a integer in megahertz. You only have to look the the questions here where some lucky soul has text to be parsed or sorted as numbers.
GHz etc is client formatting
This depends on the function of the data or the desired end result.
if you will be working with them mathematically you might be better off with two fields, but if you are simply storing/returning the value as is, a varchar is just as easy.
精彩评论