zoukankan      html  css  js  c++  java
  • 【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: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)
    

    在这里插入图片描述

  • 相关阅读:
    JAVA合并两个有序的单链表,合并之后的链表依然有序
    excel如何将一个单元格内容拆分成多个单元格?(用到了数据->分列)
    Navicat导入excel的xlsx文件提示无法打开文件
    Request对象实现请求转发
    MessageFormat.format()和String.format()
    使用Servlet动态生成验证码
    Http协议
    使用freemarker导出word
    java注解学习(1)注解的作用和三个常用java内置注解
    SSM_CRUD新手练习(6)分页后台控制器编写
  • 原文地址:https://www.cnblogs.com/liuawen/p/12854021.html
Copyright © 2011-2022 走看看