oracle create synonyms (synonym)

plsql developer

 

 

 

 

-- Create the synonym 
create or replace synonym AC59
  for GGYW_QY.AC59;

Synonyms concept

 

Oracle synonyms (synonyms) is an alias literally meaning, and function similar view, is a mapping relationship. It can save a lot of database space, the operation of different users is not much difference with a table; it extends the use of the database, enables seamless interaction between different database users; Oracle database management functionality provides synonyms . A synonym is an alias database objects, object access frequently used to simplify and improve the security of access to the object. When using synonyms, Oracle Database will be translated into the name of the corresponding program object. Similar views, synonyms does not take actual storage space, saving only a synonym definition in the data dictionary. Most Oracle database objects in the database, such as tables, views, materialized views, sequences, functions, procedures, packages, synonyms, etc., the database administrator can define the actual situation for their synonyms.

Synonyms Category

 

Oracle synonym There are two types, namely, Oracle Utilities Oracle synonyms and private synonyms. Synonyms are generally ordinary users to create private synonyms, synonyms general public created by the DBA, if you want to create a synonym for the average user, you need CREATE PUBLIC SYNONYM system privilege.

1) Oracle Public Synonyms: owned by a particular user group Public. As the name suggests, all database users can use a common synonyms. Public synonyms are often used to mark some of the more common database objects that often we all need to reference.

2) Oracle private Synonyms: it is synonymous with the corresponding public, he was created by his users all. Of course, this synonym creator, whether authorized users can have access to their own private synonyms control others.

Synonyms role

 

1) Multi-user collaborative development, you can mask the object's name and its owner. If not then synonymous, when the operation of other users table must be in the form of user name .object name, synonymous with the use of the Oracle user name can be hidden away, of course, to note here is: public synonym is simply defined as a database object public aliases, other users can access the database objects through this alias depends on whether the user is authorized.

2) to simplify the user sql statement. The above is actually a simplified reflect a sql, but if they build a long list of names, you can create a synonym for the Oracle sql table to simplify development.

3) providing location transparency for remote objects of a distributed database.

Role 4) Oracle synonyms in the database link

A database link named object is described a database to another database path can be realized by communication between different databases thereof.

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

 

Synonyms Rights Management

 

Synonyms and related rights have CREATE SYNONYM, CREATE ANY SYNONYM, CREATE PUBLIC SYNONYM privilege.

1: Users create in their own private synonym mode, the user must have the CREATE SYNONYM permission, or can not create private synonyms.

As shown below, the user DM lack CREATE SYNONYM permission, will report ORA-01031 error when creating synonyms

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 DM sys account to the account given permission to CREATE SYNONYM of

SQL> GRANT CREATE SYNONYM TO DM;

Grant succeeded.
Then create private synonyms

SQL> the CREATE SYNONYM the TEST the FOR DM.TM_WGG_ATM_GTW_MON;

Synonym the Created
2: If you need to create synonyms in other modes, you must have the CREATE ANY SYNONYM permission.

See the examples below

DM user wants to create private synonym in the SCOTT schema

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

ORA-01031: insufficient privileges
with DM sys account to the account given permission CREATE ANY SYNONYM of

SQL> GRANT CREATE ANY SYNONYM TO DM;

Grant succeeded.

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

The Created synonym
3: you need to create a public synonym CREATE PUBLIC SYNONYM system privilege.

 

Create a synonym

Create a synonym syntax is as follows:

 

Normal usage is as follows:

CREATE [OR REPLACE] [PUBLIC] SYNONYM [. Schema] Synonym names FOR [schema.] Object [@dblink];

- proprietary (private) synonyms

CREATE SYNONYM SYSN_TEST FOR TEST;

- public synonyms

CREATE PUBLIC SYNONYM PUBLIC_TEST FOR TEST;

If you are creating a synonym tables on a remote database, create a Database Link (Database Connectivity) to expand access, and then create a database synonyms use the following statement: create synonym table_name for table_name @ DB_Link;

Public synonyms are irrelevant and the user's schema, but the public do not mean all users have access to it, it must be authorized only after; the private synonym is an object 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
synonyms

SELECT * FROM SYSN_TEST;

Use synonyms ensures that the position or when the database object name is changed, the application code remains constant and only the synonyms need to be changed;

When using a schema is not specified synonym is to first look for the user's own schema, and then find common synonyms

Delete Synonym

DROP [PUBLIC] SYNONYM [schema.] Synonym names [the FORCE];

DROP SYNONYM SYSN_TEST;

DROP PUBLIC SYNONYM PUBLIC_TEST; - a synonym of the original object is deleted, the synonym is not deleted when

Compile synonyms

ALTER SYNONYM T COMPILE; - when the synonym of the original object is re-established, synonyms need to be recompiled

After the original object DDL operations, will become a synonym for state INVALID; synonymous when referring to this again, synonyms automatically compiles the status becomes VALID, without human intervention, of course, the premise is not to change the name of the original object

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
问题锦集
 

1: Public and private synonym with the same name synonymous Can it? If you can, visit to synonyms, or private synonym is a synonym for a total of priorities?

May be the case if there are public and private synonyms synonyms of the same name, is a synonym for the visit, the visit is the object pointed to private synonyms.

2: Why OE user-created public synonyms, HR users can not access it?

Because HR does not have permission to access the next OE schema objects, if the OE schema objects, etc. given to the HR user SELECT permissions, users can access HR.

3: objects, synonyms private and public synonyms whether the presence of three of the same name?

In user kerry, create table TEST

SQL>CREATE TABLE TEST

AS SELECT * FROM USER_OBJECTS WHERE 1= 0;

Create private synonyms 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 private synonym with the same name can not be, otherwise it will report ORA-00955 error

Create public synonym TEST, as shown below, can be the object of public synonyms of the same name

SQL> CREATE PUBLIC SYNONYM TEST FOR REF.REF_WGG_STUDENT;

Synonym created

TEST access, as follows: it is the content table TEST, rather than the content of the common synonyms

 
SQL> the SELECT * the FROM the TEST;

OBJECT_NAME SUBOBJECT_NAME OBJECT_ID DATA_OBJECT_ID OBJECT_TYPE CREATED TIMESTAMP LAST_DDL_TIME the STATUS TEMPORARY the GENERATED the SECONDARY
----------- ---------------- ---- ------ -------------- ------------ --------- --------- ---- ----------- ------- --------- --------- ---------
deleted after the table TEST, this time database access is public synonyms

 

The SQL> the DROP TABLE the TEST the PURGE;

the Table Dropped

the SQL> the SELECT * the FROM the TEST;

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

SQL>
Conclusion: The presence of the same name objects and public synonyms, database objects as the preferred target, the presence of the same name private objects and public objects, database preference private synonyms as the target
- --------------
Disclaimer: This article is CSDN blogger original article "night of little comfort", and follow CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source and link this statement.
Original link: https: //blog.csdn.net/wangwuyilove/article/details/46469965

Guess you like

Origin www.cnblogs.com/moonsoft/p/12100954.html