国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 數據庫 > MySQL > 正文

MySQL錯誤TIMESTAMP column with CURRENT_TIMESTAMP的解決方法

2020-01-18 23:15:56
字體:
來源:轉載
供稿:網友

在部署程序時遇到的一個問題,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 ;

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桐城市| 门源| 西华县| 麻城市| 思茅市| 七台河市| 东乌| 平乐县| 夹江县| 新民市| 凤山市| 黎川县| 巫山县| 淅川县| 诸城市| 安义县| 应用必备| 革吉县| 出国| 肥东县| 新密市| 赣榆县| 济宁市| 尉氏县| 阳西县| 高台县| 鄂温| 宁晋县| 汪清县| 罗甸县| 调兵山市| 奉节县| 隆子县| 定兴县| 和政县| 黄平县| 襄樊市| 台山市| 水城县| 封开县| 瓦房店市|