在 MySQL 4.0 中同时具有 Created 和 Last Updated 时间戳列

2021-11-20 00:00:00 mysql mysql-error-1293

我有以下表格架构;

CREATE TABLE `db1`.`sms_queue` (
  `Id` INTEGER UNSIGNED NOT NULL AUTO_INCREMENT,
  `Message` VARCHAR(160) NOT NULL DEFAULT 'Unknown Message Error',
  `CurrentState` VARCHAR(10) NOT NULL DEFAULT 'None',
  `Phone` VARCHAR(14) DEFAULT NULL,
  `Created` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `LastUpdated` TIMESTAMP NOT NULL ON UPDATE CURRENT_TIMESTAMP,
  `TriesLeft` tinyint NOT NULL DEFAULT 3,
  PRIMARY KEY (`Id`)
)
ENGINE = InnoDB;

失败并出现以下错误:

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

我的问题是,我可以同时拥有这两个字段吗?还是我必须在每次交易期间手动设置 LastUpdated 字段?

My question is, can I have both of those fields? or do I have to manually set a LastUpdated field during each transaction?

推荐答案

来自 MySQL 5.5 文档:

表中的一个 TIMESTAMP 列可以将当前时间戳作为初始化列的默认值,作为自动更新值,或两者兼而有之.不可能将当前时间戳作为一列的默认值和另一列的自动更新值.

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:

以前,每个表最多有一个 TIMESTAMP 列可以自动初始化或更新为当前日期和时间.此限制已取消.任何 TIMESTAMP 列定义都可以具有 DEFAULT CURRENT_TIMESTAMP 和 ON UPDATE CURRENT_TIMESTAMP 子句的任意组合.此外,这些子句现在可以与 DATETIME 列定义一起使用.有关详细信息,请参阅 TIMESTAMP 和 DATETIME 的自动初始化和更新.

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.

相关文章