Mysql trigger for data synchronization between two tables
Source: Internet
Author: User
Mysql uses a trigger to synchronize two tables
Currently, the local test is successful.
Assume that two local databases a and B have table1 (id, val) and table2 (id, val) in table)
Assume that you want to synchronize data updates in table 1 and table 2.
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;
I found a lot of code on the Internet, and it failed to run in phpadmin. It was always a syntax error and phpmyadmin could not create a trigger visually. So I found another mysql management tool, SQLyog. This tool is good. For java writing, you can create a trigger on the interface, and then copy the code to phpmyadmin to run it successfully!
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.