When I try to insert a timestamp into a MySQL database, I get an error: mysql> insert into alarm_service values (6, '1970-01-01 08:00:00'); ERROR 1292 (22007): Incorrect datetime value: '1970-01-01 08:00:00' for column 'time' at row 1 # View the table structure mysql> show create table alarm_service; +---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | Table | Create Table | +---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ | alarm_service | CREATE TABLE `alarm_service` ( `id` int(11) NOT NULL AUTO_INCREMENT, `time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 | +---------------+--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+ 1 row in set (0.00 sec) We can see that the error message indicates that the time value is wrong, but this is obviously a valid time point. After searching the data, I found that the reason is that in The solution is:
Below we explain the relevant content in detail. 1. MySQL time typeMySQL time types are divided into three types :
At the same time, Both For the 2. Check the time zonemysql> show variables like '%zone%'; +------------------+--------+ | Variable_name | Value | +------------------+--------+ | system_time_zone | CST | | time_zone | SYSTEM | +------------------+--------+ You can see that the current time zone is SYSTEM, which is consistent with the operating system. The system time zone is CST (China Standard Time). You can also see that the system time is East 8 (+0800): $ date -R Tue, 23 Apr 2019 11:22:47 +0800 Therefore, when we enter 3. Illegal time value For illegal time values, MySQL converts them to appropriate values for different time types: For example, if the months are January to December, when you try to insert 4. Strict modeWhen we insert an illegal time value, it will be corrected, but in strict mode, the data will not be inserted and an error will be reported instead: ERROR 1292 (22007): Incorrect datetime value: '1970-01-01 08:00:00' for column 'time' at row 1 We can adjust the behavior of mysql> show variables like '%sql_mode%'; +----------------------------+--------------------------------------------+ | Variable_name | Value | +----------------------------+--------------------------------------------+ | | sql_mode | STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION | +----------------------------+--------------------------------------------+ In this mode, illegal dates will be reported as errors. We can adjust the mode to mysql> set session sql_mode = 'ALLOW_INVALID_DATES'; Query OK, 0 rows affected (0.00 sec) mysql> show variables like '%sql_mode%'; +---------------+---------------------+ | Variable_name | Value | +---------------+---------------------+ | sql_mode | ALLOW_INVALID_DATES | +---------------+---------------------+ 1 row in set (0.00 sec) In this mode, the date is no longer fully checked for validity, only the month is checked to be in the range 1-12 and the day is in the range 1-31. This is suitable when processing user input, but this mode is only suitable for In the case of illegal values, an error will be reported if this mode is enabled; if disabled, it will be corrected to mysql> insert into alarm_service values (7, '1970-01-01 08:00:00'); Query OK, 1 row affected, 1 warning (0.00 sec) Summarize: There are two solutions to this problem:
5. Case summary Check the reason and find that it is set to: # View the statement to create the table CREATE TABLE `dimensionsConf` ( `id` int(11) NOT NULL AUTO_INCREMENT, `createTime` datetime DEFAULT CURRENT_TIMESTAMP, ) ENGINE=InnoDB AUTO_INCREMENT=178 DEFAULT CHARSET=utf8; # View the database version $mysql --version mysql Ver 14.14 Distrib 5.1.30, for unknown-linux-gnu (x86_64) using EditLine wrapper This is the end of this article about MySQL time types and modes. For more information about MySQL time types and modes, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future! You may also be interested in:
|
<<: Examples of using the Li tag in HTML
>>: TypeScript decorator definition
1. z-index is invalid in IE6. In CSS, the z-index...
There are two installation methods for MySQL: msi...
What is the purpose of this sentence? Copy code Th...
Docker Installation curl -fsSL https://get.docker...
Regarding the nginx panic problem, we first need ...
Sometimes, in order to facilitate the export and ...
Preface Today, when I was designing a feedback fo...
Multi-table query Use a single select statement t...
The display without the effect picture is just em...
1. Introduction MySQL locks can be divided into g...
Achieve results Implementation Code html <div ...
CentOS6.9 installs Mysql5.7 for your reference, t...
Table of contents Data volume Anonymous and named...
yum quick install mysql Add yum repository rpm -U...
The EXPLAIN statement is introduced in MySQL quer...