1292 - Incorrect datetime value: '0000-00-00 00:00:00' for column

解决Navicat运行sql文件报错:1292 - Incorrect datetime value: '0000-00-00 00:00:00' for column 'created_at' at row 1

 

1. 错误提示

[ERR] 1292 - Incorrect datetime value: '0000-00-00 00:00:00' for column 'created_at' at row 1

 

2. 问题分析
  • 从上面的描述我们可以看出原因是:我们给类型是datetime的xxx字段赋值0000-00-00 00:00:00是不被支持的,赋值包含两种情况:
    • 情况一:我们运行的sql文件直接提供的值就是0000-00-00 00:00:00
    • 情况二:xxx字段我们没有提供值,默认值是0000-00-00 00:00:00

3. 原因分析

原因是我们的数据库不支持datetime为0的情况,这是一个模式的设置。

4. 具体解决

4.1 查看sql_mode

# 查看当前sql_mode
select @@sql_mode;
# 查看全局sql_mode
select @@global.sql_mode;

 

  

4.2 显示结果:

mysql> select @@sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@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 |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
 
mysql> select @@global.sql_mode;
+-------------------------------------------------------------------------------------------------------------------------------------------+
| @@global.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 |
+-------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
 
mysql>

 

  

4.3 修改sql_mode(将上述查询到的sql_mode中的NO_ZERO_DATE和NO_ZERO_IN_DATE删除即可)

# 修改全局
set @@global.sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
# 修改当前
set @@sql_mode = 'ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';

 

  

 
上一篇:报错:Incorrect datetime value: '0000-00-00 00:00:00' for column 'login_time' at ro


下一篇:535,剑指 Offer-扑克牌中的顺子