1.首先建立测试表,并插入测试数据:

SQL> conn stream/stream
Connected.
SQL> create table t_test(id number,code varchar2(5),identifier varchar2(20));
Table created.
SQL> insert into t_test values(1,'01','01-01-0001-000001');  
1 row created.
SQL> insert into t_test values(2,'02','02-01-0001-000001');
1 row created.
SQL> insert into t_test values(3,'03','03-01-0001-000001');
1 row created.
SQL> insert into t_test values(4,'04','04-01-0001-000001');
1 row created.
SQL> commit;
Commit complete.
SQL> alter table t_test add constraint pk_test_id primary key (id);
Table altered

  2.检查下这张表是否可以在线重定义,无报错表示可以,报错会给出错误信息:

SQL> exec dbms_redefinition.can_redef_table('stream', 't_test');
PL/SQL procedure successfully completed

  3.建立在线重定义需要的中间表,表结构就是要将原测试表重定义成什么样子,这里建立的是按全宗号分区的分区表:

SQL> create table t_temp(id number,code varchar2(5),
  2  identifier varchar2(20)) partition by list (code)
  3  (partition part01 values('01'),
  4  partition part02 values('02'),
  5  partition part03 values('03'),
  6  partition part04 values('04'),
  7  partition part05 values(default)
  8  );
Table created
SQL> alter table t_temp add constraint pk_temp_id primary key (id);
Table altered

  4.启动在线重定义:

SQL> exec dbms_redefinition.start_redef_table('stream', 't_test', 't_temp');
PL/SQL procedure successfully completed

  这里dbms_redefinition包的start_redef_table模块有3个参数,分别是SCHEMA名字、原表的名字、中间表的名字。
5.启动在线重定义后,中间表就可以查到原表的数据。

SQL> select * from t_temp;
        ID CODE  IDENTIFIER
---------- ----- --------------------
         1 01    01-01-0001-000001
         2 02    02-01-0001-000001
         3 03    03-01-0001-000001
         4 04    04-01-0001-000001

  6.由于在生成系统中,在线重定义的过程中原数据表可能会发生数据改变,向原表中插入数据模拟数据改变。

SQL> insert into t_test values(5,'05','05-01-0001-000001');
1 row inserted
SQL> commit;
Commit complete

  7.此时原表被修改,中间表并没有更新。

SQL> select * from t_test;
        ID CODE  IDENTIFIER
---------- ----- --------------------
         5 05    05-01-0001-000001
         1 01    01-01-0001-000001
         2 02    02-01-0001-000001
         3 03    03-01-0001-000001
         4 04    04-01-0001-000001
SQL>  select * from t_temp;
        ID CODE  IDENTIFIER
---------- ----- --------------------
         1 01    01-01-0001-000001
         2 02    02-01-0001-000001
         3 03    03-01-0001-000001
         4 04    04-01-0001-000001

  8.使用dbms_redefinition包的sync_interim_table模块刷新数据后,中间表也可以看到数据更改。

SQL> exec dbms_redefinition.sync_interim_table('stream', 't_test', 't_temp');
PL/SQL procedure successfully completed
SQL> select * from t_test;
        ID CODE  IDENTIFIER
---------- ----- --------------------
         5 05    05-01-0001-000001
         1 01    01-01-0001-000001
         2 02    02-01-0001-000001
         3 03    03-01-0001-000001
         4 04    04-01-0001-000001
SQL> select * from t_temp;
        ID CODE  IDENTIFIER
---------- ----- --------------------
         1 01    01-01-0001-000001
         2 02    02-01-0001-000001
         3 03    03-01-0001-000001
         4 04    04-01-0001-000001
         5 05    05-01-0001-000001

  9.结束在线重定义

SQL> exec dbms_redefinition.finish_redef_table('stream', 't_test', 't_temp');
PL/SQL procedure successfully completed

  10.验证数据

SQL> select * from t_test;
        ID CODE  IDENTIFIER
---------- ----- --------------------
         1 01    01-01-0001-000001
         2 02    02-01-0001-000001
         3 03    03-01-0001-000001
         4 04    04-01-0001-000001
         5 05    05-01-0001-000001
SQL> select * from t_temp;
        ID CODE  IDENTIFIER
---------- ----- --------------------
         5 05    05-01-0001-000001
         1 01    01-01-0001-000001
         2 02    02-01-0001-000001
         3 03    03-01-0001-000001
         4 04    04-01-0001-000001

  11.查看各分区数据是否正确

SQL> select table_name, partition_name from user_tab_partitions where 
table_name = 'T_TEST';
TABLE_NAME                     PARTITION_NAME
------------------------------ ------------------------------
T_TEST                         PART01
T_TEST                         PART02
T_TEST                         PART03
T_TEST                         PART04
T_TEST                         PART05
SQL> select * from t_test partition(part01);
        ID CODE  IDENTIFIER
---------- ----- --------------------
         1 01    01-01-0001-000001
SQL> select * from t_test partition(part02);
        ID CODE  IDENTIFIER
---------- ----- --------------------
         2 02    02-01-0001-000001
SQL> select * from t_test partition(part03);
        ID CODE  IDENTIFIER
---------- ----- --------------------
         3 03    03-01-0001-000001
SQL> select * from t_test partition(part04);
        ID CODE  IDENTIFIER
---------- ----- --------------------
         4 04    04-01-0001-000001
SQL> select * from t_test partition(part05);
        ID CODE  IDENTIFIER
---------- ----- --------------------
         5 05    05-01-0001-000001

  12.在线重定义后,中间表已经没有意义,删掉中间表

SQL> drop table t_temp purge;
Table dropped