更改表的owner

exchange partition的方法是最安全,也是最快速的方法。

所以这里写出这种方法供大家参考。
假设有A,B两个用户,我们想把TEST表从A用户移到B用户。
我们以非分区表作为例子:
    SQL> conn a/a
    Connected.
    SQL> create table test(x int);
    Table created.
    SQL> create index test_idx on test(x);
    Index created.
    SQL> insert into test select rownum from dual connect by level <10000;
    9999 rows created.
    SQL> commit;
    Commit complete.


    SQL> conn b/b
    Connected.
    SQL> create table temp(x int) partition by range (x)
    2 (partition part0 values less than (-1),
    3 partition part1 values less than (maxvalue));
    Table created.
    SQL> create table test(x int);
    Table created.
    SQL> create index temp_idx on temp(x) local;
    Index created.
    SQL> create index test_idx on test(x);
    Index created.
    SQL> alter table temp exchange partition part1 with table a.test including indexes without validation;
    Table altered.
    SQL> alter table temp exchange partition part1 with table test including indexes without validation;
    Table altered.
    SQL> select count(*) from a.test;
    COUNT(*)
    ———-
    0
    SQL> select count(*) from b.test;
    COUNT(*)
    ———-
    9999


如果是分区表,操作过程如下:
    SQL> conn a/a
    Connected.
    SQL> create table test(x int) partition by range (x)
    2 (partition part0 values less than (100),
    3 partition part1 values less than (maxvalue));
    Table created.
    SQL> create index test_idx on test(x) local;
    Index created.
    SQL> insert into test select rownum from dual connect by level <1000;
    999 rows created.
    SQL> commit;
    Commit complete.


    SQL> conn b/b
    Connected.
    SQL> create table temp(x int);
    Table created.
    SQL> create index temp_idx on temp(x);
    Index created.
    SQL> create table test(x int) partition by range (x)
    2 (partition part0 values less than (100),
    3 partition part1 values less than (maxvalue));
    Table created.
    SQL> create index test_idx on test(x) local;
    Index created.
    SQL> select count(*) from a.test;
    COUNT(*)
    ———-
    999
    SQL> select count(*) from b.test;
    COUNT(*)
    ———-
    0
    SQL> alter table a.test exchange partition part0 with table temp including indexes without validation;  --temp表做过度
    Table altered.
    SQL> alter table test exchange partition part0 with table temp including indexes without validation;
    Table altered.
    SQL> select count(*) from a.test;
    COUNT(*)
    ———-
    900
    SQL> select count(*) from b.test;
    COUNT(*)
    ———-
    99
    SQL> alter table a.test exchange partition part1 with table temp including indexes without validation;
    Table altered.
    SQL> alter table test exchange partition part1 with table temp including indexes without validation;
    Table altered.
    SQL> select count(*) from a.test;
    COUNT(*)
    ———-
    0
    SQL> select count(*) from b.test;
    COUNT(*)
    ———-
    999
复合分区表的情况大同小异,大家可以自己试验一下。
分区表不能直接与分区表交换,所以如果对换分区表,用普通表过渡,如果对换普通表,用分区表过渡!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值