0% found this document useful (0 votes)
59 views

05 SQL Synomyms Imp

A synonym is an alias for a table, view or sequence that provides location transparency and hides the name and owner of the underlying object. There are two types: private synonyms, which are only available to the creating user, and public synonyms, which are created by a DBA and available to all users. Synonyms allow accessing objects across schemas without specifying the schema name and provide alternative names for remote objects in a distributed database. They are created using the CREATE SYNONYM statement and dropped with the DROP SYNONYM statement.

Uploaded by

Vinutha M
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

05 SQL Synomyms Imp

A synonym is an alias for a table, view or sequence that provides location transparency and hides the name and owner of the underlying object. There are two types: private synonyms, which are only available to the creating user, and public synonyms, which are created by a DBA and available to all users. Synonyms allow accessing objects across schemas without specifying the schema name and provide alternative names for remote objects in a distributed database. They are created using the CREATE SYNONYM statement and dropped with the DROP SYNONYM statement.

Uploaded by

Vinutha M
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

SYNONYM:

========
1] A synonym is a database object, which is used as an alias for a table, view or
sequence.

2] A Synonym acts as an alternative name for an existing schema object.

3] Synonym basically allows us to create a pointer to an object that exists in


different schema.

TYPES:
====== 1] PRIVATE 2] PUBLIC

SYNTAX:
=======

CREATE [PUBLIC] SYNONYM <SYNOMYM_NAME> FOR (SCHEMA_NAME).(OBJECT_NAME);

1] Private synonym is available to the particular user who creates.


2] Public synonym is created by DBA which is available to all the users.

Advantages:
============
1] Hide the name and owner of the object.
2] Provides location transparency for remote objects of a distributed database.

SYNTAX FOR VIEWING USER_SYNONYMS:


=================================
SELECT * FROM USER_SYNONYMS WHERE TABLE_NAME = 'TABLE NAME';

SYNTAX FOR VIEWING ALL_SYNONYMS:


================================
SELECT * FROM ALL_SYNONYMS WHERE TABLE_NAME = 'TABLE NAME';

SYNTAX FOR VIEWING DBA_SYNONYMS:


================================
SELECT * FROM DBA_SYNONYMS WHERE TABLE_NAME = 'TABLE NAME';

CREATING PRIVATE SYNONYM:


==========================
SQL> CREATE USER user1 IDENTIFIED BY user1;
-- User created.

SQL> GRANT CONNECT TO user1;


-- GRANT succeeded.

SQL> conn user1/user1;


--CONNECTED.

And now from �user1� if we wanted to access EMP table that belongs to SCOTT schema
then
we should refer to EMP using �schemaname.tablename� (SCOTT.EMP)

SELECT * FROM scott.emp;

From �user1� if we wanted to hide the EMP table then we should create synonym for
EMP table in �user1�.
/* Inside User1*/

SQL> CREATE SYNONYM na_emp FOR scott.emp;

-- Synonym created.

/*Once the synonym is created, we can use this synonym just like a table in select
statement*/

SQL> SELECT * FROM na_emp;

--NA_EMP is the synonym that hides actual EMP table.


From now on �user1� can directly execute �SELECT * FROM na_emp;� without referring
to SCOTT.EMP table.

Drop synonym:
=============

SQL> DROP SYNONYM na_emp;

-- Synonym dropped.

CREATING PUBLIC SYNONYM:


========================

1] Public Synonym is a synonym which can be accessed by all the users in the
database.

2] These synonyms are generally created by Database administrators.

3] To create public synonym, we should specify PUBLIC keyword in the syntax.

To create public synonym first we try to connect to �system� or �sysdba�.


========================================================================
/*connect to �system� or �sysdba� user who has the full admin access to CREATE
public synonym.*/

SQL>conn system/admin

Then create a public synonym for EMP table which is in �scott� user.

SQL> CREATE PUBLIC SYNONYM na_pub_emp FOR scott.emp;

-- Synonym created.

Provide necessary privileges to �user1� to access synonym �na_pub_emp�.


=======================================================================

SQL> GRANT ALL ON na_pub_emp TO user1;

-- grant succeeded.

Now let�s try to connect to user1 and access the public synonym.

SQL> conn user1/user1;

SQL> SELECT * FROM na_pub_emp;


Note: NA_PUB_EMP is the public synonym that hides actual EMP table.

Drop the public synonym:


========================

SQL> DROP PUBLIC SYNONYM emp_syn;

-- Synonym dropped

You might also like