以下內容整理自Oracle 官方文檔
一 概念
A synonym is an alias for any table,?view,materialized view, sequence, procedure, function, package,?type, Java classschema?object, user-defined object type, or another synonym. Because a synonymis simply an alias, it requires no storage other than its definition in thedata?dictionary.
Oracle中同義詞是任何表、視圖、物化視圖、序列、存儲過程、函數、包、類型、JAVA類對象、用戶定義類型,或是其他的同義詞的別名。由于其只是一個別名,所以除了在
數據字典中的定義不占任何空間。
Synonyms are often used for security andconvenience. For example, they can do the following:
同義詞常用于安全和方便。例如,它們可以做:
1. Mask the name and owner of anobject? 偽裝對象名稱和其所有者。
2. Provide location transparency for remoteobjects of a distributed database 為分布式數據庫遠程對象提供位置透明性
3. Simplify SQL statements for databaseusers 簡化數據庫用戶訪問對象SQL語句
4. Enable restricted?access?similar tospecialized?views when exercising fine-grained access control 當行使精細化訪問控制時提供類似指定視圖的訪問限制
You can create both public and privatesynonyms. A public synonym is owned by the special user group named PUBLIC andevery user in a database can?access?it. A private synonym is in the schema of aspecific user who has control over its availability to others.
你可以創建public和private同義詞。public同義詞屬于PUBLIC組,每個用戶都可以訪問。private同義詞屬于對象所有者,只有其顯式授權后其他用戶才可訪問。
Synonyms are very useful in bothdistributed and nondistributed database environments because they hide theidentity of the underlying object, including its location in a distributedsystem. This is advantageous because if the underlying?object?must be renamedor moved, then only the synonym needs to be redefined. Applications based onthe synonym continue to function without modification.
同義詞的優勢體現在如果其底層的對象重命名或者轉移,那么只需要重定義該同義詞。基于該同義詞的應用則無需任何修改。
Synonyms can also simplify SQL statementsfor users in a distributed database system. The following example shows how andwhy public synonyms are often created by a database administrator to hide theidentity of a base table and reduce the complexity of SQL statements. Assume thefollowing:
下面舉例說明同義詞是如何簡化用戶訪問的:
A table called SALES_DATA is in the schemaowned by the user JWARD.
JWARD用戶下有一張表 SALES_DATA
The SELECT privilege for the SALES_DATAtable is granted to PUBLIC.
PUBLIC組有SALES_DATA的查詢權限
At this point, you have to query the tableSALES_DATA with a SQL statement similar to the following:
此時,你如果查詢SALES_DATA表則需以下語句:
SELECT * FROM jward.sales_data;
Notice how you must include both the schemathat contains the table along with the table name to perform the query.
Assume that the database administratorcreates a public synonym with the following SQL statement:
假如數據庫管理員創建了一個public 同義詞:
CREATE PUBLIC SYNONYM sales FORjward.sales_data;
After the public synonym is created, youcan query the table SALES_DATA with a simple SQL statement:
你的語句將簡化為:
SELECT * FROM sales;
Notice that the public synonym SALES hidesthe name of the table SALES_DATA and the name of the schema that contains thetable.
二、CREATE SYNONYM 創建同義詞
1、語法結構:
2、前提條件:
To create a private synonym in your own schema, you must have the?CREATE?SYNONYM?system privilege.
在自己模式下創建私有同義詞需要CREATE SYNONYM權限。
To create a private synonym in another user's schema, you must have the?CREATE?ANY?SYNONYM?system privilege.
在其他用戶模式下創建私有同義詞需要CREATE ANY SYNONYM權限。
To create a?PUBLIC?synonym, you must have the?CREATE?PUBLIC?SYNONYM?system privilege.
創建公有同義詞,需要有CREATE PUBLIC SYNONYM權限。
3、示例
sqlplus / as sysdba
CREATE SYNONYM offices
FOR hr.locations;
GRANT SELECT ON hr.locations to SCOTT;
CREATE PUBLIC DATABASE LINK l_hr
CONNECT TO hr IDENTIFIED BY hr
USING 'orcl';
CREATE PUBLIC SYNONYM emp_table
FORHR.employees@l_hr;
GRANT SELECT ON HR.employees to SCOTT;
conn scott/tiger@orcl
SELECT count(*) from sys.offices;
select count(*) from emp_table;
三、DROP SYNONYM? 刪除同義詞
1、語法結構:
2、前提條件:
To drop a private synonym, either the synonym must be in your own schema or you must have the?DROP?ANY?SYNONYM?system privilege.
刪除私有同義詞需要有DROP ANY SYNONYM權限。
To drop a?PUBLIC?synonym, you must have the?DROP?PUBLIC?SYNONYM?system?privilege.
刪除公有同義詞需要有DROP PUBLIC SYNONYM權限。
3、示例
3.1刪除public同義詞,必須加public關鍵字:
SYS@orcl>DROP SYNONYM emp_table;
DROPSYNONYM emp_table
*
第 1 行出現錯誤:
ORA-01434:要刪除的專用同義詞不存在
SYS@orcl>DROP PUBLIC SYNONYM emp_table;
同義詞已刪除。
3.2刪除private同義詞:
DROP SYNONYM offices;
四、Q&A 問答
Q: 可以對同義詞做INSERT/UPDATE/DELETE操作嗎?
A:
SCOTT@orcl> UPDATE sys.offices t SETt.city='Shanghai' WHERE location_id=1000;
UPDATE sys.offices t SET t.city='Shanghai'WHERE location_id=1000
*
第 1 行出現錯誤:
ORA-01031: 權限不足
SYS@orcl> grant update on hr.locationsto scott;
授權成功。
SCOTT@orcl> /
已更新 1 行。
SO: 用戶對同義詞的操作權限都是基于對其底層對象有哪些操作權限。
-------------------------------
Dylan???Presents.