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

在這裏插入圖片描述

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)

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章