同义词是现有对象的别名,可以简化SQL语句、隐藏对象的名称和所有者、提供对对象的公共访问。 同义词有2种类型:私有同义词和公有同义词。
一、私有同义词
私有同义词只能在其模式内访问,且不能与当前模式的对象重名。 创建私有同义词需要有“create synonym”权限。
SQL> conn scott/scott @orcl;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as scott@ORCL
SQL> show user;
User is "SCOTT"
SQL> create table test1(id number);
Table created
SQL> conn system/system @orcl;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as system@ORCL
SQL> show user;
User is "SYSTEM"
SQL> select * from scott.test1;
ID
SQL> create synonym syname1 for scott.test1;
Synonym created
SQL> select * from syname1;
ID
SQL> conn scott/scott @orcl;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as scott@ORCL
SQL> show user;
User is "SCOTT"
SQL> select * from syname1;
select * from syname1
ORA-00942: table or view does not exist
SQL> create synonym syn_02 for test1;
create synonym syn_02 for test1
ORA-01031: insufficient privileges
SQL> conn system/system @orcl;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as system@ORCL
SQL> grant create synonym to scott;
Grant succeeded
SQL> conn scott/scott @orcl;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as scott@ORCL
SQL> create synonym syn_02 for test1;
Synonym created
SQL> select * from sysn_02;
SQL> select * from syn_02;
ID
SQL> conn scott/scott@orcl;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as scott@ORCL
SQL> drop synonym syn_02;
Synonym dropped
二、公有同义词
可以被数据库的所有用户访问。 创建公有同义词需要“create public synonym”权限,若只有“create synonym”权限则不能创建公有同义词。 若其他用户不具有访问某个对象的权限,则即使该用户能访问公有同义词也不能访问该对象。 一般情况,普通用户不能删除自己创建的公有同义词,需要用system用户删除公有同义词。
SQL> create table test2(id number);
Table created
SQL> create public synonym synp_01 for test2;
create public synonym synp_01 for test2
ORA-01031: insufficient privileges
SQL> conn system/system @orcl;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as system@ORCL
SQL> grant create public synonym to scott;
Grant succeeded
SQL> conn scott/scott @orcl;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as scott@ORCL
SQL> create public synonym synp_01 for test2;
Synonym created
SQL> conn system/abc123 @orcl;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as system@ORCL
SQL> select * from synp_01;
ID
SQL> create user testuser01 identified by testuser01;
User created
SQL> grant connect to testuser01;
Grant succeeded
SQL> conn testuser01/testuser01 @orcl;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as testuser01@ORCL
SQL> select * from synp_01;
select * from synp_01
ORA-00942: table or view does not exist
SQL> select * from scott.test2;
select * from scott.test2
ORA-00942: table or view does not exist
SQL> conn scott/scott @orcl;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as scott@ORCL
SQL> grant select on test2 to testuser01;
Grant succeeded
SQL> conn testuser01/testuser01@orcl;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as testuser01@ORCL
SQL> select * from scott.test2;
ID
SQL> select * from synp_01;
ID
SQL> conn scott/scott@orcl;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as scott@ORCL
SQL> drop public synonym synp_01;
drop public synonym synp_01
ORA-01031: insufficient privileges
SQL> conn system/system@orcl;
Connected to Oracle Database 11g Enterprise Edition Release 11.2.0.1.0
Connected as system@ORCL
SQL> drop public synonym synp_01;
Synonym dropped
|