违反规则实验及问题分析处理过程如下:
1.创建一个含有主键的分区表tab_partition_range
create table tab_partition_range (id number primary key,name varchar2(50))
partition by range(id)(
partition t_range_p1 values less than (10),
partition t_range_p2 values less than (20),
partition t_range_p3 values less than (30)
);
2.每个分区初始化一条记录
sec@sec> insert into tab_partition_range values (9,'Andy9')
sec@sec> insert into tab_partition_range values (19,'Andy19')
sec@sec> insert into tab_partition_range values (29,'Andy29')
sec@sec> select * from tab_partition_range;
ID NAME
---------- --------------------------------------------------
9 Andy9
19 Andy19
29 Andy29
3.创建一个不含主键的表tab_exchange
sec@sec> create table tab_exchange (id number, name varchar2(50));
4.以“将分区t_range_p2中的数据”与目标表tab_exchange交换为例,效果如下
sec@sec> alter table tab_partition_range exchange partition t_range_p2 with table tab_exchange;
Table altered.
sec@sec> select * from tab_exchange;
ID NAME
---------- --------------------------------------------------
19 Andy19
sec@sec> select * from tab_partition_range;
ID NAME
---------- --------------------------------------------------
9 Andy9
29 Andy29
呵呵……,到这里,您是不是要问了,这不是交换成功了么,t_range_p2中的数据的确按照既定目标交换到了tab_exchange中。
不要着急,实验继续下去,我们原封不动的再执行一次而上面的交换命令,目标是:tab_exchange中的数据反向的交换回到分区表中。
注释:交换分区的命令是可以互逆操作滴~~~~
sec@sec> alter table tab_partition_range exchange partition t_range_p2 with table tab_exchange;
alter table tab_partition_range exchange partition t_range_p2 with table tab_exchange
*
ERROR at line 1:
ORA-14097: column type or size mismatch in ALTER TABLE EXCHANGE PARTITION
OK,看到报错了吧,这也就是这个实验要演示的一个效果。
如何处理,聪明的您一定知道,对头,在目标表中添加主键!
Go on ...
5.在tab_exchange创建主键
sec@sec> alter table tab_exchange add primary key (id);
Table altered.
6.再次执行交换命令,成功了
sec@sec> alter table tab_partition_range exchange partition t_range_p2 with table tab_exchange;
Table altered.
sec@sec> select * from tab_exchange;
no rows selected
sec@sec> select * from tab_partition_range;
ID NAME
---------- --------------------------------------------------
9 Andy9
19 Andy19
29 Andy29
似乎什么也没有发生过一样:)
7.最后再次强调一下结论
请大家注意:交换分区时分区表有主键目标表亦需有主键!
-- The End --
1.创建一个含有主键的分区表tab_partition_range
create table tab_partition_range (id number primary key,name varchar2(50))
partition by range(id)(
partition t_range_p1 values less than (10),
partition t_range_p2 values less than (20),
partition t_range_p3 values less than (30)
);
2.每个分区初始化一条记录
sec@sec> insert into tab_partition_range values (9,'Andy9')
sec@sec> insert into tab_partition_range values (19,'Andy19')
sec@sec> insert into tab_partition_range values (29,'Andy29')
sec@sec> select * from tab_partition_range;
ID NAME
---------- --------------------------------------------------
9 Andy9
19 Andy19
29 Andy29
3.创建一个不含主键的表tab_exchange
sec@sec> create table tab_exchange (id number, name varchar2(50));
4.以“将分区t_range_p2中的数据”与目标表tab_exchange交换为例,效果如下
sec@sec> alter table tab_partition_range exchange partition t_range_p2 with table tab_exchange;
Table altered.
sec@sec> select * from tab_exchange;
ID NAME
---------- --------------------------------------------------
19 Andy19
sec@sec> select * from tab_partition_range;
ID NAME
---------- --------------------------------------------------
9 Andy9
29 Andy29
呵呵……,到这里,您是不是要问了,这不是交换成功了么,t_range_p2中的数据的确按照既定目标交换到了tab_exchange中。
不要着急,实验继续下去,我们原封不动的再执行一次而上面的交换命令,目标是:tab_exchange中的数据反向的交换回到分区表中。
注释:交换分区的命令是可以互逆操作滴~~~~
sec@sec> alter table tab_partition_range exchange partition t_range_p2 with table tab_exchange;
alter table tab_partition_range exchange partition t_range_p2 with table tab_exchange
*
ERROR at line 1:
ORA-14097: column type or size mismatch in ALTER TABLE EXCHANGE PARTITION
OK,看到报错了吧,这也就是这个实验要演示的一个效果。
如何处理,聪明的您一定知道,对头,在目标表中添加主键!
Go on ...
5.在tab_exchange创建主键
sec@sec> alter table tab_exchange add primary key (id);
Table altered.
6.再次执行交换命令,成功了
sec@sec> alter table tab_partition_range exchange partition t_range_p2 with table tab_exchange;
Table altered.
sec@sec> select * from tab_exchange;
no rows selected
sec@sec> select * from tab_partition_range;
ID NAME
---------- --------------------------------------------------
9 Andy9
19 Andy19
29 Andy29
似乎什么也没有发生过一样:)
7.最后再次强调一下结论
请大家注意:交换分区时分区表有主键目标表亦需有主键!
-- The End --
来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/519536/viewspace-610775/,如需转载,请注明出处,否则将追究法律责任。
转载于:http://blog.itpub.net/519536/viewspace-610775/