Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL - best data type for simple time with hours and minutes

I need to represent the due time for tasks in my MySQL 5 database.
Then, I need to store times like these:
_ 18
_ 9:15
_ 12:00

Now I am in dilemma whether to use the time datatype or just a 4-digit integer as I just need to store hours and minutes.
In the second case, I would store:
_ 1800
_ 0915
_ 1200

What are the implication of both the approaches?
Which solution would you implement given that the most important requirement is high performance in a huge dataset (millions of rows)?

The application code is written in PHP.

EDIT: I was thinking of an important point. If I use integers, I need to do some string manipulation with PHP when I display the time (to insert the semicolon) that probably nullify the gain with using integers

Thanks,
Dan

like image 937
Dan Avatar asked Nov 22 '10 17:11

Dan


People also ask

What is the datatype for time in MySQL?

The DATETIME type is used for values that contain both date and time parts. MySQL retrieves and displays DATETIME values in ' YYYY-MM-DD hh:mm:ss ' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . The TIMESTAMP data type is used for values that contain both date and time parts.

What is the best data type for time in SQL?

The DATETIME type is used when you need values that contain both date and time information. MySQL retrieves and displays DATETIME values in 'YYYY-MM-DD HH:MM:SS' format. The supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59'.

How do I create a datatype for time in MySQL?

MySQL permits fractional seconds for TIME , DATETIME , and TIMESTAMP values, with up to microseconds (6 digits) precision. To define a column that includes a fractional seconds part, use the syntax type_name ( fsp ) , where type_name is TIME , DATETIME , or TIMESTAMP , and fsp is the fractional seconds precision.

What data type is used for hours?

The TIME data type consists of a time in hour, minutes, seconds, optional fractions of a second, and optional time zone.


1 Answers

Use time datatype. Main benefit will be reusing existing time function embedded in MySQL.

It will probably be more efficient in terms of space using a smallint (only 2 bytes) but you will loose the ability to use existing functions to operate on fields whose semantic is a time value. I can think of formatting functions, time difference, time zones as examples of scenarios where you could take immediate advantage of the time datatype.

like image 128
Pablo Santa Cruz Avatar answered Nov 13 '22 07:11

Pablo Santa Cruz