HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Storing time with milliseconds in database

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
millisecondswithtimedatabasestoring

Problem

Just a quick question regarding storing "time" in a database. I'm logging the time of users runs which are in the format 00:00:00:00 (hours, minutes, seconds, milliseconds).

I was originally going to store it as TIME but then I realised MySQL doesn't support milliseconds in this type.

I will be storing the actual time taken from the users stopwatch, not the time when they ran and finished (this will be calculated before being inserted).

What would be the best field type to use? I was thinking either double or float, but I'm not sure that would work?

Solution

Personally, I'd use integer types for duration

Example: 340,000 milliseconds is 340000 in an unsigned int column.

To me, datetime, time, date etc are for explicit points in time. 340 seconds is meaningless in that context. Having a start date/time allows the duration to be added of course

Context

StackExchange Database Administrators Q#10400, answer score: 7

Revisions (0)

No revisions yet.