天天看點

current_schema參數的用途

使用者A包含表TBL,如果使用者A将TBL的通路權限給使用者B(grant select

any table to B),則在使用者B中要想引用使用者A的表,不使用同義詞,則需要用select * from A.TBL;

之是以這裡需要使用A.TBL的格式,是因為TBL表屬于使用者A的schema,做個比喻,schema(譯文方案)好比一個容器,存放的是一系列資料庫對象,從官方文檔的介紹說明:

“A schema is a collection of database objects. A schema is owned by a database user

and has the same name as that user. Schema objects are the logical structures that

directly refer to the database’s data. Schema objects include structures like tables,

views, and indexes. (There is no relationship between a tablespace and a schema.

Objects in the same schema can be in different tablespaces, and a tablespace can hold

objects from different schemas.)”

從中可以總結幾點:

1. 一個schema由一個資料庫使用者擁有,并且具有和那個使用者相同的名字。

2. schema對象是一種邏輯結構。

3.

表空間和schema之間沒有關系。

4.

同一個schema中的對象可以存在于不同的表空間,一個表空間可以擁有多個schema的對象。

再借鑒一個比喻,schema好比一個房間,房間中有各種各樣的對象,例如桌子、椅子,房間的主人就是user/owner,他預設擁有這個房間内所有對象的增搬拆權限,但同樣隻有他可以讓另外一個人進入房間,這就是授權。

有點扯遠了,上面說到使用者B要引用使用者A的表,不想使用“使用者A.表”的形式,其實除了使用同義詞外,還可以使用current_schema來改變目前使用者使用的schema。

文法:alter session set current_schema=名稱;

盡管目前模式轉換了,但是否有讀寫權限取決于使用者是否被授權了,換句話說,這個語句并不能決定改變了shcema,這個使用者就有新的schema中對象的讀寫權限。

實驗:

1.

使用者A授權使用者B:

grant

select any table to B;

2.

修改使用者A目前session的shcema為A:

alter session set current_schema=A;

檢視目前session的schema:

select SYS_CONTEXT('USERENV','CURRENT_SCHEMA') CURRENT_SCHEMA

from dual;

>A

show

user

USER

is "TEST_PRIV"

檢視A的TBL:

select * from TBL;

一切OK。

5.

切換目前session的schema為sys:

alter session set current_schema=sys;

>SYS

6. 檢視dba_tables:

select *

from dba_tables;

ERROR

at line 1:

ORA-00942:

table or view does not exist

說明使用者B無權通路SYS對象。

總結:

其實之是以需要current_schema,主要是申請一些隻讀賬戶時,通常是用grant授予使用者通路權限,但通路時如果沒有同義詞則需要是用“schema(/user).表”的方式,每次建立表都建立同義詞的方法也行,但畢竟很是不友善,是用current_schema就很簡單了。

Oracle提供了各種通用或細節的技術手段,目标還是為了友善使用者的使用,是以我覺得當有個問題感覺用起來不爽的時候,可以找下是否有workround,作為一個好的軟體,應該會在你想到之前就已經考慮了這個問題了:)