天天看點

Oracle 資料檔案 reuse 屬性 說明

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

       Oracle 表空間 建立參數 說明

       當我們對表空間添加資料檔案的時候,有一個reuse 屬性。 10g的官網對這個參數的說明如下:

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 将忽略該參數。

       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:

<a href="http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/clauses004.htm%23SQLRF01602">http://download.oracle.com/docs/cd/B19306_01/server.102/b14200/clauses004.htm#SQLRF01602</a>

       在Oracle 11g的官方文檔裡沒有搜到相關的資訊。 因為手頭還沒有11g的庫,是以也不好測試。 這篇blog裡測試的是基于Oracle 10g環境。

下面我們來做一些測試:

1. 建立一個表空間Dave

SQL&gt; show user;

USER is "SYS"

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

Tablespace created.

2. 建立表anqing,并指定存儲表空間dave

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

Table created.

SQL&gt; commit;

Commit complete.

SQL&gt; select count(*) from anqing;

  COUNT(*)

----------

     50391

SQL&gt; set wrap off;

SQL&gt; 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&gt; 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&gt; alter tablespace dave add datafile '/u01/app/oracle/oradata/dave2/dave02.dbf' size 50M reuse;

Tablespace altered.

SQL&gt;

4. 保持表空間的狀态,然後使用reuse 來添加資料檔案

SQL&gt; 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

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 差別

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

Database altered.

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

-- 依舊報錯,因為此時資料檔案dave01.dbf 的資訊還記錄在資料字典裡。

-- 将資料檔案還原回來

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

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

ORA-01113: file 6 needs media recovery

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

SQL&gt; recover datafile 6;

Media recovery complete.

6. 使用alter tablespace dave drop datafile 指令

該指令在删除控制檔案和實體檔案,是以沒有可用的意義。

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

[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&gt;drop tablespace dave including contents and datafiles;

       該指令也可以指定同時删除實體檔案,但那樣我們的測試就沒辦法完成,是以我們不删除datafile,僅從控制檔案裡删除表空間。

SQL&gt; drop tablespace dave including contents;

Tablespace dropped.

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

-- 重用成功

看一下資料檔案大小:

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

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

我們之前是100M,現在變成50M了。

繼續閱讀