Oracle 数据文件 reuse 属性 说明(转载)

Oracle 表空间 创建参数 说明

http://blog.csdn.net/tianlesoftware/archive/2011/01/27/6166928.aspx

当我们对表空间添加数据文件的时候,有一个reuse 属性。 10g的官网对这个参数的说明如下:

REUSE

Specify REUSE to allow Oracle to reuse an existing file.

(1)If the file already exists, then Oracle reuses the filename and applies the new size (if you specify SIZE) or retains the original size.

--如果file 已经存在,并且在创建时指定了file size,那么就重用原文件,并应用新的size,如果没有指定file size,则保留原有的大小。

(2)If the file does not exist, then Oracle ignores this clause and creates the file.

-- 如果file 不存在,oracle 将忽略该参数。

Restriction on the REUSE Clause

You cannot specify REUSE unless you have specified filename.

Whenever Oracle uses an existing file, the previous contents of the file are lost.

-- 如果Oracle 使用了已经存在的file,那么之前file里的数据将全部丢失。

From:

http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/clauses004.htm#SQLRF01602

在Oracle 11g的官方文档里没有搜到相关的信息。 因为手头还没有11g的库,所以也不好测试。 这篇blog里测试的是基于Oracle 10g环境。

下面我们来做一些测试:

1. 创建一个表空间Dave

SQL> show user;

USER is "SYS"

SQL> create tablespace dave datafile '/u01/app/oracle/oradata/dave2/dave01.dbf' size 100M;

Tablespace created.

2. 创建表anqing,并指定存储表空间dave

SQL> create table anqing tablespace dave as select * from dba_objects;

Table created.

SQL> commit;

Commit complete.

SQL> select count(*) from anqing;

COUNT(*)

----------

50391

SQL> set wrap off;

SQL> select owner,table_name,tablespace_name from dba_tables where table_name='ANQING';

OWNER TABLE_NAME TABLESPACE_NAME

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

SYS ANQING DAVE

3. 对表空间dave 添加一个新的数据文件,并使用reuse

SQL> alter tablespace dave add datafile '/u01/app/oracle/oradata/dave2/dave02.dbf' reuse;

alter tablespace dave add datafile '/u01/app/oracle/oradata/dave2/dave02.dbf' reuse

*

ERROR at line 1:

ORA-01119: error in creating database file '/u01/app/oracle/oradata/dave2/dave02.dbf'

ORA-17610: file '/u01/app/oracle/oradata/dave2/dave02.dbf' does not exist and no size specified ORA-27037: unable to obtain file status Linux Error: 2: No such file or directory Additional information: 3

-- 这种情况下,如果文件存在,会使用原始文件的大小。但dave02.dbf 不存在,我们又没有指定文件大小,所以无法创建。 我们指定size 就可以创建了。

SQL> alter tablespace dave add datafile '/u01/app/oracle/oradata/dave2/dave02.dbf' size 50M reuse;

Tablespace altered.

SQL>

4. 保持表空间的状态,然后使用reuse 来添加数据文件

SQL> alter tablespace dave add datafile '/u01/app/oracle/oradata/dave2/dave01.dbf' size 50M reuse;

alter tablespace dave add datafile '/u01/app/oracle/oradata/dave2/dave01.dbf' size 50M reuse

*

ERROR at line 1:

ORA-01537: cannot add file '/u01/app/oracle/oradata/dave2/dave01.dbf' - file already part of database

--报错,所以即使我们需要使用reuse,前提也是该数据文件已经不存在该表空间了。

5. 先将datafile offline drop,在reuse

offline drop 并不会drop datafile,仅仅是将datafile 标记为offline,我们online 之后还可以recover回来。 具体参考:

alter database datafile offline drop 与 alter tablespace drop datafile 区别

http://blog.csdn.net/tianlesoftware/archive/2011/04/06/6305600.aspx

SQL> alter database datafile '/u01/app/oracle/oradata/dave2/dave01.dbf' offline drop;

Database altered.

SQL> alter tablespace dave add datafile '/u01/app/oracle/oradata/dave2/dave01.dbf' size 50M reuse;

alter tablespace dave add datafile '/u01/app/oracle/oradata/dave2/dave01.dbf' size 50M reuse

*

ERROR at line 1:

ORA-01537: cannot add file '/u01/app/oracle/oradata/dave2/dave01.dbf' - file already part of database

-- 依旧报错,因为此时数据文件dave01.dbf 的信息还记录在数据字典里。

-- 将数据文件还原回来

SQL> alter database datafile '/u01/app/oracle/oradata/dave2/dave01.dbf' online;

alter database datafile '/u01/app/oracle/oradata/dave2/dave01.dbf' online

*

ERROR at line 1:

ORA-01113: file 6 needs media recovery

ORA-01110: data file 6: '/u01/app/oracle/oradata/dave2/dave01.dbf'

SQL> recover datafile 6;

Media recovery complete.

6. 使用alter tablespace dave drop datafile 命令

该命令在删除控制文件和物理文件,所以没有可用的意义。

SQL> alter tablespace dave drop datafile '/u01/app/oracle/oradata/dave2/dave02.dbf';

Tablespace altered.

[oracle@db2 dave2]$ pwd

/u01/app/oracle/oradata/dave2

[oracle@db2 dave2]$ ls

control01.ctl control03.ctl example01.dbf redo01.log redo03.log system01.dbf undotbs01.dbf

control02.ctl dave01.dbf huaining01.dbf redo02.log sysaux01.dbf temp01.dbf users01.dbf

-- 文件已经不存在

7. 删除表空间后,在reuse

命令如下:

SQL>drop tablespace dave including contents and datafiles;

该命令也可以指定同时删除物理文件,但那样我们的测试就没办法完成,所以我们不删除datafile,仅从控制文件里删除表空间。

SQL> drop tablespace dave including contents;

Tablespace dropped.

SQL> create tablespace dave2 datafile '/u01/app/oracle/oradata/dave2/dave01.dbf' size 50M reuse;

Tablespace created.

-- 重用成功

看一下数据文件大小:

[oracle@db2 dave2]$ ll -h dave01.dbf

-rw-r----- 1 oracle oinstall 51M Jun 3 04:31 dave01.dbf

我们之前是100M,现在变成50M了。

原文地址:https://www.cnblogs.com/moonfans/p/4207734.html