mysql – What type should I use to save just the time (no date) in a database?

Question:

I'm not sure which type I should use to work with hours on my system. Should I use DateTime , Time or TimeStamp . I would like the hours to be saved in this format:

HH:MM

Answer:

There is a specific type for this which is TIME . This type has the seconds and fraction of them as well (keep them zero if you don't need them), but overall this won't be a problem.

But it is not always necessary to use it. There are some situations where a VARCHAR type may suffice. Of course it's more limited, it doesn't have the best semantics, but it depends on usage. If you really want to avoid the seconds, it would be this way. You will have to handle the data manually.

On the other hand, it is possible that this type is not enough for what you want and you need to use another type where you have more control, even if it is at the cost of less facilities. In some cases it may be VARCHAR itself. In others it might be interesting to use DATETIME or TIMESTAMP , even if they have irrelevant information.

Just seeing the exact situation to decide which is best. But I'll go with the native types first. If so, sin for excess would be more advantageous in most cases.

Scroll to Top