MySQL“更新时间戳"列 - 触发器

2022-01-01 00:00:00 mysql triggers

我正在将 DATETIME 类型的列 tsu(时间戳更新)添加到我的多个表中.

I'm adding a column tsu (timestamp update) of type DATETIME to a number of my tables.

我需要编写 BEFORE UPDATE 触发器,将列更新为 CURRENT_TIMESTAMP(),但我无法正确完成.尝试过:

I need to write BEFORE UPDATE triggers that will update the column to CURRENT_TIMESTAMP(), but I can't get it right. Tried:

DELIMITER $$
CREATE
    TRIGGER `cams`.`tsu_update_csi` BEFORE UPDATE
    ON `cams`.`csi`
    FOR EACH ROW BEGIN
        UPDATE csi SET tsu = CURRENT_TIMESTAMP WHERE csi_code = OLD.csi_code;
    END$$
DELIMITER ;

有人能指出我正确的方向吗?MTIA

Can anyone point me in the right direction pls? MTIA

推荐答案

好的,试试这个:

DELIMITER $$ CREATE  
    TRIGGER `cams`.`tsu_update_csi` BEFORE UPDATE  
    ON `cams`.`csi`  
      FOR EACH ROW BEGIN  
        SET NEW.tsu = CURRENT_TIMESTAMP;   
END$$ DELIMITER ;

相关文章