国产一级a片免费看高清,亚洲熟女中文字幕在线视频,黄三级高清在线播放,免费黄色视频在线看

打開APP
userphoto
未登錄

開通VIP,暢享免費(fèi)電子書等14項(xiàng)超值服

開通VIP
利用oracle快照dblink解決數(shù)據(jù)庫(kù)表同步問題 - - JavaEye技術(shù)網(wǎng)站

利用oracle快照dblink解決數(shù)據(jù)庫(kù)表同步問題

本實(shí)例已完全通過測(cè)試,單向,雙向同步都可使用.

--名詞說明:源——被同步的數(shù)據(jù)庫(kù)
            目的——要同步到的數(shù)據(jù)庫(kù)

前6步必須執(zhí)行,第6以后是一些輔助信息.

--1、在目的數(shù)據(jù)庫(kù)上,創(chuàng)建dblink
drop public database link dblink_orc92_182;
Create public DATABASE LINK dblink_orc92_182 CONNECT TO bst114 IDENTIFIED BY password USING 'orc92_192.168.254.111';
--dblink_orc92_182 是dblink_name
--bst114 是 username
--password 是 password
--'orc92_192.168.254.111' 是遠(yuǎn)程數(shù)據(jù)庫(kù)名


--2、在源和目的數(shù)據(jù)庫(kù)上創(chuàng)建要同步的表(最好有主鍵約束,快照才可以快速刷新)
drop table test_user;
create table test_user(id number(10) primary key,name varchar2(12),age number(3));

--3、在目的數(shù)據(jù)庫(kù)上,測(cè)試dblink
select * from test_user@dblink_orc92_182;    //查詢的是源數(shù)據(jù)庫(kù)的表
select * from test_user;

--4、在源數(shù)據(jù)庫(kù)上,創(chuàng)建要同步表的快照日志
Create snapshot log on test_user;

--5、創(chuàng)建快照,在目的數(shù)據(jù)庫(kù)上創(chuàng)建快照
Create snapshot sn_test_user as select * from test_user@dblink_orc92_182;

--6、設(shè)置快照刷新時(shí)間(只能選擇一種刷新方式,推薦使用快速刷新,這樣才可以用觸發(fā)器雙向同步)
快速刷新
Alter snapshot sn_test_user refresh fast Start with sysdate next sysdate with primary key;
--oracle馬上自動(dòng)快速刷新,以后不停的刷新,只能在測(cè)試時(shí)使用.真實(shí)項(xiàng)目要正確權(quán)衡刷新時(shí)間.

完全刷新
Alter snapshot sn_test_user refresh complete Start with sysdate+30/24*60*60 next sysdate+30/24*60*60;
--oracle自動(dòng)在30秒后進(jìn)行第一次完全刷新,以后每隔30秒完全刷新一次

--7、手動(dòng)刷新快照,在沒有自動(dòng)刷新的情況下,可以手動(dòng)刷新快照.
手動(dòng)刷新方式1
begin
dbms_refresh.refresh('sn_test_user');
end;

手動(dòng)刷新方式2
EXEC DBMS_SNAPSHOT.REFRESH('sn_test_user','F'); //第一個(gè)參數(shù)是快照名,第二個(gè)參數(shù) F 是快速刷新 C 是完全刷新.

--8.修改會(huì)話時(shí)間格式
ALTER SESSION SET NLS_DATE_FORMAT = 'YYYY-MM-DD HH24:MI:SS';

--9.查看快照最后一次刷新時(shí)間
SELECT NAME,LAST_REFRESH FROM ALL_SNAPSHOT_REFRESH_TIMES;

--10.查看快照下次執(zhí)行時(shí)間
select last_date,next_date,what from user_jobs order by next_date;

--11.打印調(diào)試信息
dbms_output.put_line('use '||'plsql');

--12.如果你只想單向同步,那么在目的數(shù)據(jù)庫(kù)創(chuàng)建以下觸發(fā)器(當(dāng)源數(shù)據(jù)庫(kù)表改變時(shí),目的數(shù)據(jù)庫(kù)表跟著改變,但目的數(shù)據(jù)庫(kù)表改變時(shí),源數(shù)據(jù)庫(kù)表不改變).
create or replace trigger TRI_test_user_AFR
after insert or update or delete on sn_test_user
for each row
begin
if deleting then
      delete from test_user where id=:old.id;
end if;
if inserting then
      insert into test_user(id,name)
      values(:new.id,:new.name);
end if;
if updating then
     update test_user set name=:new.name where id=:old.id;
end if;
end TRI_test_user_AFR;

--13.如果你想雙向同步,請(qǐng)?jiān)谠磾?shù)據(jù)庫(kù)中執(zhí)行前6步,并在雙方都創(chuàng)建以下觸發(fā)器(當(dāng)源數(shù)據(jù)庫(kù)表改變時(shí),目的數(shù)據(jù)庫(kù)表跟著改變,目的數(shù)據(jù)庫(kù)表改變時(shí),源數(shù)據(jù)庫(kù)表也改變)
CREATE OR REPLACE TRIGGER BST114.TRI_TEST_USER_AFR
AFTER DELETE OR INSERT OR UPDATE
ON BST114.SN_TEST_USER
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
declare
    tmp_id number(10):=-1;
begin

dbms_output.put_line('begin');
if inserting then
      --select id into tmp_id from test_user where id=:new.id;   
      for p in(select id from test_user where id=:new.id)
      loop
        tmp_id:=p.id;
      end loop;
     
      dbms_output.put_line(tmp_id||'===------------');
      if (tmp_id=-1) then
          insert into test_user(id,name,age)
          values(:new.id,:new.name,:new.age);
      end if;
end if;

if updating then
     dbms_output.put_line('updated');
     for p in(select name,age from test_user where id=:old.id)
     loop
         if (p.name!=:new.name) or (p.age!=:new.age) then
              update test_user set name=:new.name,age=:new.age where id=:old.id;
         end if;
     end loop;
end if;

if deleting then
      dbms_output.put_line('deleted');
      delete from test_user where id=:old.id;
end if;
dbms_output.put_line('end');
end TRI_test_user_AFR;
--為防止雙向同步觸發(fā)器死循環(huán),所以要在觸發(fā)器中增加一些判斷,阻止死循環(huán).

--以上同步原理
1.首先創(chuàng)建一個(gè)dblink,可以訪問遠(yuǎn)程數(shù)據(jù)庫(kù)
2.在本地創(chuàng)建一個(gè)快照,映射遠(yuǎn)程數(shù)據(jù)表,當(dāng)遠(yuǎn)程數(shù)據(jù)表有變化時(shí),會(huì)反應(yīng)到快照中.
3.由于快照類似于視圖表,所以在本地為快照創(chuàng)建一個(gè)觸發(fā)器,當(dāng)快照有變化時(shí),會(huì)觸發(fā)相應(yīng)事件.
4.在觸發(fā)器中寫同步數(shù)據(jù)的代碼.

--附:快照刷新時(shí)間參數(shù)說明
一天的秒數(shù)=24小時(shí)*60分鐘*60鈔
所以要想在30秒后刷新,參數(shù)應(yīng)該這樣寫 sysdate+30/(24*60*60)
1分鐘==sysdate+60/(24*60*60)

一天的分鐘數(shù)=24小時(shí)*60分鐘
一分鐘也可以這樣寫 sysdate+1/(24*60)
30分鐘==sysdate+30/(24*60)
60分鐘==sysdate+60/(24*60)

以此類推
1小時(shí)==sysdate+1/24==sysdate+60/(24*60)
1天==sysdate+1
一個(gè)月==sysdate+30

本站僅提供存儲(chǔ)服務(wù),所有內(nèi)容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊舉報(bào)。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
兩個(gè)oracle數(shù)據(jù)庫(kù)間通過數(shù)據(jù)庫(kù)鏈接和觸發(fā)器同步兩個(gè)數(shù)據(jù)庫(kù)間數(shù)據(jù)
Oracle快照原理及實(shí)現(xiàn)總結(jié)
實(shí)戰(zhàn)演練丨SCN太大引發(fā)ORA-600[2252]
58 (轉(zhuǎn))dblink+物化視圖同步兩個(gè)數(shù)據(jù)庫(kù)的表數(shù)據(jù)
通過觸發(fā)器實(shí)現(xiàn)數(shù)據(jù)庫(kù)的即時(shí)同步
ORACLE job使用方法
更多類似文章 >>
生活服務(wù)
分享 收藏 導(dǎo)長(zhǎng)圖 關(guān)注 下載文章
綁定賬號(hào)成功
后續(xù)可登錄賬號(hào)暢享VIP特權(quán)!
如果VIP功能使用有故障,
可點(diǎn)擊這里聯(lián)系客服!

聯(lián)系客服