In many situations, a datafile or logfile must be renamed inside
Oracle. Whereas
the contents of the file remain valid, you need to define a new
physical name or
location for it. For example:
- You want to move a database file to a different disk for
performance or
maintenance reasons.
- You have restored a datafile from backup, but the disk where it
should
normally be placed has crashed and you need to relocate it to a
different disk.
- You have moved or renamed a datafile at operating system level but
forgot to
rename it within Oracle. At startup, you get ORA-01157 and ORA-01110.
If the
database is up and you try to shut it down normal or immediate, you get
ORA-01116 ad ORA-01110.
- You have multiple databases on the same machine and you need to
rename certain
database files to prevent collision and confusion.
Managing
Tablespaces and DataFiles
RENAME OR MOVE A DATAFILE FOR A SINGLE TABLESPACE
1. Make the tablespace offline.
ALTER TABLESPACE <TABLESPACE_NAME> OFFLINE;
2. Copy the datafile(s) to the new location using the
operating system copy
command.
3. Rename the datafile to the
new location. This updates the entry for the datafile(s) in the
controlfile.
ALTER TABLESPACE <TABLESPACE_NAME> RENAME DATAFILE
'/old_path/name.dbf' TO
'/old_path/name.dbf';
4. Once the alter database statement has been processed for the
datafile(s) you
can bring the tablespace online.
ALTER TABLESPACE <TABLESPACE_NAME> ONLINE;
Another method is to have the database in mount state:
1. SHUTDOWN IMMEDIATE
2. STARTUP MOUNT
3. Copy the datafile(s) to the new location using the operating system
copy
command
4. ALTER DATABASE RENAME FILE
'/old_path/name.dbf'' TO '/new_path/name.dbf';
5. 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.
ADD A
DATAFILE
Adding a File is very simple:
ALTER TABLESPACE <TABLESPACE_NAME> ADD DATAFILE
'<file
specific> SIZE xxxM;
ALTER TABLESPACE
tbs_03 ADD DATAFILE 'tbs_f04.dbf' SIZE 100M AUTOEXTEND ON NEXT 30m
MAXSIZE UNLIMITED;
DROP A
TABLESPACE
Dropping a Tablespace is very simple:
DROP TABLESPACE <TABLESPACE_NAME> INCLUDING CONTENTS AND
DATAFILES CASCADE;
You can use just DROP TABLESPACE <tablespace name>, but the
data files won’t be dropped and the tablespace must be empty (no
objects exist in the tablespace); otherwise, Oracle will return an
error. Further, the DROP TABLESPACE <tablespace name> INCLUDING
CONTENTS command drops all the objects, if they exist in the
tablespace. Next, by including the CASCADE option you can drop foreign
key references from objects in the other tablespaces to the one you’re
dropping. The DATAFILES option, which is available in Oracle9i, will
physically delete the related data files as well.
CREATE
A TABLESPACE
CREATE TABLESPACE <TABLESPACE_NAME>
DATAFILE '/opt/oracle/oradata/FGUARD/fg_data01.dbf' Size 800M
AutoExtend On Next 100M Maxsize 2000M,
'/opt/oracle/oradata/FGUARD/fg_data02.dbf' Size 800M AutoExtend On Next
100M Maxsize 2000M
EXTENT MANAGEMENT LOCAL
SEGMENT SPACE MANAGEMENT AUTO;
INCREASE
THE TABLESPACE SIZE
If you get a message like:
ORA-1654: unable to extend index IFSSYS.SYS_C002067 by 64 in
tablespace FG_DATA
Then you will need to increase the datafile size associated to that
Tablespace, so perform the following steps:
1- First, see how filled up the current datafile(s) are:
select FILE_NAME, BYTES/1024/1024
"Size MB", BLOCKS, MAXBLOCKS from dba_data_files where tablespace_name
= 'PPP_DATA';
FILE_NAME
Size MB BLOCKS MAXBLOCKS
----------------------------------------------
--------- --------- ------------------
/usr/local/oracle/oradata/webct/fg_data01.dbf
400 524284 524228
Notice that the BLOCKS 524284 = MAXBLOCKS 524288, that means that the
tablespace is maxed out.
Now we can add another datafile to that tablespace:
alter tablespace FG_DATA add
datafile '/usr/local/oracle/oradata/webct/fg_data02.dbf' size 1024M
autoextend on next 100M maxsize 4096M;
or increase the existing one:
alter database datafile
'/usr/local/oracle/oradata/webct/fg_data02.dbf' resize 800m;
Managing Control Files
The current location of the controlfiles can be queried from the
V$CONTROLFILE view, as shown below.
select name from v$controlfile;
NAME
---------------------------------------------------------------
/u01/app/oracle/product/9.2.0/oradata/DEVSOL/control01.ctl
/u01/app/oracle/product/9.2.0/oradata/DEVSOL/control02.ctl
/u01/app/oracle/product/9.2.0/oradata/DEVSOL/control03.ctl
In order to rename or move these files we must alter the value of the
control_files instance parameter.
show parameter control_files
NAME
VALUE
----------------
------------------------------------------
------------------------------
control_files
/u01/app/oracle/product/9.2.0/oradata/DEVSOL/control01.ctl,
/u01/app/oracle/product/9.2.0/oradata/DEVSOL/control02.ctl,
/u01/app/oracle/product/9.2.0/oradata/DE
To move or rename a controlfile do the following:
* Alter the control_files parameter using the ALTER
SYSTEM comamnd.
* Shutdown the database.
* Rename the physical file on the OS.
* Start the database.
The following SQL*Plus output shows how this is done for an instance
using an spfile. For instances using a pfile replace the spfile
manipulation steps with an amendment of the parameter in the init.ora
file.
ALTER SYSTEM SET
control_files='/new_path/control01.ctl',
'/new_path/control02.ctl', '/new_path/control03.ctl' SCOPE=SPFILE;
SHUTDOWN IMMEDIATE
!mv /old_path/control01.ctl
/new_path/control01.ctl
!mv
/old_path/control02.ctl /new_path/control02.ctl
!mv
/old_path/control03.ctl /new_path/control03.ctl
STARTUP
Repeating the initial query shows that the the CONTROLFILES has been
renamed in the data dictionary.
select name from v$controlfile;
Managing Logfiles
The current location of the logfiles can be queried from the V$LOGFILE
view, as shown below.
SELECT member FROM v$logfile;
MEMBER
--------------------------------------------------------
/u01/app/oracle/product/9.2.0/oradata/DEVSOL/redo01.log
/u01/app/oracle/product/9.2.0/oradata/DEVSOL/redo02.log
/u01/app/oracle/product/9.2.0/oradata/DEVSOL/redo03.log
To move or rename a logfile do the following:
* Shutdown the database.
* Rename the physical file on the OS.
* Start the database in mount mode.
* Issue the ALTER DATABASE RENAME FILE command to
rename the file within the Oracle dictionary.
* Open the database.
The following SQL*Plus output shows how this is done.
SHUTDOWN IMMEDIATE
!mv /old_path/redo01.log
/new_path/redo01.log
!mv /old_path/redo02.log /new_path/redo02.log
!mv /old_path/redo03.log /new_path/redo03.log
STARTUP MOUNT
ALTER DATABASE RENAME FILE
'/old_path/redo01.log'' TO '/new_path/redo01.log';
ALTER DATABASE RENAME FILE
'/old_path/redo02.log'' TO '/new_path/redo02.log';
ALTER DATABASE RENAME FILE
'/old_path/redo03.log'' TO '/new_path/redo03.log';
ALTER DATABASE OPEN;
Repeating the initial query shows that the the logfile has been renamed
in the data dictionary.
SELECT member FROM v$logfile;