如何理解Oracle 数据文件中的reuse属性

发布时间:2021-11-06 09:58:15 作者:柒染
来源:亿速云 阅读:427

如何理解Oracle 数据文件中的reuse属性,很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

Oracle 表空间创建参数

当我们对表空间添加数据文件的时候,有一个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里的数据将全部丢失。

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

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. Oracle 创建表空间,用户,赋值(简装)
  2. oracle12C 新特性——之在线重命名和移动数据文件

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

oracle

上一篇:MySQL数据库备份种类以及常用备份工具有哪些

下一篇:如何进行Linux与windows文件传输

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》