Oracle 普通表与分区表转换

oracleerp 2012-06-29

Oracle 9i提供了dbms_redefinition包来实现数据库的表的在线重定义功能。在实际的应用上,我们可以利用这个包来进行:
(1)堆表与分区之间进行转换。(2)重建表以减少HWM。10g能shrink,9i如果用move tablespace and rebuild index在move的时候会锁表,如果想实现在线降低HWM,只能用这个了。
(3)在线更改表结构,如更改列的前后顺序,将column_a,column_b改成column_b,column_a
1.创建测试表
create table rebuild_table as select * from dba_objects;
insert into rebuild_table select * from rebuild_table;
insert into rebuild_table select * from rebuild_table;
insert into rebuild_table select * from rebuild_table;
commit;
delete from rebuild_table;
insert into rebuild_table select * from dba_objects;
insert into rebuild_table select * from rebuild_table;
insert into rebuild_table select * from rebuild_table;
commit;
update rebuild_table set object_id=rownum;
alter  table rebuild_table add CONSTRAINT P_YY PRIMARY KEY (OBJECT_ID);
select * from rebuild_table;
2.创建分区表
create table REBUILD_PA_TABLE
(
  STATIS_DATE    NUMBER(8),
  OWNER          VARCHAR2(30),
  OBJECT_NAME    VARCHAR2(128),
  SUBOBJECT_NAME VARCHAR2(30),
  OBJECT_ID      NUMBER not null,
  DATA_OBJECT_ID NUMBER,
  OBJECT_TYPE    VARCHAR2(18),
  CREATED        DATE,
  LAST_DDL_TIME  DATE,
  TIMESTAMP      VARCHAR2(19),
  STATUS         VARCHAR2(7),
  TEMPORARY      VARCHAR2(1),
  GENERATED      VARCHAR2(1),
  SECONDARY      VARCHAR2(1)
)
partition by  list (STATIS_DATE)
(
partition p20111031 values (20111031)
);
alter table REBUILD_PA_TABLE add partition p20111101 values(20111101);
;
alter table REBUILD_PA_TABLE
  add constraint Pa_YYY primary key (OBJECT_ID)
;
-- 打开重定义
begin
dbms_redefinition.CAN_REDEF_TABLE('boc_rdm','REBUILD_TABLE');
end;
-- 开始重定义
begin
dbms_redefinition.START_REDEF_TABLE('boc_rdm','REBUILD_TABLE','REBUILD_PA_TABLE');
end;
-- 进行数据同步
begin
dbms_redefinition.sync_interim_table('BOC_RDM','REBUILD_TABLE','REBUILD_PA_TABLE');
end;
-- 重命名表,把非分区表转化为分区表
begin
dbms_redefinition.finish_redef_table('BOC_RDM','REBUILD_TABLE','REBUILD_PA_TABLE');
end;
select * from REBUILD_TABLE partition(p20111031)

相关推荐