1缩功,Mysql中datetime默認(rèn)值'0000-00-00 00:00:00'無法創(chuàng)建問題
在使用Mysql時(shí)當(dāng)建表語句中有以下語句時(shí):
create_time DATETIME NOT NULL DEFAULT ‘0000-00-00 00:00:00’
會報(bào)錯(cuò):
Invalid default value for ‘create_time’
是因?yàn)镾QL_MODE的問題沒設(shè)置好荠瘪,方案如下:
查詢當(dāng)前數(shù)據(jù)庫的SQL_MODE
show variables like 'sql_mode%'
得到以下結(jié)果:
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只在當(dāng)前會話中生效肌毅,GLOBAL為全局生效瓦宜。
笨拙的方法:
把語句改為以下就可以執(zhí)行創(chuàng)建表
create_time DATETIME NOT NULL DEFAULT ‘0000-01-01 00:00:00’!