Welcome to plsql4all.blogspot.com SQL, MYSQL, ORACLE, TERADATA, MONGODB, MARIADB, GREENPLUM, DB2, POSTGRESQL.

Thursday, 11 April 2024

Synonyms in Oracle

In Oracle, a synonym is an alternative name for a database object, such as a table, view, sequence, procedure, or function. Synonyms provide a convenient way to reference objects in the database without specifying the schema name or owner explicitly. They can be useful for simplifying SQL statements, enhancing security, and abstracting changes to object names or locations.


Here are some key points about synonyms in Oracle:


1. Purpose: Synonyms provide a level of abstraction between users and database objects, allowing users to refer to objects by a different, more user-friendly name.


2. Types of Synonyms:

   - Private Synonyms: Owned by a specific user and accessible only to that user. Private synonyms are created automatically when a user creates an object in their schema, but they can also be created explicitly.

   - Public Synonyms: Available to all users in the database. Public synonyms are typically created by database administrators and allow users to access shared objects without specifying the schema name.


3. Benefits:

   - Simplified Access: Users can reference objects by their synonym names, eliminating the need to specify the schema name or owner explicitly in SQL statements.

   - Enhanced Security: Synonyms can be used to hide the underlying schema structure and provide controlled access to objects. For example, a public synonym can provide access to a shared object while hiding its owner's identity.

   - Portability and Flexibility: Synonyms abstract changes to object names or locations, making it easier to migrate or rename objects without affecting dependent applications or SQL statements.


4. Creating Synonyms:

   - Synonyms can be created using the CREATE SYNONYM statement. For example:

   

     CREATE [PUBLIC] SYNONYM synonym_name FOR object_name;

  

     The PUBLIC keyword is optional and is used to create a public synonym accessible to all users.


5. Using Synonyms:

   - Once created, synonyms can be used in SQL statements to reference database objects. For example:

    

     SELECT * FROM synonym_name;


Overall, synonyms in Oracle provide a convenient and flexible way to simplify access to database objects, enhance security, and abstract changes to object names or locations. They are commonly used in database environments to improve usability and manageability.


In Oracle, synonyms are primarily used as aliases for database objects, such as tables, views, or procedures. Since synonyms themselves do not store data, it's not possible to provide an example with data directly related to synonyms. However, I can provide an example of creating and using a synonym for a table:


Let's say we have a table named employees in the hr schema:


CREATE TABLE hr.employees (

    employee_id NUMBER PRIMARY KEY,

    first_name VARCHAR2(50),

    last_name VARCHAR2(50),

    email VARCHAR2(100)

);


Now, let's create a synonym named emp for the employees table:


CREATE SYNONYM emp FOR hr.employees;


With the synonym created, we can now use it to reference the employees table without specifying the schema name (hr) explicitly:


-- Insert data into the employees table using the synonym

INSERT INTO emp (employee_id, first_name, last_name, email)

VALUES (1, 'Chanchal', 'Wankhade', 'chanchalwankhade@example.com');


-- Query data from the employees table using the synonym

SELECT * FROM emp;


The output would be:


EMPLOYEE_ID | FIRST_NAME | LAST_NAME | EMAIL

------------+------------+-----------+-------------------

1           | Chanchal       | Wankhade       | chanchalwankhade@example.com


In this example, the synonym emp serves as an alias for the employees table in the hr schema. We can perform various operations on the table using the synonym without explicitly specifying the schema name, making the SQL statements more concise and readable.


Here are five frequently asked questions (FAQs) about synonyms in Oracle:


1. What is a synonym in Oracle?

   - A synonym in Oracle is an alternative name for a database object, providing a convenient way to reference objects without specifying the schema name or owner explicitly.


2. What are the types of synonyms in Oracle?

   - Oracle supports two types of synonyms:

     - Private Synonyms: Owned by a specific user and accessible only to that user.

     - Public Synonyms: Available to all users in the database.


3. How do I create a synonym in Oracle?

   - To create a synonym, you can use the CREATE SYNONYM statement followed by the synonym name and the name of the object it references. For example:

   

     CREATE SYNONYM synonym_name FOR object_name;


4. Can I use synonyms to reference tables, views, and procedures in Oracle?

   - Yes, synonyms can be created for various database objects, including tables, views, sequences, procedures, functions, and packages.


5. How do I drop a synonym in Oracle?

   - To drop a synonym, you can use the DROP SYNONYM statement followed by the name of the synonym. For example:

   

     DROP SYNONYM synonym_name;

   

Please provide your feedback in the comments section above. Please don't forget to follow.