Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL 'Update Timestamp' Column - Trigger

Tags:

mysql

triggers

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

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 ;

Can anyone point me in the right direction pls? MTIA

like image 712
maxhugen Avatar asked Jan 11 '10 23:01

maxhugen


1 Answers

Okay, try this one:

DELIMITER $$ CREATE  
    TRIGGER `cams`.`tsu_update_csi` BEFORE UPDATE  
    ON `cams`.`csi`  
      FOR EACH ROW BEGIN  
        SET NEW.tsu = CURRENT_TIMESTAMP;   
END$$ DELIMITER ;
like image 53
Calvin Allen Avatar answered Nov 15 '22 18:11

Calvin Allen