Keeping tables synchronized in Oracle

て烟熏妆下的殇ゞ 提交于 2019-12-01 05:11:20

I'd create A and B as views over a single normalized (or denormalized) table, and created an INSTEAD OF trigger over these views to handle DML operations.

If the query plans matter, it's better to keep two copies of tables: A_underlying and B_underlying and create the views just like this:

CREATE VIEW A
AS
SELECT  *
FROM    A_underlying

CREATE VIEW B
AS
SELECT  *
FROM    B_underlying

The predicates will be pushed into the views, and the query plans for actual tables and views will be the same.

In INSTEAD OF triggers over both view, you should put the data into both underlying tables.

Do you really mean DDL, not DML?

With DML you can have a look at Oracles Multi Master Replication to keep the tables in synch or you could also have a look at the tool SymmetricDS for this purpose.

With DDL the only solution I'm aware of is again the Oracle advanced replication.

Put the below three statements in a stored procedure, then run it as a scheduled job as often as you like:

--Assume that "A" is a master, and "B" needs to be synched

--If no match in "A", delete from "B"
DELETE FROM B
WHERE NOT EXISTS(
                SELECT *
                FROM A
                WHERE A.PRIMARY_KEY = B.PRIMARY_KEY
                );

--If there is a match, but they are different, then update "B"
update 
  (
  select
    a.field1 as new_value1
   ,b.field1 as old_value1
   ,a.field2 as new_value2
   ,b.field2 as old_value2
   ,....
   ,a.fieldN as new_valueN
   ,b.fieldN as old_valueN
  from
    a
   ,b
 where a.primary_key = b.primary_key
 )
set
  old_value1 = new_value1
 ,old_value2 = new_value2
 ,....
 ,old_valueN = new_valueN;


--if the record is new to "A", then insert it into "B"
INSERT INTO B
SELECT *
FROM A 
WHERE NOT EXISTS(
                SELECT *
                FROM B 
                WHERE B.PRIMARY_KEY = A.PRIMARY_KEY
                );

Oracle 10g and above have implemented Change Notification as an asynchronous process. It's automatic and the package is included with the server install of Oracle 10g and above.

You can see here for some info.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!