您好,登錄后才能下訂單哦!
這篇文章主要介紹“Oracle12.2怎么修改表的列名”,在日常操作中,相信很多人在Oracle12.2怎么修改表的列名問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Oracle12.2怎么修改表的列名”的疑惑有所幫助!接下來,請跟著小編一起來學習吧!
下面的例子將演示聯機重定義使用VPD策略的表,并修改表中的一個列名,原始表jy.orders的創建語句如下:
SQL> create table jy.orders( 2 order_id number(12) primary key, 3 order_date timestamp with local time zone constraint order_date_nn not null, 4 order_mode varchar2(8), 5 customer_id number(6) constraint order_customer_id_nn not null, 6 order_status number(2), 7 order_total number(8,2), 8 sales_rep_id number(6), 9 promotion_id number(6), 10 constraint order_mode_lov 11 check (order_mode in ('direct','online')), 12 constraint order_total_min 13 check (order_total >= 0)); Table created.
創建下面的jy.auth_orders函數來創建VPD策略
SQL> create or replace function jy.auth_orders( 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 := 'sales_rep_id = 159'; 15 end if; 16 return return_val; 17 end auth_orders; 18 / Function created.
下面執行dbms_rls.add_policy過程來使用jy.auth_orders函數來給原始表jy.orders指定VPD策略:
SQL> begin 2 dbms_rls.add_policy( 3 object_schema => 'jy', 4 object_name => 'orders', 5 policy_name => 'orders_policy', 6 function_schema => 'jy', 7 policy_function => 'auth_orders'); 8 end; 9 / PL/SQL procedure successfully completed.
在這個例子中,被重定義后表中的sales_rep_id列被修改為sale_pid。當在執行重定義過程如可修改一個或多個列或列的數據類型,那么在start_refef_table過程中對于copy_vpd_opt參數必須指定dbms_redefinition.cons_vpd_manual。
1.用要執行聯機重定義操作的用戶登錄數據庫
SQL> conn jy/jy@jypdb Connected.
2.驗證原始表是否可以執行聯機重定義
SQL> begin 2 dbms_redefinition.can_redef_table( 3 uname => 'jy', 4 tname => 'orders', 5 options_flag => DBMS_REDEFINITION.CONS_USE_PK); 6 end; 7 / PL/SQL procedure successfully completed.
3.創建中間表
SQL> create table jy.int_orders( 2 order_id number(12), 3 order_date timestamp with local time zone, 4 order_mode varchar2(8), 5 customer_id number(6), 6 order_status number(2), 7 order_total number(8,2), 8 sales_pid number(6), 9 promotion_id number(6)); Table created.
注意,在中間表中sales_rep_id列被修改為sales_pid。
4.開始聯機重定義操作
SQL> begin 2 dbms_redefinition.start_redef_table ( 3 uname => 'jy', 4 orig_table => 'orders', 5 int_table => 'int_orders', 6 col_mapping => 'order_id order_id, order_date order_date, order_mode 7 order_mode, customer_id customer_id, order_status 8 order_status, order_total order_total, sales_rep_id 9 sales_pid, promotion_id promotion_id', 10 options_flag => dbms_redefinition.cons_use_pk, 11 orderby_cols => null, 12 part_name => null, 13 copy_vpd_opt => dbms_redefinition.cons_vpd_manual); 14 end; 15 / PL/SQL procedure successfully completed.
因為原始表與中間表存在不同的列名,那么copy_vpd_opt參數必須設置為dbms_redefinition.cons_vpd_manual。
5.對中間表創建VPD策略
5.1創建一個名為jy.auth_orders_sales_pid的函數來創建VPD策略,這里使用sales_pid列來代替sales_rep_id列。
SQL> create or replace function jy.auth_orders_sales_pid( 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 := 'sales_pid = 159'; 15 end if; 16 return return_val; 17 end auth_orders_sales_pid; 18 / Function created.
5.2執行dbms_rls.add_policy過程來使用jy.auth_orders_sales_pid函數來為中間表增加VPD策略
SQL> begin 2 dbms_rls.add_policy ( 3 object_schema => 'jy', 4 object_name => 'int_orders', 5 policy_name => 'orders_policy', 6 function_schema => 'jy', 7 policy_function => 'auth_orders_sales_pid'); 8 end; 9 / PL/SQL procedure successfully completed.
6.復制依賴對象
SQL> declare 2 num_errors pls_integer; 3 begin 4 dbms_redefinition.copy_table_dependents( 5 uname => 'jy', 6 orig_table => 'orders', 7 int_table => 'int_orders', 8 copy_indexes => dbms_redefinition.cons_orig_params, 9 copy_triggers => true, 10 copy_constraints => true, 11 copy_privileges => true, 12 ignore_errors => true, 13 num_errors => num_errors); 14 end; 15 / PL/SQL procedure successfully completed.
注意在這里ignore_errors參數被設置為true。原因是因為原始表對于列sales_rep_id存在索引與約束,并且在中間表中列被修改為sales_pid。
7.查詢dba_redefinition_errors視圖來檢查是否存在錯誤
SQL> set long 8000 SQL> set pages 8000 SQL> column object_name heading 'object name' format a20 SQL> column base_table_name heading 'base table name' format a10 SQL> column ddl_txt heading 'ddl that caused error' format a40 SQL> select object_name, base_table_name, ddl_txt from dba_redefinition_errors; no rows selected
8.可選操作同中間表
SQL> begin 2 dbms_redefinition.sync_interim_table( 3 uname => 'jy', 4 orig_table => 'orders', 5 int_table => 'int_orders'); 6 end; 7 / PL/SQL procedure successfully completed.
9.完成聯機重定義操作
SQL> begin 2 dbms_redefinition.finish_redef_table( 3 uname => 'jy', 4 orig_table => 'orders', 5 int_table => 'int_orders'); 6 end; 7 / PL/SQL procedure successfully completed. SQL> desc jy.orders Name Type Nullable Default Comments ------------ --------------------------------- -------- ------- -------- ORDER_ID NUMBER(12) ORDER_DATE TIMESTAMP(6) WITH LOCAL TIME ZONE ORDER_MODE VARCHAR2(8) Y CUSTOMER_ID NUMBER(6) ORDER_STATUS NUMBER(2) Y ORDER_TOTAL NUMBER(8,2) Y SALES_PID NUMBER(6) Y PROMOTION_ID NUMBER(6) Y
10.等待任何查詢中間表的語句執行完成后將其刪除
SQL> drop table jy.int_orders; Table dropped
到此重定義操作就完成了。
到此,關于“Oracle12.2怎么修改表的列名”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注億速云網站,小編會繼續努力為大家帶來更多實用的文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。