The post offers some procedures for renaming and relocating datafiles in a single tablespace. You must have the ALTER TABLESPACE
system privilege to rename datafiles of a single tablespace.
Renaming Datafiles in a Single Tablespace
To rename datafiles from a single tablespace, complete the following steps:
- Take the non-
SYSTEM
tablespace that contains the datafiles offline.For example:
ALTER TABLESPACE users OFFLINE NORMAL;
- Rename the datafiles using the operating system.
- Use the
ALTER TABLESPACE
statement with theRENAME DATAFILE
clause to change the filenames within the database.For example, the following statement renames the datafiles /u02/oracle/rbdb1/user1.dbf and /u02/oracle/rbdb1/user2.dbf to/u02/oracle/rbdb1/users01.dbf and /u02/oracle/rbdb1/users02.dbf, respectively:
ALTER TABLESPACE users
RENAME DATAFILE '/u02/oracle/rbdb1/user1.dbf',
'/u02/oracle/rbdb1/user2.dbf'
TO '/u02/oracle/rbdb1/users01.dbf',
'/u02/oracle/rbdb1/users02.dbf';The new files must already exist; this statement does not create the files. Also, always provide complete filenames (including their paths) to properly identify the old and new datafiles. In particular, specify the old datafile name exactly as it appears in the
DBA_DATA_FILES
view of the data dictionary. - Back up the database. After making any structural changes to a database, always perform an immediate and complete backup.
Relocating and Renaming Datafiles in a Single Tablespace
Here is an example that illustrates the steps involved for relocating a datafile.
Assume the following conditions:
- An open database has a tablespace named
users
that is made up of datafiles all located on the same disk. - The datafiles of the
users
tablespace are to be relocated to different and separate disk drives. - You are currently connected with administrator privileges to the open database.
- You have a current backup of the database.
Complete the following steps:
- Identify the datafile names of interest.
The following query of the data dictionary view
DBA_DATA_FILES
lists the datafile names and respective sizes (in bytes) of theusers
tablespace:SELECT FILE_NAME, BYTES FROM DBA_DATA_FILES
WHERE TABLESPACE_NAME = 'USERS';
FILE_NAME BYTES
------------------------------------------ ----------------
/U02/ORACLE/RBDB1/USERS01.DBF 102400000
/U02/ORACLE/RBDB1/USERS02.DBF 102400000 - Take the tablespace containing the datafiles offline, or shut down the database and restart and mount it, leaving it closed. Either option closes the datafiles of the tablespace.
- Copy the datafiles to their new locations and rename them using the operating system.
Note:You can execute an operating system command to copy a file by using the SQL*Plus
HOST
command. - Rename the datafiles within Oracle.
The datafile pointers for the files that make up the
users
tablespace, recorded in the control file of the associated database, must now be changed from the old names to the new names.If the tablespace is offline but the database is open, use the
ALTER TABLESPACE ... RENAME DATAFILE
statement. If the database is mounted but closed, use theALTER DATABASE ... RENAME FILE
statement.ALTER TABLESPACE users
RENAME DATAFILE '/u02/oracle/rbdb1/users01.dbf',
'/u02/oracle/rbdb1/users02.dbf'
TO '/u03/oracle/rbdb1/users01.dbf',
'/u04/oracle/rbdb1/users02.dbf'; - Bring the tablespace online, or open the database.
If the
users
tablespace is offline and the database is open, bring the tablespace back online. If the database is mounted but closed, open the database. - Back up the database. After making any structural changes to a database, always perform an immediate and complete backup.
Copying a File on a Local File System
This section includes an example that uses the COPY_FILE procedure in the DBMS_FILE_TRANSFER package to copy a file on a local file system. The following example copies a binary file named db1.dat from the /usr/admin/source directory to the /usr/admin/destination directory as db1_copy.dat on a local file system:
- In SQL*Plus, connect as an administrative user who can grant privileges and create directory objects using SQL.
- Use the SQL command CREATE DIRECTORY to create a directory object for the directory from which you want to copy the file. A directory object is similar to an alias for the directory. For example, to create a directory object called SOURCE_DIR for the /usr/admin/source directory on your computer system, execute the following statement:
- Use the SQL command CREATE DIRECTORY to create a directory object for the directory into which you want to copy the binary file. For example, to create a directory object called DEST_DIR for the /usr/admin/destination directory on your computer system, execute the following statement:
- Grant the required privileges to the user who will run the COPY_FILE procedure. In this example, the strmadmin user runs the procedure.
- Connect as strmadmin user:
- Run the COPY_FILE procedure to copy the file:
CREATE DIRECTORY SOURCE_DIR AS '/usr/admin/source';
CREATE DIRECTORY DEST_DIR AS '/usr/admin/destination';
GRANT EXECUTE ON DBMS_FILE_TRANSFER TO strmadmin;
GRANT READ ON DIRECTORY source_dir TO strmadmin;
GRANT WRITE ON DIRECTORY dest_dir TO strmadmin;
CONNECT strmadmin/strmadminpw
BEGIN
DBMS_FILE_TRANSFER.COPY_FILE(
source_directory_object => 'SOURCE_DIR',
source_file_name => 'db1.dat',
destination_directory_object => 'DEST_DIR',
destination_file_name => 'db1_copy.dat');
END;
/