Oracle 使用物化視圖實現表資料同步

來源:互聯網
上載者:User

標籤:oracle

1.建立原表和物化視圖日誌

SQL> create table t1(id int,name varchar2(30));Table created.SQL> alter table t1 add constraint pk_t1 primary key(id) using index;Table altered.SQL> create materialized view log on t1 with primary key;Materialized view log created.
2.建立目標表和物化視圖這裡我建立是refresh fast on commit類型的物化視圖
SQL> create table t2 as select * from t1 where 1=2;Table created.SQL> create materialized view t2 on prebuilt table refresh fast on commit as select * from t1;Materialized view created.
3.簡單測試 在t1插入一條資料,一提交t2即存在資料
SQL> insert into t1 values(1,‘A‘);1 row created.SQL> commit;Commit complete.SQL> select * from t2;ID NAME---------- ------------------------------ 1 A
4.ddl測試 通過測試我們發現物化視圖不支援ddl語句 我們給t1添加一個列和rename一個列
SQL> alter table t1 add ddl_test int;Table altered.SQL> alter table t1 rename column name to names;Table altered.SQL> select * from t2;ID NAME---------- ------------------------------ 1 A 2 4SQL> insert into t1 values(3,‘x‘,1234);1 row created.SQL> commit;Commit complete.SQL> select * from t1;ID NAMES    DDL_TEST---------- ------------------------------ ---------- 1 A 2 4 3 x1234SQL> select * from t2;ID NAME---------- ------------------------------ 1 A 2 4

探索資料沒有過來,我們看一下物化視圖的定義和狀態

SQL> select dbms_metadata.get_ddl(‘MATERIALIZED_VIEW‘,‘T2‘) from dual;DBMS_METADATA.GET_DDL(‘MATERIALIZED_VIEW‘,‘T2‘)--------------------------------------------------------------------------------  CREATE MATERIALIZED VIEW "SCOTT"."T2" ("ID", "NAME")  ON PREBUILT TABLE WITHSQL> select staleness from user_mviews;STALENESS-------------------COMPILATION_ERROR



Oracle 使用物化視圖實現表資料同步

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.