[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

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

原创文章,转载请注明: 转载自Simple Life

本文链接地址: [MySQL 5.6] mysql 5.6 timestamp类型列的改变

Post Footer automatically generated by wp-posturl plugin for wordpress.


Comments

Leave a Reply

Your email address will not be published. Name and email are required


Current month ye@r day *