mysql 多个timestamp设置自动更新 错误:there can be only one TIMESTAMP column with CURRENT_TIMESTAMP

mysql 5.6.5以下的版本不支持多个timestamp同时设为default current_timestamp

替代方式是使用trigger

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 `insert_example_trigger`; DROP TRIGGER IF EXISTS `update_example_trigger`; DELIMITER
// CREATE TRIGGER `insert_example_trigger` BEFORE INSERT ON `example` FOR EACH ROW SET NEW.`lastUpdated` = NOW()
//
CREATE TRIGGER `update_example_trigger` BEFORE UPDATE ON `example`
FOR EACH ROW SET NEW.`lastUpdated` = NOW()
// DELIMITER ;

 trigger与表相关联,如果表删除了trigger也就没有了

insert、update、delete三种行为只能对应最多一个trigger

 

参考文献:

http://www.jb51.net/article/50878.htm

https://www.cnblogs.com/lmule/archive/2010/09/28/1837303.html

https://www.cnblogs.com/nicholas_f/archive/2009/09/22/1572050.html

posted @ 2017-12-13 11:09  jhc888007  阅读(1923)  评论(0编辑  收藏  举报