Comparison of 5 time types of mysql

5 time types of msql: DATETIME, TIMESTAMP, DATE, TIME, YEAR.
Compare as follows:

 DATETIME

     DATETIME is used to represent the year, month, day, hour, minute, and second. It is a combination of DATE and TIME, and the recorded year (see the table above) is relatively long. If there is such a requirement in practical applications, the DATETIME type can be used.


1. The DATETIME column can be set to multiple, the default is null, and its value can be set manually.
2. The DATETIME column cannot be set to a default value.
3. The default value of the DATETIME column can be set in disguise, such as through triggers or when inserting data, setting the value of the DATETIME field to now(), which can be done, especially the latter, which is often used in program development .

 TIMESTAMP

TIMESTAMP is used to represent the year, month, day, hour, minute and second, but the recorded year (see the above table) is relatively short. The TIMESTAMP column must have a default value. The default value can be "0000-00-00 00:00:00", but cannot be null .
TIMESTAMP is related to the time zone and better reflects the current time. When inserting a date, it will be converted to the local time zone before storing; when querying a date, the date will be converted to the local time zone and then displayed. So people in different time zones see the same time differently.
The first TIMESTAMP column in the table is automatically set to the system time (CURRENT_TIMESTAMP). When inserting or updating a row without explicitly assigning a value to the TIMESTAMP column, it is also automatically set to the current system time. If there is a second TIMESTAMP column in the table, the default value is set to 0000-00-00 00:00:00.
The properties of TIMESTAMP are greatly affected by Mysql version and server SQLMode.
     If the recorded date needs to be used by people in different time zones, it is best to use TIMESTAMP.

Note: Generally, when building a table, use datetime for creation time and timestamp for update time.

 DATE

    DATE is used to represent the year, month and day. If the actual application value needs to save the year, month and day, DATE can be used.

 TIME

    TIME is used to represent hours, minutes and seconds. If the actual application value needs to save hours, minutes and seconds, TIME can be used.

 YEAR

    YEAR is used to represent the year, YEAR has 2 digits (preferably 4 digits) and the year in 4 digit format. The default is 4 bits. If the actual application only saves the year, it is perfectly fine to store the YEAR type with 1 bytes. It can not only save storage space, but also improve the operation efficiency of the table.


———————————————
Copyright statement: This article is the original article of the CSDN blogger "Grey Wolf_cxh", following the CC 4.0 BY-SA copyright agreement, please attach the original source link and this statement.
Original link: https://blog.csdn.net/weixin_39220472/article/details/80458341

Guess you like

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