日期:2014-05-16  浏览次数:20605 次

数据库远程同步数据

?

? 需求:源数据库用户表中用户密码和用户名更改时需要将远程数据库中的用户名和密码更新;

?

? 做法:

?

1、创建数据库连接:两种办法:

??

? ?1)、在源数据安装文件tnsnames.ora中添加目的数据库信息如下图所示:

??

? ?ORCLmdsjk =

? (DESCRIPTION =

? ? (ADDRESS_LIST =

? ? ? (ADDRESS = (PROTOCOL = TCP)(HOST = 193.193.193.209)(PORT = 1521))

? ? )

? ? (CONNECT_DATA =

? ? ? (SERVICE_NAME = orcl)//目的数据库的服务名;可通过 sqlplus>show parameter service_name?查看

? ? )

? )

?

? ? 在源数据库总创建数据库链接:

? ? ?create database link linkname?connect to username identified by? userpassword ?using 'ORCLmdsjk?';?

?

? 2:)、create database link linkname

? ? ? ? ? ? connect to username

? ? ? ? ? ?identified by password

? ? ? ? ? ?using '(DESCRIPTION=(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=193.193.193.209) ? (PORT=2521)))(CONNECT_DATA=(SERVICE_NAME=orcl)))';

?

2、在源数据库中建立触发器:

?

? ? ?create or replace trigger xyzsk_user_sys_trigger

?

after insert or update or delete on sec_user

?

for each row

?

declare

? -- local variables here

? flag number;

??

? --如果与209服务器断开连接则不进行数据同步

? network_remaining EXCEPTION;

? PRAGMA EXCEPTION_INIT(network_remaining, -12560);

begin

?

if inserting then

? ?insert into xt_users@xyzskxtorcl(id,username,fullname,password,enabled)

? ?values(:new.ID_,:new.NUMBER_,:new.NAME_,:new.PASSWORD_,:new.enable_);

end if;

?

if updating then

? ?select count(*) into flag from xt_users@xyzskxtorcl where ?username = :old.NUMBER_;

? ?if(flag is null or flag = 0)then

? ? ? insert into xt_users@xyzskxtorcl(id,username,fullname,password,enabled)

? ? ? values(:new.ID_,:new.NUMBER_,:new.NAME_,:new.PASSWORD_,:new.enable_);

? ?else

? ? ? update xt_users@xyzskxtorcl set username = :new.NUMBER_,fullname=:new.NAME_,password = :new.PASSWORD_?

? ? ? where username = :old.NUMBER_;

? ?end if;

??

end if;

?

if deleting then

? ?delete from xt_users@xyzskxtorcl where username = :old.NUMBER_;

end if;

?

exception

? ?when network_remaining then null;

? ?