MySQL 5.7-11.2.2 The DATE, DATETIME, and TIMESTAMP Types

The DATEDATETIME, and TIMESTAMP types are related. This section describes their characteristics, how they are similar, and how they differ. MySQL recognizes DATEDATETIME, and TIMESTAMP values in several formats, described in Section 9.1.3, “Date and Time Literals”. For the DATE and DATETIME range descriptions, “supported” means that although earlier values might work, there is no guarantee.

DATE、DATETIME和TIMESTAMP类型是相关的。本节描述它们的特征、相似之处和不同之处。MySQL可以识别几种格式的DATE, DATETIME和TIMESTAMP值,在9.1.3节“DATE and Time Literals”中描述。对于DATE和DATETIME范围描述,“受支持”意味着尽管较早的值可以工作,但不能保证。

The DATE type is used for values with a date part but no time part. MySQL retrieves and displays DATE values in 'YYYY-MM-DD' format. The supported range is '1000-01-01' to '9999-12-31'.

DATE类型用于具有日期部分而没有时间部分的值。MySQL以“YYYY-MM-DD”格式检索和显示DATE值。支持的范围是“1000-01-01”到“99999 -12-31”。

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'.

DATETIME类型用于同时包含日期和时间部分的值。MySQL以“YYYY-MM-DD hh:mm:ss”格式检索和显示DATETIME值。支持的取值范围为“1000-01-01 00:00:00”到“99999-12-31 23:59:59”。

The TIMESTAMP data type is used for values that contain both date and time parts. TIMESTAMP has a range of '1970-01-01 00:00:01' UTC to '2038-01-19 03:14:07' UTC.

TIMESTAMP数据类型用于同时包含日期和时间部分的值。TIMESTAMP的范围是'1970-01-01 00:00:01' UTC到'2038-01-19 03:14:07' UTC。

DATETIME or TIMESTAMP value can include a trailing fractional seconds part in up to microseconds (6 digits) precision. In particular, any fractional part in a value inserted into a DATETIME or TIMESTAMP column is stored rather than discarded. With the fractional part included, the format for these values is 'YYYY-MM-DD hh:mm:ss[.fraction]', the range for DATETIME values is '1000-01-01 00:00:00.000000' to '9999-12-31 23:59:59.999999', and the range for TIMESTAMP values is '1970-01-01 00:00:01.000000' to '2038-01-19 03:14:07.999999'. The fractional part should always be separated from the rest of the time by a decimal point; no other fractional seconds delimiter is recognized. For information about fractional seconds support in MySQL, see Section 11.2.7, “Fractional Seconds in Time Values”.

DATETIME或TIMESTAMP值可以包含尾随的小数秒部分,精度最高可达微秒(6位)。特别是,插入到DATETIME或TIMESTAMP列中的值的任何小数部分都将被存储,而不是丢弃。包含的小数部分,这些值的格式是“YYYY-MM-DD hh: mm: ss (.fraction)”,DATETIME值的范围是“1000-01-01 00:00:00.000000”“9999-12-31 23:59:59.999999”,和时间戳值的范围是“1970-01-01 00:00:01.000000”“2038-01-19 03:14:07.999999”。小数部分与其余部分总是用小数点隔开;不能识别其他小数秒分隔符。关于MySQL中对分数秒的支持,请参见11.2.7节“时间值中的分数秒”。

The TIMESTAMP and DATETIME data types offer automatic initialization and updating to the current date and time. For more information, see Section 11.2.6, “Automatic Initialization and Updating for TIMESTAMP and DATETIME”.

TIMESTAMP和DATETIME数据类型提供了对当前日期和时间的自动初始化和更新。

MySQL converts TIMESTAMP values from the current time zone to UTC for storage, and back from UTC to the current time zone for retrieval. (This does not occur for other types such as DATETIME.) By default, the current time zone for each connection is the server's time. The time zone can be set on a per-connection basis. As long as the time zone setting remains constant, you get back the same value you store. If you store a TIMESTAMP value, and then change the time zone and retrieve the value, the retrieved value is different from the value you stored. This occurs because the same time zone was not used for conversion in both directions. The current time zone is available as the value of the time_zone system variable. For more information, see Section 5.1.13, “MySQL Server Time Zone Support”.

MySQL将TIMESTAMP值从当前时区转换为UTC以进行存储,并将其从UTC转换为当前时区以进行检索。(对于其他类型,如DATETIME,则不会出现这种情况。)默认情况下,每个连接的当前时区是服务器的时间。时区可以根据每个连接来设置。只要时区设置保持不变,您就会得到存储的相同值。如果存储一个TIMESTAMP值,然后更改时区并检索该值,则检索到的值与存储的值不同。这是因为在两个方向上的转换没有使用相同的时区。当前时区为系统变量time_zone的值。

Invalid DATEDATETIME, or TIMESTAMP values are converted to the “zero” value of the appropriate type ('0000-00-00' or '0000-00-00 00:00:00'), if the SQL mode permits this conversion. The precise behavior depends on which if any of strict SQL mode and the NO_ZERO_DATE SQL mode are enabled; see Section 5.1.10, “Server SQL Modes”.

如果SQL模式允许这种转换,无效的DATE、DATETIME或TIMESTAMP值将被转换为适当类型的“零”值('0000-00-00'或'0000-00-00 00:00')。精确行为取决于是否启用了任何严格的SQL模式和NO_ZERO_DATE SQL模式;

Be aware of certain properties of date value interpretation in MySQL:

注意MySQL中日期值解释的某些属性:

  • MySQL permits a “relaxed” format for values specified as strings, in which any punctuation character may be used as the delimiter between date parts or time parts. In some cases, this syntax can be deceiving. For example, a value such as '10:11:12' might look like a time value because of the :, but is interpreted as the year '2010-11-12' if used in date context. The value '10:45:15' is converted to '0000-00-00' because '45' is not a valid month.MySQL允许对指定为字符串的值采用“放松”格式,其中任何标点字符都可以用作日期部分或时间部分之间的分隔符。在某些情况下,这种语法可能具有欺骗性。例如,'10:11:12'这样的值可能看起来像一个时间值,因为:,但如果在日期上下文中使用,则被解释为年份'2010-11-12'。因为“45”不是有效月份,所以值“10:45:15”会被转换为“0000-00-00”。

    The only delimiter recognized between a date and time part and a fractional seconds part is the decimal point.在日期和时间部分和小数秒部分之间识别的唯一分隔符是小数点。

  • The server requires that month and day values be valid, and not merely in the range 1 to 12 and 1 to 31, respectively. With strict mode disabled, invalid dates such as '2004-04-31' are converted to '0000-00-00' and a warning is generated. With strict mode enabled, invalid dates generate an error. To permit such dates, enable ALLOW_INVALID_DATES. See Section 5.1.10, “Server SQL Modes”, for more information.

  • 服务器要求月和日的值是有效的,而不仅仅是分别在1到12和1到31之间。禁用严格模式后,无效日期(如“2004-04-31”)将被转换为“0000-00-00”,并生成一个警告。启用严格模式时,无效日期将产生错误。要允许这样的日期,启用ALLOW_INVALID_DATES。

  • MySQL does not accept TIMESTAMP values that include a zero in the day or month column or values that are not a valid date. The sole exception to this rule is the special “zero” value '0000-00-00 00:00:00', if the SQL mode permits this value. The precise behavior depends on which if any of strict SQL mode and the NO_ZERO_DATE SQL mode are enabled; see Section 5.1.10, “Server SQL Modes”.

  • MySQL不接受在日或月列中包含零的TIMESTAMP值,或者不是有效日期的值。唯一的例外是特殊的" 0 "值'0000-00-00 00:00',如果SQL模式允许这个值。精确行为取决于是否启用了任何严格的SQL模式和NO_ZERO_DATE SQL模式;

  • Dates containing 2-digit year values are ambiguous because the century is unknown. MySQL interprets 2-digit year values using these rules:包含两位数年份值的日期是不明确的,因为世纪是未知的。MySQL使用以下规则解释2位数的年份值:

    • Year values in the range 00-69 become 2000-2069.

    • 年值在00-69范围内变为2000-2069。

    • Year values in the range 70-99 become 1970-1999.

    • 年份值在70-99之间就变成1970-1999。

    See also Section 11.2.9, “2-Digit Years in Dates”.

Note

The MySQL 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 Section 5.1.10, “Server SQL Modes”.

MySQL服务器可以在MAXDB SQL模式下运行。在本例中,TIMESTAMP与DATETIME相同。如果在创建表时启用了此模式,则TIMESTAMP列将被创建为DATETIME列。因此,这些列使用DATETIME显示格式,具有相同的值范围,并且不会自动初始化或更新到当前日期和时间。

Note

As of MySQL 5.7.22, MAXDB is deprecated; expect it to removed in a future version of MySQL.

从MySQL 5.7.22开始,MAXDB已弃用;希望在未来的MySQL版本中删除它。

上一篇:Python入门学习笔记(1)


下一篇:2021SC@SDUSC(dolphinscheduler- common2)