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

本文详细解析了MySQL 5.6版本中Timestamp列的行为变化,从旧版本到新版本的过渡,包括默认值、自动更新等关键特性,以及如何在新环境中正确配置和使用Timestamp列。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在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

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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值