程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> MySQL毛病TIMESTAMP column with CURRENT_TIMESTAMP的處理辦法

MySQL毛病TIMESTAMP column with CURRENT_TIMESTAMP的處理辦法

編輯:MySQL綜合教程

MySQL毛病TIMESTAMP column with CURRENT_TIMESTAMP的處理辦法。本站提示廣大學習愛好者:(MySQL毛病TIMESTAMP column with CURRENT_TIMESTAMP的處理辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是MySQL毛病TIMESTAMP column with CURRENT_TIMESTAMP的處理辦法正文


在安排法式時碰到的一個成績,MySQL界說舉例以下:

CREATE TABLE `example` (
  `id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `lastUpdated` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB;
這段SQL是我從項目中摘掏出來的,在測試機械上一切正常,然則安排到臨盆機械上MySQL報錯:

ERROR 1293 (HY000): Incorrect table definition; there can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT or ON UPDATE clause.

意思是只能有一個帶CURRENT_TIMESTAMP的timestamp列存在,然則為何當地測試卻沒有任何成績呢,當地測試的機械裝置的MySQL版本5.6.13,而臨盆機械上裝置的倒是5.5版本,搜刮收集後得知這兩種版本之間關於timestamp處置的差別在於:


在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.
而在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 ;

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved