| Lesson 6 | Recovering a Lost Datafile |
| Objective | Restore and recover a missing or corrupted datafile using RMAN and SQL*Plus (Oracle 11g–23c) |
When a datafile is deleted, corrupted, or inaccessible, the correct response depends on the file’s role (SYSTEM/SYSAUX/UNDO/TEMP vs. user tablespace) and whether the database runs in ARCHIVELOG mode. This guide shows a safe, RMAN-first workflow and the necessary variations for special cases (SYSTEM/SYSAUX, UNDO, TEMP, ASM/OMF, and NOARCHIVELOG).
-- Identify files needing media recovery
SELECT file#, name, error FROM v$recover_file;
-- Cross-check known datafiles
SELECT file#, name, status FROM v$datafile ORDER BY file#;
-- Alert log & trace files (for path and error details)
-- See: $ORACLE_BASE/diag/.../alert_<SID>.log
For user tablespaces, keep the database open and isolate the failure:
-- Option A: Take just the datafile offline (11g+)
ALTER DATABASE DATAFILE <file_id_or_path> OFFLINE;
-- Option B: Take the affected tablespace offline
ALTER TABLESPACE users OFFLINE IMMEDIATE;
Note: You cannot take SYSTEM or SYSAUX offline; see Special cases.
This is the safest, most complete approach. In ARCHIVELOG mode, RMAN restores the missing file from backup and applies archived redo to bring it current.
$ rman target /
-- Confirm file numbers & paths
RMAN> REPORT SCHEMA;
-- Restore and recover a single file (by number or path)
RMAN> RESTORE DATAFILE 7;
RMAN> RECOVER DATAFILE 7;
-- Bring online if you took it offline
SQL> ALTER DATABASE DATAFILE 7 ONLINE;
-- If you took the tablespace offline
SQL> ALTER TABLESPACE users ONLINE;
RMAN> RUN {
SET NEWNAME FOR DATAFILE 7 TO '/u02/oradata/PROD/users01.dbf';
RESTORE DATAFILE 7;
SWITCH DATAFILE 7;
RECOVER DATAFILE 7;
}
If you must use SQL*Plus, you still need a valid copy of the datafile (from OS-level backup or another location):
-- Copy the backup file to the original (or new) location at the OS level, then:
SQL> RECOVER DATAFILE '<full_path_to_file>';
-- Apply media recovery prompts; then:
SQL> ALTER DATABASE DATAFILE '<full_path_to_file>' ONLINE;
DROP TABLESPACE temp INCLUDING CONTENTS AND DATAFILES;
CREATE TEMPORARY TABLESPACE temp TEMPFILE '/u02/oradata/PROD/temp01.dbf' SIZE 2G;
db_create_file_dest is set, RMAN restores to the managed location; use SET NEWNAME only when relocating.In NOARCHIVELOG, you can only restore to the time of the last consistent backup. You cannot roll forward with archived redo:
RMAN> SHUTDOWN IMMEDIATE;
RMAN> STARTUP MOUNT;
RMAN> RESTORE DATABASE;
RMAN> RECOVER DATABASE; -- minimal/none (no archivelogs)
RMAN> ALTER DATABASE OPEN RESETLOGS;
-- Check datafile status
SELECT file#, name, status FROM v$datafile ORDER BY file#;
-- Validate tablespace online
SELECT tablespace_name, status FROM dba_tablespaces ORDER BY 1;
-- Optional: validate backups proactively
RMAN> VALIDATE DATABASE;
REPORT SCHEMA in RMAN or query v$datafile to map file numbers to paths.