MySQL 5.6 Reference Manual Including MySQL NDB Cluster 7.3-7.4 Reference Guide
MySQL 5.6 has fractional seconds support 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
,
where type_name
(fsp
)type_name
is
TIME
,
DATETIME
, or
TIMESTAMP
, and
fsp
is the fractional seconds
precision. For example:
CREATE TABLE t1 (t TIME(3), dt DATETIME(6));
The fsp
value, if given, must be
in the range 0 to 6. A value of 0 signifies that there is no
fractional part. If omitted, the default precision is 0.
(This differs from the standard SQL default of 6, for
compatibility with previous MySQL versions.)
Inserting a TIME
,
DATE
, or
TIMESTAMP
value with a
fractional seconds part into a column of the same type but
having fewer fractional digits results in rounding. Consider
a table created and populated as follows:
CREATE TABLE fractest( c1 TIME(2), c2 DATETIME(2), c3 TIMESTAMP(2) ); INSERT INTO fractest VALUES ('17:51:04.777', '2018-09-08 17:51:04.777', '2018-09-08 17:51:04.777');
The temporal values are inserted into the table with rounding:
mysql> SELECT * FROM fractest;
+-------------+------------------------+------------------------+
| c1 | c2 | c3 |
+-------------+------------------------+------------------------+
| 17:51:04.78 | 2018-09-08 17:51:04.78 | 2018-09-08 17:51:04.78 |
+-------------+------------------------+------------------------+
No warning or error is given when such rounding occurs. This
behavior follows the SQL standard, and is not affected by
the server sql_mode
setting.
Functions that take temporal arguments accept values with
fractional seconds. Return values from temporal functions
include fractional seconds as appropriate. For example,
NOW()
with no argument
returns the current date and time with no fractional part,
but takes an optional argument from 0 to 6 to specify that
the return value includes a fractional seconds part of that
many digits.
Syntax for temporal literals produces temporal values:
DATE '
,
str
'TIME '
,
and str
'TIMESTAMP
'
, and the
ODBC-syntax equivalents. The resulting value includes a
trailing fractional seconds part if specified. Previously,
the temporal type keyword was ignored and these constructs
produced the string value. See
Standard SQL and ODBC Date and Time Literals
str
'
In some cases, previously accepted syntax may produce different results. The following items indicate where existing code may need to be changed to avoid problems:
Some expressions produce results that differ from previous
results. Examples: The timestamp
system
variable returns a value that includes a microseconds
fractional part rather than an integer. Functions that
return a result that includes the current time (such as
CURTIME()
,
SYSDATE()
, or
UTC_TIMESTAMP()
) interpret an
argument as an fsp
value and the
return value includes a fractional seconds part of that many
digits. Previously, these functions permitted an argument
but ignored it.
TIME
values are converted to
DATETIME
by adding the time
to the current date. (This means that the date part of the
result differs from the current date if the time value is
outside the range from '00:00:00'
to
'23:59:59'
.) Previously, conversion of
TIME
values to
DATETIME
was unreliable. See
Section 11.2.8, “Conversion Between Date and Time Types”.
TIMESTAMP(
was permitted in old MySQL versions, but
N
)N
was a display width rather than
fractional seconds precision. Support for this behavior was
removed in MySQL 5.5.3, so applications that are reasonably
up to date should not be subject to this issue. Otherwise,
code must be rewritten.