Importance of Synonym in Oracle Database.
Hello Everyone,
Today I will
discuss about Synonym in Oracle as Synonym play important role in database to
hide information of object. Before Starting this blog please go through my
previous blogs of SQL in my archive folder. Let’s Discuss about Synonym. Synonym
in database is an object which is basically used as alternative name for
objects such as tables, views, sequences, stored procedures, and other database
objects. We generally use synonyms for granting access to an object from
another schema and you don't want the users from which Schema it’s own the
object.
In Another
word we can say synonym is an alias for any table, view, snapshot, sequence,
procedure, function, or package. Because a synonym is simply an alias, it
requires no storage other than its definition in the data dictionary.
Synonyms are
basically used for security purpose and convenience.
For example,
Synonym can do the following:
· Hide
the name and owner of an object.
· Provide location transparency for
remote objects of a distributed database.
· Simple use of SQL statements for
database users.
There are
two types of Synonym:
· Public Synonym
· Private Synonym
We can create both public and private
synonyms.
Note : We
need CREATE PUBLIC SYNONYMPermission to create public synonyms
Syntax for
creating Synonym :
CREATE [OR
REPLACE] [PUBLIC] SYNONYM [schema .] synonym_name
FOR [schema .] object_name [@ dblink];
Public
synonym is global and owned by the special user group named PUBLIC and every
user in a database can access it whereas a private synonym is in the schema of
a specific user who has control over its availability to others by using Grant
and Revoke.
Syntax and example for Creating Public and
Private Synonym:
Public Synonym :
CREATE PUBLIC
SYNONYM emp1
FOR
dotc.employee;
This first
CREATE SYNONYM example demonstrates how to create a synonym called emp. Now,
users of other schemas can reference the table called employee without having
to prefix the table name with the schema named dotc. Below is the example:
SELECT *
FROM emp1;
If we need
to make any changes in Synonym as per requirement for the same name we can use
Replace keyword.
CREATE OR REPLACE
PUBLIC SYNONYM emp1
FOR dotc.employee;
Note : Once
we define a synonym for an object, you can reference it in the SQL statements
such as the SELECT, INSERT, UPDATE, and DELETE statement.
Below is the output of synonym emp1
Private Synonym:
In Private
we just omit the Public keyword, A private Synonym must be unique on its
schema.
Syntax :
CREATE SYNONYM emp
FOR
dotc.employee;
Private Synonym has limitation to its own schema only, but we can give grant access to other user if required like select etc.
Synonyms are very useful in both distributed and non distributed database environments because they hide the identity of the objects, including its location in a distributed system. This is advantageous because if the underlying object must be renamed or moved, then only the synonym needs to be redefined. Applications based on the synonym continue to work without any modification. Synonym also simplify SQL statement for user in a distributed database system.
One Basic Example:
A table employee is in the schema owned by the user DOTC. The SELECT privilege for the employee table is granted to PUBLIC. At this point, you have to query the table employee with a SQL statement similar to the below query:
SELECT * FROM dotc.employee;
Here we must
include both the schema that contains the table along with the table name to
perform the query.
Suppose that
the database administrator (DBA) creates a public synonym with the following
SQL statement:
CREATE PUBLIC SYNONYM emp FOR dotc.employee;
After the
public synonym is created, you can query the table employee with a simple SQL
statement:
SELECT *
FROM emp;
Note :
Here the public synonym emp hides the
name of the table employee and the name of the schema that is dotc which
contains the table employee.
So we are
now able to understand the usage of Synonym in Database and it’s importance . Synonymy used to hide table
name as well as schema and we can simply query the synonym for data also it’s
used for security purpose as it’s hide the information from the user.
Please go
through this blog carefully as Interviewer can ask
frequent questions on Synonym to check it’s advantages in database. Please let me know
if you have any doubts.
Thanks
ReplyDeleteIt is very useful for me. Thanks...
Microsoft Azure DevOps Online Training
Microsoft Azure DevOps Live Online Training