Use DB_LINK to directly access table data in another database

The role of DBLINK: cross-domain access to the database, you can directly access the corresponding database table, you can directly migrate the database table during backup

Build DBLINK statement:

CREATE [SHARED] [PUBLIC] database link link_name
  [CONNECT TO [user] [current_user] IDENTIFIED BY password] 
  [AUTHENTICATED BY user IDENTIFIED BY password] 
  [USING 'connect_string']

public: public means that all users of the created dblink can be used. If the connection user in dblink has the select any table permission, then all users can access any table of dblink, and only the created user can use it without any parameters.

Check the database has DBLINK

select owner,object_name from dba_objects where object_type='DATABASE LINK';

or

select * from dba_db_links;

Check if you have permission to create DBLINK

Take scott user as an example

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

If the query does not return data, use sys or system user login to grant permissions

grant create public database link to scott;

Create DBLINK

Create a link named testlink, the corresponding database is the user name scott, the password is 123456

1. In the database configuration file tnsnames.ora there is a database alias: orcl (user name: scott, password: 123456)

         

           The creation method is as follows, using aliases directly in using:

create public database link testlink connect to scott identified by '123456' USING 'ORCL'

2. There is no corresponding database alias in the database configuration file tnsnames.ora: orcl (user name: scott, password: 123456)

           The creation method is as follows, fill in the corresponding database IP address in using

create database link 'linkname' connect to scott identified by '123456'
  using '(DESCRIPTION =
                (ADDRESS_LIST =
                  (ADDRESS = (PROTOCOL = TCP)(HOST = localhost)(PORT = 1521))
                )
                (CONNECT_DATA =
                  (SERVICE_NAME = orcl)
                )
              )';

Delete the corresponding DBLINK

drop databse link testlink
drop public databse link testlink

Use the corresponding DBLINK

To access the student table of another database in the local database, the statement is as follows, other testlink is the name of DBLINK, and other DML operations are the same, just use student@testlink instead of the corresponding table name.

select * from student@testlink

1. Insert

insert into student@testlink values('id','name');

2. Modify

update student@testlink set name='name' where id='id';

3. Delete

delete from student@testlink where id='id';

Create synonyms for the corresponding DBLINK, easy to use and can not let people know the name of DBLINK

create synonym student for student@testlink;

You can directly use student instead of student@tesklink in the execution of DQL and DML statements

select * from student;
insert into student values('id','name');
delete from student where id='id';
create synonym student for student;

 

Guess you like

Origin blog.csdn.net/springlan/article/details/106293124