A synonym is an alias for any table, view, materialized view, sequence, procedure, function, package, type, Java class schema object, user-defined object type, or another synonym. Because a synonym is simply an alias, it requires no storage other than its definition in the data dictionary.
Examples
CREATE SYNONYM: Examples
To define the synonym offices
for the table locations
in the schema hr
, issue the following statement:
CREATE SYNONYM offices
FOR hr.locations;
To create a PUBLIC
synonym for the employees
table in the schema hr
on the remote
database, you could issue the following statement:
CREATE PUBLIC SYNONYM emp_table
FOR [email protected];
A synonym may have the same name as the underlying object, provided the underlying object is contained in another schema.
Oracle Database Resolution of Synonyms: Example
Oracle Database attempts to resolve references to objects at the schema level before resolving them at the PUBLIC
synonym level. For example, the schemas oe
and sh
both contain tables named customers
. In the next example, user SYSTEM
creates a PUBLIC
synonym named customers
for oe.customers
:
CREATE PUBLIC SYNONYM customers FOR oe.customers;
If the user sh
then issues the following statement, then the database returns the count of rows from sh.customers
:
SELECT COUNT(*) FROM customers;
To retrieve the count of rows from oe.customers
, the user sh
must preface customers
with the schema name. (The user sh
must have select permission on oe.customers
as well.)
SELECT COUNT(*) FROM oe.customers;
If the user hr
's schema does not contain an object named customers
, and if hr
has select permission on oe.customers
, then hr
can access the customers
table in oe
's schema by using the public synonym customers
:
SELECT COUNT(*) FROM customers;
Example2
问题描述:
有两个oralce数据库用户:lyweb@martdb1和lylocal@martdb1
lylocal@martdb1下的表ft_mid_user_daily
在lyweb@martdb1下要用该表
想在lyweb@martdb1下进行如下操作:
select * from ft_mid_user_daily
问题解答:
1.在lylocal@martdb1下进行赋权限:
grant select on ft_mid_user_daily to lyweb;
2.在lyweb@martdb1下进行如下操作:
create or replace synonym ft_mid_user_daily for lylocal.ft_mid_user_daily;
需要注意的一点:
在 lyweb@martdb1中不能包含有表ft_mid_user_daily,虽然create or replace synonym 语句可以执行,但是执行select * from ft_mid_user_daily(本意为想读lylocal@martdb1下的ft_mid_user_daily中的内容),但显示的还是 lyweb@martdb1下的表ft_mid_user_daily内容,需要先把lyweb@martdb1下的表 ft_mid_user_daily删除掉,重新建立synonym即可。