[英]Unable to create 2 timestamp columns in 1 MySQL table
My sql query looks like this 我的SQL查询看起来像这样
ALTER TABLE `exercises`
ADD COLUMN `creation_dt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP AFTER `lesson_id`,
ADD COLUMN `modification_dt` timestamp NULL DEFAULT '' ON UPDATE CURRENT_TIMESTAMP AFTER `creation_dt`;
But getting error message 但是收到错误消息
How can I fix this problem? 我该如何解决这个问题?
As shown in the screenshot, this is error code 1293
. 如屏幕截图所示,这是错误代码1293
。 The reason for this error seems to be some implementation details in MySQL. 出现此错误的原因似乎是MySQL中的一些实现细节。
You could get around it by using a column of type DATETIME
but this does not allow setting the current time as default
value. 您可以通过使用DATETIME
类型的列来解决它,但这不允许将当前时间设置为default
值。 However, you can solve this in your application code or by using a trigger . 但是,您可以在应用程序代码中或使用Trigger来解决此问题。 I used a sample table (called datetimetest
) and added the trigger like this: 我使用了一个示例表(称为datetimetest
),并添加了以下触发器:
Database schema: 数据库架构:
CREATE TABLE `datetimetest` (
`id` int(11) NOT NULL,
`created` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
CREATE TRIGGER trigger_SetCreated
BEFORE INSERT ON datetimetest
FOR EACH ROW SET NEW.created = UTC_TIMESTAMP();
You can do the same for the modified
field using a trigger BEFORE UPDATE
or keep your solution as you now only have one TIMESTAMP
column that gets set to CURRENT_TIMESTAMP
ON UPDATE
. 您可以使用BEFORE UPDATE
的触发器对modified
字段执行相同的操作,也可以保留解决方案,因为现在只有一个TIMESTAMP
列被设置为CURRENT_TIMESTAMP
ON UPDATE
。
You can't have multiple columns with CURRENT_TIMESTAMP
in one table. 一个表中不能有CURRENT_TIMESTAMP
多列。 If you really need to you can use a 'before insert' trigger to emulate them'. 如果确实需要,可以使用“插入前”触发器来模拟它们。
您在一个表中不能有多个时间戳,但是,如果您想在表中插入两个日期/日期时间,请使用一个时间戳和另一种datetime类型。
时间戳不是您想的那样:-)我想您想改用datetime
列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.