oracle重新定义表,ORACLE在线重定义

本文详细介绍了如何在Oracle数据库中进行表的重建、收集统计信息、创建临时分区表、检查重定义的合理性以及进行在线重定义的过程。通过DBMS_REDEFINITION包,实现了源表到分区表的平滑转换,并在重定义完成后创建索引,最终完成基表和分区表的角色互换。整个过程保证了数据的一致性和完整性。
摘要由CSDN通过智能技术生成

一、建表和索引

SQL> create table redefie_table (

2     id number(10) primary key,

3     create_date date

4  );

Table created.

SQL> insert into redefie_table  select  rownum,created from user_objects;

33 rows created.

SQL> commit;

Commit complete.

SQL> create index idx_redefie_table on redefie_table(create_date);

Index created.

二、收集统计信息

SQL> exec dbms_stats.gather_table_stats('jerry','redefie_table',cascade=>true);

PL/SQL procedure successfully completed.

三、创建临时分区表

SQL> create table partition_redefie_table (id number primary key, time date) partition by range

2  (time)

3  (

4     partition p1 values less than (to_date('2012-7-1','yyyy-mm-dd')),

5     partition p2 values less than (to_date('2013-1-1','yyyy-mm-dd')),

6     partition p3 values less than (to_date('2013-7-1','yyyy-mm-dd')),

7     partition p4 values less than (maxvalue)

8  );

Table created.

四 检查重定义的合理性

SQL> exec dbms_redefinition.can_redef_table('jerry','redefie_table');

PL/SQL procedure successfully completed.

五、开始重定义

5.1 如果分区表和源表列名相同,则可以用如下(本例中的列名不同,所以会报错):

SQL> begin

2  dbms_redefinition.start_redef_table(

3     uname=>'JERRY',

4     orig_table=>'redefie_table',

5     int_table=>'partition_redefie_table'

6  );

7  end;

8

9  /

begin

*

ERROR at line 1:

ORA-42016: shape of interim table does not match specified column mapping

ORA-06512: at "SYS.DBMS_REDEFINITION", line 52

ORA-06512: at "SYS.DBMS_REDEFINITION", line 1646

ORA-06512: at line 2

5.2 如果分区表和源表列名不相同,则用如下(注意列名转换,源表和分区表的对应字段):

SQL> exec dbms_redefinition.start_redef_table('JERRY','redefie_table','partition_redefie_table','id id,create_date time',       dbms_redefinition.cons_use_pk);

PL/SQL procedure successfully completed.

SQL> exec dbms_redefinition.start_redef_table('JERRY','redefie_table','partition_redefie_table','id id,create_date time',       dbms_redefinition.cons_use_pk);

PL/SQL procedure successfully completed.

SQL> select count(*) from partition_redefie_table

2  ;

COUNT(*)

----------

33

SQL> select count(*) from redefie_table;

COUNT(*)

----------

33

5.3同步新表,可选操作

QL> exec dbms_redefinition.sync_interim_table( uname=>'JERRY', orig_table=>'redefie_table',    int_table=>'partition_redefie_table');

PL/SQL procedure successfully completed.

5.4创建索引,在线重定义,只定义数据,索引需要单独建立

SQL> create index idx_partition_redefie_table on partition_redefie_table(time);

Index created.

5.5 结束重定义

SQL> exec dbms_redefinition.finish_redef_table( uname=>'JERRY', orig_table=>'redefie_table',    int_table=>'partition_redefie_table');

PL/SQL procedure successfully completed.

重定义在此处的含义,基表和分区表进行了交换,基表REDEFIE_TABLE变成了分区表,分区表PARTITION_REDEFIE_TABLE变成了基表:

SQL> select T.TABLE_NAME From user_tab_partitions t where t.table_name='REDEFIE_TABLE';

TABLE_NAME

------------------------------------------------------------

REDEFIE_TABLE

REDEFIE_TABLE

REDEFIE_TABLE

REDEFIE_TABLE

SQL> select T.TABLE_NAME From user_tab_partitions t where t.table_name='PARTITION_REDEFIE_TABLE';

no rows selected

六、删除临时表

SQL> drop table partition_redefie_table;

Table dropped.

七、索引重命名

SQL> select t.index_name from user_indexes t ;

INDEX_NAME

------------------------------------------------------------

SYS_C0013125

IDX_PARTITION_REDEFIE_TABLE

SQL> alter index IDX_PARTITION_REDEFIE_TABLE rename to idx_redefie_table;

SQL> select t.index_name from user_indexes t ;

INDEX_NAME

------------------------------------------------------------

IDX_REDEFIE_TABLE

SYS_C0013125

阅读(805) | 评论(0) | 转发(0) |

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值