Friday, June 8, 2012

SQL : SYNONYMS


A synonym is an alias for a database object (table, view, procedure, function, package, sequence, etc.). Synonyms may be used to reference the original object in SQL as wel as PL/SQL. 

They can be used to hide ownership and location of the database objects they refer to and minimize the impact of moving or renaming the database objects.  

There are two types of synonyms:
  • private
    Private synonyms exist only in a specific user schema. The owner of the synonym maintains control over availability to other users.
  • public
    A public synonym is available to all users

Example
In your database you have a schema called HRM. This schema contains a table called EMPLOYEES. To query the table you use the following SELECT statement:

SELECT * FROM HRM.EMPLOYEES;

-- first we grant select privileges to all database users
GRANT SELECT ON HRM.EMPLOYEES TO PUBLIC;

-- next, we create a public synonym for your table
CREATE PUBLIC SYNONYM EMPLOYEE_DATA FOR HRM.EMPLOYEES;

-- from now on, anyone can query your table using the synonym
SELECT * FROM EMPLOYEE_DATA;

No comments:

Post a Comment