简体   繁体   中英

MySQL Trigger to INSERT to table after UPDATE on another table?

I am trying to create a TRIGGER that will update a table after updating another table. I need it so that if a number in the number column is updated, then this needs to be recorded in another table. This is what I have so far..

DELIMITER $$
CREATE TRIGGER record_update_to_user_number
    AFTER UPDATE ON user FOR EACH ROW
    IF (NEW.number != OLD.number) THEN//if new updated number is not equal t                                    to  number currently in the table then..

        DECLARE insertednumber TINYINT;  //variable to store updated num

        SET insertednumber = NEW.number;

        INSERT INTO user_changes (username, currentdate, numberchange)
        VALUES (current_user(), CURDATE(), insertednumber);
    END IF;
    DELIMITER ;

Test update to table:

UPDATE user SET number='69' WHERE number='68'; //test update to table

As you can see, I'm trying to store the newly inserted user number into a variable which will then be used to populate the insertednumber column of the user_changes table. Any help would be greatly appreciated, thanks!

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