13.3 Synonyms
Key Takeaways
A synonym is an alias or alternative name for a schema object (table, view, sequence, procedure, package, or synonym) stored in the data dictionary that provides location transparency.
Private synonyms exist within a specific user's schema and are accessible only to the owner (or users with schema qualification and grants), whereas Public synonyms belong to the PUBLIC schema and are accessible database-wide.
Oracle resolves unqualified object names through a strict 3-tier hierarchy: Local Schema Object -> Private Synonym -> Public Synonym.
Synonyms provide abstraction and location transparency but do NOT confer object privileges; a user accessing an object via a synonym must still possess direct or role-based grants.
Dropping a synonym does not affect the underlying base object; conversely, dropping the underlying object leaves behind a dangling synonym that raises ORA-00942 at runtime.
13.3 Synonyms
In enterprise database environments, schema architectures often span multiple user schemas, remote database servers, and evolving object names. Referencing database objects using full schema-qualified names (such as hr.employees or finance.monthly_ledger@ny_sales_link) introduces tight coupling, security exposure of schema topologies, and extensive code refactoring when objects move.
A Synonym is an alias or alternative pointer for any database schema object. Synonyms store no physical data of their own; they store a metadata definition in the Oracle data dictionary (USER_SYNONYMS, ALL_SYNONYMS, DBA_SYNONYMS) that transparently redirects SQL statements to the underlying target object.
Supported Objects and Purposes of Synonyms
Synonyms can be created for virtually any named database object:
- Tables and Object Tables
- Views and Materialized Views
- Sequences
- Stored PL/SQL Procedures, Functions, and Packages
- Java Stored Assets and User-Defined Types
- Other Synonyms (Synonym Chaining)
Core Architectural Benefits
- Location Transparency: Insulates client applications from schema renames, table migrations, and remote database link (
@dblink) locations. If a table moves from schemaHRtoGLOBAL_HR, updating the synonym redirects all applications without modifying a single line of application SQL. - Simplifying SQL Syntax: Eliminates the need for users to prefix object names with schema qualifiers (
hr.employeesbecomes simplyemp). - Security Abstraction: Obscures the actual schema owner and object names from end users and external client tools.
Private Synonyms vs. Public Synonyms
Oracle Database provides two distinct categories of synonyms: Private Synonyms and Public Synonyms.
+-------------------------------------------------------------------------+
| PRIVATE VS. PUBLIC SYNONYMS |
+-------------------------------------------------------------------------+
| |
| USER SCHEMA (e.g. 'SCOTT'): DATABASE-WIDE ('PUBLIC' SCHEMA): |
| CREATE SYNONYM emp FOR hr.emp; CREATE PUBLIC SYNONYM dept FOR hr.dept;|
| - Resides in SCOTT schema - Belongs to PUBLIC schema |
| - Owned by SCOTT - Owned by PUBLIC |
| - Visible to SCOTT only - Visible to ALL database users |
| - Requires CREATE SYNONYM - Requires CREATE PUBLIC SYNONYM |
| |
+-------------------------------------------------------------------------+
Exhaustive Comparison Matrix
| Architectural Feature | Private Synonym | Public Synonym |
|---|---|---|
| Owner / Schema | Owned by the creating user schema | Owned by the special PUBLIC user group/schema |
| Scope / Visibility | Visible to the owner (or other users via schema.synonym_name) | Globally accessible to all database users |
| Creation Privilege | CREATE SYNONYM (in own schema) or CREATE ANY SYNONYM | CREATE PUBLIC SYNONYM (administrative privilege) |
| Drop Privilege | DROP SYNONYM (in own schema) or DROP ANY SYNONYM | DROP PUBLIC SYNONYM (administrative privilege) |
| Namespace | Shares the schema namespace with tables, views, sequences, etc. | Resides in a separate, global PUBLIC namespace |
| Primary Use Case | Individual developer convenience and local object masking | Enterprise-wide shared utility tables, standard views, packages |
Synonym Creation Syntax & Clauses
Creating a synonym utilizes the CREATE SYNONYM DDL statement:
CREATE [OR REPLACE] [PUBLIC] SYNONYM synonym_name
FOR [schema.]object_name[@dblink_name];
Syntax Elements Breakdown
OR REPLACE:Re-creates the synonym if it already exists, allowing the target object definition to change without dropping the synonym or invalidating dependent object privileges.PUBLIC:Creates a public synonym accessible to all users. OmittingPUBLICcreates a private synonym in the current schema.synonym_name:The identifier assigned to the synonym.FOR [schema.]object_name:The target object being aliased. Ifschemais omitted, Oracle assumes the object resides in the current user's schema.@dblink_name:Specifies a database link to an object residing on a remote Oracle database server.
Creation Examples
-- 1. Create a private synonym in the current schema pointing to HR.EMPLOYEES
CREATE SYNONYM emp_table FOR hr.employees;
-- 2. Create or replace a public synonym pointing to HR.DEPARTMENTS
CREATE OR REPLACE PUBLIC SYNONYM departments FOR hr.departments;
-- 3. Create a private synonym pointing to a remote table via database link
CREATE SYNONYM remote_sales FOR sales.orders@ny_headquarters_db;
Oracle 3-Tier Object Name Resolution Hierarchy
When an unqualified identifier (e.g., SELECT * FROM employees;) appears in a SQL statement, the Oracle SQL parser resolves the object name using a strict 3-Tier Name Resolution Hierarchy.
+-------------------------------------------------------------------------+
| ORACLE OBJECT NAME RESOLUTION ENGINE |
+-------------------------------------------------------------------------+
| |
| User executes: SELECT * FROM INVOICES; |
| |
| +-------------------+ |
| | TIER 1: LOCAL | |
| | SCHEMA OBJECT | |
| +---------+---------+ |
| | |
| +----------------+----------------+ |
| | (Found) | (Not Found) |
| v v |
| [ Access Local Table, +-------------------+ |
| View, or Private | TIER 2: PRIVATE | |
| Synonym 'INVOICES' ] | SYNONYM (LOCAL) | |
| +---------+---------+ |
| | |
| +----------------+----------------+ |
| | (Found) | |
| v v |
| [ Resolve Private +--------+-|
| Synonym Target ] | TIER 3: |
| | PUBLIC |
| | SYNONYM |
| +----+-----+|
| | |
| +----------------+ |
| | (Found) | (No)|
| v v |
| [ Resolve Public [ ORA-00942: |
| Synonym Target ] Table or |
| View does |
| not exist] |
| |
+-------------------------------------------------------------------------+
Precedence and Shadowing / Masking Rules
- Local Objects Always Prevail: If user
SCOTTowns a local table namedDEPARTMENTS, any query executed bySCOTTreferencingDEPARTMENTSwill always accessSCOTT.DEPARTMENTS, completely ignoring any public synonym namedDEPARTMENTS. - Private Synonyms Shadow Public Synonyms: If user
SCOTTcreates a private synonym namedDEPARTMENTSpointing toHR.DEPARTMENTS,SCOTT's query will accessHR.DEPARTMENTS, masking the public synonym. - Public Synonyms Serve as Fallbacks: A public synonym is resolved only if no local object and no private synonym with that name exists in the querying user's schema.
Privilege Decoupling: Synonyms Do NOT Confer Security Grants
A critical conceptual trap on the 1Z0-071 examination is the relationship between synonyms and object privileges.
[!CRITICAL] Synonyms are Pointers, Not Permissions: Creating a synonym (private or public) does NOT grant any privileges on the underlying object. A synonym is purely an alias.
Privilege Verification Scenario
- User
ADMINcreates a public synonym:CREATE PUBLIC SYNONYM emp FOR hr.employees; - User
SCOTTconnects and runs:SELECT * FROM emp; - Result: If
SCOTThas not been grantedSELECTonhr.employees(either directly or via a role), the query fails with:ORA-00942: table or view does not exist(orORA-01031: insufficient privileges). - For
SCOTTto queryemp,HRor a DBA must execute:GRANT SELECT ON hr.employees TO scott;(orTO PUBLIC).
Dangling Synonyms and Object Lifecycle Analysis
Oracle does not enforce referential integrity checks between a synonym and its underlying target object at creation time.
1. Creating Synonyms for Non-Existent Objects
You can successfully create a synonym for an object that does not currently exist. Oracle compiles the synonym metadata into the data dictionary without error.
-- Table future_inventory does not exist yet!
CREATE SYNONYM inv_syn FOR future_inventory;
-- Synonym created.
-- Attempting to query the synonym fails at runtime:
SELECT * FROM inv_syn;
-- ORA-00942: table or view does not exist
2. The Dangling Synonym Phenomenon
If a base table is dropped, any synonyms pointing to that table are not automatically dropped or updated. They become dangling synonyms.
+-------------------------------------------------------------------------+
| DANGLING SYNONYM LIFECYCLE |
+-------------------------------------------------------------------------+
| |
| 1. CREATE SYNONYM emp_syn FOR employees; |
| - Synonym points to valid EMPLOYEES table |
| - Query SELECT * FROM emp_syn succeeds |
| |
| 2. DROP TABLE employees; |
| - EMPLOYEES table is dropped from database |
| - EMP_SYN remains in USER_SYNONYMS (Dangling Synonym) |
| |
| 3. SELECT * FROM emp_syn; |
| - Fails with ORA-00942: table or view does not exist |
| |
| 4. CREATE TABLE employees (...); |
| - Table recreated with matching name |
| - EMP_SYN immediately works again without re-creation! |
| |
+-------------------------------------------------------------------------+
Dropping Synonyms
Synonyms are dropped using the DROP SYNONYM or DROP PUBLIC SYNONYM statement:
-- Drop a private synonym in the current schema
DROP SYNONYM emp_table;
-- Drop a public synonym (requires DROP PUBLIC SYNONYM privilege)
DROP PUBLIC SYNONYM departments;
Rules for Dropping Synonyms
- Base Object Unaffected: Dropping a synonym deletes only the alias definition from
USER_SYNONYMSorALL_SYNONYMS. It has zero effect on the underlying base table, view, sequence, or stored procedure. - Public Synonym Syntax Requirement: To drop a public synonym, the
PUBLICkeyword must be included (DROP PUBLIC SYNONYM name;). ExecutingDROP SYNONYM name;only attempts to drop a private synonym of that name in the user's schema, and fails withORA-01434: private synonym to be dropped does not existwhen no such private synonym exists.ORA-01432: public synonym to be dropped does not existis the matching error forDROP PUBLIC SYNONYM.
Querying Synonym Metadata in Data Dictionary
| Data Dictionary View | Scope & Description |
|---|---|
USER_SYNONYMS | Displays all private synonyms owned by the currently connected user schema. |
ALL_SYNONYMS | Displays all private synonyms accessible to the user plus all public synonyms. |
DBA_SYNONYMS | Displays all private and public synonyms across the entire Oracle database instance. |
SELECT synonym_name, table_owner, table_name, db_link
FROM user_synonyms
WHERE synonym_name = 'EMP_TABLE';
User SCOTT executes the following query in his database session:
SELECT * FROM regions;
Assume the following objects exist in the database:
- A public synonym named 'REGIONS' pointing to 'HR.REGIONS'
- A private table named 'REGIONS' in SCOTT's schema
- SCOTT holds SELECT privileges on both HR.REGIONS and SCOTT.REGIONS
Which object is queried by Oracle?
Oracle raises an ambiguous object error because both a local table and a public synonym exist.
Oracle queries the local table SCOTT.REGIONS because local schema objects take precedence over public synonyms.
Oracle queries HR.REGIONS via the public synonym because public synonyms have database-wide precedence.
Oracle merges the rows from SCOTT.REGIONS and HR.REGIONS into a single unified result set.
A database administrator creates a public synonym using the following command:
CREATE PUBLIC SYNONYM orders FOR sales.orders;
User BLAKE connects to the database and executes: SELECT * FROM orders; Assuming BLAKE does not own a local object named 'ORDERS' and has NOT been granted SELECT on SALES.ORDERS, what is the result?
The query succeeds and returns all rows from SALES.ORDERS because public synonyms automatically grant SELECT access to all users.
The query succeeds, but returns only column metadata without displaying stored table rows.
The query fails with error 'ORA-00942: table or view does not exist' (or insufficient privileges) because creating a synonym does not grant object privileges.
Oracle automatically creates a private copy of the SALES.ORDERS table in BLAKE's schema.
A developer creates a private synonym: CREATE SYNONYM prod_syn FOR products; and later drops the base table: DROP TABLE products;. What is the status of PROD_SYN, and what occurs if the developer executes DROP SYNONYM prod_syn;?
PROD_SYN becomes a dangling synonym; executing DROP SYNONYM prod_syn successfully removes the synonym from the data dictionary without affecting any other database objects.
Dropping the PRODUCTS table automatically deletes PROD_SYN from USER_SYNONYMS via cascading deletion.
Executing DROP SYNONYM prod_syn fails with 'ORA-00942' because the underlying table no longer exists.
PROD_SYN is permanently locked in the data dictionary and cannot be dropped until PRODUCTS is recreated.
Sections you finish are checked off in the contents.