记录
MySQL5.7 datetime 默认值设为‘0000-00-00 00:00:00’值出错
我的MySQL版本 mysql --version
5.7.28
C:Usersx1c>mysql --version
mysql Ver 14.14 Distrib 5.7.28, for Win64 (x86_64)
C:Usersx1c>
我要创建的表结构
DROP TABLE IF EXISTS `dianpingdb`.`user`;
CREATE TABLE `dianpingdb`.`user` (
`id` int NOT NULL AUTO_INCREMENT,
`created_at` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`updated_at` datetime NOT NULL DEFAULT '0000-00-00 00:00:00',
`telphone` varchar(40) NOT NULL DEFAULT '',
`password` varchar(200) NOT NULL DEFAULT '',
`nick_name` varchar(40) NOT NULL DEFAULT '',
`gender` int NOT NULL DEFAULT 0,
PRIMARY KEY (`id`),
UNIQUE `telphone_unique_index` USING BTREE (`telphone`) comment ''
) COMMENT='';
error错误信息:
Invalid default value for ‘created_at’.
我之前MySQL5.6是可以这样创建的设为‘0000-00-00 00:00:00’值的,但
MySQL5.7版本以后,datetime不再支持默认值写入"0000-00-00 00:00:00"的。
那怎么解决MySQL5.7设置datetime默认值设为‘0000-00-00 00:00:00’值呢?
使用root登陆数据库
mysql -u root -p
执行查看下sql_mode
select @@sql_mode;
设置下
SET GLOBAL sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
mysql -u root -p
select @@sql_mode;
SET GLOBAL sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
去掉 NO_ZERO_IN_DATE, NO_ZERO_DATE
即可。
NO_ZERO_IN_DATE
,不允许DATE日期为零 ,
NO_ZERO_DATE
,不允许插入零日期,插入零日期会抛出错误。
可以创建设置‘0000-00-00 00:00:00’了。
C:Usersx1c>mysql -u root -p
Enter password: ******
Welcome to the MySQL monitor. Commands end with ; or g.
Your MySQL connection id is 802
Server version: 5.7.28 MySQL Community Server (GPL)
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> 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> SET GLOBAL sql_mode='ONLY_FULL_GROUP_BY,STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
Query OK, 0 rows affected, 1 warning (0.00 sec)