CREATE TABLE AlarmHistory
(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
value DOUBLE NOT NULL,
startedStamp TIMESTAMP NOT NULL,
finishedStamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL,
);
尝试创建上表时出现以下错误:“SQL 错误 (1293):表定义不正确;在 DEFAULT 或 ON UPDATE 子句中只能有一个带有 CURRENT_TIMESTAMP 的 TIMESTAMP 列”。
我的问题是这是一个错误吗?因为当然,我有两个 TIMESTAMP 列,但其中只有一个具有默认定义。当我删除 startedStamp 时,我没有错误。
最佳答案
根据 MySQL 手册,版本 5.5,Automatic Initialization and Updating for TIMESTAMP
With neither
DEFAULT CURRENT_TIMESTAMP
norON UPDATE CURRENT_TIMESTAMP
, it is the same as specifying bothDEFAULT CURRENT_TIMESTAMP
andON UPDATE CURRENT_TIMESTAMP
.
CREATE TABLE t1 (
ts TIMESTAMP
);
但是,
With a constant, the default is the given value. In this case, the column has no automatic properties at all.
CREATE TABLE t1 (
ts TIMESTAMP DEFAULT 0
);
所以,这应该可行:
CREATE TABLE AlarmHistory
(
id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
value DOUBLE NOT NULL,
startedStamp TIMESTAMP DEFAULT 0 NOT NULL,
finishedStamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP NOT NULL
);
关于MySQL 错误 "There can be only one TIMESTAMP column with CURRENT_TIMESTAMP in DEFAULT clause"即使我没有做错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23054394/