MySQL database date type recommendation and comparison

In development, we often need to store dates in the database. Today we will talk about the date types in the database.

1. First of all, it is recommended to use date to store time and date types
during development. Some people will use string format to store time and date, but judging from the size of its data type, the number of bytes occupied by date is less than that of varchar type. And it cannot be used for some functions that require the use of operation time and date. For date parameters, use date type, or if a time and date are required, use Datetime.

2. The difference between DateTime and Timestamp.
Both can represent time of type YYYY-MM-DD HH:MM:SS. But there is a difference between the two.
Difference 1:
The DateTime type is responsible for saving the time corresponding to the time zone set by the current session. Once the database where the data is stored changes the time zone, the parameters read out will be incorrect.
After Timestamp stores the date, relevant calculations will be performed according to the time zone of the current database each time it is accessed. Therefore, the values ​​read out for different time zones are different.
Difference 2:
DateTime uses 8 bytes to store data and uses the actual format as the data storage format. Its data storage range is 1000-01-01 00:00:00 ~ 9999-12-31 23:59:59.
Timestamp uses 4 bytes to store data and uses UTC (Universal Time Coordinated) as the data storage format. Its data storage range is 1970-01-01 00:00:01 ~ 2037-12-31 23:59 :59.
It can be seen from the range of stored data that DateTime can represent a larger time range.

Guess you like

Origin blog.csdn.net/leaning_java/article/details/126548201