The difference and use of date, datetime and timestamp in MySQL5.6

  • The DATE type is used for values ​​that have a date part but no time part . MySQL retrieves and displays DATE values ​​in the format. Supported ranges are 'YYYY-MM-DD' '1000-01-01' '9999-12-31'

  • The DATETIME type is used for values ​​that contain both date and time parts . MySQL retrieves and displays DATETIME values ​​in the format. The supported ranges are: 'YYYY-MM-DD hh:mm:ss' '1000-01-01 00:00:00' '9999-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. **

The difference and use of datetime and timestamp in MySQL

Similarities between TIMESTAMP and DATETIME:

  • Both can be used to represent values ​​that contain both date and time parts: dates of type YYYY-MM-DD HH:MM:SS[.fraction].

Differences between TIMESTAMP and DATETIME:

  1. The storage methods of the two are different

For TIMESTAMP, it converts the time inserted by the client from the current time zone to UTC (Coordinated Universal Time) for storage. When querying, it is converted into the current time zone of the client and returned .
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 timezone setting remains the same, you can return the same value stored.

And for DATETIME, do not make any changes, basically input and output as is .

  1. The time ranges that the two can store are different

The time range that timestamp can store is: '1970-01-01 00:00:01.000000' to '2038-01-19 03:14:07.999999'.
The time range that datetime can store is: '1000-01-01 00:00:00.000000' to '9999-12-31 23:59:59.999999'.

Reference document: https://dev.mysql.com/doc/refman/5.6/en/datetime.html

Guess you like

Origin blog.csdn.net/weixin_45334970/article/details/123104565