时间戳默认值不同于 current_timestamp
Timestamp default value different than current_timestamp
我正在尝试向此 table 添加时间戳列:
CREATE TABLE `task` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`timecreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`session` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
然后我尝试执行此代码以添加另一个具有不同默认值的时间戳列:
ADD COLUMN `timeexpiration` TIMESTAMP NOT NULL DEFAULT TIMESTAMPADD(MINUTE, 15, CURRENT_TIMESTAMP)
ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TIMESTAMPADD(MINUTE, 15, CURRENT_TIMESTAMP) AFTER `timecreated`,'
ADD COLUMN `timeexpiration` TIMESTAMP NOT NULL DEFAULT (CURRENT_TIMESTAMP + INTERVAL 15 MINUTE) AFTER `timecreated`'
ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(CURRENT_TIMESTAMP + INTERVAL 15 MINUTE) AFTER `timecreated`,
但这些都不行。
MySQL 5.6.1 之前的版本不会让两个 TIMESTAMP 列出现在同一个 table 中,除非你正确地指出没有默认值并允许 null。
MySQL 5.6.+ 允许 table.
中有两个或多个 TIMESTAMP 列
阿曼怎么说,不可能在同一个table中使用两个时间戳。您可以改用 DATETIME。尝试这样的事情,
ALTER TABLE task ADD COLUMN `timeexpiration` DATETIME DEFAULT NULL AFTER timecreated;
也许这有帮助。
提示:您必须删除 , PRIMARY KEY (id
) 之后最后一个字段的末尾,.
您不能有两个使用 CURRENT_TIMESTAMP 默认值的时间戳列,但是您可以在插入前使用触发器:
ALTER TABLE task
ADD COLUMN `timeexpiration` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00';
DELIMITER //
CREATE TRIGGER task_timeexpiration_default BEFORE INSERT ON task FOR EACH ROW
BEGIN
SET NEW.`timeexpiration` = TIMESTAMPADD(MINUTE, 15, CURRENT_TIMESTAMP);
END;//
我正在尝试向此 table 添加时间戳列:
CREATE TABLE `task` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(45) NOT NULL,
`timecreated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP,
`session` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
然后我尝试执行此代码以添加另一个具有不同默认值的时间戳列:
ADD COLUMN `timeexpiration` TIMESTAMP NOT NULL DEFAULT TIMESTAMPADD(MINUTE, 15, CURRENT_TIMESTAMP)
ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'TIMESTAMPADD(MINUTE, 15, CURRENT_TIMESTAMP) AFTER `timecreated`,'
ADD COLUMN `timeexpiration` TIMESTAMP NOT NULL DEFAULT (CURRENT_TIMESTAMP + INTERVAL 15 MINUTE) AFTER `timecreated`'
ERROR 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(CURRENT_TIMESTAMP + INTERVAL 15 MINUTE) AFTER `timecreated`,
但这些都不行。
MySQL 5.6.1 之前的版本不会让两个 TIMESTAMP 列出现在同一个 table 中,除非你正确地指出没有默认值并允许 null。
MySQL 5.6.+ 允许 table.
中有两个或多个 TIMESTAMP 列阿曼怎么说,不可能在同一个table中使用两个时间戳。您可以改用 DATETIME。尝试这样的事情,
ALTER TABLE task ADD COLUMN `timeexpiration` DATETIME DEFAULT NULL AFTER timecreated;
也许这有帮助。
提示:您必须删除 , PRIMARY KEY (id
) 之后最后一个字段的末尾,.
您不能有两个使用 CURRENT_TIMESTAMP 默认值的时间戳列,但是您可以在插入前使用触发器:
ALTER TABLE task
ADD COLUMN `timeexpiration` TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00';
DELIMITER //
CREATE TRIGGER task_timeexpiration_default BEFORE INSERT ON task FOR EACH ROW
BEGIN
SET NEW.`timeexpiration` = TIMESTAMPADD(MINUTE, 15, CURRENT_TIMESTAMP);
END;//