You are on page 1of 5

Example:

18:30:32 SQL> delete scott.emp;


13 rows deleted.
18:30:38 SQL> commit;
Commit complete.

After commit user found his made delete operation on wrong table now he
wants to back all deleted records.

The perform a flashback of SCOTT.EMP

1. obtain SCN information about deleted records and commit;


SQL> select current_scn from v$database;
CURRENT_SCN
-----------
2456211

2. Ensure that enough undo data exists to rewind the table to the specified
target.
SQL> SELECT NAME, VALUE/60 MINUTES_RETAINED
2 FROM V$PARAMETER
3 WHERE NAME = 'undo_retention';

NAME MINUTES_RETAINED
------------------------------ ----------------
undo_retention 60

3. Ensure that row movement is enabled for all objects that you are
rewinding with Flashback Table.
SQL> alter table SCOTT.EMP enable row movement;
Table altered.

4. Determine whether the table that you intend to flash back has
dependencies on other tables. If dependencies exist, then decide whether to
flash back these tables as well.
SQL> SELECT other.owner, other.table_name
2 FROM sys.all_constraints this, sys.all_constraints other
3 WHERE this.owner = 'SCOTT'
4 AND this.table_name = 'EMP'
5 AND this.r_owner = other.owner
6 AND this.r_constraint_name = other.constraint_name
7 AND this.constraint_type='R';
OWNER TABLE_NAME
------------------------------ ------------------------------
SCOTT DEPT
5. Execute a FLASHBACK TABLE statement for the objects that you want to
flash back.
SQL> flashback table SCOTT.EMP
2 to timestamp to_timestamp ('2008-05-17 18:30:31','YYYY-MM-DD
HH24:MI:SS');
Flashback complete.

NOTE: User performed delete operation at 18:30:31 so he flashback table


with TIMESTAMP.

6. Optionally, query the table to check the data.


SQL> select count(*) from scott.emp;
COUNT(*)
----------
13

NOTE: Flashback table operation is complete and lost data is


recovered.

Keeping Triggers Enabled During Flashback Table

It is recommended to add ENABLE TRIGGER keyword with FLASHBACK


TABLE statement. For enable database trigger which create on flashback
table.

For example:

SQL> Flashback table SCOTT.EMP to timestamp to_timestamp (‘2008-


05-17 18:30:31’,’YYYY-MM-DD HH24:MI:SS’) ENABLE TRIGGERS;

CREATE TABLE t
ENABLE ROW MOVEMENT AS
SELECT owner, table_name, tablespace_name
FROM all_tables
WHERE 1=2;

desc t

SELECT table_name, row_movement


FROM user_tables;

CREATE RESTORE POINT zero;

INSERT INTO t
SELECT owner, table_name, tablespace_name
FROM all_tables
WHERE owner = 'SYS';

COMMIT;

CREATE RESTORE POINT one;

INSERT INTO t
SELECT owner, table_name, tablespace_name
FROM all_tables
WHERE owner = 'WMSYS';

COMMIT;

CREATE RESTORE POINT two;

INSERT INTO t
SELECT owner, table_name, tablespace_name
FROM all_tables
WHERE owner = 'CTXSYS';

COMMIT;

SELECT owner, COUNT(*)


FROM t
GROUP BY owner;

SELECT scn, time, name


FROM gv$restore_point;

FLASHBACK TABLE t TO RESTORE POINT two;

SELECT owner, COUNT(*)


FROM t
GROUP BY owner;

FLASHBACK TABLE t TO RESTORE POINT one;

SELECT owner, COUNT(*)


FROM t
GROUP BY owner;

FLASHBACK TABLE t TO RESTORE POINT zero;


SELECT owner, COUNT(*)FROM t GROUP BY owner;

Procedures with and Example:


---------------------------------
1)Create a Tablespace and Table inside it.

SQL> create tablespace test_restore datafile '/oradata2/test_restore01.dbf' size 5m;


Tablespace created.

SQL> create table test tablespace test_restore as select level a1 from dual connect by level <99; Table
created.

2)Note the SCN and Drop the Tablespace with including contents option.
---------------------------------------------------------------------------
SQL> select current_scn from v$database;
CURRENT_SCN
-----------
938686

SQL> drop tablespace test_restore including contents;


Tablespace dropped.

3)Mount the database.

SQL> shutdown immediate


Database closed.
Database dismounted.
ORACLE instance shut down.

SQL> startup mount


ORACLE instance started.
Total System Global Area 167772160 bytes
Fixed Size 2019288 bytes
Variable Size 104857640 bytes
Database Buffers 54525952 bytes
Redo Buffers 6369280 bytes
Database mounted.

4)Perform FlashBack.

SQL> flashback database to scn 938686;


flashback database to scn 938686
*
ERROR at line 1:
ORA-38795: warning: FLASHBACK succeeded but OPEN RESETLOGS would get error below
ORA-01245: offline file 5 will be lost if RESETLOGS is done
ORA-01111: name for data file 5 is unknown - rename to correct file
ORA-01110: data file 5:
'/oracle/app/oracle/product/10.2.0/db_1/dbs/UNNAMED00005'

5)The datafile Become Unnamed. So rename it with original data file location.

SQL> alter database rename file '/oracle/app/oracle/product/10.2.0/db_1/dbs/UNNAMED00005' to


2 '/oradata2/test_restore01.dbf';
Database altered.

6)Now perforem Flashback and Open the database with read only mode.

SQL> flashback database to scn 938686;


Flashback complete.

SQL> alter database open read only;


Database altered.

SQL> select count(*) from test;


COUNT(*)
----------
98

7)Now you can follow the step 6 choice b)in Peforming Flashback (export) and then recover database.
And later create tablespace and import the contents of tablespace.

You might also like