我們有這樣一個需求:在資料庫中建立兩個使用者,使用者A 用於建立一些視圖,直接存取自己資料庫中一個模式下的表,以及通過資料庫鏈路訪問其他資料庫中的表;另一個使用者B 能訪問到這個使用者A 中的視圖,並且只能訪問視圖,且訪問串連數有限制。
這個使用者B 是用來給其他系統訪問的,因此對許可權和資源使用都需要有限制條件。
這種需求在很多行業的應用中都很常見。假如這是一道面試題,您該如何去回答呢?
我採用下面的方法來回答這個問題。
第一步,建立新使用者A 和B 。
這裡使用者名稱稱分別為ryd_interface_src 和ryd_interface ,對這兩個使用者都授予非常有限的許可權。
drop user ryd_interface_src cascade;
create user ryd_interface_src identified by ryd_interface_src;
grant connect,create view to ryd_interface_src;
drop user ryd_interface cascade;
create user ryd_interface identified by ryd_interface;
grant connect,create synonym to ryd_interface;
第二步,登入資料庫一個模式中,授權給使用者A ,使得使用者A 能建立視圖
conn qlzqclient/qlzqclient
grant select on INVEST_CLOCK to ryd_interface_src with grant option;
grant select on INVEST_LOG to ryd_interface_src with grant option;
這裡授權方法加了一個with grant option ,請注意。
第三步,登入資料庫使用者A 中,建立視圖
conn ryd_interface_src/ryd_interface_src
create or replace view run_views as
select id as doc_id, title,fbsj as upload_date,'' as branch_code from qlzq.runs_lantern@CLIENT_QLZQWEB
where EXT1='1' and sysdate > START_TIME
and sysdate < SOLID_TIME
union
select a.doc_id,a.title,a.upload_date,a.branch_code from qlzq.cms_doc_single_attr@CLIENT_QLZQWEB a
left join qlzq.cms_doc_category_map@CLIENT_QLZQWEB b on a.doc_id=b.doc_id
where
a.state =1 and a.is_delete =0
and a.upload_date> sysdate-90
and b.cat_id=4;
create or replace view INVEST_CLOCK_VIEWS as
select * from qlzqclient.INVEST_CLOCK;
create or replace view INVEST_LOG_VIEWS as
select * from qlzqclient.INVEST_LOG;