总结:
ALTER TABLESPACE 语句+OFFLINE子句:该语句的操作对象针对的是表空间,在NOARCHIVELOG模式下和在ARCHIVELOG模式下都可以(因为表空间此时会做检查点而不失去数据,故不用依赖归档机制来不失去数据)。
ALTER DATABASE 语句+OFFLINE子句:该语句的操作对象针对的是数据文件,只有在ARCHIVELOG模式下可以(因为数据文件此时不会做检查点而不失去数据,故得用依赖归档机制来不失去数据)。例外, 在NOARCHIVELOG模式下, 可以 ALTER DATABASE DATAFILE '/u02/oracle/rbdb1/users3.dbf' OFFLINE DROP;
注释:
带OFFLINE子句的这两个语句肯定是应用于open阶段,非mount阶段。????
(新)带OFFLINE子句的这两个语句肯定是应用于open阶段,而不是其他阶段,如mount阶段。因为数据文件要能离线,首先说明数据文件得可以处于打开状态(在open阶段),即此时数据文件是可以处于联机状态的。
一.DataFile脱机或联机的两种方法:
① ALTER DATABASE 语句修改单独的DataFile
② ALTER TABLESPACE 语句修改所有的DataFile
① ALTER DATABASE 语句修改单独的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模式下,数据文件脱机后会造成数据的遗失(因为单纯的offline datafile,将不会触发【文件】检查点),所以只能使用ALTER DATABASE语句下带有DATAFILE和OFFLINE DROP子句的选项将该DataFile直接取消。
例如该DataFile只包含临时段数据,并没有备份时
ALTER DATABASE DATAFILE '/u02/oracle/rbdb1/users3.dbf' OFFLINE DROP;
注:此操作后,该文件能重新联机(online)吗?
② ALTER TABLESPACE 语句修改所有的DataFile
1、修改TableSpace中所有DataFile或TempFile的可用性(即脱机和联机)【在NOARCHIVELOG模式下和在ARCHIVELOG模式下都适用】
ALTER TABLESPACE ... DATAFILE {ONLINE|OFFLINE}
ALTER TABLESPACE ... TEMPFILE {ONLINE|OFFLINE}
注:修改某TableSpace中的所有数据文件,但是TableSpace本身的状态不改变。
ALTER TABLESPACE语句下只会做表空间级别的检查点,不会产生文件检查点吗?所以,没有类似ALTER tablespace。。。。 DATAFILE ... OFFLINE的语句来修改单个数据文件的可用状态??
表空间或数据文件的可用状态和读写状态是并列的吗?
========================================================================
在归档模式下,我们不能使用'alter database datafile ... offline' ,但是可以使用 'alter tablespace ... offline'(包括 ALTER TABLESPACE ... DATAFILE OFFLINE吗)。
在数据库的read-only模式下,我们可以'使用alter database datafile ... offline' ,但是不能使用'alter tablespace ... offline'(包括 ALTER TABLESPACE ... DATAFILE OFFLINE吗)。
总结:
① ALTER TABLESPACE(database吧??)可以在数据库装载(mount)状态时发布(执行),无需打开(open)状态
② 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
注释:
alter_tablespace
tablespace_state_clauses
Use these clauses to set or change the state of the tablespace.
ONLINE | OFFLINE
Specify ONLINE
to bring the tablespace online. SpecifyOFFLINE
to take the tablespace offline and prevent further access to its segments. When you take a tablespace offline, all of its data files are also offline.
Suggestion:
Before taking a tablespace offline for a long time, consider changing the tablespace allocation of any users who have been assigned the tablespace as either a default or temporary tablespace. While the tablespace is offline, such users cannot allocate space for objects or sort areas in the tablespace. See ALTER USER for more information on allocating tablespace quota to users.Restriction on Taking Tablespaces Offline You cannot take a temporary tablespace offline.
OFFLINE NORMAL SpecifyNORMAL
to flush all blocks in all data files in the tablespace out of the system global area (SGA). You need not perform media recovery on this tablespace before bringing it back online.This is the default.
OFFLINE TEMPORARY If you specifyTEMPORARY
, then Oracle Database performs a checkpoint for all online data files in the tablespace but does not ensure that all files can be written.Files that are offline when you issue this statement may requiremedia recovery before you bring the tablespace back online.
OFFLINE IMMEDIATE If you specifyIMMEDIATE
, then Oracle Database does not ensure that tablespace files are availableanddoes not perform a checkpoint. You must perform media recovery on the tablespace before bringing it back online.
OFFLINE NORMAL时,一要该表空间的所有数据文件联机,二才会做做检查点。
You need not performmedia recovery on this tablespace before bringing it backonline.
OFFLINE TEMPORARY时会对此时联机的该表空间的数据文件做检查点,
而此时已经脱机的该表空间的数据文件(用的手段:alter database datafile offline)则不做检查点。requiremedia recoverybefore you bring the tablespace backonline.
OFFLINE IMMEDIATE不对该表空间的所有数据文件(用的手段:alter database datafile offline)做检查点。requiremedia recoverybefore you bring the tablespace back online.