触发器实现同一服务器下不同数据库 中 某些表 同步

引用网站:
https://www.cnblogs.com/northeastTycoon/p/9405147.html
https://blog.csdn.net/qq_37126357/article/details/77940818
https://blog.csdn.net/qq_36396104/article/details/80469997
https://blog.csdn.net/LGHunter/article/details/83344442
https://www.jb51.net/article/49207.htm

1.查看触发器
SHOW TRIGGERS
2.删除触发器
DROP TRIGGER [ schema_name.] trigger_name
Schema_name 表示数据库名称,是可选的。如果省略了schema ,将从当前的数据库中舍弃触发程序;trigger_name是要删除的触发器的名称。

Insert 触发器

CREATE TRIGGER insert_test AFTER INSERT ON test FOR EACH ROW 
BEGIN
    INSERT INTO vaethink.test(id,name) VALUES(new.id,new.name);
END

Update 触发器

create trigger upd_test  
after UPDATE on test for each row   
begin  
    update vaethink.test set name = new.name
        where id = new.id;
end;  

Delete 触发器 删除时为old

create trigger del_test  
before DELETE on test for each row
begin  
    DELETE FROM vaethink.test where vaethink.test.id = old.id;
end;

测试时的记录:

SHOW   TRIGGERS

DROP trigger cf_test

INSERT INTO test(id,name) VALUES(2,'hah');

UPDATE test SET `name` = '123456' WHERE id = 1

DELETE FROM test WHERE id = 2

你可能感兴趣的:(触发器实现同一服务器下不同数据库 中 某些表 同步)