Having both a Created and Last Updated timestamp columns in MySQL 4.0(在 MySQL 4.0 中同时具有 Created 和 Last Updated 时间戳列)
问题描述
我有以下表格架构;
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.
这篇关于在 MySQL 4.0 中同时具有 Created 和 Last Updated 时间戳列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在 MySQL 4.0 中同时具有 Created 和 Last Updated 时间戳列


- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- SQL 临时表问题 2022-01-01
- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 更改自动增量起始编号? 2021-01-01