- 最后登录
- 2023-8-16
- 在线时间
- 1686 小时
- 威望
- 2135
- 金钱
- 50532
- 注册时间
- 2011-10-12
- 阅读权限
- 200
- 帖子
- 5207
- 精华
- 39
- 积分
- 2135
- UID
- 2
|
3#
发表于 2012-4-20 14:15:01
FOR EXAMPLE:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bit Production
With the Partitioning, OLAP and Data Mining options
SQL> conn system/oracle
Connected.
SQL> CREATE USER vpd IDENTIFIED BY oracle
DEFAULT TABLESPACE users
2 3 TEMPORARY TABLESPACE temp;
GRANT CONNECT, RESOURCE TO vpd;
-- Included to enable the use of autotrace
GRANT select_catalog_role TO vpd;
User created.
SQL>
SQL>
Grant succeeded.
SQL> SQL> SQL> -- Included to enable the use of autotrace
Grant succeeded.
SQL> SQL> SQL> GRANT SELECT ANY dictionary TO vpd;
Grant succeeded.
SQL>
SQL> -- Included to enable the creation of policies
SQL> GRANT execute on DBMS_RLS to vpd;
Grant succeeded.
SQL> SQL> -- Included to enable the creation of application contexts
SQL> GRANT CREATE ANY CONTEXT to vpd;
Grant succeeded.
SQL> CREATE TABLE "VPD"."CUSTOMER"
2 (
3 "CUST_NUM" VARCHAR2(50 BYTE) NOT NULL ENABLE,
4 "CUST_NAME" VARCHAR2(50 BYTE),
5 "CUST_ADDRESS1" VARCHAR2(50 BYTE),
6 "CUST_ADDRESS2" VARCHAR2(50 BYTE),
7 "CUST_CITY" VARCHAR2(50 BYTE),
8 "CUST_PCODE_ZIP" VARCHAR2(20 BYTE),
9 "CUST_STATE" VARCHAR2(50 BYTE),
10 "CUST_COUNTRY" VARCHAR2(50 BYTE),
11 "REGION" VARCHAR2(50 BYTE),
12 CONSTRAINT "TABLE1_PK" PRIMARY KEY ("CUST_NUM") USING INDEX)
13 PCTFREE 10 INITRANS 2 MAXTRANS 255
14 TABLESPACE "USERS" ;
Table created.
SQL> SQL> CREATE TABLE "VPD"."SALESPERSON"
2 (
3 "SALESPERSON_NUM" VARCHAR2(20 BYTE) NOT NULL ENABLE,
4 "FNAME" VARCHAR2(50 BYTE),
5 "LNAME" VARCHAR2(50 BYTE),
6 "USERNAME" VARCHAR2(20 BYTE),
7 "EMAIL" VARCHAR2(50 BYTE),
8 "MANAGER_USERNAME" VARCHAR2(20 BYTE),
9 "HIRE_DATE" DATE,
10 "REGION" VARCHAR2(50 BYTE),
11 CONSTRAINT "SALESPERSON_PK" PRIMARY KEY ("SALESPERSON_NUM") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS TABLESPACE "USERS" ENABLE
12 )
13 PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
14 TABLESPACE "USERS" ;
Table created.
SQL> SQL>
SQL>
SQL> CREATE TABLE "VPD"."SALES"
2 (
3 "CUST_NUM" VARCHAR2(20 BYTE) NOT NULL ENABLE,
4 "SALESPERSON_NUM" VARCHAR2(20 BYTE) NOT NULL ENABLE,
5 "SALES_NUM" NUMBER NOT NULL ENABLE,
6 "LINE_NUM" NUMBER NOT NULL ENABLE,
7 "QUANTITY" NUMBER,
8 "COST" NUMBER,
9 "TOTAL" NUMBER,
10 "PRODUCT" VARCHAR2(50 BYTE),
11 "SALES_DATE" DATE,
CONSTRAINT "SALES_PK" PRIMARY KEY ("SALES_NUM", "LINE_NUM") USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 TABLESPACE "USERS" ENABLE,
12 13 CONSTRAINT "CUST_SALES_FK" FOREIGN KEY ("CUST_NUM") REFERENCES "VPD"."CUSTOMER" ("CUST_NUM") ENABLE,
CONSTRAINT "SALESPERSON_SALES_FK" FOREIGN KEY ("SALESPERSON_NUM") REFERENCES "VPD"."SALESPERSON" ("SALESPERSON_NUM") ENABLE
)
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING TABLESPACE "USERS" ;
14 15 16
Table created.
conn vpd/vpd
SQL> CREATE OR REPLACE
2 FUNCTION CUST_REGION_SEC
3 ( p_schema IN VARCHAR2 DEFAULT NULL, P_object IN VARCHAR2 DEFAULT NULL)
4 RETURN VARCHAR2
5 IS
6 BEGIN
7
8 RETURN ' REGION = (SELECT region FROM salesperson WHERE username = user)';
9
10 EXCEPTION
11 WHEN OTHERS
12 THEN RETURN ('1=2');
13 END;
14 /
Function created.
conn system/oracle
SQL> BEGIN
2 DBMS_RLS.ADD_POLICY (object_schema => 'VPD', object_name =>
3 'CUSTOMER', policy_name => 'CUST_REGION_POL', function_schema
4 => 'VPD', policy_function => 'CUST_REGION_SEC');
5 END;
6 /
PL/SQL procedure successfully completed.
SQL> conn vpd/vpd
Connected.
SQL> set autotrace on;
SQL> set linesize 200 pagesize 1400
SQL> select * from customer;
no rows selected
Execution Plan
----------------------------------------------------------
Plan hash value: 242466158
----------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 228 | 4 (0)| 00:00:01 |
|* 1 | TABLE ACCESS FULL | CUSTOMER | 1 | 228 | 2 (0)| 00:00:01 |
|* 2 | TABLE ACCESS FULL| SALESPERSON | 1 | 39 | 2 (0)| 00:00:01 |
----------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - filter("REGION"= (SELECT /*+ */ "REGION" FROM VPD."SALESPERSON"
"SALESPERSON" WHERE "USERNAME"=USER@!))
2 - filter("USERNAME"=USER@!)
Note
-----
- dynamic sampling used for this statement
Statistics
----------------------------------------------------------
321 recursive calls
0 db block gets
78 consistent gets
0 physical reads
0 redo size
875 bytes sent via SQL*Net to client
458 bytes received via SQL*Net from client
1 SQL*Net roundtrips to/from client
12 sorts (memory)
0 sorts (disk)
0 rows processed |
|