Detailed explanation of ORACLE synonyms

synonym concept

Oracle's synonyms (synonyms) literally mean aliases, which are similar to the functions of views and are 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 can realize seamless interaction between different database users; Oracle database provides the function of synonym management . A synonym is an alias for a database object, which is often used to simplify object access and improve the security of object access. When using a synonym, Oracle Database translates it into the name of the corresponding schema object. Similar to views, synonyms do not occupy actual storage space, only the definitions of synonyms are saved in the data dictionary. For most of the database objects in the Oracle database, such as tables, views, materialized views, sequences, functions, stored procedures, packages, synonyms, etc., the database administrator can define synonyms for them according to the actual situation.

Synonym classification

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

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

2) Oracle private synonym: It corresponds to the public synonym, and it 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 his own private synonym through authorization.

Synonym function

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

2) Simplify the sql statement for the user. The above one is actually a manifestation of simplification of 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 in 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 database, through which communication between different databases can be achieved.

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

Synonym rights management

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

1: The user creates a private synonym in his own mode. The user must have the CREATE SYNONYM permission, otherwise he cannot create a private synonym.

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 TEST FOR DM.TM_WGG_ATM_GTW_MON

ORA-01031: insufficient privileges

Use the sys account to grant the CREATE SYNONYM permission to the DM account

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 the CREATE ANY SYNONYM permission.

See the example below

User DM wants to create private synonyms in SCOTT mode

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

ORA-01031: insufficient privileges

Use sys account to grant CREATE ANY SYNONYM permission to DM account

SQL> GRANT CREATE ANY SYNONYM TO DM; 

Grant succeeded.

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

Synonym created

3: To create a public synonym, the CREATE PUBLIC SYNONYM system permission is required.

create synonyms

The syntax for creating a synonym is as follows:
insert image description here
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 a database synonym: create synonym table_name for table_name@DB_Link;

The public synonym has nothing to do with the user's schema, but the public means that not all users can access it, and it must be authorized to do so; the private synonym is the object of the schema

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;

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

When using a synonym that does not specify a schema, it is first searched in the user's own schema, and then in the public synonym

delete synonym

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 will not be deleted

compile synonym

ALTER SYNONYM T COMPILE; --When the original object of the synonym is recreated, 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 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

problem collection

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

Yes, if the public synonym and the private synonym have the same name, when accessing the synonym, the object pointed to by the private synonym is accessed.

2: Why can't HR users access the public synonyms created by OE users?

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

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

Under user kerry, create a 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, a public synonym can have the same name as an object

SQL> CREATE PUBLIC SYNONYM TEST FOR REF.REF_WGG_STUDENT;

Synonym created

When accessing TEST, like this: it's 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 the table TEST is deleted, the database access is the public synonym

SQL> DROP TABLE TEST PURGE;
 
Table dropped
 
SQL> SELECT * FROM TEST;
 
         ID NAME
----------- --------------------------------
          1 12
 

Conclusion: When there is an object with the same name and a public synonym, the database preferentially selects the object as the target; when there are private objects and public objects with the same name, the database preferentially selects the private synonym as the target


Original link: https://www.cnblogs.com/kerrycode/archive/2012/12/19/2824963.html

Guess you like

Origin blog.csdn.net/Ruishine/article/details/129843381