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.
而在MySQL 5.6.5做出了以下改变:
复制代码 代码如下:
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.
根据网上的解决方案,可以使用触发器来替代一下:
复制代码 代码如下:
CREATE TABLE `example` ( `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT, `created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `lastUpdated` DATETIME NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB; DROP TRIGGER IF EXISTS `update_example_trigger`; DELIMITER // CREATE TRIGGER `update_example_trigger` BEFORE UPDATE ON `example` FOR EACH ROW SET NEW.`lastUpdated` = NOW() // DELIMITER ;
您可能感兴趣的文章:
MySQL timestamp的类型与时区实例详解mysql之TIMESTAMP(时间戳)用法详解MySQL timestamp自动更新时间分享Sqlserver timestamp数据类使用介绍mysql From_unixtime及UNIX_TIMESTAMP及DATE_FORMAT日期函数SQL计算timestamp的差值的方法