1.找到要删除对象
举例:
--test表空间10号数据文件删除。
(1)表分区移动。
select distinct 'alter table '||owner||'.'||segment_name||' move partition '||partition_name||' tablespace '||'TEST2'
from dba_extents
where tablespace_name='TEST'
and segment_type ='TABLE PARTITION'
and file_id=10 ;
(2)索引分区移动
select distinct 'alter index '||owner||'.'||segment_name||' rebuild partition '||partition_name||' online tablespace TEST2'
from dba_extents
where tablespace_name='TEST'
and segment_type ='INDEX PARTITION'
and file_id=10 ;
(3)普通表移动
select distinct 'alter table '||owner||'.'||segment_name||' move tablespace TEST2'
from dba_extents
where tablespace_name='TEST'
and segment_type ='TABLE'
and file_id=10 ;
(4)重建普通索引到表空间test2;
select distinct 'alter index '||owner||'.'||segment_name||' rebuild online tablespace '||v_tbs_tmp
from dba_extents
where tablespace_name='TEST'
and segment_type ='INDEX'
and file_id=10 ;
注意:
除了表,索引以外,还要检查其他的数据库对象是否在当前文件中,如果在也要移动走。
2.生成移动表分区,索引分区,普通表,普通索引的语句
alter table test.test1 move partition test_p1 tablespace test2;
alter index test.idx_p_test1 rebuild partition idx_p1 online tablespace test2;
alter table test.test2 move tablespace test2;
alter index test.Idx_test2 rebuild online tablespace test2;
3.删除表壳空间的数据文件10。
alter tablespace test drop datafile 10;
4.重新移动回原来的表空间
alter table test.test1 move partition test_p1 tablespace test;
alter index test.idx_p_test1 rebuild partition idx_p1 online tablespace test;
alter table test.test2 move tablespace test;
alter index test.Idx_test2 rebuild online tablespace test;
5.总结
删除数据文件的表空间的基本思路就是将要删除的数据文件中的对象先临时移动到别的表空间。
删除要删除的数据文件,然后将移动出去的数据库对象重新移动回来。需要注意,如果有其他的对象也要移动到别的表空间,然后再移动回来。不能漏掉对象。导致数据库不完整。