所谓虚拟专用数据库(VPD)指的是,通过在数据库里进行配置,从而让不同的用户只能查看某 个表里的部份数据。VPD分为以下两个级别。
Select * from sales_list ;
Select * from sales_list where seller_id='S0020';
Select * from sales_list where values>1000 ;
Select * from sales_list where qty_sold>1000 and seller_id='S0020';
SQL> select sys_context('userenv','ip_address') "IP",
sys_context('userenv','db_name') "DB" from dual;
IP DB
--------------- ---------
152.68.32.60 ora10g
SQL> create or replace context sales_ctx using oe.sales_app_pkg;
SQL> drop context sales _ctx;
dbms_session.set_context ('context_name', 'attribute_name', 'attribute_value')
SQL> show user
USER is "SYS"
SQL> exec dbms_session.set_context('sales_ctx','seller_id','S0020');
BEGIN dbms_session.set_context('sales_ctx','seller_id','S0020'); END;
*
ERROR at line 1:
ORA-01031: insufficient privileges
ORA-06512: at "SYS.DBMS_SESSION", line 90
ORA-06512: at line 1
SQL> connect oe/oe
SQL> create or replace package sales_app_pkg is
2 procedure set_sales_context;
3 end;
4 /
SQL> create or replace package body sales_app_pkg is
2 procedure set_sales_context is
3 begin
4 dbms_session.set_context('sales_ctx','seller_id',user);
5 end;
6 end;
7 /
SQL> grant select on sales_list to public;
SQL> grant update on sales_list to public;
SQL> grant execute on sales_app_pkg to public;
SQL> connect hr/hr
SQL> exec oe.sales_app_pkg.set_sales_context;
SQL> select sys_context('sales_ctx','seller_id') from dual;
SYS_CONTEXT('SALES_CTX','SELLER_ID')
--------------------------------------------------------------------------------
HR
SQL> create or replace package sales_app_pkg is
2 procedure set_sales_context;
3 function where_condition
4 (p_schema_name varchar2,p_tab_name varchar2)
5 return varchar2;
6 end;
7 /
SQL> create or replace package body sales_app_pkg is
2 procedure set_sales_context is
3 v_user varchar2(30);
4 begin
5 dbms_session.set_context('sales_ctx','seller_id',user);
6 end;
7
8 function where_condition
9 (p_schema_name varchar2,p_tab_name varchar2) return varchar2 is
10 v_seller_id varchar2(100) := upper(sys_context('sales_ctx','seller_id'));
11 v_where_condition varchar2(2000);
12 begin
13 if v_seller_id like 'S%' then
14 v_where_condition := 'seller_id = ' || '''' || v_seller_id || '''';
15 else
16 v_where_condition := null;
17 end if;