Oracle - 04 Synonyms and Database Links

Oracle - 04 Synonyms and Database Links
Oracle's synonyms (synonyms) literally mean the meaning of aliases, similar to the intended function, which is a mapping relationship. This article describes how to create synonym statements, delete synonyms, and view synonym statements.

Summary of synonyms for oracle:
Literally understood is the meaning of aliases, similar to the intended function. It is a mapping relationship.

1. Create a synonym statement:
create public synonym table_name for user.table_name;

the first user_table and the second user_table can be different.
  In addition, if you want to create a synonym for a table on a remote database, you need to create a Database Link (database connection) to expand access, and then use the following statement to create a database synonym: create synonym table_name for table_name@DB_Link;
  Of course, you It may be necessary to authorize the current user (user2) in the user user: grant select/delete/update on user2

2. Delete synonyms:
drop public synonym table_name;

3. View all synonyms:
select * from dba_synonyms

Synonyms have the following benefits: save a lot of money In the database space, there is not much difference between the operations of different users on the same table; the extended use range of the database can realize seamless interaction between different database users; synonyms can be created on different database servers and connected through the network.

The function of synonym management is provided in Oracle database. An Oracle synonym is an alias for a database schema object and is often used to simplify and secure object access.

AD:
The management of users in Oracle is managed by using permissions, that is to say, if we want to use the database, we must have permissions, but if someone else grants us permissions, we can also access the database To operate, but we must type the name of the owner of the table before the name of the authorized table, so this is more troublesome, what should we do in this case? Create an Oracle synonym! This way we can directly use the synonym to use the table.

1. The concept
of synonyms The function of synonym management is provided in the Oracle database. A synonym is an alias for a database schema object and is often used to simplify and secure object access. When a synonym is used, Oracle Database translates it into the name of the corresponding schema object. Similar to the view, the synonym does not occupy the actual storage space, only the definition of the synonym is saved in the data dictionary. Most of the database objects in the Oracle database, such as tables, views, synonyms, sequences, stored procedures, packages, etc., database administrators can define synonyms for them according to the actual situation.

2. Classification of
Oracle synonyms There are two types of Oracle synonyms, namely public Oracle synonyms and private Oracle synonyms.
1) Public Oracle synonym: owned by a special user group Public. As the name implies, public synonyms are available to all users in the database. Common synonyms are often used to denote some common database objects, and these objects often need to be referenced by everyone.
2) Private Oracle synonym: it corresponds to the public synonym, which is owned by the user who created it. Of course, the creator of this synonym can control whether other users have the right to use their own private synonym through authorization.

3. Oracle synonym creation and deletion
The syntax for creating public Oracle synonyms: Create [public] synonym synonym name for [username.]objectName;
Drop [public] synonym synonym name

4. The role of Oracle synonyms
  Synonyms have the following benefits:
       save a lot of database space, and operate on different users There is not much difference in the same table;
       the scope of use of the extended database can realize seamless interaction between different database users;
       synonyms can be created on different database servers and connected through the network

1) In multi-user collaborative development, you can The name of the masked object and its holder. If there is no synonym, when operating the tables of other users, you must use the form of user name.object name. After using the Oracle synonym, the user name can be hidden. Of course, it should be noted here that the public synonym only defines a database object. Public alias, whether other users can access the database object through this alias depends on whether the user has been authorized.
2) Simplify SQL statements for users. The above one is actually a manifestation of simplifying SQL. At the same time, if the name of the table you built is very long, you can create an Oracle synonym for this table to simplify SQL development.
3) Provide location transparency for remote objects of distributed databases.

5. The role of Oracle synonyms in the database chain The
database chain is a named object, indicating the path from one database to another database, through which communication between different databases can be realized.

Create database link database chain name connect to user name identified by password using 'Oracle connection string'; access objects through object name@database chain name. The role of synonyms in the database chain is to provide location transparency.


The link string specified after using is the network service name of the remote database. This service name is stored in the TNSNAMES.ORA file, in which the protocol, host name, port and database name are defined.
 
Note: The account that creates the database link must have the system authority of CREATE DATABASE LINK or CREATE PUBLIC DATABASE LINK, and the account used to log in to the remote database must have the CREATE SESSION authority. Both of these permissions are included in the CONNECT role (CREATE PUBLIC DATABASE LINK permissions are in the DBA).
 
A public database link is available to all users in the database, while a private link is only available to the user who created it and is not authorized. It is not possible for one user to authorize a private database link to another user, a database link is either public or private.
 
When creating a database link, you can also use the default login method, that is, do not specify the user name and password of the remote database:
create public database link zrhs_link using 'zrhs';
In the case of not specifying the user name and password, ORACLE uses the current user name and password Log in to the remote database
 
 
If TNSNAMES.ORA is not configured, the second way to create a db link:
 create database link test connect to scott identified by tiger using
 '(DESCRIPTION =
    (ADDRESS_LIST =
      (ADDRESS = (PROTOCOL = TCP)(HOST = 192.168.1.112)(PORT = 1521))
    )
    (CONNECT_DATA =
      (SERVICE_NAME = orcl)
    )'
 
 
query the created db link:
select * from sys .link$;I
 
always thought that only the private db link can check the password, the original public dblink can also see the password.
Just need to log in with the sys user to query. To 
query the user password of the dblink, log in with sys
select * from SYS.link$;
select * from SYS.link$ l, SYS.user$ u
 
 WHERE l.owner# IN (SELECT kzsrorol
                      FROM x$kzsro) AND l.owner# = u.user#;
 
If it is a private dblink, you can log in with owner , query user_db_links
select * from user_db_links;
 
after the db link is created, access the object
To access an object, pass object name@database chain name. 
 
4. Deletion of synonyms
drop [public] synonym synonym name
To delete a private synonym, omit the public keyword
To delete a public synonym, include the public keyword
For example :
drop synonym emp; -- delete a private synonym named emp
drop public synonym public_emp; -- delete the public synonym named public_emp


Synonym concept
Oracle's synonyms (synonyms) literally mean the meaning of aliases, similar to the function of views, which is a mapping relationship. It can save a lot of database space, and there is not much difference in the operation of the same table for different users; it expands the scope of use of the database and enables seamless interaction between different database users; Oracle database provides the function of synonym management . A synonym is an alias for a database object and is often used to simplify and secure object access. When a synonym is used, Oracle Database translates it into the name of the corresponding schema object. Similar to the view, the synonym does not occupy the actual storage space, only the definition of the synonym is saved in the data dictionary. Most database objects in the Oracle database, such as tables, views, materialized views, sequences, functions, stored procedures, packages, synonyms, etc., database administrators can define synonyms for them according to the actual situation.

Synonym classification
There are two types of Oracle synonyms, namely Oracle public synonyms and Oracle private synonyms. Synonyms created by ordinary users are generally private synonyms, and public synonyms are generally created by DBAs. If ordinary users want to create synonyms, they need the system authority CREATE PUBLIC SYNONYM.

1) Oracle public synonym: owned by a special user group Public. As the name implies, public synonyms are available to all users in the database. Common synonyms are often used to denote some common database objects, and these objects often need to be referenced by everyone.

2) Oracle private synonym: it corresponds to the public synonym, which is owned by the user who created it. Of course, the creator of this synonym can control whether other users have the right to use their own private synonym through authorization.

Suppose oracle has the following two users: admin , visitor
We have created the testtable table under admin and empowered the visitor user
and then we use the visitor to log in: When querying this table, we must write
select * from admin.testtable
if Directly writing select * from testtable oracle will report an error that the table does not exist,
but we can create a [private synonym] in the visitor, let testtable = admin.testtable
, so that you can directly enter select * from testtable to query under the visitor,
but if there are many users It will be very troublesome, because each user needs to add [private synonym] testtable=admin.testtable,
so [public synonym] appears
directly under the admin user to create a public synonym testtable = testtable
After doing this, all users will be You can use
select * from testtable to access directly without prefixing, and without adding [private synonym]

Example in the project:
Oracle user infsa has table a, assign permissions to infss

user Execute
grant all on a to infss under infsa;
create public synonym inf for a;

synonym function
1) In multi-user collaborative development, the object's name and its holder. If there is no synonym, when operating the tables of other users, you must use the form of user name.object name. After using the Oracle synonym, the user name can be hidden. Of course, it should be noted here that the public synonym only defines a database object. Public alias, whether other users can access the database object through this alias depends on whether the user has been authorized.

2) Simplify SQL statements for users. The above one is actually a manifestation of simplifying SQL. At the same time, if the name of the table you built is very long, you can create an Oracle synonym for this table to simplify SQL development.

3) Provide location transparency for remote objects of distributed databases.

4) The role of Oracle synonyms in database links A

database link is a named object that describes the path from one database to another, through which communication between different databases can be achieved.

Create database link database chain name connect to user name identified by password using 'Oracle connection string'; access objects through object name@database chain name. The role of synonyms in the database chain is to provide location transparency.

 

Synonym Permission Management
Permissions related to synonyms include CREATE SYNONYM, CREATE ANY SYNONYM, and CREATE PUBLIC SYNONYM.

1: Users create private synonyms in their own mode. This user must have the CREATE SYNONYM permission, otherwise they cannot create private synonyms.
As shown below, the user DM lacks the CREATE SYNONYM permission, and an ORA-01031 error will be reported when creating a synonym SQL

> CREATE SYNONYM TEST FOR

DM.TM_WGG_ATM_GTW_MON
;
CREATE SYNONYM permissions

SQL> GRANT CREATE SYNONYM TO DM;
Grant succeeded.
Then create a private synonym

SQL> CREATE SYNONYM TEST FOR DM.TM_WGG_ATM_GTW_MON;
Synonym created

2: If you need to create synonyms in other modes, you must have CREATE ANY SYNONYM permissions .

See example below
User DM wants to create private synonym in SCOTT mode
SQL> CREATE SYNONYM SCOTT.EM FOR SOCTT.EMP;
CREATE SYNONYM SCOTT.EM FOR SOCTT.EMP

ORA-01031: insufficient privileges
Grant CREATE ANY SYNONYM permission to DM account with sys account

SQL> GRANT CREATE ANY SYNONYM TO DM;
Grant succeeded.

SQL> CREATE SYNONYM SCOTT.EM FOR SOCTT.EMP;
Synonym created

3: CREATE PUBLIC SYNONYM system permission is required to create public synonyms . Creating

 

synonyms The syntax for creating synonyms is as follows: Common usage is as follows: CREATE [OR REPLACE] [PUBLIC] SYNONYM [schema.] synonym name FOR [schema.] object [ @dblink ]; --proprietary (private) synonym CREATE SYNONYM SYSN_TEST FOR TEST; --Public synonyms CREATE PUBLIC SYNONYM PUBLIC_TEST FOR TEST; If you want to create a synonym for a table on a remote database, you need to create a Database Link (database connection) to expand access, and then use the following statement to create Database synonyms: create synonym table_name for table_name@DB_Link; public synonyms have nothing to do with the user's schema, but public means that not all users can access it and must be authorized to do so; private synonyms are schema objects View synonyms













 


SQL> SELECT * FROM DBA_SYNONYMS WHERE SYNONYM_NAME IN ( 'SYSN_TEST','PUBLIC_TEST');

OWNER SYNONYM_NAME TABLE_OWNER TABLE_NAME DB_LINK

-------------------------- ---- ------------------------------
PUBLIC PUBLIC_TEST ETL TEST

ETL SYSN_TEST ETL TEST

SQL> SELECT * FROM USER_SYNONYMS
use synonyms
SELECT * FROM SYSN_TEST;

Using synonyms can ensure that when the location of the database or the name of the object changes, the code of the application remains stable and unchanged, and only the synonyms need to be changed;

when using a synonym that does not specify a schema, first in the user's own schema DROP [ PUBLIC ] SYNONYM [ schema. ] Synonym name [ FORCE ]; DROP SYNONYM SYSN_TEST;

DROP PUBLIC SYNONYM PUBLIC_TEST;--When the original object of the synonym is deleted, the synonym does not Removed compile synonym






ALTER SYNONYM T COMPILE; --When the original object of the synonym is re-established, the synonym needs to be recompiled. After the DDL operation is performed

on the original object, the status of the synonym will become INVALID; when the synonym is referenced again, the synonym will be automatically compiled and the status It will become VALID without manual intervention, of course, the premise is that the name of the original object is not changed.

SQL> SELECT * FROM T;

     ID NAME
----------- ------------ -

SQL> SELECT * FROM TEST;

     ID NAME
----------- --------------

SQL> ALTER TABLE TEST ADD SEX NUMBER(1);

Table altered

SQL > SELECT OBJECT_NAME, STATUS FROM ALL_OBJECTS WHERE OBJECT_NAME='T';

OBJECT_NAME STATUS
------------------------------ ---- ---
T INVALID
Question Collection
 

1: Can public synonyms and private synonyms have the same name? If so, when accessing synonyms, do public or private synonyms take precedence?

Yes, if there is a situation where a public synonym and a private synonym have the same name, when the synonym is accessed, the object pointed to by the private synonym is accessed.

2: Why cannot HR users access the public synonyms created by OE users?

Because HR does not have permission to access objects in OE mode, if OE mode grants SELECT objects and other permissions to HR users, then HR users can access them.

3: Can objects, private synonyms, and public synonyms have the same name?

Under user kerry, create table TEST
SQL>CREATE TABLE TEST
AS SELECT * FROM USER_OBJECTS WHERE 1= 0;

Create private synonym TEST
SQL>CREATE SYNONYM TEST FOR REF.REF_WGG_STUDENT;

CREATE SYNONYM TEST FOR REF.REF_WGG_STUDENT
ORA-00955: name is already used by an existing object

Note: The object (table) and the private synonym cannot have the same name, otherwise an ORA-00955 error will be reported.
Create a public synonym TEST, as shown below, the public synonym can have the same name as the object
SQL> CREATE PUBLIC SYNONYM TEST FOR REF.REF_WGG_STUDENT;

Synonym created

when accessing TEST as follows: it is the content of the table TEST, not the content of the public synonym
SQL>SELECT * FROM TEST;

OBJECT_NAME SUBOBJECT_NAME OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE CREATED LAST_DDL_TIME TIMESTAMP STATUS TEMPORARY GENERATED SECONDARY
----------- ---------------- ---------- - ------------- ------------ --------- ------------- --- -------- ------- --------- --------- ---------
After deleting the table TEST, the database at this time Accessed is the public synonym

 

SQL> DROP TABLE TEST PURGE;
Table dropped

SQL> SELECT * FROM TEST;

         ID NAME
----------- ---------------- ----------------
          1 12

SQL>
Conclusion: When there are objects with the same name and public synonyms, the database prefers the object as the target, and when there are private objects and public objects with the same name, the database prefers private objects. Synonyms as target


Oracle synonym database link synonymdatabase link 
1. Background
There are two sids: Asid, Bsid, there are two users Auser1, Auser2 under Asid, and one user Buser1 under Bsid. Among them, Auser2 and Buser1 have established dblinks. Now I want to access the three tables table1, table2, table3 information and function function1 on Auser1 by logging in to Buser1.

2. Step
1. Log in to Auser1 to authorize Auser2 (add delete if you need to delete it)
grant insert,select,update on table1 to Auser2;
grant insert,select,update on table2 to Auser2;
grant insert,select,update on table3 to Auser2 ;
grant execute on function1 to Auser2; ②Log in to

Auser2 to create synonyms
create synonym table1 for Auser1.table1;
create synonym table2 for Auser1.table1;
create synonym table3 for Auser1.table1;
create synonym function1 for Auser1.function1; ③Log in

to Buser1 to find db_link Name (for example, the name of the db_link found is mydblink1)
select username,db_link from user_db_links

④Create a synonym
for Buser1 create synonym table1 for Auser2.table1@mydblink1;
create synonym table2 for Auser2.table2@mydblink1;
create synonym table3 for Auser2.table3@mydblink1;
create synonym function1 for Auser2.function1@mydblink1

;
Buser1 can access table1 by selecting * from table1.

3. Points to note It is
also possible to create a synonym of procedure, but commit is not allowed during such execution. If you want to submit, please do commit() in JDBC.


Summary of oracle dblink usage
A database link in oracle is an object that defines a path from one database to another. A database link allows you to query remote tables and execute remote programs. In any distributed environment, dblink is necessary, and note that the database link is a one-way connection. When creating a database link, oracle stores the relevant database link information in the data dictionary. When using database link, oracle then accesses the corresponding remote database through the connection information predefined by the oracle net user to complete the corresponding work. .

1. Before creating a database link, you need to pay attention:
(1) Confirm whether the network connection from the local database to the remote database is normal, and tnsping must be successful.
(2) Confirm that there are corresponding access rights on the remote database.

2. The oracle database link can be divided into the following three categories:
(1) private: a user-level dblink is created. Only the user who created the dblink can use the dblink to access the remote database, and only the user can delete this dblink. dblinks.
(2) public: A database-level dblink is created, and all users with database access rights in the local database or pl/sql programs can use this dblink.
(3) global: The dblink at the network level is created, which is for the oracle network.

3. Permissions required to create a dblink:
If you create a new user, you must grant it the following permissions to create a dblink: create database link, create public database link, and create session.

4. Create a dblink:
-- If public is not specified, the default is private, and the host can be followed by an ip address or a parsed domain name
CREATE PUBLIC DATABASE LINK db_link_test02 CONNECT TO rms IDENTIFIED BY rms
  USING '(DESCRIPTION= 
          (ADDRESS_LIST = 
                (ADDRESS=(PROTOCOL=tcp)(HOST=mom_uat_new)(PORT=1521)))
          (CONNECT_DATA=
              (SERVICE_NAME=ormst1)
              )
         )';
5. Check dblink:

Check dblink, you can go to dba_objects, dba_db_links two tables Check:
SELECT *
  FROM dba_objects do
 WHERE do.object_type = 'DATABASE LINK';

SELECT *
  FROM dba_db_links ddl;
In addition to the above two tables, there are all_db_links, user_db_links.

6. Use dblink:
-- The simplest usage
SELECT *
  FROM table_name@database_link;

-- When you don't want others to know the database link name, you can use synonyms to wrap

CREATE synonym table_name FOR table_name@database_link;
SELECT *
  FROM table_name;

-- You can also create a view to encapsulate
CREATE view table_name_v AS
  SELECT *
    FROM table_name@database_link;

7. Delete dblink:
-- Delete public type dblink
DROP PUBLIC DATABASE LINK dblink_name;

-- Delete private type dblink, only the creator himself Can delete
DROP DATABASE LINK dblink_name;
 

1. Synonyms:
In a distributed database environment, in order to identify a database object, the host name, server name, object owner and object name must be specified. This undoubtedly increases the difficulty of access for visitors. To provide a simple, uniquely identifying name for a database object when different users use it, you can create synonyms for the database object.

For example: Suppose we have a user user in the database database who owns the table table, then when other users in the database database need to access the table table, they need to use the user.table method to access, which requires us to access the table table. You must know who the owner of the table table is. In order to avoid this phenomenon, we can create a synonym to point to the user.table table, then any user can directly use the synonym to access the user's table table.

Objects that synonyms can refer to are tables, views, procedures, functions, packages, and sequences.
There are two types of synonyms: public synonyms and private synonyms. Public synonyms are shared by all users in the database, while private synonyms are available only to themselves.

Use SQL to create synonyms:
 Sql code
create [public] synonym [schema.]synonymName for [schema.]object[@dblink]  
--public means to create a public synonym, which is private by default. @dblink indicates that the created is a synonym for the remote database, and dblink is the name of the remote database link.  
 

Use SQL to delete synonyms:
 Sql code
drop [public] synonym [schema.]synonymName --public  
means to delete a public synonym, if not specified, the default is to delete a private synonym, and an error will be reported when the corresponding private synonym does not exist.  
 


2. Database link:
As a distributed database system, Oracle provides the function of using a remote database. If the table is in the remote database, in order to specify the access path of an object in the remote database, a database link must be created, so that the local user can log in to the remote database through this database link to use its data. There are also two types of database links: public and private, private ones can only be accessed by the creator, and the default is private.
 
Create a database link:
 Sql code
        create [public] database link dababaseLinkName [connect to user identified by password] using connectString. --When  
creating a database link, you must specify the username and password linked to the database and connect to the remote database server name. If no user is specified, Oracle 

-- A link to the remote database will be established using the local username and password. Assumption: I am now logged in to the local Oracle database as admin/admin, then if I  
do not specify user and password when I create the database--link, Oracle will use the local user name and password admin/admin to log in to establish a database link.  
--connectString is similar to this form "127.0.0.1:1521/Orcl", which is the host ip: port number/database service name. The port number is 1521 by default, and the host ip is local by default.  
--So connectString can be written as 'ORCL' when establishing a local database link, that is, the database service name.  
-- Example of creating a database link: create public database link localLink connect to username identified by password using '127.0.0.1:1521/ORCL';  
 
 
Using a remote database link:
After creating a remote database link, we can use it. Suppose we now create a database link orclLink to the remote database ORCL, there is a table t_module in the current user of ORCL, and user scott has a table emp, then we can use the following methods to access the data in t_module:
 
Sql code
select * from t_module@orclLink  
 
but when we need to access scott's emp table, we need to join the schema. The access method is as follows:
 
Sql code
select * from scott.emp@myLink  
 
We can also create remote synonyms for t_module:
 
Sql code
create synonym moduleSyn for t_module@orclLink When  
 
creating synonyms for other user tables such as scott's emp table, you need to add schema, such as:
 
Sql code
create synonym scottEmp for scott.emp@orclLink  
 
After establishing the remote synonym, we can use the following access methods:
 
Sql code
select * from moduleSyn;  
select * from scottEmp;  
 

delete database link:
 
Sql code
drop [public] database link databaseLinkName

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326871658&siteId=291194637