A2-04-10.MySQL DATA TYPES-Mastering MySQL TIME Data Type

时间:2022-06-01 19:22:56

转载自:http://www.mysqltutorial.org/mysql-time/

Mastering MySQL TIME Data Type

 

Summary: in this tutorial, we will introduce you to the MySQL TIME data type and show you useful temporal functions to manipulate time data effectively.

Introduction to MySQL TIME data type

MySQL uses the 'HH:MM:SS' format for querying and displaying a time value that represents a time of day, which is within 24 hours. To represent a time interval between two events, MySQL uses the 'HHH:MM:SS' format, which is larger than 24 hours.

To define a TIME column, you use the following syntax:

For example, the following snippet defines a column named start_at with TIME data type.

TIME value ranges from -838:59:59 to 838:59:59. In addition, a TIME value can have fractional seconds part that is up to microseconds precision (6 digits). To define a column whose data type is TIMEwith a fractional second precision part, you use the following syntax:

N is an integer that represents the fractional part, which is up to 6 digits.

The following snippet defines a column with TIME data type including 3 digits of fractional seconds.

TIME value takes 3 bytes for storage. In case a TIME value includes fractional second precision, it will take additional bytes based on the number of digits of the fractional second precision. The following table illustrates the storage required for fractional second precision.

Fractional Second Precision Storage (BYTES)
0 0
1, 2 1
3, 4 2
5, 6 3

For example, TIME and TIME(0) takes 3 bytes. TIME(1) and TIME(2) takes 4 bytes (3  + 1); TIME(3) and TIME(6) take 5 and 6 bytes.

MySQL TIME data type example

Let’s take a look at an example of using the TIME data type for columns in a table.

First, create a new table named tests that consists of four columns: idnamestart_at, and end_at. The data types of the start_at and end_at columns are TIME.

Second, insert a row into the tests table.

Third, query data from the tests table.

A2-04-10.MySQL DATA TYPES-Mastering MySQL TIME Data Type

Notice that we use 'HH:MM:SS' as the literal time value in the INSERT statement. Let’s examine all the valid time literals that MySQL can recognize.

MySQL TIME literals

MySQL recognizes various time formats besides the 'HH:MM:SS' format that we mentioned earlier.

MySQL allows you to use the 'HHMMSS' format without delimiter ( : ) to represent time value. For example, '08:30:00' and '10:15:00' can be rewritten as '083000' and '101500'.

A2-04-10.MySQL DATA TYPES-Mastering MySQL TIME Data Type

However, 108000 is not a valid time value because 80 does not represent the correct minute. In this case, MySQL will raise an error if you try to insert an invalid time value into a table.

MySQL issued the following error message after executing the above statement.

In addition to the string format, MySQL accepts the HHMMSS as a number that represents a time value. You can also use SSMMSS. For example, instead of using '082000', you can use 082000 as follows:

A2-04-10.MySQL DATA TYPES-Mastering MySQL TIME Data Type

For the time interval, you can use the 'D HH:MM:SS' format where D represents days with a range from 0 to 34. A more flexible syntax is 'HH:MM''D HH:MM''D HH', or 'SS'.

If you use the delimiter:, you can use 1 digit to represent hours, minutes, or seconds. For example, 9:5:0 can be used instead of '09:05:00'.

A2-04-10.MySQL DATA TYPES-Mastering MySQL TIME Data Type

Useful MySQL TIME functions

MySQL provides several useful temporal functions for manipulating TIME data.

Getting to know the current time

To get the current time of the database server, you use the CURRENT_TIME function. The CURRENT_TIMEfunction returns the current time value as a string ( 'HH:MM:SS') or a numeric value ( HHMMSS) depending on the context where the function is used.

The following statements illustrate the CURRENT_TIME function in both string and numeric contexts:

A2-04-10.MySQL DATA TYPES-Mastering MySQL TIME Data Type

Adding and Subtracting time from a TIME value

To add a TIME value to another TIME value, you use the ADDTIME function. To subtract a TIME value from another TIME value, you use  the SUBTIME function.

The following statement adds and subtracts 2 hours 30 minutes to and from the current time.

A2-04-10.MySQL DATA TYPES-Mastering MySQL TIME Data Type

In addition, you can use the TIMEDIFF() function to get a difference between two TIME values.

 

Formatting MySQL TIME values

Although MySQL uses 'HH:MM:SS' when retrieving and displaying the a TIME value, you can display the TIME value in your preferred way using the TIME_FORMAT function.

The TIME_FORMAT function is like the DATE_FORMAT function except that the TIME_FORMAT function is used to format a TIME value only.

See the following example.

A2-04-10.MySQL DATA TYPES-Mastering MySQL TIME Data Type

In the time format string above:

  •  %h means two-digit hours from 0 to 12.
  •  %i means two-digit minutes from 0 to 60.
  •  %p means AM or PM.

Extracting hour, minute, and second from a TIME value

To extract the hour, minute, and second from a TIME value, you use HOURMINUTE, and SECONDfunctions as follows:

A2-04-10.MySQL DATA TYPES-Mastering MySQL TIME Data Type

Getting UTC time value

To get the UTC time, you use UTC_TIME function as follows:

A2-04-10.MySQL DATA TYPES-Mastering MySQL TIME Data Type

In this tutorial, we have been covered a lot about MySQL TIME data type and some commonly used temporal functions for manipulating TIME values.