Chinaunix首页 | 论坛 | 博客
  • 博客访问: 591037
  • 博文数量: 78
  • 博客积分: 2196
  • 博客等级: 大尉
  • 技术积分: 826
  • 用 户 组: 普通用户
  • 注册时间: 2010-01-29 12:46
文章分类

全部博文(78)

文章存档

2012年(7)

2011年(42)

2010年(29)

我的朋友

分类: LINUX

2011-08-31 22:08:50

Oracle RDBMS databases stores data logically in the form of tablespaces and physically in the form of data files. The datafiles are added to the tablespaces as database space requirement grows bigger. However, there are several reasons you may want to remove or delete data files from a tablespace. Such as accidentally add a wrongly sized or unwanted datafile to a tablespace, or the data space usage has became smaller and some data files want to be removed, or attempt to recover Oracle database which fails to start due to missing or corrupted datafiles by removing them, Oracle does not provide an easy way or user interface to delete or drop datafiles from a tablespace. Once a datafile is made part of a tablespace, it can no longer be detached or removed from the tablespace, albeit there are several workarounds.

How to Completely Drop the Whole Tablespace with All Datafiles

The easiest way to drop a or multiple datafiles is by dropping the entire tablespace together with its datafiles. If you no longer need the data contents of the tablespace, the following command will drop the tablespace, the datafile, and the tablespace’s contents from the data dictionary. All of the objects that where contained in that tablespace are permanently removed.

DROP TABLESPACE INCLUDING CONTENTS AND DATAFILES;

If you don’t specify “AND DATAFILES”, Oracle will not drop the physical datafile after the DROP TABLESPACE command, but you can always delete the files from operating system shell (If the data files are locked, restart the server).

But always perform a backup of the database, as the “DROP TABLESPACE” command is irreversible. It’s also a good practice to check and identify how many datafiles a tablespace actually has before performing the drop action in order to avoid mistake.

How to Check How Many Datafiles a Tablespace Has

To determine and identify all datafiles that link to a tablespace, use the following query, with tablespace name in capital letter:

SELECT file_name, tablespace_name
FROM dba_data_files
WHERE tablespace_name =’’;

If a tablespace contains multiple datafiles, and you just want to drop one or some of the datafiles and keep the remaining datafiles together with the objects and contents, the objects or data must be exported for the affected table space. Once exported, the tablespace can be dropped with above “DROP TABLESPACE” command. Then, recreate the tablespace with the datafile(s) required (that you initially want to keep), and then import the objects into the recreated tablespace.

If one or more datafiles is missing after a recovery process or accidental deletion, you can use ALTER DATABASE DATAFILE OFFLINE DROP command to make the datafile offline so that database can starts up after which the troubled tablespace can be dropped. (See .)

How to Resize a Datafile to Minimum Size

Another alternative to drop the datafile is by shrinking the size of datafile instead of dropping. This option is only possible if there is no extents in the datafile. If there are none, it’s possible to resize the data file down to a very small file (2 blocks), where Oracle database will no longer create any extent in the datafile. However, this workaround does not remove the datafile from tablespace nor delete the file physically, but it reduce the risk as no data will be truncated as only empty blocks are reduced. It simply makes it unusable and takes up almost close to no disk space. To resize a datafile, use the following query:

alter database datafile ‘’ resize 8M;

How to Determine the Extents inside Datafile

To check and determine the extents that exists in a datafile, use this query:

SELECT owner, segment_name
FROM dba_extents a, dba_data_files b
WHERE a.file_id = b.file_id
AND b.file_name = ‘


        AND DATAFILES

        When you specify INCLUDING CONTENTS, the AND DATAFILES clause lets you instruct the database to delete the associated operating system files as well. Database writes a message to the alert log for each operating system file deleted. This clause is not needed for Oracle-managed files, because they are removed from the system even if you do not specify AND DATAFILES.

   只有在创建datafile时使用OMF,oracle才会在不用and datafiles子句的情况下也会自动删除datafiles

   要drop tablespace连带datafile和包含的对象的语法是
   drop tablespace tablespace_name including contents and datafiles;

阅读(2348) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~