Home > Database > Mysql Tutorial > body text

mysql 触发器实现两个表的数据同步_MySQL

WBOY
Release: 2016-06-01 13:23:15
Original
882 people have browsed it

bitsCN.com mysql通过触发器实现两个表的同步
目前,在本地测试成功。
假设本地的两个数据库a和b,a下有表table1(id, val) b下有表table2(id, val)
假设希望当table1中数据更新,table2中数据同步更新。
代码:
DELIMITER $$
CREATE
/*[DEFINER = { user | CURRENT_USER }]*/
TRIGGER `a`.`触发器名` 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 ;
网上找了很多代码,在phpadmin中运行都不成功,总是语法错误,phpmyadmin也不能可视化建立触发器,所以找了另一个mysql管理工具,SQLyog。这个工具比较好,java写的,可以通过界面创建触发器,然后再把代码拷到phpmyadmin中运行,成功!bitsCN.com

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template