Create Synonym in Oracle - Complete Guide with Examples
Create Synonym in Oracle - Complete Guide
Section titled “Create Synonym in Oracle - Complete Guide”Synonyms in Oracle provide alternative names for database objects, simplifying access and providing a layer of abstraction. This guide covers everything you need to know about creating and managing synonyms.
Quick Reference
Section titled “Quick Reference”-- Create private synonymCREATE SYNONYM emp FOR hr.employees;
-- Create public synonymCREATE PUBLIC SYNONYM emp FOR hr.employees;
-- Create or replace synonymCREATE OR REPLACE SYNONYM emp FOR hr.employees;
-- Drop synonymDROP SYNONYM emp;DROP PUBLIC SYNONYM emp;Synonym Types
Section titled “Synonym Types”Private Synonyms
Section titled “Private Synonyms”Private synonyms are owned by a specific user and only accessible within that schema (unless granted to others).
-- Create private synonym for a tableCREATE SYNONYM orders FOR sales.customer_orders;
-- Create synonym for a viewCREATE SYNONYM active_customers FOR sales.v_active_customers;
-- Create synonym for a sequenceCREATE SYNONYM order_seq FOR sales.order_id_seq;
-- Create synonym for a procedureCREATE SYNONYM process_order FOR sales.pkg_orders.process_order;Public Synonyms
Section titled “Public Synonyms”Public synonyms are accessible to all database users without qualification.
-- Create public synonym (requires CREATE PUBLIC SYNONYM privilege)CREATE PUBLIC SYNONYM employees FOR hr.employees;
-- All users can now access:SELECT * FROM employees; -- Instead of hr.employeesWhen to use public synonyms:
- Application tables accessed by multiple schemas
- Utility packages used database-wide
- Hiding schema names from applications
Syntax Reference
Section titled “Syntax Reference”CREATE SYNONYM
Section titled “CREATE SYNONYM”CREATE [OR REPLACE] [PUBLIC] SYNONYM [schema.]synonym_nameFOR [schema.]object_name[@dblink];| Parameter | Description |
|---|---|
| OR REPLACE | Replace existing synonym without dropping first |
| PUBLIC | Create database-wide synonym |
| schema | Owner schema (default: current user) |
| synonym_name | Name for the synonym |
| object_name | Target object (table, view, sequence, etc.) |
| @dblink | Optional database link for remote objects |
Examples
Section titled “Examples”-- Basic table synonymCREATE SYNONYM emp FOR hr.employees;
-- Synonym with schema qualifierCREATE SYNONYM scott.emp FOR hr.employees;
-- Remote object synonym (via database link)CREATE SYNONYM remote_orders FOR orders@production_db;
-- Replace existing synonymCREATE OR REPLACE SYNONYM emp FOR hr.employees_v2;Required Privileges
Section titled “Required Privileges”For Private Synonyms
Section titled “For Private Synonyms”-- Grant ability to create synonymsGRANT CREATE SYNONYM TO username;
-- Grant ability to create any synonym (DBA)GRANT CREATE ANY SYNONYM TO username;For Public Synonyms
Section titled “For Public Synonyms”-- Grant ability to create public synonymsGRANT CREATE PUBLIC SYNONYM TO username;
-- Grant ability to drop public synonymsGRANT DROP PUBLIC SYNONYM TO username;Access to Underlying Object
Section titled “Access to Underlying Object”-- User must have privileges on the base objectGRANT SELECT ON hr.employees TO app_user;
-- Then create synonymCREATE SYNONYM app_user.emp FOR hr.employees;Common Use Cases
Section titled “Common Use Cases”1. Hide Schema Names from Applications
Section titled “1. Hide Schema Names from Applications”-- Create public synonyms for application tablesCREATE PUBLIC SYNONYM customers FOR app_schema.customers;CREATE PUBLIC SYNONYM orders FOR app_schema.orders;CREATE PUBLIC SYNONYM products FOR app_schema.products;
-- Application code uses simple names:SELECT * FROM customers; -- Works for any user2. Simplify Cross-Schema Access
Section titled “2. Simplify Cross-Schema Access”-- Without synonymSELECT * FROM hr.employees eJOIN hr.departments d ON e.department_id = d.department_id;
-- Create synonymsCREATE SYNONYM emp FOR hr.employees;CREATE SYNONYM dept FOR hr.departments;
-- With synonyms (cleaner)SELECT * FROM emp eJOIN dept d ON e.department_id = d.department_id;3. Database Link Abstraction
Section titled “3. Database Link Abstraction”-- Create synonym for remote tableCREATE SYNONYM remote_inventory FOR inventory@warehouse_db;
-- Local queries work seamlesslySELECT * FROM remote_inventory WHERE quantity < 10;4. Version Migration
Section titled “4. Version Migration”-- Point synonym to new table versionCREATE OR REPLACE SYNONYM products FOR schema.products_v2;
-- All existing code continues to work-- Rollback is easy:CREATE OR REPLACE SYNONYM products FOR schema.products_v1;Managing Synonyms
Section titled “Managing Synonyms”View Existing Synonyms
Section titled “View Existing Synonyms”-- All synonyms owned by current userSELECT synonym_name, table_owner, table_name, db_linkFROM user_synonymsORDER BY synonym_name;
-- All synonyms accessible to current userSELECT owner, synonym_name, table_owner, table_nameFROM all_synonymsWHERE table_owner = 'HR'ORDER BY synonym_name;
-- All public synonymsSELECT synonym_name, table_owner, table_nameFROM dba_synonymsWHERE owner = 'PUBLIC'ORDER BY synonym_name;Drop Synonyms
Section titled “Drop Synonyms”-- Drop private synonymDROP SYNONYM synonym_name;
-- Drop public synonymDROP PUBLIC SYNONYM synonym_name;
-- Generate drop statements for all user synonymsSELECT 'DROP SYNONYM ' || synonym_name || ';'FROM user_synonyms;Find Invalid Synonyms
Section titled “Find Invalid Synonyms”-- Synonyms pointing to non-existent objectsSELECT s.owner, s.synonym_name, s.table_owner, s.table_nameFROM dba_synonyms sWHERE NOT EXISTS ( SELECT 1 FROM dba_objects o WHERE o.owner = s.table_owner AND o.object_name = s.table_name);Best Practices
Section titled “Best Practices”- Use meaningful names - Synonyms should be intuitive
- Document public synonyms - Track what they point to
- Use OR REPLACE - Prevents “name already exists” errors
- Grant minimum privileges - Only CREATE SYNONYM, not CREATE ANY
Don’ts
Section titled “Don’ts”- Don’t create circular synonyms - Synonym A pointing to Synonym B pointing to A
- Don’t use synonyms for temporary objects - Creates confusion
- Don’t create public synonyms for sensitive data - Security risk
- Avoid synonym chains - Synonym to synonym to object
Troubleshooting
Section titled “Troubleshooting”ORA-00955: Name Already Used
Section titled “ORA-00955: Name Already Used”-- Check what's using the nameSELECT object_type, owner, object_nameFROM dba_objectsWHERE object_name = UPPER('&name');
-- Use OR REPLACE to overwriteCREATE OR REPLACE SYNONYM emp FOR hr.employees;ORA-01775: Looping Chain of Synonyms
Section titled “ORA-01775: Looping Chain of Synonyms”-- Find the chainSELECT * FROM dba_synonymsWHERE synonym_name = 'PROBLEM_SYNONYM' OR table_name = 'PROBLEM_SYNONYM';
-- Fix by dropping and recreating properlyDROP SYNONYM problem_synonym;CREATE SYNONYM problem_synonym FOR actual_schema.actual_table;ORA-00980: Synonym Translation No Longer Valid
Section titled “ORA-00980: Synonym Translation No Longer Valid”-- Underlying object was dropped-- Find what synonym points to:SELECT table_owner, table_nameFROM user_synonymsWHERE synonym_name = 'BROKEN_SYNONYM';
-- Either recreate the object or drop the synonymDROP SYNONYM broken_synonym;Related Topics
Section titled “Related Topics”- ORA-00955: Name Already Used - Object naming conflicts
- Database Links - Remote database access
- Security Scripts - Privilege management