你的位置:首页 > 数据库

[数据库]oracle数据同步方案


数据同步方案:
--用DBLINK 创建与所需同步表的链接
------------------------------------------------------------------------------------------------
--参数
--dblink1 名称
--csys用户名称
--****     用户密码
--HOST 服务器地址
--CONNECT_DATA 实例
------------------------------------------------------------------------------------------------
--创建DBlink
create database link dblink1

connect to csys identified by ****
using '(DESCRIPTION =
(ADDRESS_LIST =
(ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.5.134)(PORT = 1521))
)
(CONNECT_DATA =
(SERVICE_NAME = orcl )
)
)';
-----------------------------------------------------------------------------------------------

--需要在该文件路径下增加如下配置D:\oracle\product\10.2.0\client_1\NETWORK\ADMIN\sqlnet.ora
DESCRIPTION =

  (ADDRESS_LIST =

  (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.2.8)(PORT = 1521))

  )

  (CONNECT_DATA =

  (SERVER = DEDICATED)

  (SERVICE_NAME = orcl)

  )
----------------------------------------------------------------------------------------------
--删除DBlink
DROP DATABASE link dblink1;
--使用 说明 codevalue是表 DBlink1 是创建的DBlink
select *from codevalue@dblink1 ;
--查看数据库中的所有DBlink的连接
select owner,object_name from dba_objects where object_type='DATABASE LINK';
-----------------------------------------------------------------------------------------------
--创建物化视图
--同步的主表
--同步主表存在主键
--创建物化视图日志
create materialized view log on csb1 with primary key;
--查询物化视图日志
select * from mlog$_csb1;


--同步主表不存在主键
--创建物化视图日志
create materialized view log on csb1 with rowid;
-------------------------------------------------------------------------------------------------
--基表创建物化视图(基于物化视图定时刷新--10分钟)
--存在主键
create materialized view mv_csb1 refresh fast start with sysdate next trunc(sysdate,'mi')+10/1440 as select * from csb1@dblink_cs.regress.rdbms.dev.us.oracle.com;

--不存在主键
create materialized view mv_csb1 refresh fast with rowid start with sysdate next trunc(sysdate,'mi')+10/1440 as select * from csb1@dblink_cs.regress.rdbms.dev.us.oracle.com;

--查询物化视图
select * from dba_jobs;
---------------------------------------------------------------------------------------------------------------
--创建物化视图(基于JOB定时刷新)
将上述中的时间参数去掉
-- job定时任务
--创建procedure
create or replace procedure refresh_mv
as
begin
dbms_mview.refresh('mv_csb1');
end refresh_mv;
--在DBMS_JOB系统包中设置相关参数
--What值
begin refresh_mv;end;
--间隔
trunc(sysdate,'mi')+10/1440
--------------------------------------------------------------------------------------------------------------