Using an update trigger to update another table

点点圈 提交于 2019-12-20 05:50:31

问题


The program I am working on has a poorly designed backend and basically there are two different tables needed to hold the exact same information. I am trying to write a trigger that will update table B with the same information that was just updated in table A. Oh and this is using a MYSQL database I am not sure if I am just having a syntax error or if I am missing concepts, any help would be much appreciated... here is what I have as of yet

DELIMITER $$         
DROP TRIGGER IF EXISTS after_update_A;

CREATE TRIGGER `after_update_A` 

    AFTER UPDATE ON `A`  FOR EACH ROW
    BEGIN
        UPDATE TABLE B
        SET  username = NEW.username
           , password = NEW.password
           , email = NEW.email
        WHERE id = NEW.id
    END

    $$
DELIMITER ;

And the errors I get are all some what similar to this... ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= users.username , B.email = users.email D' at line 4


回答1:


You need to close every statement inside the trigger with a ;, and I do mean every.

CREATE TRIGGER `after_update_A` AFTER UPDATE ON `A` FOR EACH ROW
BEGIN
    UPDATE TABLE B
    SET  username = NEW.username
       , password = NEW.password
       , email = NEW.email
    WHERE id = NEW.id;    //<<-----------
END $$



回答2:


I have the feeling that you are compiling .sql as .sh

the trigger is .sql and should be executed using

mysql-batch-commands.html

p.s. What @Johan says is obviously correct as well

p.s.2 Now I see another error: you will need a delimiter between the drop and create statements



来源:https://stackoverflow.com/questions/7404805/using-an-update-trigger-to-update-another-table

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!