A synonym is an alias for a schema object of an Oracle tenant. You can create a synonym for a table, view, materialized view, sequence, stored procedure, function, package, object type, user-defined object type, or another synonym. As an alias, a synonym requires no storage other than its definition in the data dictionary. This topic describes the types, basic features, and required privileges of synonyms.
Synonym types
You can create private and public synonyms.
A public synonym can be used by every database user and is owned by a group named PUBLIC. A private synonym is contained in the schema of a specific user and is available only to its owner and users who are granted access to the underlying object.
Public synonyms can be created by database administrators and regular users who are granted the CREATE PUBLIC SYNONYM privilege.
Basic features
Synonyms extend the availability of database objects and allow them to be accessed by different database users with the required privileges. This design simplifies and secures access to database objects.
Create a synonym
You can execute the CREATE SYNONYM statement to create a synonym. The syntax is as follows:
CREATE [ OR REPLACE ] [ PUBLIC ]
SYNONYM [ schema. ]synonym
FOR [ schema. ]object;
Parameters
| Parameter | Description |
|---|---|
| OR REPLACE | Specifies to re-create a synonym if it already exists. |
| PUBLIC | Specifies to create a public synonym, which can be used by all users with the corresponding privileges on the underlying object. A public synonym is used in parsing the reference to an object only when no schema is specified for the object. If PUBLIC is omitted, the synonym is private. A private synonym name must be unique in its schema and can be accessed only by using its schema. |
| [schema. ]synonym | Specifies the schema to which the synonym to be created belongs. If you have specified PUBLIC, you cannot specify a schema for the synonym. The synonym field specifies the name of the synonym. |
| [schema. ]object | Specifies the name of the object that the synonym corresponds to. |
You can check the synonyms that you have created in the USER_SYNONYMS, ALL_SYNONYMS, and DBA_SYNONYMS views.
Examples
Create a private synonym.
obclient> CREATE TABLE t1(c1 INT); obclient> CREATE SYNONYM s1 FOR t1; obclient> INSERT INTO s1 VALUES(1); obclient> SELECT * FROM s1; +------+ | c1 | +------+ | 1 | +------+ 1 row in setCreate a public synonym.
obclient> CREATE PUBLIC SYNONYM syn_pub FOR t1; obclient> SELECT * FROM syn_pub; +------+ | c1 | +------+ | 1 | +------+ 1 row in set
Drop a synonym
You can execute the DROP SYNONYM statement to drop synonyms. The syntax is as follows:
DROP [PUBLIC] SYNONYM [ schema. ]synonym;
Parameters
| Parameter | Description |
|---|---|
| PUBLIC | You can specify PUBLIC to drop a public synonym. If you do not specify PUBLIC, a private synonym is dropped. |
| [ schema. ]synonym | Specifies the schema to which the synonym to be dropped belongs. If you have specified PUBLIC, you cannot specify a schema for the synonym. The synonym field specifies the name of the synonym. |
Examples
Drop a private synonym.
obclient> DROP SYNONYM test.s1; Query OK, 0 rows affectedDrop a public synonym.
obclient> DROP PUBLIC SYNONYM syn_pub; Query OK, 0 rows affected
Considerations
When creating synonyms, make sure that the following privilege requirements are met:
To create a private synonym in your own schema, you must have the
CREATE SYNONYMprivilege.To create a private synonym in a schema of another user, you must have the
CREATE ANY SYNONYMprivilege.To create a public synonym, you must have the
CREATE PUBLIC SYNONYMprivilege.You do not need to have the access privilege on the underlying object of the synonym that you want to create.
/*Log in to the database as the SYS user.*/
obclient> CREATE USER syn_user IDENTIFIED BY syn_user;
obclient> GRANT CREATE ON syn_user.* TO syn_user;
obclient> GRANT SELECT ON syn_user.* TO syn_user;
/*Log in to the database as the syn_user user. The synonym is not created.*/
obclient> CREATE SYNONYM syn_1 FOR t1;
ERROR-00600: internal error code, arguments: -5036, Access denied; you need (at least one of) the CREATE SYNONYM privilege(s) for this operation
/*Log in to the database as the SYS user and grant the syn_user user the CREATE SYNONYM privilege.*/
obclient> GRANT CREATE SYNONYM ON *.* TO syn_user;
/*Log in to the database as the syn_user user.*/
obclient> CREATE SYNONYM syn_1 FOR t1;