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)

  • 相关阅读:
    Linux系统分支之Ubuntu
    运维工具之Netdata
    Antd Tree组件虚拟滚动空白问题
    没有root权限的情况下安装vim
    C++ / Python测量程序执行时间
    Linux dmidecode 命令介绍
    网卡到底是什么
    flannel的革命性的变化是在哪里呢?
    kube-proxy
    cilium
  • 原文地址:https://www.cnblogs.com/yiyuf/p/4430984.html
Copyright © 2011-2022 走看看