mysql 5.5和5.6版本关于timestamp not null类型字段关于null的处理

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

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=utf8mysql

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

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

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

从上面实验看,在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 726ci


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)get

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)it

直接报错,拦截语句插入。在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)io

相关文章
相关标签/搜索