在Oracle12.1之前的版本中要重命名数据文件或移动数据文件需要关闭数据库或把表空间/数据文件置为offline状态才可以,参考之前总结的
要实现这一功能需要使用ALTERDATABASEMOVEDATAFILE语句,语法如下ALTER DATABASE MOVE DATAFILE ( 'filename' | 'ASM_filename' | file_number )
[ TO ( 'filename' | 'ASM_filename' ) ]
[ REUSE ] [ KEEP ]
下面在12.2版本测试这一功能zx@ORA12C>select * from v$version;
BANNER CON_ID
-------------------------------------------------------------------------------- ----------
Oracle Database 12c Enterprise Edition Release 12.2.0.1.0 - 64bit Production 0
PL/SQL Release 12.2.0.1.0 - Production 0
CORE12.2.0.1.0Production 0
TNS for Linux: Version 12.2.0.1.0 - Production 0
NLSRTL Version 12.2.0.1.0 - Production 0
1、创建测试表空间及数据文件:sys@ORA12C>create tablespace t_move datafile '/home/oracle/t_move.dbf' size 50m ;
Tablespace created.
sys@ORA12C>col name for a50
sys@ORA12C>select d.name,d.status from v$datafile d,v$tablespace t where t.ts#=d.ts# and t.name='T_MOVE';
NAME STATUS
-------------------------------------------------- -------
/home/oracle/t_move.dbf ONLINE
现在数据文件'/home/oracle/t_move.dbf'为ONLINE状态
2、执行重命名操作sys@ORA12C>alter database move datafile '/home/oracle/t_move.dbf' to '/home/oracle/t_move01.dbf';
Database altered.
sys@ORA12C>select d.name,d.status from v$datafile d,v$tablespace t where t.ts#=d.ts# and t.name='T_MOVE';
NAME STATUS
-------------------------------------------------- -------
/home/oracle/t_move01.dbf ONLINE
sys@ORA12C>!ls -l /home/oracle/t_move01.dbf
-rw-r----- 1 oracle oinstall 52436992 Jul 11 16:07 /home/oracle/t_move01.dbf
sys@ORA12C>!ls -l /home/oracle/t_move.dbf
ls: cannot access /home/oracle/t_move.dbf: No such file or directory
可以看到文件名从t_move.dbf改为了t_move01.dbf,原文件已经不存在了。
3、执行移动目录操作sys@ORA12C>alter database move datafile '/home/oracle/t_move01.dbf' to '/u01/app/oracle/oradata/ora12c/t_move01.dbf';
Database altered.
sys@ORA12C>!ls -l /u01/app/oracle/oradata/ora12c/t_move01.dbf
-rw-r----- 1 oracle oinstall 52436992 Jul 11 16:10 /u01/app/oracle/oradata/ora12c/t_move01.dbf
sys@ORA12C>!ls -l /home/oracle/t_move01.dbf
ls: cannot access /home/oracle/t_move01.dbf: No such file or directory
sys@ORA12C>select d.name,d.status from v$datafile d,v$tablespace t where t.ts#=d.ts# and t.name='T_MOVE';
NAME STATUS
-------------------------------------------------- -------
/u01/app/oracle/oradata/ora12c/t_move01.dbf ONLINE
从上面的结果可以看到数据文件从'/home/oracle'目录移动到了'/u01/app/oracle/oradata/ora12c'目录下了。
4、copy数据文件到目标目录,保留原文件sys@ORA12C>alter database move datafile '/u01/app/oracle/oradata/ora12c/t_move01.dbf' to '/home/oracle/t_move01.dbf' keep;
Database altered.
sys@ORA12C>select d.name,d.status from v$datafile d,v$tablespace t where t.ts#=d.ts# and t.name='T_MOVE';
NAME STATUS
-------------------------------------------------- -------
/home/oracle/t_move01.dbf ONLINE
sys@ORA12C>!ls -l /home/oracle/t_move01.dbf
-rw-r----- 1 oracle oinstall 52436992 Jul 11 16:15 /home/oracle/t_move01.dbf
sys@ORA12C>!ls -l /u01/app/oracle/oradata/ora12c/t_move01.dbf
-rw-r----- 1 oracle oinstall 52436992 Jul 11 16:15 /u01/app/oracle/oradata/ora12c/t_move01.dbf
从上面的结果可以看到数据文件改为了/home/oracle/t_move01.dbf,但原来的数据文件仍然被保留。
5、移动数据文件到ASM存储--文件系统到ASM
ALTER DATABASE MOVE DATAFILE '/u01/oracle/rbdb1/user1.dbf'
TO '+dgroup_01/data/orcl/datafile/user1.dbf';
--ASM到ASM
ALTER DATABASE MOVE DATAFILE '+dgroup_01/data/orcl/datafile/user1.dbf'
TO '+dgroup_02/data/orcl/datafile/user1.dbf';