共計 3684 個字符,預計需要花費 10 分鐘才能閱讀完成。
這篇文章主要為大家展示了“Oracle 12.2 中如何聯機重定義使用 VPD 策略的表”,內容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓丸趣 TV 小編帶領大家一起研究并學習一下“Oracle 12.2 中如何聯機重定義使用 VPD 策略的表”這篇文章吧。
原始表 jy.employees 的創建語句如下:
SQL create table jy.employees( 2 employee_id number(6) primary key,
3 first_name varchar2(20),
4 last_name varchar2(25)
5 constraint emp_last_name_nn not null,
6 email varchar2(25) constraint emp_email_nn not null,
7 phone_number varchar2(20),
8 hire_date date constraint emp_hire_date_nn not null,
9 job_id varchar2(10) constraint emp_job_nn not null,
10 salary number(8,2),
11 commission_pct number(2,2),
12 manager_id number(6),
13 department_id number(4),
14 constraint emp_salary_min check (salary 0),
15 constraint emp_email_uk unique (email)
16 );
Table created.
使用下面的 jy.auth_emp_dep_100 函數來創建 VPD 策略
SQL create or replace function jy.auth_emp_dep_100(
2 schema_var in varchar2,
3 table_var in varchar2
4 )
5 return varchar2
6 as
7 return_val varchar2 (400);
8 unm varchar2(30);
9 begin
10 select user into unm from dual;
11 if (unm = jy) then
12 return_val := null;
13 else
14 return_val := department_id = 100
15 end if;
16 return return_val;
17 end auth_emp_dep_100;
18 /
Function created.
執行 dbms_rls_add_policy 過程來對原始表 jy.employees 表使用 jy.auth_emp_dep_100 函數來指定 VPD 策略
SQL begin
2 dbms_rls.add_policy(
3 object_schema = jy ,
4 object_name = employees ,
5 policy_name = employees_policy ,
6 function_schema = jy ,
7 policy_function = auth_emp_dep_100
8 end;
9 /
PL/SQL procedure successfully completed.
在這個例子中,表 jy.employees 表重定義后將會禁用所有的觸發器。注意重定義將不會修改列名或數據類型。因此在執行 start_refef_table 過程時 copy_vpd_opt 參數設置為 dbms_redefinition.cons_vpd_auto。
1. 用要執行聯機重定義操作的用戶登錄數據庫
SQL conn jy/jy@jypdb
Connected.
2. 驗證原始表是否可以執行聯機重定義
SQL begin
2 dbms_redefinition.can_redef_table(hr , employees ,DBMS_REDEFINITION.CONS_USE_PK);
3 end;
4 /
PL/SQL procedure successfully completed.
3. 創建中間表 jy.int_employees
SQL create table jy.int_employees( 2 employee_id number(6),
3 first_name varchar2(20),
4 last_name varchar2(25),
5 email varchar2(25),
6 phone_number varchar2(20),
7 hire_date date,
8 job_id varchar2(10),
9 salary number(8,2),
10 commission_pct number(2,2),
11 manager_id number(6),
12 department_id number(4));
Table created.
4. 開始聯機重定義操作
SQL begin
2 dbms_redefinition.start_redef_table(
3 uname = jy ,
4 orig_table = employees ,
5 int_table = int_employees ,
6 col_mapping = NULL,
7 options_flag = DBMS_REDEFINITION.CONS_USE_PK,
8 orderby_cols = NULL,
9 part_name = NULL,
10 copy_vpd_opt = DBMS_REDEFINITION.CONS_VPD_AUTO);
11 end;
12 /
PL/SQL procedure successfully completed.
當 copy_vpd_opt 參數被設置為 dbms_redefinition.cons_vpd_auto 時,只有表的所有者與調用聯機重定義操作的用戶可以在聯機重定義期間訪問該表。col_mapping 參數設置為 NULL。當 copy_vpd_opt 參數設置為 dbms_redefinition.cons_vpd_auto 時,col_mapping 參數必須設置為 NULL 或 *。
5. 復制依賴對象
SQL declare
2 num_errors pls_integer;
3 begin
4 dbms_redefinition.copy_table_dependents(
5 uname = jy ,
6 orig_table = employees ,
7 int_table = int_employees ,
8 copy_indexes = DBMS_REDEFINITION.CONS_ORIG_PARAMS,
9 copy_triggers = TRUE,
10 copy_constraints = TRUE,
11 copy_privileges = TRUE,
12 ignore_errors = FALSE,
13 num_errors = num_errors);
14 end;
15 /
PL/SQL procedure successfully completed.
6. 對中間表禁用所有的觸發器
SQL alter table jy.int_employees disable all triggers;
Table altered.
7. 可選操作同步中間表
SQL begin
2 dbms_redefinition.sync_interim_table(
3 uname = jy ,
4 orig_table = employees ,
5 int_table = int_employees
6 end;
7 /
PL/SQL procedure successfully completed.
8. 完成聯機重定義操作
SQL begin
2 dbms_redefinition.finish_redef_table(
3 uname = jy ,
4 orig_table = employees ,
5 int_table = int_employees
6 end;
7 /
PL/SQL procedure successfully completed.
9. 等待任何查詢中間表的語句執行完成后將其刪除
SQL drop table jy.int_employees;
Table dropped
到此重定義操作就完成了。
以上是“Oracle 12.2 中如何聯機重定義使用 VPD 策略的表”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注丸趣 TV 行業資訊頻道!