Synonyms v11

A synonym is an identifier that can be used to reference another database object in a SQL statement. A synonym is useful in cases where a database object would normally require full qualification by schema name to be properly referenced in a SQL statement. A synonym defined for that object simplifies the reference to a single, unqualified name.

Advanced Server supports synonyms for:

  • tables
  • views
  • materialized views
  • sequences
  • procedures
  • functions
  • types
  • objects that are accessible through a database link
  • other synonyms

Neither the referenced schema or referenced object must exist at the time that you create the synonym; a synonym may refer to a non-existent object or schema. A synonym will become invalid if you drop the referenced object or schema. You must explicitly drop a synonym to remove it.

As with any other schema object, Advanced Server uses the search path to resolve unqualified synonym names. If you have two synonyms with the same name, an unqualified reference to a synonym will resolve to the first synonym with the given name in the search path. If public is in your search path, you can refer to a synonym in that schema without qualifying that name.

When Advanced Server executes an SQL command, the privileges of the current user are checked against the synonym’s underlying database object; if the user does not have the proper permissions for that object, the SQL command will fail.

Creating a Synonym

Use the CREATE SYNONYM command to create a synonym. The syntax is:

CREATE [OR REPLACE] [PUBLIC] SYNONYM [<schema>.]<syn_name>
       FOR <object_schema>.<object_name>[<@dblink_name>];

Parameters:

syn_name

syn_name is the name of the synonym. A synonym name must be unique within a schema.

schema

schema specifies the name of the schema that the synonym resides in. If you do not specify a schema name, the synonym is created in the first existing schema in your search path.

object_name

object_name specifies the name of the object.

object_schema

object_schema specifies the name of the schema that the object resides in.

dblink_name

dblink_name specifies the name of the database link through which a target object may be accessed.

Include the REPLACE clause to replace an existing synonym definition with a new synonym definition.

Include the PUBLIC clause to create the synonym in the public schema. Compatible with Oracle databases, the CREATE PUBLIC SYNONYM command creates a synonym that resides in the public schema:

CREATE [OR REPLACE] PUBLIC SYNONYM <syn_name> FOR
<object_schema>.<object_name>;

This just a shorthand way to write:

CREATE [OR REPLACE] SYNONYM public.<syn_name> FOR
<object_schema>.<object_name>;

The following example creates a synonym named personnel that refers to the enterprisedb.emp table.

CREATE SYNONYM personnel FOR enterprisedb.emp;

Unless the synonym is schema qualified in the CREATE SYNONYM command, it will be created in the first existing schema in your search path. You can view your search path by executing the following command:

SHOW SEARCH_PATH;

   search_path
-----------------------
 development,accounting
(1 row)

In our example, if a schema named development does not exist, the synonym will be created in the schema named accounting.

Now, the emp table in the enterprisedb schema can be referenced in any SQL statement (DDL or DML), by using the synonym, personnel:

INSERT INTO personnel VALUES (8142,'ANDERSON','CLERK',7902,'17-DEC-06',1300,NULL,20);

SELECT * FROM personnel;

 empno | ename   |  job     | mgr |    hiredate      | sal    | comm  |deptno
-------+---------+----------+-----+------------------+--------+-------+------
 7369  | SMITH   | CLERK    |7902 |17-DEC-80 00:00:00| 800.00 |       |  20
 7499  | ALLEN   | SALESMAN |7698 |20-FEB-81 00:00:00| 1600.00|300.00 |  30
 7521  | WARD    | SALESMAN |7698 |22-FEB-81 00:00:00| 1250.00|500.00 |  30
 7566  | JONES   | MANAGER  |7839 |02-APR-81 00:00:00| 2975.00|       |  20
 7654  | MARTIN  | SALESMAN |7698 |28-SEP-81 00:00:00| 1250.00|1400.00|  30
 7698  | BLAKE   | MANAGER  |7839 |01-MAY-81 00:00:00| 2850.00|       |  30
 7782  | CLARK   | MANAGER  |7839 |09-JUN-81 00:00:00| 2450.00|       |  10
 7788  | SCOTT   | ANALYST  |7566 |19-APR-87 00:00:00| 3000.00|       |  20
 7839  | KING    | PRESIDENT|     |17-NOV-81 00:00:00| 5000.00|       |  10
 7844  | TURNER  | SALESMAN |7698 |08-SEP-81 00:00:00| 1500.00| 0.00  |  30
 7876  | ADAMS   | CLERK    |7788 |23-MAY-87 00:00:00| 1100.00|       |  20
 7900  | JAMES   | CLERK    |7698 |03-DEC-81 00:00:00| 950.00 |       |  30
 7902  | FORD    | ANALYST  |7566 |03-DEC-81 00:00:00| 3000.00|       |  20
 7934  | MILLER  | CLERK    |7782 |23-JAN-82 00:00:00| 1300.00|       |  10
 8142  | ANDERSON| CLERK    |7902 |17-DEC-06 00:00:00| 1300.00|       |  20
(15 rows)

Deleting a Synonym

To delete a synonym, use the command, DROP SYNONYM. The syntax is:

DROP [PUBLIC] SYNONYM [<schema>.] <syn_name>

Parameters:

syn_name

syn_name is the name of the synonym. A synonym name must be unique within a schema.

schema

schema specifies the name of the schema in which the synonym resides.

Like any other object that can be schema-qualified, you may have two synonyms with the same name in your search path. To disambiguate the name of the synonym that you are dropping, include a schema name. Unless a synonym is schema qualified in the DROP SYNONYM command, Advanced Server deletes the first instance of the synonym it finds in your search path.

You can optionally include the PUBLIC clause to drop a synonym that resides in the public schema. Compatible with Oracle databases, the DROP PUBLIC SYNONYM command drops a synonym that resides in the public schema:

DROP PUBLIC SYNONYM <syn_name>;

The following example drops the synonym, personnel:

DROP SYNONYM personnel;