DB-DatabaseLink: Wikipedia

ylbtech-DB-DatabaseLink: Wikipedia

dblink (Database Link) database link name suggests is a link to the database  , like a telephone line, a channel is, when we have to cross a local database, access data in the table to another database , local database, it is necessary to create a remote database dblink, you can access data in a remote database tables as access to local databases as through a local database dblink.

1. Back to top
1、
English name: dblink
Meaning: link to the database
Permissions : PUBLIC DATABASE LINK system privilege
The connection string: connectstring

table of Contents

2、
2. Return to top
1、

Basic Information

format
1. Create dblink syntax:
CREATE [SHARED] [PUBLIC] database link link_name
  [CONNECT TO [user] [current_user] IDENTIFIED BY [password] 
  [AUTHENTICATED BY user IDENTIFIED BY password] 
  [USING 'connect_string']
Description:
1) Permissions : Create a database link account must have system privilege CREATE DATABASE LINK or CREATE PUBLIC DATABASE LINK, the account used to log into remote database must have CREATE SESSION privilege. Both rights are included in the CONNECT role (CREATE PUBLIC DATABASE LINK privileges in the DBA). A public database link for all users in the database are available, while a private link is available only to the user who created it. By one user to another user authorized private database link is not possible, a database link is either public or private.
2) link: When a parameter of the source terminal (parameter) GLOBAL_NAMES = TRUE, link name must be the same as the global database name of the remote database global_name); otherwise, can be named.
3) current_user use this option to create a global type of dblink. If there are multiple databases in a distributed system. If you want you can use the same name in each database to access a database, you must create a database to a db_link in each database, too much trouble. So there is this option if you only created once. All databases can use this db_link to visit. To use this feature, you must have oracle nameserver or ORACLE directory server. And a database of parameters global_names = true. I have not specifically been created, not the environment.
4) connectstring: the connection string, the connection string is defined in the tnsnames.ora remote database can be specified directly in the creation of dblink.
5) username, password: the user name of the remote database password. If not specified, the current user name and password to log in to a remote database, when you create a connected user types dblink, need to verify if the data dictionary, you need user name and password on both sides of the same database.
 

classification

Dblink and type of owner
Types of
Owner description
Private Creating a user owns the dblink dblink Established under a specific schema of the local database databaselink. Only by establishing the
databaselink schema of the session can use this databaselink to access a remote database.
Only Owner can also delete its own privatedatabaselink.
Public
Owner is PUBLIC.
Public databaselink is the database level, all of the local database of users have access to the database or pl / sql procedure can be used to access this databaselink the corresponding remote database.
Global Owner is PUBLIC. Global的database link是网络级的,When an Oracle network uses a directory server, the directory server automatically create and manages global database links (as net service names) for every Oracle Database in the network. Users and PL/SQL subprograms in any database can use a global link to access objects in the corresponding remote database.
  Note: In earlier releases of Oracle Database, a global database link referred to a database link that was registered with an Oracle Names server. The use of an Oracle Names server has been deprecated. In this document, global database links refer to the use of net service names from the directory server.
dblink create the required permissions
Privilege Database Required For
CREATE DATABASE LINK Local Creation of a privase database link.
CREATE PUBLIC DATABASE LINK Local Creation of a public database link.
CREATE SESSION Remote
Creation of any type of
database link.
 

The basic syntax

Create a dblink
There are two general ways to create a dblink , but the user must have permission to create dblink before creating dblink. Want to know about the dblink privilege to sys user to log on to the local database:
select * from user_sys_privs t
  where t.privilege like upper('%link%');
Query result set:
  . 1 the CREATE DATABASE the LINK the SYS NO
  2 the SYS the DROP the PUBLIC DATABASE the LINK NO
  . 3 the PUBLIC the CREATE DATABASE the LINK the SYS NO
  can be seen, there are three dblink permissions in the database:
1.CREATE DATABASE LINK (dblink can only be created by the creator can use, other users can not use),
  2.CREATE the PUBLIC DATABASE LINK (dblink all users of public representation created can be used),
  3.DROP the PUBLIC DATABASE LINK.
  In sys user, the CREATE PUBLIC DATABASE LINK, DROP PUBLIC DATABASELINK permissions granted to your users:
grant CREATE PUBLIC DATABASE LINK,DROP PUBLIC DATABASE LINK to scott;
Then log on to the local database user scott
1, the local service has been configured
  create public database link link_name
connect to username identified by password
using 'connect_string';
Note: link_name name is connected, you can customize;
username is the database login user name;
password is the database user login password;
connect_string database connection string.
Database connection string is the current client database alias name in the TNSNAMES.ORA file defined can NET8 EASY CONFIG or directly modify the TNSNAMES.ORA in the definition.
  2, the establishment of direct links to
  the Create Database Link link_name
  Connect to password username IDENTIFIED by
  a using '(the DESCRIPTION =
  (the ADDRESS_LIST =
  (aDDRESS = (the PROTOCOL = the TCP) (the HOST = XXXX) (PORT = 1521))
  )
  (the CONNECT_DATA =
  (SERVICE_NAME = the SSID)
  )
  )';
  Host = IP address database, service_name = database ssid.
  In fact, two ways to configure the dblink is about the same, personal feeling is the second method is better, so that is not affected by local services.
  Note: If you create a global dblink, you must use the systm or sys user, add public before the database.
dblink inquiry
View all database link, enter the system administrator at the SQL> operator, run the command:
SQL>select owner,object_name from dba_objects where object_type='DATABASE LINK';
  或者
select * from dba_db_links;
delete dblink

  DROP PUBLIC DATABASE LINK link_name;
Use dblink
SELECT ...... FROM table name @ database link name;
Query, delete and insert data and operating the local database is the same, just table names need to be written, "table name @dblink server" only.
Example: Query Beijing emp database table data select * from emp @ BeiJing;
Beijing set up a database where the database connection string BeiJing;
Synonymous with

  Examples from emp @ BeiJing can create synonyms to replace:
  the CREATE SYNONYM synonym name FOR table name;
CREATE SYNONYM synonym name FOR table name @ database link name;
如:create synonym bj_scott_emp for emp@BeiJing;
So you can use the Distributed Link bj_scott_emp to replace with @ symbol @ BeiJing operating emp
  DB LINK is independent of the user to create (USER_DB_LINKS of USERNAME) function, other users can not use this connection without permission can not delete it.
 

Examples

Note that the oracle parameters, there is a parameter called global_names, if the parameter is TRUE, then when using db link, db link name must be and visited the instance name of the database is consistent, otherwise it will report ORA-2085 wrong.
. 1 Machine:
  the IP: 192.168.100.162
  the ORACLE_SID: the HX
  Machine 2:
  the IP: 192.168.100.4
  the ORACLE_SID: PREPAID
  . 1. Increase user Machine database 2
  as the oracle user login Machine 2
  $ Export the ORACLE_SID = PREPAID
  $ sqlplus / AS sysdba
  SQL> the Create the User the Test IDENTIFIED by the Test;
  the User the Created.
  SQL> Grant Connect, Resource to the Test;
  . Grant succeeded.
  2. Modify Machine tnsnames.ora file 1 of
  the following additions:
  prepaid_test =
  (the DESCRIPTION =
  (ADDRESS = (PROTOCOL = TCP) (HOST = 192.168.100.4) (PORT = 1521))
  (CONNECT_DATA =
  (SERVER = DEDICATED)
  (SID = PREPAID )
  )
  )
  3. Create db link
以oracle用户登录Machine 1
  $export ORACLE_SID=HX
  $sqlplus / as sysdba
  SQL>create public database link prepaid_test connect to test identified by test using 'prepaid_test';
  Database link created.
  SQL>show parameter global_names
  NAME TYPE VALUE
  ------------------------------------ ----------- ------------------------------
  global_names boolean TRUE
  SQL>select * from dual@prepaid_test;
  select * from dual@prepaid_test
  *
  ERROR at line 1:
  ORA-02085: database link PREPAID_TEST connects to PREPAID
  SQL>alter system set global_names=false;
  System altered.
  SQL>select * from dual@prepaid_test;

  D
  -
  X
  可以看到,当global_names=false时,db link可以连接远程数据库。
  $oerr ora 2085
  02085, 00000, "database link %s connects to %s"
  // *Cause: a database link connected to a database with a different name.
  // The connection is rejected.
  // *Action: create a database link with the same name as the database it
  // connects to, or set global_names=false.
  //

  4.修改Machine 1的tnsnames.ora文件
  修改prepaid_test为prepaid:
  prepaid =
  (DESCRIPTION =
  (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.100.4)(PORT = 1521))
  (CONNECT_DATA =
  (SERVER = DEDICATED)
  (SID = prepaid)
  )
  )
  5.重新创建db link
  以oracle用户登录Machine 1
  $export ORACLE_SID=HX
  $sqlplus / as sysdba
  SQL>drop public database link prepaid_test;
  Database link dropped.
  SQL>create public database link prepaid connect to test identified by test using 'prepaid';
  Database link created.
  SQL>alter system set global_names=true;
  System altered.
  SQL>show parameter global_names
  NAME TYPE VALUE
  ------------------------------------ ----------- ------------------------------
  global_names boolean TRUE
  SQL>select * from dual@prepaid;

  D
  -
  the X-
  can be seen, when global_names = true, consistent db link instance name and the name of the remote database If you create, you can still visit.
2、
3. Back to top
 
4. Top
 
5. Top
 
 
6. Back to top
 
warn Author: ylbtech
Source: http://ylbtech.cnblogs.com/
This article belongs to the author and blog Park total, welcome to reprint, but without the author's consent declared by this section must be retained, and given the original connection in the apparent position of the article page, otherwise reserves the right to pursue legal responsibilities.

Guess you like

Origin www.cnblogs.com/storebook/p/12121173.html