MySQL--创建时间和更新时间字段

这是我写的第一篇技术博客, 刚刚毕业, 已经投入工作快一个月了, 写一下刚刚遇到的问题, 在创建数据库的时候, 我在一个表中需要创建时间和更新时间这两个字段, 所以sql命令如下:

 `release_time` timestamp DEFAULT CURRENT_TIMESTAMP COMMENT '发布时间',   
`update_time` timestamp DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',

点击运行, 数据库发来了错误警示

there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in default or ON UPDATE clause.

网上查找了一下原因, 我在本地测试用的数据库是MySQL版本5.5, 在MySQL 5.5的文档中有这么一段话

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.

意思就是在一个表中只能有一个TIMESTAMP类型字段可以有CURRENT_TIMESTAMP作为默认值.

在MySQL-5.6.1有如下改变:

Previously, at most one TIMESTAMP column per table could be automatically initialized or

updated to the current date and time. This restriction has been lifted. Any TIMESTAMP column

definition can have any combination of DEFAULT CURRENT_TIMESTAMP and ON UPDATE

CURRENT_TIMESTAMP clauses. In addition, these clauses now can be used with DATETIME column

definitions. For more information, see Automatic Initialization and Updating for TIMESTAMP and DATETIME.

就是说允许任何一个TIMESTAMP或者DATETIME类型字段将CURRENT_TIMESTAMP作为默认值了.

你可能感兴趣的:(mysql)