Oracle Synonyms Summary

Source: Internet
Author: User
Tags create database oracle database

Synonyms concept

The Oracle synonym (synonyms) is literally the meaning of the alias, and similar to the function of the view, is a mapping relationship. It can save a lot of database space, the operation of different users of the same table does not make much difference; it expands the scope of database usage, enables seamless interaction among different database users, and provides synonym management functionality in Oracle databases. Synonyms are an alias for database objects and are often used to simplify object access and improve the security of object access. When using synonyms, the Oracle database translates it to the name of the corresponding schema object. Like a view, synonyms do not occupy the actual storage space, only the definition of a synonym is saved in the data dictionary. Database administrators can define synonyms for most database objects in an Oracle database, such as tables, views, materialized views, sequences, functions, stored procedures, packages, synonyms, and so on.

Synonym classification

There are two types of Oracle synonyms, namely, Oracle Common synonyms and Oracle private synonyms. Common user created synonyms are generally private synonyms, public synonyms are generally created by the DBA, ordinary users if you want to create synonyms, you need create public synonym this system permissions.

1 Oracle Common synonyms: owned by a special user group public. As the name implies, all users in the database can use common synonyms. Common synonyms are often used to label some of the more common database objects, which are often referred to by everyone.

2 Oracle private synonym: It corresponds to a common synonym, and he is owned by the user who created him. Of course, the creator of this synonym can control whether other users have permission to use their own private synonyms by authorizing them.

Synonym function

1 The cooperative development of multi-user can screen the name of the object and its holder. If there are no synonyms, when you manipulate other users ' tables, Must pass the user name. In the form of an object name, you can hide the user name after using an oracle synonym, but note that the public synonym simply defines a common alias for the database object, and whether other users can access the database object through the alias, and see if the user has been authorized to do so.

2 simplifies SQL statements for users. One of the above is a simple representation of simplifying SQL, and if you build a table with a long name, you can create an Oracle synonym for the table to simplify SQL development.

3 provides location transparency for remote objects in a distributed database.

4 The role of Oracle synonyms in database links

A database link is a named object that describes the path of a database to another database, 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 to the object is through the object name @ database chain name. The role of synonyms in the database chain is to provide location transparency.

Synonym Rights Management

The permissions associated with synonyms are create synonym, create any synonym, create public synonym permissions.

1: The user creates a private synonym in its own mode, and the user must have the Create synonym permission, otherwise the private synonym cannot be created.

As shown below, the user DM is missing the Create synonym permission and the ORA-01031 error is reported when the synonym is created

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
With the SYS account to the DM account to give the 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 the permission for create any synonym.

Look at the example below

User DM wants to create a private synonym in Scott mode

sql> CREATE synonym SCOTT. EM for SOCTT. EMP;
CREATE synonym SCOTT. EM for SOCTT. Emp

Ora-01031:insufficient Privileges
With the SYS account to the DM account to give the Create any synonym permission

sql> GRANT CREATE any synonym to DM;

Grant succeeded.

sql> CREATE synonym SCOTT. EM for SOCTT. EMP;

Synonym created
3: Creating a public synonym requires the Create synonym system permissions.

Create synonyms

The syntax for creating synonyms is as follows:

clip_image001

The common usage is as follows:

CREATE [OR REPLACE] [public] synonym [schema.] Synonym name for [schema.] Object [@dblink];

– Proprietary (private) synonyms

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 extend the access, and then use the following statement to create a thesaurus: the CREATE synonym table_name for table_ Name@db_link;

Public synonyms are not related to the user's schema, but the public meaning is not accessible to all users, and must be authorized before they can proceed; private synonyms are objects 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;

Use synonyms to ensure that when the location of the database or object name changes, the application code remains stable, only need to change synonyms;

When using a synonym that does not specify a schema, first look in the user's own schema, and then look for the common synonyms

Remove synonyms

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 is not deleted

Compiling synonyms

ALTER synonym T COMPILE; – When the original object of the synonym is re-established, the synonym needs to be recompiled

After DDL operations on the original object, the state of the synonym becomes invalid, and when the synonym is referenced again, the synonym is automatically compiled, the state becomes valid, without human intervention, assuming that the original object's name does not change

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 Kam Set

1: Can a common synonym have the same name as a private synonym? If you can, when accessing synonyms, is a common synonym or a private synonym preferred.

Yes, if a common synonym and a private synonym have the same name, the access synonym is the object to which the private synonym is directed.

2: Why the OE user created a common synonym, the HR user can not access it.

Because HR does not have access to the objects in OE mode, HR users can access the OE mode if it gives HR users permissions such as select Objects.

3: Whether objects, private synonyms, public synonyms can exist with the same name of the three cases.

Under User Kerry, create a table test

Sql>create TABLE TEST

As SELECT * from user_objects WHERE 1 = 0;

Create a 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 a existing object

Note: Objects (tables) cannot have the same name as private synonyms, or they will report ORA-00955 errors

Create a public synonym test, as shown below, where the common synonym can have the same name as the object

sql> CREATE public synonym TEST for REF. Ref_wgg_student;

Synonym created

When you access test, as follows: It is the content of the table test, not the contents of the public synonyms

Sql> SELECT * from TEST;

object_name subobject_name object_id data_object_id object_type CREATED last_ddl_time TIMESTAMP STATUS Temporary Generated secondary

When the table test is deleted, the database accesses a common synonym

sql> DROP TABLE TEST PURGE;

Table dropped

Sql> SELECT * from TEST;

     ID NAME
      1 12

Sql>
Conclusion: When there is an object with the same name and a common synonym, the database chooses the object as the target, and when there is a private object and a common object with the same name, the database prioritizes the private synonym as the target

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.