create table jy.t1(c1 number); Table created SQL> create in..."/>
溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Oracle 12.2使用手動創(chuàng)建與注冊依賴對象來執(zhí)行聯(lián)機重定義

發(fā)布時間:2020-08-09 03:55:18 來源:ITPUB博客 閱讀:151 作者:eric0435 欄目:關系型數(shù)據(jù)庫

下面的例子將使用手動創(chuàng)建與注冊依賴對象的方法來執(zhí)行聯(lián)機重定義操作,原始表創(chuàng)建如下:

SQL> create table jy.t1(c1 number);
Table created

SQL> create index jy.t1_idx_1 on jy.t1(c1);
Index created

假設在聯(lián)機重定義之后列c1變?yōu)榱薱2。在這種情況下,使用copy_table_dependents過程試圖對中間表的c1列創(chuàng)建索引t1_idx,因為不存在列c1就會出現(xiàn)錯誤。因此必須在列c2上創(chuàng)建索引然后進行注冊。

聯(lián)機重定義操作如下:
1.用要執(zhí)行聯(lián)機重定義操作的用戶登錄數(shù)據(jù)庫

SQL> conn jy/jy@jypdb
Connected.

2.驗證原始表t1是否可以執(zhí)行聯(lián)機重定義操作

SQL> begin
  2  dbms_redefinition.can_redef_table(
  3    uname => 'jy',
  4    tname => 't1',
  5    options_flag => DBMS_REDEFINITION.CONS_USE_ROWID);
  6  end;
  7  /

PL/SQL procedure successfully completed.

3.手動創(chuàng)建中間表jy.int_t1并且在列c2上創(chuàng)建索引jy.int_t1_idx_1

SQL> create table jy.int_t1(c2 number);

Table created.

SQL> create index jy.int_t1_idx_1 on jy.int_t1(c2);

Index created.

4.開始執(zhí)行聯(lián)機重定義操作

SQL> begin
  2  dbms_redefinition.start_redef_table(
  3    uname => 'jy',
  4    orig_table => 't1',
  5    int_table => 'int_t1',
  6    col_mapping => 'c1 c2',
  7    options_flag => DBMS_REDEFINITION.CONS_USE_ROWID);
  8  end;
  9  /

PL/SQL procedure successfully completed.

5.注冊原始(索引t1_idx_1)與中間(int_t1_idx_1)依賴對象

SQL> begin
  2  dbms_redefinition.register_dependent_object(
  3    uname => 'jy',
  4    orig_table => 't1',
  5    int_table => 'int_t1',
  6    dep_type => DBMS_REDEFINITION.CONS_INDEX,
  7    dep_owner => 'jy',
  8    dep_orig_name => 't1_idx_1',
  9    dep_int_name => 'int_t1_idx_1');
 10  end;
 11  /

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 => 't1',
  7    int_table => 'int_t1',
  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.

7.可選操作同步中間表

SQL> begin
  2  dbms_redefinition.sync_interim_table(
  3    uname => 'jy',
  4    orig_table => 't1',
  5    int_table => 'int_t1');
  6  end;
  7  /

PL/SQL procedure successfully completed.

8.完成聯(lián)機重定義操作

SQL> begin
  2  dbms_redefinition.finish_redef_table(
  3    uname => 'jy',
  4    orig_table => 't1',
  5    int_table => 'int_t1');
  6  end;
  7  /

PL/SQL procedure successfully completed.
SQL> select dbms_metadata.get_ddl(object_type =>'TABLE',name =>'T1',schema => 'JY') from dual;

DBMS_METADATA.GET_DDL(OBJECT_TYPE=>'TABLE',NAME=>'T1',SCHEMA=>'JY')
--------------------------------------------------------------------------------

  CREATE TABLE "JY"."T1"
   (    "C2" NUMBER
   ) SEGMENT CREATION DEFERRED
  PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255
 NOCOMPRESS LOGGING
  TABLESPACE "TEST"


1 row selected.

可以看到表jy.t1已經(jīng)成功能聯(lián)機重定義

9.等待任何查詢中間表的語句執(zhí)行完成后將其刪除

SQL> desc jy.t1
Name Type   Nullable Default Comments
---- ------ -------- ------- --------
C2   NUMBER Y

SQL> drop table jy.t1 purge;
Table dropped

到此重定義操作就完成了。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。

AI