开发者

varchar or decimal data type for minutes, seconds, milliseconds

开发者 https://www.devze.com 2023-01-23 15:47 出处:网络
I have a MySQL database table with cross country running times in it. I am at a fork in the road and questioning whether I should convert the current datatype for the running times (varchar) to a deci

I have a MySQL database table with cross country running times in it. I am at a fork in the road and questioning whether I should convert the current datatype for the running times (varchar) to a decimal.

The one aspect which attracts me to the varchar datatype is that I don't have to convert the incoming running results (parsed via a php script) to seconds and then back again when it is retrieved dynamically. My processing script ensures that each time is 8 characters lo开发者_JAVA百科ng unless an athlete DNFs (Did not finish) which is also information that I'd like to store. The DNF appears as "DNF" in the results.

So should a running time be stored as 17:40.57 or 1060.57? What are the advantages and disadvantages to each? Is there a better datatype than what I have already assumed as the correct types?

Also, if you elect 1060.57 as your answer, then how would I logically store DNFs or DNSs?


I had originally suggested TIME and DATETIME data types, but was unaware that MySQL does not store microseconds in a column of any temporal data type (IE: TIME, DATETIME, etc).

FLOAT is obvious poor - even MySQL states it should only be used when precision is not a concern.

VARCHAR/CHAR isn't a great idea, because there's no means to enforce consistency of the format. You could mix mm:ss:ff and the decimal format - both would be accepted, but would obviously look odd when displayed.

DECIMAL would be the best choice, for sake of data consistency & validation given the limitations of MySQL. But it means custom functionality to get info out if you want different formatting, which would've been available if MySQL's temporal functions supported enough precision.

Other free databases, like PostgreSQL, SQL Server Express or Oracle Express might be worth considering as an alternative for sake of better data type support.


I'd store it as a DECIMAL number of seconds, with additional columns to store the DNFs or DNSs. If you want, you can use a single enum for DNF and DNS, since they are mutually exclusive (assuming DNS is Did Not Start). This allows SUM, arithmetic, etc. Storing as a VARCHAR doesn't allow any interesting processing or filtering.

EDIT: changed to DECIMAL, for precision.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号