mysql5.6 0000-00-00 00:00:00_【Problems】MySQL5.7 datetime 默认值设为‘0000-00-00 00:00:00'值出错...

记录

MySQL5.7 datetime 默认值设为‘0000-00-00 00:00:00’值出错

我的MySQL版本 mysql --version 5.7.28

C:\Users\x1c>mysql --version

mysql Ver 14.14 Distrib 5.7.28, for Win64 (x86_64)

C:\Users\x1c>

我要创建的表结构

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’了。

d442def0887ef47e3ef6420723a84ec1.png

C:\Users\x1c>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)

756c81a4a4af55ac3a71069b02b5a08a.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值