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

首頁 > 數(shù)據(jù)庫 > MySQL > 正文

MySQL錯(cuò)誤TIMESTAMP column with CURRENT_TIMESTAMP的解決方法

2024-07-24 12:47:16
字體:
供稿:網(wǎng)友

在部署程序時(shí)遇到的一個(gè)問題,MySQL定義舉例如下:

復(fù)制代碼 代碼如下:

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是我從項(xiàng)目中摘取出來的,在測(cè)試機(jī)器上一切正常,但是部署到生產(chǎn)機(jī)器上MySQL報(bào)錯(cuò):
復(fù)制代碼 代碼如下:

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

意思是只能有一個(gè)帶CURRENT_TIMESTAMP的timestamp列存在,但是為什么本地測(cè)試卻沒有任何問題呢,本地測(cè)試的機(jī)器安裝的MySQL版本5.6.13,而生產(chǎn)機(jī)器上安裝的卻是5.5版本,搜索網(wǎng)絡(luò)后得知這兩種版本之間對(duì)于timestamp處理的區(qū)別在于:


在MySQL 5.5文檔有這么一段話:

復(fù)制代碼 代碼如下:

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做出了以下改變:
復(fù)制代碼 代碼如下:

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.

根據(jù)網(wǎng)上的解決方案,可以使用觸發(fā)器來替代一下:
復(fù)制代碼 代碼如下:

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的類型與時(shí)區(qū)實(shí)例詳解mysql之TIMESTAMP(時(shí)間戳)用法詳解MySQL timestamp自動(dòng)更新時(shí)間分享Sqlserver timestamp數(shù)據(jù)類使用介紹mysql From_unixtime及UNIX_TIMESTAMP及DATE_FORMAT日期函數(shù)SQL計(jì)算timestamp的差值的方法
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 油尖旺区| 宜兰市| 邵武市| 江津市| 祁连县| 阿拉善左旗| 若羌县| 正阳县| 桐梓县| 安图县| 卓资县| 龙井市| 井陉县| 乐东| 宽城| 兴国县| 宝应县| 航空| 巴东县| 扬州市| 长葛市| 宜州市| 稷山县| 仁化县| 高平市| 建瓯市| 乌拉特前旗| 资中县| 靖远县| 三都| 云安县| 余干县| 高碑店市| 黄冈市| 崇文区| 晋宁县| 德阳市| 张掖市| 大安市| 阳东县| 建德市|