Wednesday, July 31, 2013

Rename or Move a Datafiles to Different location


***TO MOVE THE DATAFILES TO DIFFERENT LOCATION/PARTITION***

________________________________________________________________________________________________
Scenario1: RENAME AND OR MOVE DATAFILE(S) WITH THE DATABASE SHUT DOWN
________________________________________________________________________________________________

1. If the database is up, shut it down.

2. Copy the datafile to the new name/location at operating system level.

3. Mount the database.

Ø  STARTUP MOUNT

This command will read the control file but will not mount the datafiles.

4. Rename the file inside Oracle.

Ø  ALTER DATABASE RENAME FILE
'/FULL_PATH_OF_OLD_LOCATION/AND_DATAFILE_NAME.DBF'
TO
'/FULL_PATH_OF_NEW_LOCATION/AND_DATAFILE_NAME.DBF';

Do this for all the datafiles that were renamed or moved at the operating system level.
5. Open the database.
Ø  ALTER DATABASE OPEN;
6. Query v$dbfile to confirm that the changes made were correct.
Ø  SELECT * FROM V$DBFILE;
7. Remove the datafile(s) from the old location at the operating system level.



_____________________________________________________________________________________________
Scenario 2:  RENAME AND OR MOVE A LOGFILE
_____________________________________________________________________________________________
1. Shutdown the database.

2. Copy the logfile to the new name/location at operating system level.

3. Mount the database.

Ø  STARTUP MOUNT

4. Rename the file.

Ø   ALTER DATABASE RENAME FILE
      '/FULL_PATH_OF_OLD_LOCATION/AND_REDO_LOG_NAME.LOG'
       TO
      '/FULL_PATH_OF_NEW_LOCATION/AND_REDO_LOG_NAME.LOG';

5. Open the database.

Ø   ALTER DATABASE OPEN;

6. Remove the logfile(s) from the old location at the operating system level.

________________________________________________________________________________________________________
Scenario3: MOVE DATAFILE ONLINE or OPEN STATE or MOVE DATAFILE(S) WITH THE DATABASE OPEN
_________________________________________________________________________________________________________________________

Note: 
Datafiles can be renamed or moved while the database is open. However, the tablespace must be made READ-ONLY. This will allow users to select from the tablespace, but prevents them from doing inserts, updates, and deletes. The amount of time the tablespace is required to be read only will depend on how large the datafile(s) are and how long it takes to copy the datafile(s) to the new location. 

      Making the tablespace read only freezes the file header, preventing updates from being made to the file header. Since this datafile is then at a read only state, it is possible to copy the file while the database is open.

To do this you must follow these steps:

1. Determine how many datafiles are associated with the tablespace.

Ø   SELECT FILE_NAME, STATUS FROM DBA_DATA_FILES
WHERE TABLESPACE_NAME = '<YOUR_TABLESPACE_NAME>';

2. Make sure that all datafiles returned have the status AVAILABLE.

3. Make the tablespace is read only.

Ø   ALTER TABLESPACE <YOUR_TABLESPACE_NAME> READ ONLY;

4. Make sure that the tablespace is defined as read only in the data dictionary.

Ø   SELECT TABLESPACE_NAME, STATUS FROM DBA_TABLESPACES
WHERE TABLESPACE_NAME = '<YOUR_TABLESPACE_NAME>';

TABLESPACE_NAME              STATUS
------------------------------ ------------------------
<YOUR_TABLESPACE_NAME> READ ONLY

5. Copy the datafile(s) to the new location using the operating system copy command. Once the datafile(s) have been copied to the new location compare the sizes of the datafiles. Make sure that the sizes match.

NOTE:

The same method could be used to rename a datafile as a kind of in-place copy.
However, on Windows that fails with a message like:
"Cannot rename USERS01: It is being used by another person or program.
Close any programs that might be using the file and try again."
6. Once the datafiles have been copied to the new location alter the tablespace offline.

Ø    ALTER TABLESPACE <YOUR_TABLESPACE_NAME> OFFLINE;

·          At this point the tablespace is not accessible to users.

7. Once the tablespace is offline you will need to rename the datafile(s) to the new location. This updates the entry for the datafile(s) in the controlfile.

Ø   ALTER DATABASE RENAME FILE
           '/FULL_PATH_OF_OLD_LOCATION/AND_DATAFILE_NAME.DBF'
          TO
        '/FULL_PATH_OF_NEW_LOCATION/AND_DATAFILE_NAME.DBF';

·          You will need to do this for all datafiles associated with this tablespace.

You can use the ALTER TABLESPACE ... RENAME DATAFILE syntax as well.

8. Once the alter database statement has been processed for the datafile(s) you can bring the table space online.

Ø  ALTER TABLESPACE <YOUR_TABLESPACE_NAME> ONLINE;

9. After you bring the tablespace back online you can make the tablespace read/write again.

Ø   ALTER TABLESPACE <YOUR_TABLESPACE_NAME> READ WRITE;

10. You can check the status of the tablespace to make sure it is read/write.
You can also verify that the controlfile has been updated by doing the following:

Ø  ALTER DATABASE BACKUP CONTROLFILE TO TRACE;

This will produce a readable copy of the contents of your controlfile which will be placed in your user_dump_dest directory.

Optionally, you can query V$DATAFILE, which gets information from the controlfile as well.

11. Remove the datafile(s) from the old location at the O/S level.


No comments:

Post a Comment