一.DataFile脱机或联机的两种方法:
① ALTER DATABASE 语句修改单独的DataFile
② ALTER TABLESPACE 语句修改所有的DataFile
1、在ARCHIVRLOG模式下的更改DataFile状态
ALTER DATABASE DATAFILE '/u02/oracle/rbdb1/stuff01.dbf' ONLINE;
ALTER DATABASE DATAFILE '/u02/oracle/rbdb1/stuff01.dbf' OFFLINE;
或者用文件号来表示 :
SQL>select file#,name,checkpoint_change# from v$datafile;
FILE# |
NAME |
CHECKPOINT_CHANGE# |
1 |
D:/ORACLE/ORADATA/DBA/SYSTEM01.DBF |
1194854 |
2 |
D:/ORACLE/ORADATA/DBA/UNDOTBS01.DBF |
1194854 |
3 |
D:/ORACLE/ORADATA/DBA/SYSAUX01.DBF |
1194854 |
4 |
D:/ORACLE/ORADATA/DBA/USERS01.DBF |
1194854 |
5 |
D:/ORACLE/ORADATA/DBA/TEST01.DBF |
1203262 |
ALTER DATABASE DATAFILE 5 ONLINE;
ALTER DATABASE DATAFILE 5 OFFLINE;
注:只有在ARCHIVELOG模式下才可使用ALTER DATABASE来更改DataFile
2、在NOARCHIVELOG模式下使DataFile脱机
由于在NOARCHIVELOG模式下,数据文件脱机后会造成数据的遗失,所以只能使用ALTER DATABASE语句下带有DATAFILE和OFFLINE DROP子句的选项将该DataFile直接取消,例如该DataFile只包含临时段数据,并没有备份时
ALTER DATABASE DATAFILE '/u02/oracle/rbdb1/users3.dbf' OFFLINE DROP;
3、修改TableSpace中所有DataFile或TempFile的可用性
ALTER TABLESPACE ... DATAFILE {ONLINE|OFFLINE}
ALTER TABLESPACE ... TEMPFILE {ONLINE|OFFLINE}
注:修改某TableSpace中的所有数据文件,但是TableSpace本身的状态不改变。
我们不能使用'alter database datafile ... offline' 在归档模式下,但是 'alter tablespace ... offline' 可以。
我们不是使用'alter tablespace ... offline'在数据库的read-only模式下,但是'alter database datafile ... offline' 可以。
总结:
① ALTER TABLESPACE可以在数据库装载状态时发布,无需打开
② 涉及到系统表空间、撤销表空间、默认临时表空间时,必须是未打开的数据库
③ ALTER DATABASE DATAFILE 语句中必须填入文件全名
二. 表空间 与 数据文件 脱机的区别
1. ALTER TABLESPACE ... OFFLINE
Does a checkpoint on the datafiles
Takes the datafiles offline
表空间Offline时,数据文件的SCN会被冻结,而且表空间的数据文件offline/online时又会发生文件检查点,使单个数据文件SCN和数据库其他问题不一致。
表空间online时,Oracle会取得当前SCN,解冻offline文件SCN,和当前SCN同步。
tablespace offline有几种选项可供选择normal, temporary,immediate, for recovery,而在datafile中则没有这些选项。
SQL>select dbms_flashback.get_system_change_number from dual;
GET_SYSTEM_CHANGE_NUMBER
------------------------
1203246
SQL>alter tablespace test offline;
Tablespace altered.
SQL>select file#,name,checkpoint_change# from v$datafile;
FILE# |
NAME |
CHECKPOINT_CHANGE# |
1 |
D:/ORACLE/ORADATA/DBA/SYSTEM01.DBF |
1194854 |
2 |
D:/ORACLE/ORADATA/DBA/UNDOTBS01.DBF |
1194854 |
3 |
D:/ORACLE/ORADATA/DBA/SYSAUX01.DBF |
1194854 |
4 |
D:/ORACLE/ORADATA/DBA/USERS01.DBF |
1194854 |
5 |
D:/ORACLE/ORADATA/DBA/TEST01.DBF |
1203262 |
SQL>select dbms_flashback.get_system_change_number from dual;
GET_SYSTEM_CHANGE_NUMBER
------------------------
1203328
SQL>alter tablespace test online;
Tablespace altered.
SQL>select file#,name,checkpoint_change# from v$datafile;
FILE# |
NAME |
CHECKPOINT_CHANGE# |
1 |
D:/ORACLE/ORADATA/DBA/SYSTEM01.DBF |
1,194,854 |
2 |
D:/ORACLE/ORADATA/DBA/UNDOTBS01.DBF |
1,194,854 |
3 |
D:/ORACLE/ORADATA/DBA/SYSAUX01.DBF |
1,194,854 |
4 |
D:/ORACLE/ORADATA/DBA/USERS01.DBF |
1,194,854 |
5 |
D:/ORACLE/ORADATA/DBA/TEST01.DBF |
1,203,343 |
SQL>select dbms_flashback.get_system_change_number from dual;
GET_SYSTEM_CHANGE_NUMBER
------------------------
1203440
2. ALTER DATABASE DATAFILE ... OFFLINE
单纯的offline datafile,将不会触发文件检查点,只有针对offline tablespace的时候才会触发文件检查点,这也是为什么online datafile需要media recovery而online tablespace不需要。
注:只有在ARCHIVELOG模式下才可使用ALTER DATABASE来更改DataFile
SQL>select file#,name,checkpoint_change# from v$datafile;
FILE# |
NAME |
CHECKPOINT_CHANGE# |
1 |
D:/ORACLE/ORADATA/DBA/SYSTEM01.DBF |
1,194,854 |
2 |
D:/ORACLE/ORADATA/DBA/UNDOTBS01.DBF |
1,194,854 |
3 |
D:/ORACLE/ORADATA/DBA/SYSAUX01.DBF |
1,194,854 |
4 |
D:/ORACLE/ORADATA/DBA/USERS01.DBF |
1,194,854 |
5 |
D:/ORACLE/ORADATA/DBA/TEST01.DBF |
1,203,343 |
SQL>select dbms_flashback.get_system_change_number from dual;
GET_SYSTEM_CHANGE_NUMBER
------------------------
1219831
SQL>alter database datafile 5 offline;
Database altered.
SQL>select file#,name,checkpoint_change# from v$datafile;
FILE# |
NAME |
CHECKPOINT_CHANGE# |
1 |
D:/ORACLE/ORADATA/DBA/SYSTEM01.DBF |
1,194,854 |
2 |
D:/ORACLE/ORADATA/DBA/UNDOTBS01.DBF |
1,194,854 |
3 |
D:/ORACLE/ORADATA/DBA/SYSAUX01.DBF |
1,194,854 |
4 |
D:/ORACLE/ORADATA/DBA/USERS01.DBF |
1,194,854 |
5 |
D:/ORACLE/ORADATA/DBA/TEST01.DBF |
1,203,343 |
SQL>select dbms_flashback.get_system_change_number from dual;
GET_SYSTEM_CHANGE_NUMBER
------------------------
1219882
SQL>alter database datafile 5 online;
Error at line 1
ORA-01113: 文件 5 需要介质恢复
ORA-01110: 数据文件 5: 'D:/ORACLE/ORADATA/DBA/TEST01.DBF'
SQL>recover datafile 5;
完成介质恢复
SQL>alter database datafile 5 online;
Database altered.
SQL>select file#,name,checkpoint_change# from v$datafile;
FILE# |
NAME |
CHECKPOINT_CHANGE# |
1 |
D:/ORACLE/ORADATA/DBA/SYSTEM01.DBF |
1,194,854 |
2 |
D:/ORACLE/ORADATA/DBA/UNDOTBS01.DBF |
1,194,854 |
3 |
D:/ORACLE/ORADATA/DBA/SYSAUX01.DBF |
1,194,854 |
4 |
D:/ORACLE/ORADATA/DBA/USERS01.DBF |
1,194,854 |
5 |
D:/ORACLE/ORADATA/DBA/TEST01.DBF |
1,219,929 |
SQL>select dbms_flashback.get_system_change_number from dual;
GET_SYSTEM_CHANGE_NUMBER
------------------------
1220043