1. 程式人生 > >mysql 多個timestamp 錯誤:there can be only one TIMESTAMP column with CURRENT_TIMESTAMP

mysql 多個timestamp 錯誤:there can be only one TIMESTAMP column with CURRENT_TIMESTAMP

post div blog primary bsp pos mule ins one

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

mysql 多個timestamp 錯誤:there can be only one TIMESTAMP column with CURRENT_TIMESTAMP