Big data - play with data - oracle to create dblink and application

1. Application scenarios for creating DBLINK

When oracle performs cross-library access, it can be realized by creating a dblink.

2. Create DBLINK application scenarios

Configure two database aliases in tnsnames.ora: orcl (username: wangyong password: 1988), orcl2 (username: wangyong password: 123456), create a database link in orcl to access orcl2
insert image description here

3. Authorization

Before creating a database link, we need to judge whether the logged-in user has the authority to create a database link, so we execute the following statement (login to orcl with wangyong user):

– Check whether the wangyong user has the permission to create a database link

select * from user_sys_privs where privilege like upper('%DATABASE LINK%') AND USERNAME='WANGYONG';

If the query returns rows, it means that you have the permission to create database link, otherwise, you need to use sys to log in orcl to grant creation permission to user WANGYONG

– Grant the wangyong user the permission to create dblinks

grant create public database link to wangyong;

At this time, execute the above SQL statement to check whether it has permission, and you will find a return line, indicating that the user WANGYONG already has the permission to create a database link

4. Create DBLINK

create public database link TESTLINK2 connect to WANGYONG identified by "123456" USING 'ORCL21'

or:

create public database link TESTLINK2 
connect to  WANGYONG 
identified by "123456"  
using '
  (DESCRIPTION =
    (ADDRESS = (PROTOCOL = TCP)(HOST = 127.0.0.1)(PORT = 1521))
    (CONNECT_DATA =
      (SERVER = DEDICATED)
      (SERVICE_NAME = orcl2)
    )
  )
';

5. Delete dblink

drop public database link TESTLINK2 ;

6. Explanation

The database link is followed by the name of DBLINK, which can be named whatever is meaningful;

connect to is followed by the username of the target database, without quotation marks.

identified by is followed by the password of the target database, enclosed in double quotes.

HOST = followed by the IP of the target database.

PORT = followed by the port number of the target database.

SERVICE_NAME = followed by the name of the target database.

ps: Pay attention to the format of the surface statement after using. Pay attention to the spaces in it, a single space key, and wrong spaces will invalidate the created DBLINK.

7. Use DBLINK to add, delete, modify and check:

–Back up the data in the target database table in the current database:

CREATE TABLE USERINFO AS SELECT * FROM USERINFO@TESTLINK2 

– Query the USERINFO table data in the target database:

SELECT * FROM USERINFO@TESTLINK2 

Guess you like

Origin blog.csdn.net/s_unbo/article/details/130169591
Recommended