天天看點

ORACLE授權使用者查詢另一個使用者下的表與視圖

實際應用中,會遇到在某個使用者下需要查詢另一個使用者下的表資料或視圖的情況,然而在沒有授權時,會提示無權限操作的錯誤。那就需要通過授權處理後,再能進行查詢操作,下面我們來看看是怎麼處理的。

一、系統權限說明:

1、使用者權限

CREATE SESSIOIN 連接配接到資料庫

CREATE TABLE    在使用者的方案中建立表

CREATE SEQUENCE 在使用者的方案中建立序列

CREATE VIEW     在使用者的方案中創視圖

CREATE PROCEDURE在使用者的方案中建立存儲過程,函數或包

1.1、例子:授予系統權限

DBA能夠授予使用者指定的系統權限

GRANT create session,create table,

       create sequence,create view

 TO scott;

二、建立使用者隻用于查詢其它使用者庫的表和視圖

1、建立使用者

<code>create user 使用者名 identified by 密碼;</code>

<code>grant connect,</code><code>select</code> <code>any table to 使用者名;</code>

<code>這樣建立的使用者就可以連接配接資料庫和隻有對任何表有查詢權限了</code>

<code>grant connect to 使用者名  </code><code>//</code><code>隻有連接配接權限</code>

2、授權查詢表與視圖權限

2.1、a使用者下授權查詢所有表給b使用者(a使用者登入執行下面語句)

<code>select</code> <code>'grant select on a.'</code> <code>|| tname || </code><code>' to b;'</code> <code>from tab;</code>

<code>'GRANTSELECTONA.'</code><code>||TNAME||</code><code>'TOB;'</code>

<code>------------------------------------------------------</code>

<code>grant </code><code>select</code> <code>on a.VOTE_NUM to b;</code>

<code>grant </code><code>select</code> <code>on a.TMP_MSG to b;</code>

<code>grant </code><code>select</code> <code>on a.VOTE_IP to b;</code>

<code>grant </code><code>select</code> <code>on a.QUESTION to b;</code>

<code>grant </code><code>select</code> <code>on a.QUESTION_COUNT to b;</code>

<code>grant </code><code>select</code> <code>on a.RECORD_DICT to b;</code>

<code>grant </code><code>select</code> <code>on a.BM_COLUMN to b;</code>

<code>grant </code><code>select</code> <code>on a.BM_COLUMN_CLASSIFY_REL to b;</code>

<code>grant </code><code>select</code> <code>on a.BM_INFO_CLASSIFY to b;</code>

<code>grant </code><code>select</code> <code>on a.BM_MODULE to b;</code>

<code>grant </code><code>select</code> <code>on a.BM_MODULE_AUTH to b;</code>

<code>或</code>

<code>select</code> <code>'grant select on '</code><code>||table_name||</code><code>' to b;'</code>  <code>from user_tables;</code>

<code>'GRANTSELECTON'</code><code>||TABLE_NAME||</code><code>'TOB;'</code>

<code>----------------------------------------------------</code>

<code>grant </code><code>select</code> <code>on VOTE_NUM to b;</code>

<code>grant </code><code>select</code> <code>on TMP_MSG to b;</code>

<code>grant </code><code>select</code> <code>on VOTE_IP to b;</code>

<code>grant </code><code>select</code> <code>on QUESTION to b;</code>

<code>grant </code><code>select</code> <code>on QUESTION_COUNT to b;</code>

<code>grant </code><code>select</code> <code>on RECORD_DICT to b;</code>

<code>grant </code><code>select</code> <code>on BM_COLUMN to b;</code>

<code>grant </code><code>select</code> <code>on BM_COLUMN_CLASSIFY_REL to b;</code>

<code>說明:在a使用者下執行該語句,執行後會生成對所有表的賦權限語句,拷貝出來執行就可以了。</code>

2.2、a使用者下授權查詢單個表給b使用者

<code>grant </code><code>select</code> <code>on a.tablename to b;</code>

2.3、a使用者下授權查詢所有序列給b使用者

<code>select</code> <code>'grant select on '</code> <code>|| sequence_name || </code><code>' to b;'</code> <code>from dba_sequences where sequence_owner=</code><code>'A'</code><code>;</code>

2.4、--Oracle查詢使用者視圖

<code>select</code> <code>* from user_views;</code>

2.5、a使用者下授權查詢視圖給test11使用者

<code>select</code> <code>'grant select on a.'</code> <code>|| view_name || </code><code>' to test11;'</code> <code>from user_views;</code>

<code>視圖查詢如下:</code>

<code>'GRANTSELECTON'</code><code>||VIEW_NAME||</code><code>'TOTEST11;'</code>

<code>---------------------------------------------------------</code>

<code>grant </code><code>select</code> <code>on CONFIRM_RESERVATION_VIEW to test11;</code>

<code>grant </code><code>select</code> <code>on DEPARTMENT_RESERVATION_VIEW to test11;</code>

<code>grant </code><code>select</code> <code>on DEPART_CANCEL_RESERVATION_VIEW to test11;</code>

<code>grant </code><code>select</code> <code>on DOCTOR_CANCEL_RESERVATION_VIEW to test11;</code>

<code>grant </code><code>select</code> <code>on DOCTOR_RESERVATION_VIEW to test11;</code>

<code>grant </code><code>select</code> <code>on GRPSS to test11;</code>

<code>grant </code><code>select</code> <code>on HOSPITAL_ALL_SCHEDULE_VIEW to test11;</code>

<code>grant </code><code>select</code> <code>on HOSPITAL_DEPARTMENT_VIEW to test11;</code>

<code>grant </code><code>select</code> <code>on HOSPITAL_DEP_SCHEDULE_VIEW to test11;</code>

<code>grant </code><code>select</code> <code>on HOSPITAL_DOCTOR_VIEW to test11;</code>

<code>grant </code><code>select</code> <code>on HOSPITAL_DOC_SCHEDULE_VIEW to test11;</code>

<code>grant </code><code>select</code> <code>on PATIENT_COUNT_RESERVATION_VIEW to test11;</code>

<code>grant </code><code>select</code> <code>on PATIENT_RESERVATION_VIEW to test11;</code>

<code>grant </code><code>select</code> <code>on PATIENT_RESERVATION_VIEW2 to test11;</code>

<code>grant </code><code>select</code> <code>on PATIENT_RES_VIEW to test11;</code>

<code>grant </code><code>select</code> <code>on PRVIEW to test11;</code>

<code>grant </code><code>select</code> <code>on RES_VIEW to test11;</code>

<code>grant </code><code>select</code> <code>on SS to test11;</code>

備注:授權更新、删除的 文法和授權查詢類似,隻是關鍵字不同而已。

三、撤消權限

1、授權a使用者下取消給b使用者删除單個表的權限

revoke delete on a.tablename from b;

2、授權a使用者下取消給b使用者更新單個表的權限

revoke update on a.tablename from b;

3、擁有dba權限的使用者下取消給b使用者建立dblink的權限

revoke create database link from b;

4、擁有dba權限的使用者下取消給tes11使用者查詢任何表的權限

revoke select any table from test11;

四、事例:

1、在rh_test使用者下授權查詢所有表給wd使用者

<code>select 'grant select on rhip_test.' || tname || ' to wd;' from tab;</code>

<code>'GRANTSELECTONRH_TEST.'||TNAME||'TOWD;'</code>

<code>----------------------------------------------------------------</code>

<code>grant select on rh_test.BIZ_CODE_REL to wd;</code>

<code>grant select on rh_test.BIZ_RMIM_DIC to wd;</code>

<code>grant select on rh_test.BIZ_RMIM_VERSION to wd;</code>

<code>grant select on rh_test.BIZ_RMIM_VERSION_DETAIL to wd;</code>

<code>grant select on rh_test.BIZ_RMIM_VERSION_SUBDETAIL to wd;</code>

<code>grant select on rh_test.BIZ_SYSTEM_LOGIN to wd;</code>

<code>grant select on rh_test.BIZ_TREE_PATH to wd;</code>

<code>grant select on rh_test.CLINIC_TRANSFER to wd;</code>

<code>grant select on rh_test.CODE_SYSTEM_DIC to wd;</code>

<code>grant select on rh_test.ETL_PATIENT_INDEX to wd;</code>

<code>grant select on rh_test.HOSPITAL_DIC to wd;</code>

<code>grant select on rh_test.HOSPITAL_SUBSYSTEM to wd;</code>

<code>grant select on rh_test.MAIL_RECORD to wd;</code>

<code>grant select on rh_test.MEDICAL_RECORD to wd;</code>

<code>grant select on rh_test.PATIENT_INDEX to wd;</code>

<code>grant select on rh_test.RHIP_SYSCONFIG to wd;</code>

<code>grant select on rh_test.SYSTEMLOGIN to wd;</code>

<code>将上面查出的語句執行一下即可。</code>

2、a使用者下授權查詢單個表給test11使用者

<code>select 'GRANT SELECT ON' || table_name || 'to test11;'  from user_tables</code>

<code>得到的結果如下:</code>

<code>GRANT SELECT ON WEBSERVICE_USER to test11</code>

<code>GRANT SELECT ON USERLESS_PATIENT to test11;</code>

<code>再把上面得到的結果逐一執行一遍:</code>

<code>建立的表要想被userA通路,也得執行grant語句:</code>

<code>grant select on 建立的表 to userA;</code>

3、授權a使用者下授權更新單個表給b使用者

grant update on a.tablename to b;

4、授權a使用者下授權删除單個表給b使用者

grant delete on a.tablename to b;

5、擁有dba權限的使用者下授權建立dblink給b使用者

grant create database link to b;

      本文轉自ling118 51CTO部落格,原文連結http://blog.51cto.com/meiling/2062463:,如需轉載請自行聯系原作者