Synonym

Creating a Synonym for an Object Simplify access to objects by creating a synonym(another name for an object).

With synonyms ,you can:

Create an easier reference to a table that is owned by another user Shorten length object names



CREATE [PUBLIC] SYNONYM synonym FRO object;


PUBLIC关键词,如果指定了,则全局都可以使用 如果没有指定,则只能在当前schema下使用;

/*  实验:  当删除一个表中的字段时,要指定表的真实名称,而不能使用同义词;否则会报错:ORA-00942: table or view does not exist */

/*  同义词的使用场景:  

  1、类似于接口使用,假如为一张表指定了同义词,那么不管如何修改表的结构,只要你不修改表名称,同义词可以照样使用,不受影响.

  2、同样在创建synonym时,可以指定PUBLIC KEYWORD,(授权后)就可以将此对象共享给其他用户使用。  

     举例:    (1)、在schema ARCER下创建同义词netstore_pub(创建时有PUBLIC关键词,如果不指定PUBLIC,即使授权了,也是无法使用的.)    

          (2)、然后授权grant select on netstore_pub to HR;将同义词netstore_pub的查询权限授权给HR用户;   

                         (3)、切换到HR用户下,执行查询netstore_pub即可;

*/

example: demo01

--为表NETSTORE_INCOMEEXPENDITURELIST创建同义词netstore



SQL> CREATE SYNONYM netstore FOR NETSTORE_INCOMEEXPENDITURELIST;


--修改NETSTORE_INCOMEEXPENDITURELIST表名称为NETSTORE_ICE_LIST



SQL> RENAME NETSTORE_INCOMEEXPENDITURELIST TO NETSTORE_ICE_LIST;


Table renamed. --再次查询同义词netstore报错;



SQL> select numid from netstore; 

select numid from netstore

* ERROR at line 1: ORA-00980: synonym translation is no longer valid


Creating and Removing Synonyms

Create a shortened name for the DEPT_SUM_VU view



CREATE SYNONYM d_sum FROM DEPT_SUM_VU;


Drop a synonym: DROP SYNONYM d_sum;