Joe Kahla
Joe Kahla

Reputation: 15

MySQL, Synchronise two tables

I have two data base A & B, in each one I have a table called answer, I want to use the 2nd one as an archive table, I want to create a trigger that copies the last inserted row in A.answer to B.answer.

Here what I did

  CREATE TRIGGER `a` AFTER INSERT ON `A`.`answer` 
     FOR EACH ROW INSERT INTO `B`.`answer`  SELECT * FROM `answer`

This trigger works, but copy all the answers inserted in A.answer to B.answer. The problem is : I dont want to copy all answers, but only the last one. (remark : I dont know the id of the inserted answer, so dont tell me to add a ' WHERE answer.id = xx '). Thanks for your help

Upvotes: 0

Views: 118

Answers (1)

fthiella
fthiella

Reputation: 49089

You could write your trigger this way:

CREATE TRIGGER `a` AFTER INSERT ON `A`.`answer` 
FOR EACH ROW
  INSERT INTO `B`.`answer` VALUES (NEW.col1, NEW.col2, ..., NEW.colN)

where you have to specify all column names.

Please see fiddle here.

Upvotes: 1

Related Questions