Date and Time Type Overview
A summary of the temporal data types follows. For additional information about properties and storage requirements of the temporal types, see , "Date and Time Types", and , "Data Type Storage Requirements". For descriptions of functions that operate on temporal values, see , "Date and Time Functions".
For the DATE
and DATETIME
range descriptions, "supported" means that although earlier values might work, there is no guarantee.
MySQL 5.6.4 and up 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
, 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 MariaDB versions.)
MySQL 5.6.5 introduces expanded automatic initialization and updating of temporal types. Any TIMESTAMP
column in a table can have these properties, rather than at most one column per table. In addition, these properties are now available for DATETIME
columns.
DATE
A date. The supported range is
'1000-01-01'
to'9999-12-31'
. MariaDB displaysDATE
values in'YYYY-MM-DD'
format, but permits assignment of values toDATE
columns using either strings or numbers.DATETIME[(
fsp
)]A date and time combination. The supported range is
'1000-01-01 00:00:00.000000'
to'9999-12-31 23:59:59.999999'
. MariaDB displaysDATETIME
values in'YYYY-MM-DD HH:MM:SS[.fraction]'
format, but permits assignment of values toDATETIME
columns using either strings or numbers.As of MariaDB 5.6.4, an optional
fsp
value in the range from 0 to 6 may be given to specify fractional seconds precision. A value of 0 signifies that there is no fractional part. If omitted, the default precision is 0.As of MariaDB 5.6.5, automatic initialization and updating to the current date and time for
DATETIME
columns can be specified usingDEFAULT
andON UPDATE
column definition clauses, as described in , "Automatic Initialization and Updating forTIMESTAMP
andDATETIME
".TIMESTAMP[(
fsp
)]A timestamp. The range is
'1970-01-01 00:00:01.000000'
UTC to'2038-01-19 03:14:07.999999'
UTC.TIMESTAMP
values are stored as the number of seconds since the epoch ('1970-01-01 00:00:00'
UTC). ATIMESTAMP
cannot represent the value'1970-01-01 00:00:00'
because that is equivalent to 0 seconds from the epoch and the value 0 is reserved for representing'0000-00-00 00:00:00'
, the "zero"TIMESTAMP
value.As of MariaDB 5.6.4, an optional
fsp
value in the range from 0 to 6 may be given to specify fractional seconds precision. A value of 0 signifies that there is no fractional part. If omitted, the default precision is 0.Unless specified otherwise, the first
TIMESTAMP
column in a table is defined to be automatically set to the date and time of the most recent modification if not explicitly assigned a value. This makesTIMESTAMP
useful for recording the timestamp of anINSERT
orUPDATE
operation. You can also set anyTIMESTAMP
column to the current date and time by assigning it aNULL
value, unless it has been defined with theNULL
attribute to permitNULL
values.Automatic initialization and updating to the current date and time can be specified using
DEFAULT
andON UPDATE
column definition clauses. By default, the firstTIMESTAMP
column has these properties, as previously noted. As of MariaDB 5.6.5, anyTIMESTAMP
column in a table can be defined to have these properties. Before 5.6.5, at most oneTIMESTAMP
column per table can have them, but it is possible to suppress them for the first column and instead assign them to a differentTIMESTAMP
column. See , "Automatic Initialization and Updating forTIMESTAMP
andDATETIME
".NoteThe
TIMESTAMP
format that was used prior to MariaDB is not supported in MariaDB 5.6; see MySQL 3.23, 4.0, 4.1 Reference Manual for information regarding the old format.TIME[(
fsp
)]A time. The range is
'-838:59:59.000000'
to'838:59:59.000000'
. MariaDB displaysTIME
values in'HH:MM:SS[.fraction]'
format, but permits assignment of values toTIME
columns using either strings or numbers.As of MariaDB 5.6.4, an optional
fsp
value in the range from 0 to 6 may be given to specify fractional seconds precision. A value of 0 signifies that there is no fractional part. If omitted, the default precision is 0.YEAR[(2|4)]
A year in two-digit or four-digit format. The default is four-digit format. In four-digit format, the permissible values are
1901
to2155
, and0000
. In two-digit format, the permissible values are70
to69
, representing years from 1970 to 2069. MariaDB displaysYEAR
values inYYYY
format, but permits assignment of values toYEAR
columns using either strings or numbers.
The SUM()
and AVG()
aggregate functions do not work with temporal values. (They convert the values to numbers, losing everything after the first nonnumeric character.) To work around this problem, convert to numeric units, perform the aggregate operation, and convert back to a temporal value. Examples:
SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(Notetime_col
))) FROMtbl_name
; SELECT FROM_DAYS(SUM(TO_DAYS(date_col
))) FROMtbl_name
;
The MariaDB server can be run with the MAXDB
SQL mode enabled. In this case, TIMESTAMP
is identical with DATETIME
. If this mode is enabled at the time that a table is created, TIMESTAMP
columns are created as DATETIME
columns. As a result, such columns use DATETIME
display format, have the same range of values, and there is no automatic initialization or updating to the current date and time. See , "Server SQL Modes".