create database a
create database b
use a
create table table1(id int, val int)
use b
create table table2(id int, val int)
然后建触发器
use a
DELIMITER //
CREATE TRIGGER tr_Insert_t1
AFTER INSERT ON table1
FOR EACH ROW
BEGIN
-- 检查当前 环境,避免递归.
IF @disable_trigger IS NULL THEN
-- 设置禁用触发器标志.
SET @disable_trigger = 1
-- 插入目标表
INSERT INTO
b.table2
VALUES
(new.id, new.val)
-- 恢复禁用触发器标志.
SET @disable_trigger = NULL
END IF
END
//
DELIMITER
use b
DELIMITER //
CREATE TRIGGER tr_Insert_t2
AFTER INSERT ON table2
FOR EACH ROW
BEGIN
-- 检查当前 环境,避免递归.
IF @disable_trigger IS NULL THEN
-- 设置禁用触发器标志.
SET @disable_trigger = 1
-- 插入目标表
INSERT INTO
a.table1
VALUES
(new.id, new.val)
-- 恢复禁用触发器标志.
SET @disable_trigger = NULL
END IF
END
//
DELIMITER
测试执行效果:
mysql>use b
Database changed
mysql>insert into table2 values(2,2)
Query OK, 1 row affected (0.01 sec)
mysql>use a
Database changed
mysql>insert into table1 values (1, 1)
Query OK, 1 row affected (0.00 sec)
mysql>select * from table1
+------+------+
| id | val |
+------+------+
|2 |2 |
|1 |1 |
+------+------+
2 rows in set (0.00 sec)
mysql>select * from b.table2
+------+------+
| id | val |
+------+------+
|2 |2 |
|1 |1 |
+------+------+
2 rows in set (0.00 sec)
这一需求在不同机器上的,1,通过replication (master-slaves)实现了这两张表的复制功能,
2,mysql的版本是5.1.54,基于记录的复制(Row-Based Replication)。
3,但是在备库调用存储过程时出了问题,这个存储过程中使用了UUID_short()函数,在存储过程这个函数不能产生新值。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)