Oracle 普通表與分區錶轉換

來源:互聯網
上載者:User

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)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.