zoukankan      html  css  js  c++  java
  • mysql 5.5和5.6版本关于timestamp not null类型字段关于null的处理

    Server version: 5.5.33-31.1-log Percona Server (GPL), Release rel31.1, Revision 566

    mysql> CREATE TABLE `t1` (
    `ID` int(11) NOT NULL DEFAULT '1',
    `NAME` varchar(10) NOT NULL DEFAULT '',
    `CREATE_TIME` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8

    mysql> select * from t1;
    Empty set (0.00 sec)

    mysql> insert into t1 (create_time) values (null);
    Query OK, 1 row affected (0.00 sec)

    mysql> select * from t1;
    +----+------+---------------------+
    | ID | NAME | CREATE_TIME |
    +----+------+---------------------+
    | 1 | | 2015-04-15 17:27:09 |
    +----+------+---------------------+
    1 row in set (0.00 sec)

    从上面实验看,在5.533版本中,create_time虽然定义为not null ,但是实际是能插入null只的,而且自动转换为了current_time。接下来看5.6的操作:
    Server version: 5.6.22-71.0-log Percona Server (GPL), Release 71.0, Revision 726


    mysql> CREATE TABLE `t1` (
    `ID` int(11) NOT NULL DEFAULT '1',
    `NAME` varchar(10) NOT NULL DEFAULT '',
    `CREATE_TIME` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00'
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

    mysql> select * from t1;
    Empty set (0.00 sec)

    mysql> insert into t1 (create_time) values (null);
    ERROR 1048 (23000): Column 'CREATE_TIME' cannot be null
    mysql> select * from t1;
    Empty set (0.00 sec)

    直接报错,拦截语句插入。在5.6中是通过开启参数来实现解决这个BUG的。下面是摘自官方文档的描述
    With explicit_defaults_for_timestamp enabled, inserting NULL into a TIMESTAMP NOT NULL column now produces an error
    (as it already did for other NOT NULL data types), instead of inserting the current timestamp. (Bug #68472, Bug #16394472)

  • 相关阅读:
    【java基础学习一】int[]、Integer[]、String[] 排序( 正序、倒叙)、去重
    【转】jqGrid 各种参数 详解
    CSS 中文字体的英文名称 (simhei, simsun) 宋体 微软雅黑
    Web应用程序项目XX已配置为使用IIS
    zsh安装及配置
    vscode安装及配置
    matlab2018a安装及配置
    teminator安装及配置
    clion安装及配置
    pcl之octree的使用
  • 原文地址:https://www.cnblogs.com/yiyuf/p/4430984.html
Copyright © 2011-2022 走看看