Synonyms v16
A synonym is an identifier that you can use to reference another database object in a SQL statement. A synonym is useful in cases where a database object normally requires 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.
EDB Postgres Advanced Server supports synonyms for:
- Tables
- Views
- Materialized views
- Sequences
- Packages
- Procedures
- Functions
- Types
- Objects that you can access through a database link
- Other synonyms
Neither the referenced schema or referenced object must exist when you create the synonym. A synonym can refer to a nonexistent object or schema. A synonym becomes invalid if you drop the referenced object or schema. You must explicitly drop a synonym to remove it.
As with any other schema object, EDB Postgres 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 resolves 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 EDB Postgres Advanced Server executes an SQL command, the privileges of the current user are checked against the synonym’s underlying database object. If the user doesn't have the proper permissions for that object, the SQL command fails.
Creating a synonym
Use the CREATE SYNONYM
command to create a synonym. The syntax is:
Parameters
syn_name
syn_name
is the name of the synonym. A synonym name must be unique in a schema.
schema
schema
specifies the name of the schema that the synonym resides in. If you don't 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 you can access a target object.
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:
This is a shorthand way to write:
This example creates a synonym named personnel
that refers to the enterprisedb.emp
table:
Unless the synonym is schema qualified in the CREATE SYNONYM
command, it's created in the first existing schema in your search path. You can view your search path by executing the following command:
Example
In the example, if a schema named development
doesn't exist, the synonym are created in the schema named accounting
.
Now you can reference the emp
table in the enterprisedb
schema in any SQL statement (DDL or DML) by using the synonym personnel
:
Deleting a synonym
To delete a synonym, use the command DROP SYNONYM
. The syntax is:
Parameters
syn_name
syn_name
is the name of the synonym. A synonym name must be unique in 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 can have two synonyms with the same name in your search path. To disambiguate the name of the synonym that you're dropping, include a schema name. Unless a synonym is schema qualified in the DROP SYNONYM
command, EDB Postgres 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:
Example
The following example drops the synonym personnel
:
- On this page
- Creating a synonym
- Deleting a synonym