Mysql5.7 default parameter error_MYSQL ERROR 1067: Invalid default value for NO_ZERO_IN_DATE, NO_ZERO_DATE These two parameters limit time cannot be 0

mysql1067 default parameter error_MYSQL ERROR 1067: Invalid default value for 'xxx_time' solution

Recently, I upgraded mysql to 5.7, and wordpress reported an error when exporting data. When adding a field to a table, I will suddenly find that the default value of a date type field will be reported as wrong, depressed~. The same problem may also appear in mysql8.0.

See what your field name is, mine is a time field, and the type is datetime. Thinking that the default value of the type may be limited, check sql_mode. Sure enough: NO_ZERO_IN_DATE, NO_ZERO_DATE These two parameters limit time cannot be 0.

68741a1f9448d9c78d254db93388fee6.png

1. Error screenshot

MySQL:ERROR 1067 (42000): Invalid default value for ‘end_time’

2. Error Analysis

The first TIMESTAMP column in the table (if not declared as NULL or present with a DEFAULT or ON UPDATE clause) will automatically be assigned the DEFAULT CURRENT_TIMESTAMP and ON UPDATE CURRENT_TIMESTAMP attributes

TIMESTAMP columns after the first (if not declared as NULL or display DEFAULT clause) will be automatically assigned DEFAULT '0000-00-00 00:00:00' (zero timestamp), which does not satisfy NO_ZERO_DATE in sql_mode and reports an error .

Note: There are two types of sql_mode, one is null value and the other is strict mode, which will give many default settings. Strict mode is used by default after MySQL5.7.

NO_ZERO_DATE: If this value is set, the MySQL database does not allow inserting a zero date, and inserting a zero date will throw an error instead of a warning.

3. Solution

Method 1: first execute select @@sql_mode, copy the queried value and delete NO_ZERO_DATE in it, and then execute set sql_mode = 'modified value'.

This method only takes effect in the current session

Method 2: First execute select @@global.sql_mode, copy the queried value and delete NO_ZERO_DATE in it, and then execute set global sql_mode = 'modified value'.

This method takes effect in the current service and fails after restarting the MySQL service

Method 3: In the mysql installation directory, open the my.ini or my.cnf file. Under wamp, SQL_MODE is not set in MySQL 5.7.

1. Find [mysqld] in the my.ini file

2. If there is no SQL_MODE, add it, if there is, modify it

sql_mode=”STRICT_TRANS_TABLES,NO_ENGINE_SUBSTITUTION”

or

sql_mode=ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION

3. Restart MySQL; service mysqld restart

Guess you like

Origin blog.csdn.net/happyzhlb/article/details/122766938