When Mysql creates a table, the date and time type is selected (reposted)

Repost address: http://www.cnblogs.com/Jie-Jack/p/3793304.html

The date and time types supported by mysql (5.5) are: DATETIME, TIMESTAMP, DATE, TIME, YEAR.

Several types are compared as follows :

datetime type take up space date format minimum maximum value zero value means
 DATETIME  8 bytes  YYYY-MM-DD HH:MM:SS
 1000-01-01 00:00:00
9999-12-31 23:59:59 
0000-00-00 00:00:00
 TIMESTAMP  4 bytes  YYYY-MM-DD HH:MM:SS  19700101080001
sometime in 2038
00000000000000
 DATE  4 bytes
 YYYY-MM-DD 1000-01-01 
9999-12-31 
0000-00-00
 TIME  3 bytes
 HH:MM:SS  -838:59:59 838:59:59 
00:00:00
 YEAR  1 bytes
 YYYY 1901 
2155 
0000

 

 

 

 

 

 

 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.

 TIMESTAMP

  • TIMESTAMP is used to represent the year, month, day, hour, minute and second, but the recorded year (see the table above) is relatively short.
  • 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.

 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.

 

---------------------------------------------------------------------------------------------------------------------------------------------------------------

 

Each datetime type has a valid range of values. If this range is exceeded, an error will be reported in the default SQLMode, and a value of zero (see table above) will be stored.

 

When inserting or updating, the datetime type allows a "loose" syntax, taking DATETIME as an example (similar to other datetime types):

  • A string in the format YYYY-MM-DD HH:MM:SS or YY-MM-DD HH:MM:SS. Any symbol can be used as a separator for the date part or the time part. For example: "14-06-18 14:54:10", "14*06*18 14.54.10", " 14+06+18 14=54=10 " are equivalent. For string values ​​containing datetimes, if the month, day, hour, minute, and second values ​​are less than 10, you do not need to specify two digits. For example: "2014-2-3 2:3:6", "2014-02-03 02:03:06" are equivalent.
  • YYYYMMDDHHMMSS 或 YYMMDDHHMMSS 格式的字符串。如果字符串对于日期时间类型是合法的就可以解释为日期时间类型。例如:“20140618145410” 和 “140618145410”将被解释为 “2014-06-18 14:54:10” ,但是 “20140618145480” 是不合法的(秒数不合法),将被解释为 “0000-00-00 00:00:00”。
  • YYYYMMDDHHMMSS 或 YYMMDDHHMMSS 格式的数字。如果该数字对日期时间类型是合法的就可以解释为日期时间类型。例如:“20140618145410” 和 “140618145410” 将被解释为 “2014-06-18 14:54:10” 。数值的长度应为6、8、12、14。如果数值长度是 8 或 14 位长,则假定为 YYYYMMDD 或 YYYYMMDDHHMMSS 格式。如果数值为 6 或 12 位长,则假定为 YYMMDD 或 YYMMDDHHMMSS 格式。

 


 

end

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326205173&siteId=291194637