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)