Date, DateTime and TimeStamp types in MySQL database

DATETIME, DATE and TIMESTAMP types are related. This article describes their characteristics and how they are similar yet different.

   The DATETIME type is used when you need a value that contains both date and time information. MySQL retrieves and displays DATETIME values ​​in the format 'YYYY-MM-DD HH:MM:SS', the supported range is '1000-01-01 00:00:00' to '9999-12-31 23:59:59' . ("Supported" means that while earlier values ​​may work, there is no guarantee they will.)

  The DATE type is used when you only need date values, without the time part. MySQL retrieves and displays DATE values ​​in 'YYYY-MM-DD' format, the supported range is '1000-01-01' to '9999-12-31'.

  The TIMESTAMP column type provides a type that you can use to automatically mark INSERT or UPDATE operations with the current date and time. If you have multiple TIMESTAMP columns, only the first one is updated automatically.

  Automatic updating of the first TIMESTAMP column occurs under any of the following conditions:

  Columns are not explicitly specified in an INSERT or LOAD DATA INFILE statement.

  The column was not explicitly specified in an UPDATE statement and some other column changed value. (Note that an UPDATE sets a column to the value it already has. This will not cause the TIMESTAMP column to be updated, because if you set a column to its current value, MySQL ignores the change for efficiency.)

  You explicitly set the TIMESTAMP column to NULL.

  TIMESTAMP columns other than the first can also be set to the current date and time, as long as the column is set to NULL, or NOW().

   By explicitly setting the desired value, you can set any TIMESTAMP column to a value other than the current date and time, even for the first TIMESTAMP column. For example, if, when you create a row, you want a TIMESTAMP to be set to the current date and time, but not later whenever the row is updated, you can use this property:

  Have MySQL set the column when the row is created, which will initialize it to the current date and time.

  When you perform subsequent changes to other columns in the row, explicitly set the TIMESTAMP column to its current value.

  On the other hand, you may find it easy to use a DATETIME column that you initialize with NOW() when the row is created and away from subsequent changes.

  TIMESTAMP values ​​can be from the beginning of sometime in 1970 until the year 2037, with a precision of one second, and the value is displayed as a number.

  Retrieving and displaying TIMESTAMP values ​​in MySQL depends on the format of the display size as shown in the table below. The "full" TIMESTAMP format is 14 bits, but TIMESTAMP columns can be created with shorter display sizes:

Double click code to select all
1
2
3
4
5
6
7
8
列类型     显示格式
TIMESTAMP(14) YYYYMMDDHHMMSS
TIMESTAMP(12) YYMMDDHHMMSS
TIMESTAMP(10) YYMMDDHHMM
TIMESTAMP(8) YYYYMMDD
TIMESTAMP(6) YYMMDD
TIMESTAMP(4) YYMM
TIMESTAMP(2) YY

  All TIMESTAMP columns have the same storage size, regardless of display size. The most common display sizes are 6, 8, 12, and 14. You can specify an arbitrary display size at table creation time, but values ​​of 0 or greater are forced to 14. Odd-valued dimensions in the range from 1 to 13 are forced to the next larger even number.

  Using any of a common set of formats, you can specify DATETIME, DATE, and TIMESTAMP values:

   A string in the format 'YYYY-MM-DD HH:MM:SS' or 'YY-MM-DD HH:MM:SS'. A "loose" syntax is allowed -- any punctuation can be used as a separator between the date part and the time part. For example, '98-12-31 11:30:45', '98.12.31 11+30+45', '98/12/31 11*30*45', and '98@12@31 11^30^45 ' is equivalent.

  A string in the format 'YYYY-MM-DD' or 'YY-MM-DD'. A "relaxed" syntax is allowed. For example, '98-12-31', '98.12.31', '98/12/31' and '98@12@31' are equivalent.

   A string in the format 'YYYYMMDDHHMMSS' or 'YYMMDDHHMMSS' without any delimiters, for example, '19970523091528' and '970523091528' are interpreted as '1997-05-23 09:15:28', but '971122459015' is not valid (it has meaningless minutes part) and becomes '0000-00-00 00:00:00'.

   A string in 'YYYYMMDD' or 'YYMMDD' format without any delimiters, if the string is considered a date. For example, '19970523' and '970523' are interpreted as '1997-05-23', but '971332' is invalid (it has meaningless month and day parts) and becomes '0000-00-00'.

  A number in the format YYYYMMDDHHMMSS or YYMMDDHHMMSS if the number is considered a date. For example, 19830905132800 and 830905132800 are interpreted as '1983-09-05 13:28:00'.

  A number in YYYYMMDD or YYMMDD format if the number is considered a date. For example, 19830905 and 830905 are interpreted as '1983-09-05'.

  A function whose return value can be accepted in a DATETIME, DATE or TIMESTAMP context, such as NOW() or CURRENT_DATE.

  Invalid DATETIME, DATE or TIMESTAMP values ​​are converted to "zero" values ​​of the appropriate type ('0000-00-00 00:00:00', '0000-00-00' or 00000000000000).

   For values ​​specified as strings that include the date part separator, it is not necessary to specify 2 digits for month or day values ​​less than 10, '1979-6-9' is the same as '1979-06-09'. Likewise, for values ​​specified as strings that include the time part separator, it is not necessary to specify 2 digits for hours, months or seconds less than 10, '1979-10-30 1:2:3' and '1979-10- 30 01:02:03' is the same.

  Specified as a number should be 6, 8, 12 or 14 digits long. If the number is 8 or 14 digits long, it is assumed to be in YYYYMMDD or YYYYMMDDHHMMSS format and the year is given by the first 4 digits. If the number is 6 or 12 digits long, it is assumed to be in YYMMDD or YYMMDDHHMMSS format and the year is given by the first 2 digits. Numbers that are not one of these lengths are accounted for by padding leading zeros to the nearest length.

  Strings specified as delimiters are interpreted with their given length. If the string length is 8 or 14 characters, the year is assumed to be given in the first 4 characters, otherwise the year is assumed to be given in the first 2 characters. For multiple parts presented in a string, the string is interpreted from left to right to find out the year, month, day, hour, minute and second values, which means, you should not use less than 6 characters string. For example, if you specify '9903', which you think will represent March 1999, you will find that MySQL inserts a "zero" date into your table because of the year and month values ​​99 and 03, but the date part is missing ( zero), so the value is not a valid date.

  TIMESTAMP columns store legal values ​​with the full precision of the specified value, regardless of display size. This has several implications:

  Always specify year, month, and day, even if your column type is TIMESTAMP(4) or TIMESTAMP(2). Otherwise, the value will not be a valid date and 0 will be stored.

  If you use ALTER TABLE to widen a narrow TIMESTAMP column, previously "hidden" information will be displayed.

  Likewise, shrinking a TIMESTAMP column does not result in loss of information, except that it feels like less information is displayed when the value is displayed.

   Although TIMESTAMP values ​​are stored with full precision, the only function that operates directly on stored values ​​is UNIX_TIMESTAMP(), other functions operate on formatted retrieved values, which means you cannot use functions such as HOUR() or SECOND(), Unless the relevant part of the TIMESTAMP value is included in the formatted value. For example, the HH portion of a TIMESTAMP column is displayed unless the display size is at least 10, so trying to use HOUR() on shorter TIMESTAMP values ​​produces a meaningless result.

  To some extent, you can assign a value of one date type to an object of a different date type. However, this may be worth some change or loss of information:

  If you assign a DATE value to a DATETIME or TIMESTAMP object, the time portion of the resulting value is set to '00:00:00' because DATE values ​​do not contain time information.

  If you assign a DATETIME or TIMESTAMP value to a DATE object, the time portion of the resulting value is removed because the DATE type does not store time information.

   Remember that although DATETIME, DATE and TIMESTAMP values ​​can all be specified with the same format set, not all types have the same range of values. For example, a TIMESTAMP value cannot be earlier than 1970 or later than 2037, which means that a date such as '1968-01-01', when valid as a DATETIME or DATE value, is not a valid TIMESTAMP value, and if assigned to Such an object, it will be transformed to 0.

  When specifying date values, beware of certain pitfalls:

  The loose format that allows the value to be specified as a string can be cheated. For example, values ​​such as '10:11:12' may look like time values ​​because of the ":" separator, but if used in a date, the context will be interpreted as '2010-11-12' as the year. The value '10:45:15' will be converted to '0000-00-00' because '45' is not a valid month.

  Year values ​​specified as 2 digits are ambiguous because the century is unknown. MySQL interprets 2-digit year values ​​using the following rules:

  Year values ​​in the range 00-69 are converted to 2000-2069.

  Years in the range 70-99 were transformed to 1970-1999.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326870241&siteId=291194637