执行表增加字段语句报错
mysql> ALTER TABLE ha_question ADD COLUMN question_number INT;
ERROR (): Invalid default value for 'created_time'
查看created_time字段
mysql> desc ha_question;
+-----------------------+------------------+------+-----+---------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+------------------+------+-----+---------------------+-----------------------------+
| created_time | timestamp | NO | | 0000-00-00 00:00:00 | |
+-----------------------+------------------+------+-----+---------------------+-----------------------------+
创建都不行,所以猜测应该是系统中某个参数的设置问题。看来关于date和time相关的参数没有什么异常。
联想到限制能限制的sql的只有sql_mode。果然:NO_ZERO_IN_DATE,NO_ZERO_DATE这两个参数限制时间不能为0.
查看sql_mode
mysql> show variables like 'sql_mode';
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| Variable_name | Value |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
| sql_mode | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION |
+---------------+-------------------------------------------------------------------------------------------------------------------------------------------+
row in set (0.24 sec)
NO_ZERO_DATE
在严格模式,不要将 '0000-00-00'做为合法日期。你仍然可以用IGNORE选项插入零日期。在非严格模式,可以接受该日期,但会生成警告。
NO_ZERO_DATE:若设置该值,MySQL数据库不允许插入零日期,插入零日期会抛出错误而不是警告。
NO_ZERO_IN_DATE
在严格模式,不接受月或日部分为0的日期。如果使用IGNORE选项,我们为类似的日期插入'0000-00-00'。在非严格模式,可以接受该日期,但会生成警告。
处理的办法:
1, 修改sql_mode,去掉NO_ZERO_IN_DATE,NO_ZERO_DATE这两个参数
2, 修改表的default值,去掉default也可以。
mysql> alter table ha_question modify created_time timestamp not null;
Query OK, rows affected (0.07 sec)
Records: Duplicates: Warnings: mysql> ALTER TABLE ha_question ADD COLUMN question_number INT;
Query OK, rows affected (28.07 sec)
Records: Duplicates: Warnings:
这个出现的原因应该是建表前sql_mode没有这两个值,建表后修改的sql_mode。
个人感觉修改表的风险小一点,以免修改sql mode影响其他表的使用。
修改前的
mysql> desc ha_question;
+-----------------------+------------------+------+-----+---------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+------------------+------+-----+---------------------+-----------------------------+
| created_time | timestamp | NO | | -- :: | |
+-----------------------+------------------+------+-----+---------------------+-----------------------------+
修改后的
mysql> desc ha_question;
+-----------------------+------------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-----------------------+------------------+------+-----+-------------------+-----------------------------+
| created_time | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-----------------------+------------------+------+-----+-------------------+-----------------------------+
转自
ERROR 1067 (42000): Invalid default value for 'currentTime' - CSDN博客 https://blog.csdn.net/xionglang7/article/details/44499307
MySQL:ERROR 1067 (42000): Invalid default value for 'end_time' - 禁忌夜色153 - 博客园 https://www.cnblogs.com/jinjiyese153/p/7300510.html