The date and time data types for representing temporal values are 用于表示时间值的日期和时间数据类型是DATE, TIME, DATETIME, TIMESTAMP, and YEAR.DATE、TIME、DATETIME、TIMESTAMP和YEAR。
For the 对于DATE and DATETIME range descriptions, “supported” means that although earlier values might work, there is no guarantee.DATE和TIMEDATE范围的描述,“支持”意味着尽管早期的值可能有效,但不能保证。
MySQL permits fractional seconds for MySQL允许TIME, DATETIME, and TIMESTAMP values, with up to microseconds (6 digits) precision. TIME、DATETIME和TIMESTAMP值的小数秒,精度高达微秒(6位)。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. ,其中type_name(fsp)type_name是TIME、DATETIME或TIMESTAMP,fsp是分秒精度。For example:例如:
CREATE TABLE t1 (t TIME(3), dt DATETIME(6), ts TIMESTAMP(0));
The fsp value, if given, must be in the range 0 to 6. fsp值(如果给定)必须在0到6之间。A value of 0 signifies that there is no fractional part. 值为0表示没有小数部分。If omitted, the default precision is 0. 如果省略,则默认精度为0。(This differs from the standard SQL default of 6, for compatibility with previous MySQL versions.)(为了与以前的MySQL版本兼容,这与标准SQL默认值6不同。)
Any 表中的任何TIMESTAMP or DATETIME column in a table can have automatic initialization and updating properties; see Section 11.2.5, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.TIMESTAMP或DATETIME列都可以具有自动初始化和更新属性;请参见第11.2.5节,“时间戳和日期时间的自动初始化和更新”。
A date. 日期。The supported range is 支持的范围是'1000-01-01' to '9999-12-31'. '1000-01-01'到'9999-12-31'。MySQL displays MySQL以DATE values in ' format, but permits assignment of values to YYYY-MM-DD'DATE columns using either strings or numbers.'YYYY-MM-DD'格式显示DATE值,但允许使用字符串或数字为DATE列赋值。
A date and time combination. 日期和时间的组合。The supported range is 支持的范围是'1000-01-01 00:00:00.000000' to '9999-12-31 23:59:59.999999'. '1000-01-01 00:00:00.000000'到'9999-12-31 23:59:59.999999'。MySQL displays MySQL以DATETIME values in ' format, but permits assignment of values to YYYY-MM-DD hh:mm:ss[.fraction]'DATETIME columns using either strings or numbers.'的格式显示YYYY-MM-DD hh:mm:ss[.fraction]'DATETIME值,但是允许使用字符串或数字为DATETIME列赋值。
An optional 可以给出0到6范围内的可选fsp value in the range from 0 to 6 may be given to specify fractional seconds precision. fsp值,以指定小数秒精度。A value of 0 signifies that there is no fractional part. 值为0表示没有小数部分。If omitted, the default precision is 0.如果省略,则默认精度为0。
Automatic initialization and updating to the current date and time for 可以使用DATETIME columns can be specified using DEFAULT and ON UPDATE column definition clauses, as described in Section 11.2.5, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.DEFAULT和ON UPDATE列定义子句指定DATETIME列的当前日期和时间的自动初始化和更新,如第11.2.5节“TIMESTAMP和DATETIME的自动初始化和更新”所述。
A timestamp. 时间戳。The range is 范围是'1970-01-01 00:00:01.000000' UTC to '2038-01-19 03:14:07.999999' UTC. '1970-01-01 00:00:01.000000'UTC到'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). TIMESTAMP值存储为自纪元('1970-01-01 00:00:00'UTC)以来的秒数。A TIMESTAMP 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.TIMESTAMP不能表示值'1970-01-01 00:00:00',因为这相当于从纪元开始的0秒,而值0保留用于表示'0000-00-00 00:00:00',即时间戳值“零”。
An optional 可以给出0到6范围内的可选fsp value in the range from 0 to 6 may be given to specify fractional seconds precision. fsp值,以指定小数秒精度。A value of 0 signifies that there is no fractional part. 值为0表示没有小数部分。If omitted, the default precision is 0.如果省略,则默认精度为0。
The way the server handles 服务器处理TIMESTAMP definitions depends on the value of the explicit_defaults_for_timestamp system variable (see Section 5.1.8, “Server System Variables”).TIMESTAMP定义的方式取决于explicit_defaults_for_timestamp系统变量的值(请参阅第5.1.8节“服务器系统变量”)。
If 如果启用了explicit_defaults_for_timestamp is enabled, there is no automatic assignment of the DEFAULT CURRENT_TIMESTAMP or ON UPDATE CURRENT_TIMESTAMP attributes to any TIMESTAMP column. explicit_defaults_for_timestamp,则不会自动将DEFAULT CURRENT_TIMESTAMP或ON UPDATE CURRENT_TIMESTAMP属性分配给任何TIMESTAMP列。They must be included explicitly in the column definition. 它们必须显式地包含在列定义中。Also, any 另外,任何未显式声明为TIMESTAMP not explicitly declared as NOT NULL permits NULL values.not NULL的TIMESTAMP都允许NULL值。
If 如果禁用了explicit_defaults_for_timestamp is disabled, the server handles TIMESTAMP as follows:explicit_defaults_for_timestamp,服务器将按如下方式处理时间戳:
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. TIMESTAMP列定义为如果未显式赋值,则自动设置为最近修改的日期和时间。This makes 这使得TIMESTAMP useful for recording the timestamp of an INSERT or UPDATE operation. TIMESTAMP对于记录INSERT或UPDATE操作的时间戳非常有用。You can also set any 您还可以通过为任何TIMESTAMP column to the current date and time by assigning it a NULL value, unless it has been defined with the NULL attribute to permit NULL values.TIMESTAMP列指定一个NULL值,从而将其设置为当前日期和时间,除非它已使用NULL属性定义为允许空值。
Automatic initialization and updating to the current date and time can be specified using 可以使用DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP column definition clauses. DEFAULT CURRENT_TIMESTAMP和ON UPDATE current_TIMESTAMP列定义子句指定自动初始化和更新到当前日期和时间。By default, the first 默认情况下,第一个TIMESTAMP column has these properties, as previously noted. TIMESTAMP列具有这些属性,如前所述。However, any 但是,表中的任何TIMESTAMP column in a table can be defined to have these properties.TIMESTAMP列都可以定义为具有这些属性。
A time. 时间。The range is 范围是从'-838:59:59.000000' to '838:59:59.000000'. '-838:59:59.000000'到'838:59:59.000000'。MySQL displays MySQL以TIME values in ' format, but permits assignment of values to hh:mm:ss[.fraction]'TIME columns using either strings or numbers.'格式显示时间值,但允许使用字符串或数字为时间列赋值。hh:mm:ss[.fraction]'
An optional 可以给出0到6范围内的可选fsp value in the range from 0 to 6 may be given to specify fractional seconds precision. fsp值,以指定小数秒精度。A value of 0 signifies that there is no fractional part. 值为0表示没有小数部分。If omitted, the default precision is 0.如果省略,则默认精度为0。
A year in 4-digit format. 四位数格式的一年。MySQL displays MySQL以YEAR values in YYYY format, but permits assignment of values to YEAR columns using either strings or numbers. YYYY格式显示年份值,但允许使用字符串或数字为年份列赋值。Values display as 值显示为1901 to 2155, or 0000.1901到2155或0000。
For additional information about 有关YEAR display format and interpretation of input values, see Section 11.2.4, “The YEAR Type”.YEAR显示格式和输入值解释的更多信息,请参阅第11.2.4节“年份类型”。
As of MySQL 8.0.19, the 从MySQL 8.0.19开始,不推荐使用显式显示宽度的YEAR(4) data type with an explicit display width is deprecated; you should expect support for it to be removed in a future version of MySQL. YEAR(4)数据类型;您应该期望在MySQL的未来版本中删除对它的支持。Instead, use 请改用不带显示宽度的YEAR without a display width, which has the same meaning.YEAR,其含义相同。
MySQL 8.0 does not support the 2-digit MySQL 8.0不支持MySQL旧版本中允许的2位YEAR(2) data type permitted in older versions of MySQL. YEAR(2)数据类型。For instructions on converting to 4-digit 有关转换为4位数年份的说明,请参阅MySQL5.7参考手册中的2位数YEAR(2)限制和迁移到4位数年份。YEAR, see 2-Digit YEAR(2) Limitations and Migrating to 4-Digit YEAR, in MySQL 5.7 Reference Manual.
The SUM() and AVG() aggregate functions do not work with temporal values. SUM()和AVG()聚合函数不能处理时态值。(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(time_col))) FROMtbl_name; SELECT FROM_DAYS(SUM(TO_DAYS(date_col))) FROMtbl_name;