OGG 測試有主鍵表和無主鍵表的同步情況

2021-09-24 10:25:48 字數 4847 閱讀 1925

環境說明:ogg 版本11.2.1.0.1,源端test,有主鍵,test3表無主鍵,目標端的trail檔案未被刪除。

sql> insert into test3 values(1,'test');

1 row created.

sql> insert into test3 values(1,'test');

1 row created.

sql> insert into test3 values(2,'test');

1 row created.

sql> commit;

commit complete.

sql> select * from test3;

id name

---------- --------------------------------

1 test

1 test

2 test

--目標端檢視資料

sql> select * from test3;

id name

---------- --------------------------------

1 test

1 test

2 test

--檢視源端日誌

2018-06-19 10:24:54 warning ogg-00869 oracle goldengate capture for oracle, ext1.prm: no unique key is defined for table 'test3'. all viable columns will be used to represent the key, but may not guarantee uniqueness. keycols may be used to define the key.

資料雖然被同步過來但是日誌有警告,警告表沒有主鍵。

--源端刪除資料

sql> delete from test3 where id=1;

3 rows deleted.

sql> commit;

commit complete.

sql> select * from test3;

id name

---------- --------------------------------

2 test

--目標端檢視結果

sql> select * from test3;

id name

---------- --------------------------------

2 test

刪除運算元據也被同步過來

--源端更新資料

sql> update test3 set name='test3' where id=1;

3 rows updated.

sql> commit;

commit complete.

sql> select * from test3;

id name

---------- --------------------------------

2 test

1 test3

1 test3

1 test3

--目標端檢視資料

sql> select * from test3;

id name

---------- --------------------------------

2 test

1 test

1 test

1 test

通過測試可以看到源端的update操作,對目標端的操作是隨機性的不能保證資料的一致性。

測試目標端的表被truncate,然後讓replicat從trail檔案號0 ,或者指定檔案號抽取資料來還原目標表資料。

1)停止目標端replicat

ggsci (***ba) 9> stop rep1

sending stop request to replicat rep1 ...

request processed.

2)truncate 目標端測試表

test 表有主鍵,test3沒主鍵

sql> truncate table test;

table truncated.

sql> truncate table test3;

table truncated.

3)初始化replicat 從trail 檔案號0 開始抽取資料

注意從指定的trail檔案號抽取資料,要保證次trail檔案在dirdat目錄下存在,否則程序會中止。

ggsci (***ba) 10> alter replicat rep1,extseqno 0,extrba 0

replicat altered.

ggsci (***ba) 11> start rep1

sending start request to manager ...

replicat rep1 starting

4)檢視目標端表資料

可以看到有主鍵的表資料同步正常,無主鍵的表同步不正常,多出一條資料,原因是因為沒有主鍵,無法做唯一性校驗,資料就會出現不確定性的結果。

sql> select * from test;

id name

---------- --------

1 test

2 test

sql> select * from test3;

id name

---------- --------------------------------

2 test

1 test

1 test

1 test3

1 test3

測試目標端的表資料不做修改,然後讓replicat從trail檔案號0 ,或者指定檔案號抽取資料來還原目標表資料。

1)停止目標端replicat

ggsci (***ba) 9> stop rep1

sending stop request to replicat rep1 ...

request processed.

2)初始化replicat 從trail 檔案號0 開始抽取資料

注意從指定的trail檔案號抽取資料,要保證次trail檔案在dirdat目錄下存在,否則程序會中止。

ggsci (***ba) 10> alter replicat rep1,extseqno 0,extrba 0

replicat altered.

ggsci (***ba) 11> start rep1

sending start request to manager ...

replicat rep1 starting

3)檢視目標端表資料

可以看到有主鍵的表資料同步正常,無主鍵的表同步不正常,出現重複資料,原因是因為沒有主鍵,無法做唯一性校驗,資料就會出現不確定性的結果。

sql> select * from test;

id name

---------- --------

1 test

2 test

sql> select * from test3;

id name

---------- --------------------------------

2 test

2 test

1 test

1 test

1 test

1 test

1 test

1 test

1 test

1 test

10 rows selected.

測試目標端的表被誤刪除,然後讓replicat從trail檔案號0 ,或者指定檔案號抽取資料來還原目標表資料。

1)停止目標端replicat

ggsci (***ba) 9> stop rep1

sending stop request to replicat rep1 ...

request processed.

2)目標端刪除一條資料

test 表有主鍵

sql> delete from test where id=2;

1 row deleted.

sql> commit;

commit complete.

3)初始化replicat 從trail 檔案號0 開始抽取資料

注意從指定的trail檔案號抽取資料,要保證次trail檔案在dirdat目錄下存在,否則程序會中止。

ggsci (***ba) 10> alter replicat rep1,extseqno 0,extrba 0

replicat altered.

ggsci (***ba) 11> start rep1

sending start request to manager ...

replicat rep1 starting

4)檢視目標端表資料

可以看到有主鍵的表資料同步正常,資料被還原,前提要保證trail數量可以保證能恢復和源端資料一致性

sql> select * from test;

id name

---------- --------

1 test

2 test

總結:通過測試我們可以知道,ogg 對有主鍵的表的同步不會出現問題,無主鍵的表無法保證資料一致性,所以建議需要同步的表一定要加上主鍵,防止源端,目標端資料不一致。

java hibernate 無主鍵表 更新

遇到這樣的問題,用hibernate 開發,myeclipse 反向生成對映檔案,現有表eventalert,此表是個沒有主鍵的表,生成的對映檔案,如下 如上,虛擬出類 com.tomsync.vo.eventalertid 作為它的主鍵,在專案中,讀取,沒有問題,當我要對 com.tomsync....

oracle 查詢有主鍵的表的方法

select distinct b.table name from all cons columns a,all constraints b where a.constraint name b.constraint name and constraint type p and a.owner iti...

查詢沒有主鍵的表 沒有索引的表

1 查詢沒有主鍵的表 沒有索引的表 select so.name as 沒有主鍵的表 from sysobjects so where so.xtype u and objectproperty so.id tablehasprimarykey 0 order by name select so.n...