Tuesday, July 31, 2012

Moving or renaming Oracle datafiles.

If you need to move or rename Oracle datafiles you can do it two different ways.

You can use alter database or alter tablespace clause to move or rename datafiles.

With alter database you need to shutdown the database instance to make needed changes.

And with alter tablespace clause without need to shutdown database instance. But you can't use alter tablespace for datafiles moving/renaming with SYSTEM, UNDO or TEMP tablespaces. And if you are using alter tablespace for datafiles moving/renaming it will take those datafiles offline. And this means that if your applications are trying to use data from those datafiles during offline they'll get errors. So usually you don't want to use this clause with production databases. Changes like these in production enviroment need to be tested first in test enviroment and then done in maintenance window with alter database clause and clean shutdown.

You can also use RMAN for renaming/moving the datafiles but that also needs to use offline.
Althought with RMAN the offline time is smaller because you can copy datafile into new path before you take it offline. But I prefer the planned maintenance window with alter database clause. 

Datafiles moving with alter tablespace clause:
----
1. Change the tablespace offline.

sqlplus / as sysdba
SQL> alter tablespace test_data offline;

2. Renaming/moving the datafile using operating system commands.
For example in linux:
cp /data/oracle/datafiles/test_data_01.dbf /data_new/oracle/datafiles/test_data_01.dbf
or renaming it at the same time:
cp /data/oracle/datafiles/test_data_01.dbf /data_new/oracle/datafiles/test_data_05.dbf

3. Use the alter tablespace clause to rename the file in the database.

SQL> alter tablespace test_data rename datafile '/data/oracle/datafiles/test_data_01.dbf' TO '/data_new/oracle/datafiles/test_data_01.dbf';

or renaming it at the same time:
SQL> alter tablespace test_data rename datafile '/data/oracle/datafiles/test_data_01.dbf' TO '/data_new/oracle/datafiles/test_data_05.dbf';

4. Change the tablespace back online. After this you can again use this tablespace all datafiles normally.
SQL> alter tablespace test_data online;

----


Datafiles moving with alter database clause:
----
1. Shutdown the database instance.
sqlplus / as sysdba
SQL> shutdown immediate;
2. Renaming/moving the datafile using operating system commands.
For example in linux:
cp /data/oracle/datafiles/test_data_01.dbf /data_new/oracle/datafiles/test_data_01.dbf
or renaming it at the same time:
cp /data/oracle/datafiles/test_data_01.dbf /data_new/oracle/datafiles/test_data_05.dbf
3.Start database into mount and rename/move datafiles in the database with alter database clause. You need to use full path and filenames.
SQL> startup mount;

SQL> alter database rename file '/data/oracle/datafiles/test_data_01.dbf' TO '/data_new/oracle/datafiles/test_data_01.dbf';

or renaming it at the same time:
SQL> alter database rename file '/data/oracle/datafiles/test_data_01.dbf' TO '/data_new/oracle/datafiles/test_data_05.dbf';
4. Open the database.
SQL> alter database open;
----

No comments:

Post a Comment