----------------------------------
5.迁移索引到新的表空间
----------------------------------


--新建测试环境
create tablespace test
datafile 'E:\APP\ADMINISTRATOR\ORADATA\ORCL\test01.DBF'
size 500m
autoextend on
next 100m maxsize unlimited
extent management local autoallocate
segment   space management auto;


conn scott/tiger




create table scott.emp01 
as
select * from scott.emp;






create index index01 on scott.emp01(ename) ;


*************第一步:迁移索引到别的表空间




--查询表的索引以及对应的表空间


col index_name for a10
col tablespace_name for a10
select index_name,table_name,status,tablespace_name
from user_indexes
where table_name='EMP01';




INDEX_NAME TABLE_NAME STATUS   TABLESPACE
---------- ---------- -------- ----------
INDEX01    EMP01      VALID    USERS




--迁移索引index01到新的表空间test


alter index scott.index01 rebuild tablespace test;




--查询表的索引以及对应的表空间


col index_name for a10
col tablespace_name for a10
select index_name,table_name,status,tablespace_name
from user_indexes
where table_name='EMP01';




INDEX_NAME TABLE_NAME STATUS   TABLESPACE
---------- ---------- -------- ----------
INDEX01    EMP01      VALID    TEST




*************第二步:迁移表到别的表空间


--查看表所在的表空间


select table_name,tablespace_name from dba_tables
where owner='SCOTT' and table_name='EMP01';


TABLE_NAME TABLESPACE
---------- ----------
EMP01      USERS




--迁移表到新表空间


alter table emp01 move tablespace test;


--查看表所在的表空间


select table_name,tablespace_name from dba_tables
where owner='SCOTT' and table_name='EMP01';


TABLE_NAME TABLESPACE
---------- ----------
EMP01      TEST




注意:表迁移到新的表空间后,要重建该表相关的所有索引.因为此时索引是属于UNUSABLE状态.






--查看索引状态




col index_name for a10
col tablespace_name for a10
select index_name,table_name,status,tablespace_name
from user_indexes
where table_name='EMP01';


INDEX_NAME TABLE_NAME STATUS   TABLESPACE
---------- ---------- -------- ----------
INDEX01    EMP01      UNUSABLE TEST






--重建该表的索引


alter index scott.index01 rebuild;




--查看索引状态




col index_name for a10
col tablespace_name for a10
select index_name,table_name,status,tablespace_name
from user_indexes
where table_name='EMP01';


INDEX_NAME TABLE_NAME STATUS   TABLESPACE
---------- ---------- -------- ----------
INDEX01    EMP01      VALID    TEST