Oracle 同义词(Synonym) 是数据库对象的一个别名,Oracle 可以为表、视图、序列、过程、函数、程序包等指定一个别名。同义词有两种类型:
创建同义词的语法是:CREATE [OR REPLACE] [PUBLIC] SYSNONYM [schema.]synonym_name FOR [schema.]object_name
语法说明:
管理员用户可以访问任何用户的数据库对象。SYSTEM 用户访问 SCOTT 用户的 tab 表时,必须使用 SCOTT.TAB。
案例1:创建一个用户 UserA,该用户拥有 CONNECT 角色和 RESOURCE 角色。为 SCOTT 用户的 TAB 表创建同义词,并通过同义词访问该 TAB 表。
SQL> CONN system/manager@orcl; Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 Connected as system SQL> CREATE USER UserA IDENTIFIED BY UserA; User created SQL> GRANT CONNECT TO UserA; Grant succeeded SQL> GRANT RESOURCE TO UserA; Grant succeeded SQL> GRANT CREATE SYNONYM TO UserA; Grant succeeded
SQL> CONN UserA/UserA@ORCL; Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 Connected as UserA SQL> CREATE SYNONYM MyTab FOR SCOTT.TAB; Synonym created
SQL> SELECT * FROM MYTAB; SELECT * FROM MYTAB
ORA-00942: 表或视图不存在
SQL> CONNECT SCOTT/tiger@ORCL Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 Connected as SCOTT
SQL> GRANT ALL ON TAB TO UserA; Grant succeeded SQL> CONNECT UserA/UserA@ORCL; Connected to Oracle Database 10g Enterprise Edition Release 10.2.0.3.0 Connected as UserA
SQL> SELECT ENAME,JOB,SAL FROM MyTab WHERE SAL>2000; ⑤
DROP [PUBLIC] SYNONYM [schema.]synonym_name
语法说明: