天天看点

[MySQL 5.6] mysql 5.6 timestamp类型列的改变

在5.5及之前的版本中,timestamp列的行为比较特殊:

* timestamp columns not explicitly declared with the null attribute are assigned the not null attribute.

* the first timestamp column in a table, if not declared with the null attribute or an explicit default or on update clause, is automatically assigned the default current_timestamp and on update current_timestamp attributes.

* timestamp columns following the first one, if not declared with the null attribute or an explicit default clause, are automatically assigned default ‘0000-00-00 00:00:00′ (the “zero” timestamp). for inserted rows that specify no explicit value for such a column, the column is assigned ‘0000-00-00 00:00:00′ and no warning occurs.

简而言之,timestamp列,如果没有显示定义为null,则被认为not null;第一个timestamp列如果没有显式的定义为null,或者有一个显式的默认值,被自动设置为current_timestamp,但第二个timestmap列的默认值就被设置为’0000-00-00 00:00:00′

这种行为在5.6是被弃用的,转而使用新的显示timestamp行为,简单说,就是你定义的是什么就是什么,如果想让timestamp列自动更新,需要显式指定default current_timestamp 或者on update current_timestamp

5.6新的行为使用参数explicit_defaults_for_timestamp 来控制,默认为off,表示之前版本的行为,但启动mysqld的时候会报warning,也不排除未来会把这个参数调整默认为on

所以还是适应这种改变吧。。。

继续阅读