改變表的欄位順序dbms_REDEFINITION

zhaoqing0803發表於2013-03-19

    官方解釋:The DBMS_REDEFINITION package provides an interface to perform. an online redefinition of tables.To achieve online redefinition, incrementally maintainable local materialized views are used. These logs keep track of the changes to the master tables and are used by the materialized views during refresh synchronization.

   舉例說明:

    1、建立一張臨時表

      create table tmp_tab1
(
id int,
name varchar2(30)
);

alter table tmp_tab1 add constraint pk_id primary key (id);

insert into tmp_tab1
values(1,'aa');

insert into tmp_tab1
values(2,'bb');
commit;

   2、看錶能否重新定義:

BEGIN
          DBMS_REDEFINITION.CAN_REDEF_TABLE('test_dba','tmp_tab1',DBMS_REDEFINITION.CONS_USE_PK);
END;
/

    3:建立符合條件的中間表

drop table int_tmp_tab1;
create table int_tmp_tab1
as
select ID,
cast(0 as int) name1,
NAME
 from tmp_tab1;

4:開始定義原表
BEGIN
  DBMS_REDEFINITION.START_REDEF_TABLE('test_dba', 'tmp_tab1','int_tmp_tab1');
END;
/

5:對中間表規劃好,建立原表的索引約束

alter table int_tmp_tab1 add constraint pk_id1 primary key (id);

6:進行同步表定義
begin
   dbms_redefinition.sync_interim_table('test_dba','tmp_tab1','int_tmp_tab1');
end; 

7:進行完成原表的修改
begin
  dbms_redefinition.finish_redef_table(  'test_dba',
                                         'tmp_tab1',
                                       'int_tmp_tab1');
end;

8:刪除中間表
drop table   int_tmp_tab1 ;

 

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23205405/viewspace-756545/,如需轉載,請註明出處,否則將追究法律責任。

相關文章