日期:2014-05-16  浏览次数:20397 次

更改Oracle数据文件路径(原创)

近日,群里的朋友经常遇到数据文件需要删除或者改名的问题,这里,笔者进行了下总结,也算是抛砖引玉,希望大家拍砖。
删除数据文件
在10g中,如果是未被Oracle写入数据的数据文件则可以被直接删除,如果是已经被写入数据的数据文件,则无法被直接删除。请见下例
SQL> create tablespace test datafile '/home/oracle/test1.dbf' size 1m;
Tablespace created.
SQL>? create table test tablespace test
? 2? as
? 3* select * from dba_objects where rownum <=8000
Table created.
SQL> select SEGMENT_NAME,BYTES/1024/1024 Mb from user_segments where SEGMENT_NAME='TEST';
SEGMENT_NAME???????????????? MB????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????
------------------????????? ------------?????????????????????????????????????????????????????????????????
TEST????????????????????? ? ?? .875?
SQL> alter tablespace test add datafile '/home/oracle/test2.dbf' size 50m;
Tablespace altered.
SQL> insert into test select * from dba_objects ;
50323 rows created.
SQL> commit;
Commit complete.
SQL> select SEGMENT_NAME,BYTES/1024/1024 from dba_segments where SEGMENT_NAME='TEST'
SEGMENT_NAME???????? BYTES/1024/1024
-------------------- ---------------
TEST?????????????????????????????? 7
QL> alter tablespace test add datafile '/u01/app/test3.dbf' size 1m;
Tablespace altered.
SQL> alter tablespace test drop datafile '/u01/app/test3.dbf';
Tablespace altered.
SQL> alter tablespace test drop datafile '/u01/app/test2.dbf';
alter tablespace test drop datafile '/u01/app/test2.dbf'
*
ERROR at line 1:
ORA-03262: the file is non-empty

可以看到,只有非空的数据文件才能进行删除,已经写入数据的数据文件不能进行删除。
更改数据文件位置
更改数据文件位置,笔者认为有3种方法
1、利用rman的set newname来更改数据文件位置,对应用影响较小。

2、利用手工备份,然后拷贝数据到指定位置,原理和rman类似。

3、利用exp导出数据,重建表空间后再倒入数据,笔者认为该方法对应用影响较大,需结合实际情况考虑,这里不做详细介绍。
利用rman的set newname来更改数据文件位置

SQL> select file_name,status,file_id from dba_data_files;
FILE_NAME???????????????????????????????????????????????????????? STATUS??????????????????? FILE_ID
-------------------------------------??????????????? ---------------------------????????
---------------------------

/u01/app/oradata/orcl/users01.dbf??????? ? ? ???? AVAILABLE????????????????? 5
/u01/app/oradata/orcl/sysaux01.dbf?????????????? AVAILABLE????????????????? 2
/u01/app/oradata/orcl/undotbs01.dbf???????????? AVAILABLE????????????????? 3
/u01/app/oradata/orcl/system01.dbf?????????????? AVAILABLE????????????????? 1
/u01/app/oradata/orcl/example01.dbf????????? ?? AVAILABLE????????????????? 4
/home/oracle/test1.dbf?????????????????????????????????? ? ?? ? AVAILABLE?????????? ? ????? 6
/home/oracle/test2.dbf????????????????????????????????? ? ?? ?? AVAILABLE??????? ? ???????? 7
通过使用rman的set newname命令更改数据文件位置,整个过程如下:
1、由于 set newname命令是更改rman repository的信息 ,并根据