###更改表空间
####1 重命名表空间
将表空间
/home/oracle/app/oracle/product/11.2.0/dbhome_1/dbs/tbs_01.dbf
迁移到该目录下/home/oracle/app/oracle/oradata/tonytest/datafile/tbs_01.dbf
#1. 将表空间tbs_01, 设置为offline状态SQL> select tablespace_name, status from dba_tablespaces where tablespace_name = 'TBS_01';TABLESPACE_NAME STATUS------------------------------ ---------TBS_01 ONLINESQL> alter tablespace tbs_01 offline normal;Tablespace altered.SQL> select tablespace_name, status from dba_tablespaces where tablespace_name = 'TBS_01';TABLESPACE_NAME STATUS------------------------------ ---------TBS_01 OFFLINESQL> select count(*) from emp1;select count(*) from emp1 *ERROR at line 1:ORA-00376: file 13 cannot be read at this timeORA-01110: data file 13:'/home/oracle/app/oracle/product/11.2.0/dbhome_1/dbs/tbs_01.dbf'#2. 拷贝tbs_01.dbf 文件到/home/oracle/app/oracle/oradata/tonytest/datafile/ 目录下[oracle@hzvscmdb dbs]$ mv tbs_01.dbf /home/oracle/app/oracle/oradata/tonytest/datafile/#3. 重名了表空间的数据文件alter tablespace tbs_01rename datafile 'tbs_01.dbf'to '/home/oracle/app/oracle/oradata/tonytest/datafile/tbs_01.dbf';#4. 将表空间设置为ONLINESQL> alter tablespace tbs_01 online ;Tablespace altered.SQL> select tablespace_name, status from dba_tablespaces where tablespace_name = 'TBS_01';TABLESPACE_NAME STATUS------------------------------ ---------TBS_01 ONLINESQL> select count(*) from emp1; COUNT(*)---------- 108
####2 添加与删除数据文件到表空间
#1. 添加数据文件到表空间SQL> select file_name, tablespace_name from dba_data_files where tablespace_name = 'TBS_01';FILE_NAME TABLESPACE_NAME-------------------------------------------------- --------------------/home/oracle/app/oracle/oradata/tonytest/datafile/ TBS_01tbs_01.dbfalter tablespace tbs_01add datafile '/home/oracle/app/oracle/oradata/tonytest/datafile/tbs_02.dbf' size 5M autoextend on next 10k maxsize 10M;alter tablespace tbs_01add datafile '/home/oracle/app/oracle/oradata/tonytest/datafile/tbs_02.dbf' size 5M autoextend on next 10k maxsize 10M;SQL> select file_name, tablespace_name from dba_data_files where tablespace_name = 'TBS_01';FILE_NAME TABLESPACE_NAME-------------------------------------------------- --------------------/home/oracle/app/oracle/oradata/tonytest/datafile/ TBS_01tbs_01.dbf/home/oracle/app/oracle/oradata/tonytest/datafile/ TBS_01tbs_02.dbf#2. 删除数据文件到表空间alter tablespace tbs_01drop datafile '/home/oracle/app/oracle/oradata/tonytest/datafile/tbs_02.dbf' ;SQL> select file_name, tablespace_name from dba_data_files where tablespace_name = 'TBS_01';FILE_NAME TABLESPACE_NAME-------------------------------------------------- --------------------/home/oracle/app/oracle/oradata/tonytest/datafile/ TBS_01tbs_01.dbf
####3 设置表空间只读与只写状态
- 读写:表空间已联机,可进行读写。
- 只读:指定“只读”使表空间转换到只读模式。此状态下,可以完成(提交或回退)现有的事务处理,但是,不允许对表空间中的对象进一步执行数据操纵语言(DML)操作。表空间已联机,但处于只读状态。不能使SYSTEM 或SYSAUX 表空间处于只读模式。
注:不能将还原表空间和临时表空间设为只读。
#设置表空间只读状态SQL> alter tablespace tbs_01 read only; -- 挂住状态SQL> select ts.name, df.status, df.enabled from v$datafile df, v$tablespace ts where DF.TS# = TS.TS# and TS.NAME = 'TBS_01';NAME STATUS ENABLED------------------------------ ------- ----------TBS_01 ONLINE READ ONLYSQL> select employee_id, salary from emp1 where employee_id = 199;EMPLOYEE_ID SALARY----------- ---------- 199 5600SQL> update emp1 set salary = 5600 where employee_id =200;update emp1 set salary = 5600 where employee_id =200 *ERROR at line 1:ORA-00372: file 13 cannot be modified at this timeORA-01110: data file 13:'/home/oracle/app/oracle/oradata/tonytest/datafile/tbs_01.dbf'SQL> commit;Commit complete.SQL> alter tablespace tbs_01 read only;Tablespace altered. -- commit 之后#设置表空间读写状态SQL> alter tablespace tbs_01 read write;Tablespace altered.SQL> select ts.name, df.status, df.enabled from v$datafile df, v$tablespace ts where DF.TS# = TS.TS# and TS.NAME = 'TBS_01';NAME STATUS ENABLED------------------------------ ------- ----------TBS_01 ONLINE READ WRITE
####4 设置表空间脱机状态
- Normal(正常):如果表空间中的任一数据文件都不存在任何错误状态,通过正常方式便可使表空间脱机。当Oracle DB 使表空间脱机时,通过对表空间的所有数据文件设置检查点,可以确保将所有数据写入磁盘。
SQL> select to_char(offline_change#), to_char(online_change#) from v$datafile where name like '%tbs_01.dbf';TO_CHAR(OFFLINE_CHANGE#) TO_CHAR(ONLINE_CHANGE#)---------------------------------------- ----------------------------------------11795750037680 11795750037795SQL> alter tablespace tbs_01 offline normal;Tablespace altered.SQL> select to_char(offline_change#), to_char(online_change#) from v$datafile where name like '%tbs_01.dbf';TO_CHAR(OFFLINE_CHANGE#) TO_CHAR(ONLINE_CHANGE#)---------------------------------------- ----------------------------------------11795750040313 11795750040336SQL> alter tablespace tbs_01 online;Tablespace altered.SQL> select to_char(offline_change#), to_char(online_change#) from v$datafile where name like '%tbs_01.dbf';TO_CHAR(OFFLINE_CHANGE#) TO_CHAR(ONLINE_CHANGE#)---------------------------------------- ----------------------------------------11795750040366 11795750043082
- Temporary(临时):如果表空间中的一个或多个文件存在错误状态,也可以使表空间暂时脱机。当Oracle DB 使数据文件(尚未脱机的)脱机时,将对这些数据文件设置检查点。如果没有任何文件脱机,但是您使用了临时子句,则在使表空间重新联机时不需要执行介质恢复。但是,如果因写错误而导致表空间的一个或多个文件脱机,而且设置了表空间临时脱机,那么表空间需要执行恢复后才能重新联机。
SQL> alter tablespace tbs_01 offline temporary;Tablespace altered.SQL> select to_char(offline_change#), to_char(online_change#) from v$datafile where name like '%tbs_01.dbf';TO_CHAR(OFFLINE_CHANGE#) TO_CHAR(ONLINE_CHANGE#)---------------------------------------- ----------------------------------------11795750040366 11795750043082
- Immediate(立即):Oracle DB 可以使表空间立即脱机,而不需要对任何数据文件设置检查点。如果指定了“Immediate(立即)”,则必须先对表空间执行介质恢复,才能使表空间联机。如果数据库在NOARCHIVELOG 模式下运行,则无法立即使表空间脱机。
SQL> alter tablespace tbs_01 offline immediate;Tablespace altered.SQL> select to_char(offline_change#), to_char(online_change#) from v$datafile where name like '%tbs_01.dbf';TO_CHAR(OFFLINE_CHANGE#) TO_CHAR(ONLINE_CHANGE#)---------------------------------------- ----------------------------------------11795750040366 11795750043082SQL> alter tablespace tbs_01 online;Tablespace altered.