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)

  • 相关阅读:
    Oracle:解锁scott用户及设置密码
    js生成条形码
    返回头部效果
    密码强度
    事件委托小效果
    圆形导航效果
    进度条效果
    标题跟随效果
    随机抽人小效果
    点击创建效果
  • 原文地址:https://www.cnblogs.com/yiyuf/p/4430984.html
Copyright © 2011-2022 走看看