Oracle 資料檔案 reuse 屬性 說明

dbhelper發表於2014-12-04

有關表空間建立的相關引數,參考:

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.

1If 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,則保留原有的大小。

2If 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

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了。

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/8494287/viewspace-1356165/,如需轉載,請註明出處,否則將追究法律責任。

相關文章