Oracle delete data file basic command (transfer)

Before we go into details, we must make one thing clear: Oracle does not provide a method to delete data files like deleting tables and views. Data files are part of tablespaces, so tablespaces cannot be "removed".

 

1. How to use offline data files

For non-archive mode use: alter database datafile '...' offline drop;

For archive mode use: alter database datafile '...' offline;

illustrate:

1) The above command just OFFLINE the data file instead of deleting the data file in the database. The information of this data file still exists in the control file. Querying v$datafile still shows the file.

2) The effect of offline and offline drop is the same in archive mode

3) After offline, objects that exist on this datafile will not be accessible

4) In noarchivelog mode, as long as the online redo log is not rewritten, the online operation can be performed after recovering the file

 

Actual use case:

Solutions for Can't Enter System After Directly Deleting Data Files

 

Under normal circumstances, the correct way to drop a tablespace is:

DROP TABLESPACE tablespace_name INCLUDING CONTENTS AND DATAFILES;

 

If the data file is directly deleted without deleting it through the above command, the database cannot be opened.

 

If you delete the data file directly

When a normal user logs in, an error is reported:

ORA-01033: ORACLE initialization or shutdown in progress

sys user can log in normally

But when the operation is performed (SELECT count(1) FROM user_tables), an error will be reported:

ORA-01219: database not open: queries only allowed in fixed tables/views

If the command alter database open is executed to open the database, the following error is reported:

ORA-01157: unable to identify/lock datafile 12 - see DBWR trace file

ORA-01110: data file 12: 'E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\TSTEST001.DBF'

 

Indicates that the data file was not found in the database

Because the data file is physically deleted without being offline, the Oracle data is inconsistent, so the startup fails.

It can be solved by the following methods

 

Solution:

sqlplus sys/orcl@orcl as sysdba;

SQL> alter database datafile 'E:\ORACLE\PRODUCT\10.2.0\ORADATA\ORCL\TSTEST001.DBF' offline drop;

SQL> alter database open;

SQL> drop tablespace CTBASEDATA;

 

2. Starting from Oracle 10G R2, you can use: Alter tablespace tablespace_name drop datafile file_name; to delete an empty data file, and the corresponding data dictionary information will also be cleared:

 

sys@ORCL>select file_id,file_name,tablespace_name from dba_data_files

  2  where tablespace_name='USERS';

FILE_ID  FILE_NAME                        TABLESPACE_NAME

-------    --------------------------------------------       ------------------

4        /u01/app/oracle/oradata/orcl/users01.dbf    USERS

 

sys@ORCL>alter tablespace users add datafile

  2  '/u01/app/oracle/oradata/orcl/users02.dbf' size 5M autoextend off;

Tablespace altered.

sys@ORCL>select file_id,file_name,tablespace_name from dba_data_files

  2  where tablespace_name='USERS';

FILE_ID  FILE_NAME                          TABLESPACE_NAME

-------    --------------------------------------------        -------------------

4        /u01/app/oracle/oradata/orcl/users01.dbf     USERS

9        /u01/app/oracle/oradata/orcl/users02.dbf     USERS

 

sys@ORCL>drop table test;

Table dropped.

sys@ORCL>create table test tablespace users

  2  as

  3  select * from dba_objects;

Table created.

sys@ORCL>select SEGMENT_NAME,FILE_ID,BLOCKS from dba_extents

  2  where file_id=9;

SEGMENT_NAME                      FILE_ID     BLOCKS

------------------------------                  ----------      ----------

TEST                                    9          8

TEST                                    9          8

TEST                                    9          8

TEST                                    9          8

TEST                                    9          8

TEST                                    9          8

TEST                                    9          8

TEST                                    9          8

TEST                                    9          8

TEST                                    9          8

TEST                                    9          8

TEST                                    9          8

TEST                                    9          8

TEST                                    9          8

TEST                                    9          8

TEST                                    9        128

TEST                                    9        128

 

17 rows selected.

sys@ORCL>alter table test move tablespace PERFSTAT; -- move the table to another tablespace

Table altered.

sys@ORCL>select SEGMENT_NAME,FILE_ID,BLOCKS from dba_extents

  2  where file_id=9;

no rows selected

sys@ORCL>alter tablespace users drop datafile

  2  '/u01/app/oracle/oradata/orcl/users02.dbf';

Tablespace altered.

sys@ORCL>select file_id,file_name,tablespace_name from dba_data_files

  2  where tablespace_name='USERS';

FILE_ID  FILE_NAME                       TABLESPACE_NAME

-------    --------------------------------------------      ---------------------

4        /u01/app/oracle/oradata/orcl/users01.dbf   USERS

 

3. Oracle 10g can delete files in temporary tablespace

alter database tempfile '/home/oracle/temp01.dbf' drop including datafiles;

 

Difference between ALTER DATABASE and ALTER TABLESPACE OFFLINE

one. Two methods for DataFile offline or online:

① ALTER DATABASE statement modifies a separate DataFile

② ALTER TABLESPACE statement modifies all DataFiles

1. Change DataFile state in ARCHIVRLOG mode

ALTER DATABASE DATAFILE '/u02/oracle/rbdb1/stuff01.dbf' ONLINE;

ALTER DATABASE DATAFILE '/u02/oracle/rbdb1/stuff01.dbf' OFFLINE;

Or use the file number to represent :

ALTER DATABASE DATAFILE 5 ONLINE;

ALTER DATABASE DATAFILE 5 OFFLINE;

Note: ALTER DATABASE can only be used in ARCHIVELOG mode to change DataFile

 

2. Take DataFile offline in NOARCHIVELOG mode

In NOARCHIVELOG mode, the data file will be lost after being offline, so you can only use the options with the DATAFILE and OFFLINE DROP clauses in the ALTER DATABASE statement to cancel the DataFile directly. For example, the DataFile only contains temporary segment data. when there is no backup

 

ALTER DATABASE DATAFILE '/u02/oracle/rbdb1/users3.dbf' OFFLINE DROP;

3. Modify the availability of all DataFile or TempFile in TableSpace

ALTER TABLESPACE ... DATAFILE {ONLINE|OFFLINE}

ALTER TABLESPACE ... TEMPFILE {ONLINE|OFFLINE}

 

Note: Modify all data files in a TableSpace, but the state of the TableSpace itself does not change.

Summarize:

① ALTER TABLESPACE can be issued when the database is loaded without opening

② When it comes to system table space, undo table space, and default temporary table space, it must be an unopened database

③ The full name of the file must be filled in the ALTER DATABASE DATAFILE statement

 

two. Difference between tablespace and datafile offline

1. ALTER TABLESPACE ... OFFLINE

Does a checkpoint on the datafiles

Takes the datafiles offline

When the tablespace is offline, the SCN of the data file will be frozen, and the file checkpoint will occur when the data file of the tablespace is offline/online, making the SCN of a single data file inconsistent with other database problems.

When the tablespace is online, Oracle will obtain the current SCN, unfreeze the offline file SCN, and synchronize with the current SCN.

There are several options for tablespace offline normal, temporary, immediate, for recovery, which are not available in datafile.

 

2.  ALTER DATABASE DATAFILE ... OFFLINE

Simple offline datafile will not trigger file checkpoint, only when offline tablespace will trigger file checkpoint, which is why online datafile needs media recovery but online tablespace does not.

Note: ALTER DATABASE can only be used in ARCHIVELOG mode to change DataFile

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327080368&siteId=291194637