本文作者:苏生米沿
本文地址:http://blog.youkuaiyun.com/sushengmiyan/article/details/50326259
在使用SQL的时候,希望在更新数据的时候自动填充更新时间,那么在MySQL中是如何实现的呢?
如创建表users
其中,UPDATETIME TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP NULL COMMENT '更新时间',会在更新的时候自动填充。啦啦啦。高兴吧。
不要高兴如此早,如果你使用的是mysql5.4版本,那么你表创建的时候都会报错,别说更新时间了,看:
ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause
出错鸟,创建表就失败了。
看5.5版本的时间戳文档:http://dev.mysql.com/doc/refman/5.5/en/timestamp-initialization.html
-
One
TIMESTAMP
column in a table can have the current timestamp as the default value for initializing the column, as the auto-update value, or both. It is not possible to have the current timestamp be the default value for one column and the auto-update value for another column.
可以看到这个版本对时间戳有限制。
再看5.7版本的
http://dev.mysql.com/doc/refman/5.7/en/timestamp-initialization.html
已经没有这个限制了。我觉得,低版本的就只能另想办法,手动或者使用触发器来做更新时间这件事情啦。要么就直接升级版本解决问题。。