Trigger in multiple mysql database tables using php

If I have table t1 in db1 and t2 in db2. now for any operation on the table t1 db1 I want the same operation to be performed on t2 db2.

Consider the scenario ... if I insert in t1, then one entry must be added on t2. db1 and db2 are both in the same database.

can someone tell me what specific steps i have to follow to execute this scenario ... or how to open both database connections before the trigger starts?

+4
source share
2 answers

You can write INSERT, UPDATE & DELETE triggers in one table to reflect data in another table.

Manually here

 CREATE TRIGGER insert_t1 BEFORE INSERT ON db1.t1 delimiter // FOR EACH ROW BEGIN INSERT INTO db2.t2 VALUES (...); END;// delimiter ; 
0
source

It is also a way to update data from different databases of the same server in accordance with the changes applied to the corresponding database

 USE db1; DROP TRIGGER IF EXISTS t1; DELIMITER $$ CREATE /*[DEFINER = { user | CURRENT_USER }]*/ TRIGGER `t1` AFTER INSERT ON `db1`.`dt1` FOR EACH ROW BEGIN INSERT INTO `db2`.`dt2` (ID,Fname) VALUES (NEW.ID,NEW.Fname); END$$ DELIMITER ; ////Update USE db1; DROP TRIGGER IF EXISTS t4; DELIMITER $$ CREATE /*[DEFINER = { user | CURRENT_USER }]*/ TRIGGER `t4` AFTER UPDATE ON `db1`.`dt1` FOR EACH ROW BEGIN UPDATE `db2`.`dt2` SET `dt2`.`Fname`=NEW.Fname WHERE `dt2`.`ID`=NEW.ID; END$$ DELIMITER ; ////Delete USE db1; DROP TRIGGER IF EXISTS t2; DELIMITER $$ CREATE /*[DEFINER = { user | CURRENT_USER }]*/ TRIGGER `t2` AFTER DELETE ON `db1`.`dt1` FOR EACH ROW BEGIN DELETE FROM `db2`.`dt2` WHERE `dt2`.`ID`=OLD.ID; END$$ DELIMITER ; 
0
source

Source: https://habr.com/ru/post/1432038/


All Articles