oracle symonym_ORACLE SYNONYM详解
以下內(nèi)容整理自O(shè)racle 官方文檔
一 概念
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中同義詞是任何表、視圖、物化視圖、序列、存儲(chǔ)過程、函數(shù)、包、類型、JAVA類對象、用戶定義類型,或是其他的同義詞的別名。由于其只是一個(gè)別名,所以除了在
數(shù)據(jù)字典中的定義不占任何空間。
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 為分布式數(shù)據(jù)庫遠(yuǎn)程對象提供位置透明性
3. Simplify SQL statements for databaseusers 簡化數(shù)據(jù)庫用戶訪問對象SQL語句
4. Enable restricted?access?similar tospecialized?views when exercising fine-grained access control 當(dāng)行使精細(xì)化訪問控制時(shí)提供類似指定視圖的訪問限制
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.
你可以創(chuàng)建public和private同義詞。public同義詞屬于PUBLIC組,每個(gè)用戶都可以訪問。private同義詞屬于對象所有者,只有其顯式授權(quán)后其他用戶才可訪問。
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.
同義詞的優(yōu)勢體現(xiàn)在如果其底層的對象重命名或者轉(zhuǎn)移,那么只需要重定義該同義詞。基于該同義詞的應(yīng)用則無需任何修改。
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的查詢權(quán)限
At this point, you have to query the tableSALES_DATA with a SQL statement similar to the following:
此時(shí),你如果查詢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:
假如數(shù)據(jù)庫管理員創(chuàng)建了一個(gè)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 創(chuàng)建同義詞
1、語法結(jié)構(gòu):
2、前提條件:
To create a private synonym in your own schema, you must have the?CREATE?SYNONYM?system privilege.
在自己模式下創(chuàng)建私有同義詞需要CREATE SYNONYM權(quán)限。
To create a private synonym in another user's schema, you must have the?CREATE?ANY?SYNONYM?system privilege.
在其他用戶模式下創(chuàng)建私有同義詞需要CREATE ANY SYNONYM權(quán)限。
To create a?PUBLIC?synonym, you must have the?CREATE?PUBLIC?SYNONYM?system privilege.
創(chuàng)建公有同義詞,需要有CREATE PUBLIC SYNONYM權(quán)限。
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、語法結(jié)構(gòu):
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權(quán)限。
To drop a?PUBLIC?synonym, you must have the?DROP?PUBLIC?SYNONYM?system?privilege.
刪除公有同義詞需要有DROP PUBLIC SYNONYM權(quán)限。
3、示例
3.1刪除public同義詞,必須加public關(guān)鍵字:
SYS@orcl>DROP SYNONYM emp_table;
DROPSYNONYM emp_table
*
第 1 行出現(xiàn)錯(cuò)誤:
ORA-01434:要?jiǎng)h除的專用同義詞不存在
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 行出現(xiàn)錯(cuò)誤:
ORA-01031: 權(quán)限不足
SYS@orcl> grant update on hr.locationsto scott;
授權(quán)成功。
SCOTT@orcl> /
已更新 1 行。
SO: 用戶對同義詞的操作權(quán)限都是基于對其底層對象有哪些操作權(quán)限。
-------------------------------
Dylan???Presents.
總結(jié)
以上是生活随笔為你收集整理的oracle symonym_ORACLE SYNONYM详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 激光手术大概多少钱啊?
- 下一篇: input ios问题 小程序_微信小程