简体   繁体   中英

Unable to create 2 timestamp columns in 1 MySQL table

My sql query looks like this

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 . The reason for this error seems to be some implementation details in 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. However, you can solve this in your application code or by using a trigger . I used a sample table (called datetimetest ) and added the trigger like this:

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 .

You can't have multiple columns with CURRENT_TIMESTAMP in one table. If you really need to you can use a 'before insert' trigger to emulate them'.

您在一个表中不能有多个时间戳,但是,如果您想在表中插入两个日期/日期时间,请使用一个时间戳和另一种datetime类型。

时间戳不是您想的那样:-)我想您想改用datetime列。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM