MySQL triggers to implement data synchronization of two tables _mysql
Source: Internet
Author: User
MySQL implements synchronization of two tables through triggers
Currently, the local test was successful.
Suppose there is a table table2 (ID, val) under table table1 (ID, Val) under the local two databases A and b,a.
Suppose you want the data to be updated in the Table1, and the data in Table2 is synchronized.
Code:
DELIMITER $$
CREATE
/*[definer = {User | Current_User}]*/
TRIGGER ' a '. ' Trigger name ' before UPDATE
On ' a '. ' Table1 '
For each ROW BEGIN
IF new.id!=old.id THEN
UPDATE ' B '. ' Table2 ' SET ' B '. ' Table2 '. Id=new.id WHERE ' b '. ' Table2 '. Val=old.val;
End IF;
end$$
DELIMITER;
Find a lot of code online, in the Phpadmin run is not successful, always syntax error, phpMyAdmin can not be visualized to create triggers, so find another MySQL management tool, SQLyog. This tool is better, Java write, you can create triggers through the interface, and then the code to phpMyAdmin to run, success!
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service