国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 丘北县| 荥经县| 荥阳市| 格尔木市| 万年县| 周口市| 临高县| 天峻县| 黑龙江省| 白朗县| 连南| 建宁县| 章丘市| 平舆县| 淄博市| 盘锦市| 长沙市| 常宁市| 灵川县| 乐清市| 遂平县| 高台县| 宁南县| 公主岭市| 迁西县| 常山县| 九江县| 鲜城| 平陆县| 阜阳市| 荔浦县| 德化县| 玛曲县| 仁寿县| 邵东县| 平凉市| 左贡县| 阿克苏市| 宜春市| 东阿县| 招远市|