mysql date time type and precision issues

foreword

This article mainly sorts out the differences between the five date and time types time, date, datetime, timestamp and year provided by mysql , and the accuracy of date and time types

An online bug, mysql data type is timestamp, the time printed in the log is 2021-02-23 10:16:55.781 but the last stored in the database is 2021-02-23 10:16:56, which triggered this article Research on timestamp precision issues.

datetime type in mysql

The date and time types in mysql are time, date, datetime, timestamp and year

type of data minimum value maximum value zero value means
date 1000-01-01 9999-12-31 0000-00-00
datetime 1000-01-01 00:00:00 9999-12-31 23:59:59 0000-00-00 00:00:00[.000000]
time -838:59:59 838:59:59 00:00:00[.000000]
timestamp 19700101080001 2038-01-19 03:14:07.999999 (accurately speaking, it should be in the UTC range) 0000000000000000[000000]
year 1901 2155 0000

每种日期和时间类型都有一个有效范围。如果插入的值超过了这个范围,系统就会报错。 对于数据类型TIME、DATETIME和TIMESTAMP,MySQL 5.6.4增加了对小数秒(fsp:[fractional seconds storage])的支持,小数秒可以有6位(微秒)精度

Explain
date in detail : date value expressed in yyyy-mm-dd format, date is used to represent year, month and day, if the actual application value needs to save year, month and day, date can be used.

datetime : yyyy-mm-dd hh:mm:ss format, 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 above table) is relatively long.

time : time value expressed in hh:mm:ss format, time is used to represent hours, minutes and seconds (with negative time representation)

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 and can only reach 2038-01-19 03:14:07.999999 (accurately speaking, it should be in the UTC range). timestamp is related to time zone and can better reflect the current time. When inserting a date, it will be converted to the local time zone before storing; when querying the date, it will be converted to the local time zone before displaying. So people in different time zones see the same time differently.
The attribute of timestamp is greatly affected by the 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.

year : year is used to represent the year, year has 2 digits (4 digits are better) and 4 digits format year. The default is 4 bits. If the actual application only saves the year, then it is perfectly fine to use 1 bytes to save the year type. It can not only save storage space, but also improve the operation efficiency of the table.

occupied bytes

In MySQL 5.6, several important improvements have been made to these types:
For data types TIME, DATETIME, and TIMESTAMP, MySQL 5.6.4 adds support for fractional seconds (fsp: [fractional seconds storage]). These types now allow up to 6 digits (microseconds) of precision for the optional fractional part .
MySQL 5.6.5 introduces extended support: automatically use the current timestamp as the initial value and update it. In previous releases, these attributes were only available on most individual TIMESTAMP columns in tables. Now, they can be used on any TIMESTAMP column, and they can also be used on DATETIME columns.
MySQL 5.6.6 drops support for YEAR(2) and instead allows creation of columns like YEAR(4).

If you want to declare a temporal type column that contains fractional seconds, you need to write the definition as type_name(fsp), where type_name is TIME, DATETIME or TIMESTAMP, and fsp is the precision of fractional seconds. For example, the TIME column below allows 3 and 6 decimal places, respectively:

CREATE TABLE `date_test` (
  `d3` time(3),
  `d4` time(6)
) ENGINE=InnoDB AUTO_INCREMENT=0  COMMENT='test测试';

fsp值的取值范围必须为0~6。如果未给定,则默认为0。

type of time

The time type uses 3 bytes to represent time. In MySQL, the value of Time type is displayed in the form of HH:MM:SS. Among them, HH means hour; MM means minute, the value range is 0~~59; SS means second, the value range is 0~~59.
The Time type can range from '-838:59:59' ~~ '838:59:59'. Although the range of the hour is 0~~23, in order to represent a time interval for a special need, the range of the Time type is expanded. It also supports negative values.

The expression method of field assignment of the Time type is as follows:
1. A string representation in the format of 'D HH:MM:SS'. Among them, D represents the number of days, and the value range is 0~~34. When saving, the value of hours is equal to (D*24+HH). For example, input '2 11:30:50', the Time type will be converted to 59:30:50.
certainly. It is not necessary to strictly follow this format when inputting, or it can be in the form of 'HH:MM:SS','HH:MM','D HH:MM','D HH','SS', etc. For example, if you input '30', the Time type will be automatically converted to 00:00:30.

2. String in 'HHMMSS' format or numerical representation in HHMMSS format, for example, input '123456', Time type will be converted to 12:34:56; input 123456, Time type will be converted to 12:34:56. If 0 or '0' is entered, the TIME type will be converted to 0000:00:00.

3. Use current_time or current_time() or now() to input the current system time.

The difference between datetime and timestamp

1. Similarities

Both datetime and timestamp can represent data in the format of YYYY-MM-DD HH:MM:SS.

From MySQL 5.6.4 onwards both can contain fractional seconds with a precision up to microseconds (6 digits).

There is one point to note here, that is, before MySQL5.6.4, these two cannot represent decimals.

Two, the difference

1: The storage range is different

The storage range of datetime is 1000-01-01 00:00:00.000000 to 9999-12-31 23:59:59.999999, and the range of timestamp is 1970-01-01 00:00:01.000000 to 2038-01-19 03: 14:07.999999 (accurately speaking, it should be in the UTC range)

2: Time zone related

Datetime storage has nothing to do with time zone (accurately, datetime only supports one time zone, which is the time zone of the current server when storing), while timestamp storage is related to time zone.
When MySQL stores the timestamp, it first converts the time from the current server's time zone to UTC (Universal Coordinated Time) for storage, and then converts the time from UTC to the current time zone for return when querying. That is to say, when the time stored using timestamp is returned, it will change with the time zone of the database. The storage of datetime has nothing to do with the time zone. Whatever the data is stored, it will be returned.
timestamp is more suitable for recording time. For example, my time in East Eight District is now 2021-06-08 10:23:45, and you are in Japan (the time in East Nine District is 2021-06-08 11:23:45), I am chatting with you, the database records the time, after taking it out, the time is 2021-06-08 10:23:45 for me, and 2021-06-08 11:23:45 for you in Japan. So there is no need to consider the calculation of the time zone.

3: Storage space size

Before 5.6.4, datetime storage occupies 8 bytes, while timestamp occupies 4 bytes; but after 5.6.4, since these two types allow decimal parts, the storage space occupied is different from before; MySQL specification It is stipulated that the non-decimal part of datetime requires 5 bytes instead of 8 bytes, and the non-decimal part of timestamp requires 4 bytes, and the decimal part of both parts requires 0 to 3 bytes. Depending on the fractional seconds precision of the stored value.

pit in mysql date type

time, timestamp, datetime data types are rounded

The current mysql version is 8.0.18

CREATE TABLE `date_test` (
  `id` int(20) NOT NULL AUTO_INCREMENT COMMENT '主键id',
  `d1` date,
  `d2` datetime,
  `d3` time,
  `d4` timestamp,
  `d5` year,
  primary key(id)
) ENGINE=InnoDB AUTO_INCREMENT=0  COMMENT='test测试';
insert into date_test(d2,d4) values('2021-02-23 10:16:55.781','2021-02-23 10:16:55.781');

insert image description here
Conclusion: When the time, timestamp, and datetime data types do not specify precision, they will be rounded by default.
Problem Solved: You can set the precision of the field, such as timestamp(3), timestamp(6)

Guess you like

Origin blog.csdn.net/doublepg13/article/details/128301235