实战:oracle 分区交换-归档数据

本文详细介绍了在Oracle数据库中进行分区交换以实现数据归档的过程,包括创建分区表、基表,填充数据,然后将基表数据交换到分区表中,以及处理交换后的索引状态。示例涵盖了未分区表与分区表交换和分区表之间的交换操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

交换分区的操作步骤如下:


1. 创建分区表t1,假设有2个分区,P1,P2.
2. 创建基表t11存放P1规则的数据。
3. 创建基表t12 存放P2规则的数据。
4. 用基表t11和分区表T1的P1分区交换。 把表t11的数据放到到P1分区
5. 用基表t12 和分区表T1p2 分区交换。 把表t12的数据存放到P2分区。


----1.未分区表和分区表中一个分区交换


create table t1
(
sid int not null primary key,
sname  varchar2(50)
)
PARTITION BY range(sid)
( PARTITION p1 VALUES LESS THAN (5000) tablespace test,
  PARTITION p2 VALUES LESS THAN (10000) tablespace test,
  PARTITION p3  VALUES LESS THAN (maxvalue) tablespace test
) tablespace test;




SQL> select count(*) from t1;


  COUNT(*)
----------
         0






create table t11
(
sid int not null primary key,
sname  varchar2(50)
) tablespace test;




create table t12
(
sid int not null primary key,
sname  varchar2(50)
) tablespace test;




create table t13
(
sid int not null primary key,
sname  varchar2(50)
) tablespace test;


--循环导入数据
declare
        maxrecords constant int:=4999;
        i int :=1;
    begin
        for i in 1..maxrecords loop
          insert into t11 values(i,'ocpyang');
        end loop;
    dbms_output.put_line(' 成功录入数据! ');
    commit;
    end; 
/




declare
        maxrecords constant int:=9999;
        i int :=5000;
    begin
        for i in 5000..maxrecords loop
          insert into t12 values(i,'ocpyang');
        end loop;
    dbms_output.put_line(' 成功录入数据! ');
    commit;
    end; 
/






declare
        maxrecords constant int:=70000;
        i int :=10000;
    begin
        for i in 10000..maxrecords loop
          insert into t13 values(i,'ocpyang');
        end loop;
    dbms_output.put_line(' 成功录入数据! ');
    commit;
    end; 
/


commit;






SQL> select count(*) from t11;


  COUNT(*)
----------
      4999


SQL> select count(*) from t12;


  COUNT(*)
----------
      5000


SQL> select count(*) from t13;


  COUNT(*)
----------
     60001






--交换分区




alter table t1 exchange partition p1 with table t11;




SQL> select count(*) from t11;   --基表t11数据为0


  COUNT(*)
----------
         0

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值