---
title: "oracle dblink应用"
author: "logKing2340"
date: "2015-04-23T11:01:15Z"
tags: []
categories: ["数据库"]
slug: "oracle-dblink"
draft: false
lastmod: "2015-04-23T11:01:15Z"
id: 2205308
url: "https://logking2340.iteye.com/blog/2205308"
---

一、database link概念：一个数据库到另一个数据的路径对象，允许查询远程表及执行远程程序。

      特别提示：是单向的连接

      建立前提：1.两台机器网络连接正常 2.在远程数据库上面有相应的访问权限

二、dblink 基本语法：

      CTEATE DATABASE LINK LINK\_NAME CONNECT TO \[USER]\[CURRENT\_USER] IDENTIFIED BY          

      \[PASSWORD] USING \[CONNECT\_STRING]

 

 例子：create database link branch connect to dbuser identified by "password" using "192.168.0.5:1521/cheng";

三、连接成功后验证

    SELECT * FROM TABLE\_NAME@branch;--访问远程数据库的表

四、通过merge语句完成表数据同步：将数据源的数据和分支数据同步，简单语法如下：

merge into tb\_sys\_sqlscripe@branch b using tb\_sys\_sqlscripe c on (b.pk=c.pk) 

--从center将表merge到branch,同步的依据是两个表的pk

when matched then  update set b.sqlscripe=c.sqlscripe,b.author=c.author 

--如果pk值是相同则将指定表的值更新到目标表

when not matched then    insert values (c.pk, c.fk, c.createtime, c.lastmodifytime,c.author,c.module,c.deleteflag, c.scripttype);

--如果pk值不一至，则将源表中的数据整条插入到目标表中

commit;              

--记得merge后必须commit,否则更改未能提交