Using Trigger to update table in another database

2020-02-12 06:56发布

问题:

im using the following trigger to update the user table in another database in mysql 5.0.7 The creation of trigger gives no error but upon updating the user table in the first database the trigger is not working. Any suggestions?

DELIMITER $$         
DROP TRIGGER IF EXISTS after_update_user;

CREATE TRIGGER after_update_user;

AFTER UPDATE ON db_test.user  FOR EACH ROW;
BEGIN
    UPDATE TABLE db_testplus.user;
    SET  name = NEW.name;

    WHERE id = NEW.id;
END

$$
DELIMITER ;

I also used this code without the semicolons but still the same

DELIMITER $$         
DROP TRIGGER IF EXISTS after_update_user

CREATE TRIGGER after_update_user

AFTER UPDATE ON db_test.user  FOR EACH ROW
BEGIN
    UPDATE TABLE db_testplus.user
    SET  name = NEW.name

    WHERE id = NEW.id
END;

$$
DELIMITER ;

Finally the code that worked

delimiter |
DROP TRIGGER IF EXISTS after_update_user|
 CREATE TRIGGER after_update_user AFTER UPDATE ON db_test.user
  FOR EACH ROW BEGIN
     UPDATE db_testplus.user SET name = NEW.name WHERE id = NEW.id;
  END;
|
delimiter ;

回答1:

Could you please check below

AFTER UPDATE ON db_test.user  FOR EACH ROW
BEGIN
    UPDATE TABLE db_testplus.user
    SET  name = NEW.name

    WHERE id = NEW.id
END;


回答2:

Try this;

CREATE TRIGGER after_update_user

AFTER UPDATE ON db_test.user  FOR EACH ROW
  UPDATE TABLE db_testplus.user
  SET  name = NEW.name
  WHERE id = NEW.id;

Omitting begin-end keywords worked for me.



回答3:

This works for me in MySQL 5.1.73:

CREATE TRIGGER `after_update`
AFTER UPDATE ON `test_table`
FOR EACH ROW UPDATE another_db.updated_table
SET  some_name = NEW.some_name
WHERE id = NEW.id