When oracle performs cross-database access, it can be achieved by creating a dblink. Today, I will briefly introduce how to create a dblink and complete the operations such as inserting, modifying, and deleting through dblink.
First understand the environment: 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
Step 1: Grant permissions
Before creating the database link, we need to judge whether the logged-in user has the authority to create the database link, so we execute the following statement (log in orcl with the wangyong user):
-- Check whether wangyong user has permission to create database link select * from user_sys_privs where privilege like upper('%DATABASE LINK%') AND USERNAME='WANGYONG';
If the query returns a row, it means that you have the permission to create database link, otherwise, you need to use sys to log in orcl to give the WANGYONG user the permission to create
-- Grant permission to create dblink to user wangyong grant create public database link to wangyong;
At this point, execute the above sql statement to check whether you have permission, you will find a return line, indicating that the user WANGYONG has the permission to create a database link
The second step; create a database link
There are two creation methods I have learned: 1) graphically created through pl/sql developer, 2) created through sql statements in sqlplus, and look at them in turn
1) pl/sql developer graphical creation
After filling in the form, click the "Apply" button to create it successfully.
2) sql statement creation
-- Note that if the password starts with a number, enclose it with "" create public database link TESTLINK2 connect to WANGYONG identified by "123456" USING 'ORCL21'
In this way, the simple creation of a simple database is completed
Step 3: Operation
First, we need to create a new table in the ORCL2 library and insert some data, as shown below:
Now, we access this table COMPANY belonging to WANGYONG in the orcl2 library in orcl through the database link
As you can see from the screenshot, the table of user WANGYONG in ORCL2 can be successfully accessed in ORCL
Next, use the same method to insert, modify, and delete operations, look at the screenshots in turn, execute the query statement after each operation, and compare the execution effects:
1) Insert
2) Modify
3) delete
At this point, a simple dblink operation is enough. For the above link string, you can also create a synonym instead, which will save you a little trouble.
-- create synonym
create synonym TESTSYNONYM FOR company@TESTLINK1;
Then in the above query, insert, modify, and delete, you can directly use WYSYNONYM instead of company@TESTLINK1. For example, the query statement can be changed to the following way (insert, modify, delete similar):
-- Query the table COMPANY of the WANGYONG user in ORCL2 SELECT * FROM TESTSYNONYM order by id