1,Mysql中datetime默认值'0000-00-00 00:00:00'无法创建问题
在使用Mysql时当建表语句中有以下语句时:
create_time DATETIME NOT NULL DEFAULT ‘0000-00-00 00:00:00’
会报错:
Invalid default value for ‘create_time’
是因为SQL_MODE的问题没设置好,方案如下:
查询当前数据库的SQL_MODE
show variables like '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
可以看到NO_ZERO_IN_DATE,NO_ZERO_DATE是DATE不能全部为0的原因,所以把模式中的NO_ZERO_IN_DATE,NO_ZERO_DATE去掉就可以了。
操作:
mysql> SET GLOBAL sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION';
修改完之后需要退出,从新链接
qiuwangdeMacBook-Pro:etc qiu$ mysql -u root -p
Enter password:
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 10
Server version: 8.0.15 Homebrew
Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql> show variables like 'sql_mode%';
+---------------+------------------------------------------------------------------------------------------+
| Variable_name | Value |
+---------------+------------------------------------------------------------------------------------------+
| sql_mode | ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_ENGINE_SUBSTITUTION |
+---------------+------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql>
SET [SESSION|GLOBAL] sql_mode=’modes’
其中:SESSION只在当前会话中生效,GLOBAL为全局生效。
笨拙的方法:
把语句改为以下就可以执行创建表
create_time DATETIME NOT NULL DEFAULT ‘0000-01-01 00:00:00’!