ORACLE 資料同步

2021-08-31 08:00:44 字數 3435 閱讀 4810

這是我之前做過的資料庫同步的問題,最近老是看到有人在csdn裡問資料庫同步的問題,所以我就想把我的實現方案共享一下。多多交流。用到的表是我臨時建立的表。有不妥之處請見諒,畢竟是免費的東西。

在本地建立兩張表(t1,t2),這兩張表和遠端的表結構一樣,通過觸發器實現資料的同步,然後對本地的兩張表進行物化,再在物化檢視上建立觸發器,實時的修改t表(並沒有考慮大字段的情況)。

該方案是測試成功的,源資料庫是oracle10.2.0.3.0,目標資料庫是oracle10.2.0.3.0,並且做到了資料的實時更新。

1、在源資料庫建立database link,確保兩台伺服器可以連通。

首先在oracle 的e:\oracle\product\10.2.0\db_1\network\admin\tnsnames.ora配置檔案裡新增遠端oracle伺服器的例項,具體如下:

[quote]

orcl103 =

(description =

(address = (protocol = tcp)(host = 192.168.1.103)(port = 1521))

(connect_data =

(server = dedicated)

(service_name = orcl)

))[/quote]

然後執行建立database link的語句:

sql code

[quote]create database link orcl103.regress.rdbms.dev.us.oracle.com

connect to ccpph1

using 'orcl103';[/quote]

然後測試是否已連通,在命令視窗輸入:

select count(*) from t1@orcl103;

2、在本地建表(t1, t2),要和源資料庫中的表結構一致。

sql code[quote]

--t1表

create table t1 as select * from t1@orcl103;

alter table t1 add constraint pk_id primary key (id);

--t2表

create table t2 as select * from t2@orcl103;

alter table t2 add constraint pk_id primary key (id);[/quote]

3、在源資料庫上分別建立觸發器

sql code [quote]--t1表觸發器

create or replace trigger tri_t1

after insert or update or delete on t1

for each row

begin

if deleting then

dbms_output.put_line('刪除');

delete from t1@orcl101 where id=:old.id;

end if;

if inserting then

dbms_output.put_line('插入');

insert into t1@orcl101(id,name)

values(:new.id,:new.name);

end if;

if updating then

dbms_output.put_line('修改');

update t1@orcl101 set id=:new.id,name=:new.name where id=:old.id;

end if;

end tri_t1;

--t2表觸發器

create or replace trigger tri_t2

after insert or update or delete on t2

for each row

begin

if deleting then

dbms_output.put_line('刪除');

delete from t2@orcl101 where id=:old.id;

end if;

if inserting then

dbms_output.put_line('插入');

insert into t2@orcl101(id,name)

values(:new.id,:new.name);

end if;

if updating then

dbms_output.put_line('修改');

update t2@orcl101 set id=:new.id,name=:new.name where id=:old.id;

end if;

end tri_t2;[/quote]

4、在目標資料庫建立物化檢視日誌

--建立物化檢視日誌

sql code

[quote]create materialized view log on t1 with rowid;

create materialized view log on t2 with rowid;[/quote]

5、在目標資料庫建立物化檢視

sql code

[quote]create materialized view mv_t

refresh fast on commit

asselect t1.rowid as t1rowid,t2.rowid as t2rowid,t1.id,t1.name,t2.id as t2id,t2.name as t2name from t1 t1,t2 t2 where t1.id = t2.id;[/quote]

6、在目標資料庫建立基於物化檢視的觸發器

sql code

[quote]create or replace trigger tri_t

after insert or update or delete on mv_t

for each row

begin

if deleting then

dbms_output.put_line('刪除');

delete from t where id=:old.id;

end if;

if inserting then

dbms_output.put_line('插入');

insert into t(id,name)

values(:new.id,:new.name);

end if;

if updating then

dbms_output.put_line('修改');

update t set id=:new.id,name=:new.name where id=:old.id;

end if;

end tri_t;

[/quote]

oracle資料同步

首先建立乙個 dblink database link sql create database link kings dblink 的名字,同步的時候要用到這個名字,可以隨便取 connect tokings 連線到那個資料庫的使用者名稱 identified by kings123 連線那個資料庫...

oracle資料同步例項

建立dblink create database link db117 例項 connect to test u identified by test u using description address list address protocol tcp host 192.168.1.117 p...

Oracle 自動同步資料指令碼

前段時間在處理乙個生產異常的時候發現,我們的測試資料庫和 資料庫已經很久都沒有同步生產上的資料了。我們開發人員在處理異常的時候往往要模擬一條資料來進行除錯,若遇到需要大量接近生產的基礎資料進行除錯的時候就比較痛苦了。而目前遇到這種情況則需要實施人員到生產資料庫備份資料,通過oracle匯出將資料導成...