Flashback table restores the existing tables to earlier versions using timestamp and SCN number. It is available in and after Oracle 10g version.
Flashback also helps in restoration of tables altered using DDL statements to a prior state.
UNDO_RETENTION parameter controls the undo of data of flashback operation. In simple words, restoration of tables depends on the availability of undo data.
Rollback is not possible once the FLASHBACK TABLE is triggered. Best practice is to record the current SCN number before issuing a FLASHBACK TABLE statement.
You must enable row movement during the creation of the table to carry out flashback operation on it. Row movement can also be enabled using ALTER command later on.
ALTER TABLE employees ENABLE ROW MOVEMENT;
FLASHBACK TABLE table_name [TO BEFORE DROP] | [TO TIMESTAMP time_stamp] | [TO SCN scn_number] | [ENABLE TRIGGERS | DISABLE TRIGGERS];
table_name – Name of the table to restore
time_stamp – User defined timestamp
scn_number – Oracle SCN (Service Change Number) number
Restoring Table to Earlier State
Below example shows FLASHBACK TABLE command used to restore the table to an earlier state
Create table emp_temp from table employees whose department number is 10.
CREATE TABLE emp_temp SELECT * FROM employees WHERE dept_no = 10;
Fetching the data from emp_temp retrieves the list of employees belonging to department number 10.
SELECT dept_no, salary FROM emp_temp;
Now, delete the employees with salary less than 2000 from the emp_temp table using below query.
DELETE FROM emp_temp WHERE salary < 2000;
This will leave only 2 records in the emp_temp table.
With below query, we can restore table emp_temp to 2 minutes prior to current time using flashback operation
FLASHBACK TABLE emp_temp TO TIMESTAMP (SYSTIMESTAMP – INTERVAL '2' MINUTE);
Querying table emp_temp again will retrieve the original results
Another way of restoring table emp_temp to particular timestamp is as follows
FLASHBACK TABLE emp_temp TO TIMESTAMP TO_TIMESTAMP('2018-09-19 13:34:12', 'YYYY-MM-DD HH24:MI:SS');
Retrieve Table Using SCN
You can also retrieve the older version of the table using SCN number.
Let us continue with the above example of TO TIMESTAMP
The 2 minutes prior version of table emp_temp using SCN number is available by below query
FLASHBACK TABLE emp_temp TO SCN (3187302511937);
This SCN number is of 2 minutes prior state of table emp_temp.
So the output after flashback operation while querying emp_temp table will be
Retrieving a Dropped Table
Suppose you had dropped table employees accidentally in a hurry to go home by firing a below query
DROP TABLE employees;
To retrieve the table employees back, use TO BEFORE DROP clause of FLASHBACK TABLE.
FLASHBACK TABLE employees TO BEFORE DROP;
The retrieved employees table can also be renamed to emp with the below query
FLASHBACK TABLE employees TO BEFORE DROP RENAME TO emp;
ENABLE or DISABLE TRIGGERS
By default Oracle database disable triggers during the flashback. These triggers are enabled when flashback operation is completed.
You need to specify ENABLE TRIGGERS to keep the triggers enabled during flashback operation.
FLASHBACK TABLE emp_temp TO SCN (3187302511937) ENABLE TRIGGERS;
However, the triggers that are already disabled before flashback operation cannot be enabled using this option.
DISABLE TRIGGERS acts the same as a default behaviour in flashback operation.
RECYCLEBIN and USER_RECYCLEBIN
USER_RECYCLEBIN view retrieves the dropped or truncated tables
SELECT * FROM user_recyclebin;
RECYCLEBIN is the synonym of USER_RECYCLEBIN view.
TO SCN Clause
The table can be returned to a point in time of mentioned SCN number using TO SCN clause.
Below query retrieves SCN number from the table v$database table
SELECT current_scn FROM v$database;
You can also convert a timestamp to SCN number and vice versa using SCN_TO_TIMESTAMP and TIMESTAMP_TO_SCN functions
SELECT TIMESTAMP_TO_SCN(SYSTIMESTAMP – INTERVAL '2' MINUTE) FROM dual;
SCN_TO_TIMESTAMP(3187302511937) will retrieve the timestamp of this SCN number.
TO TIMESTAMP Clause
In order to restore the table back to particular timestamp, use TO TIMESTAMP clause.
TO BEFORE DROP Clause
The clause TO BEFORE DROP retrieves back the dropped table.
Tables dropped with PURGE option are not eligible for rollback.
TO BEFORE DROP clause is also unable to recover tables dropped TRUNCATE TABLE command.
RENAME TO Clause
RENAME TO clause retrieves the table from recycle bin with a new name during flashback operation.
Retrieve Table After TRUNCATE TABLE
Tables dropped by TRUNCATE TABLE can be restored using TO TIMESTAMP and TO SCN clause. Recovery of lost data is not possible in this case.
I hope you like the page and find it useful. We highly appreciate comments and feedback.
Hi I am Paras.
Thanks for stopping by at OracleMine.com. Speaking about my brief introduction, I work for a multinational organisation in Oracle related technologies. Being an avid blogger, I would like to inform you about my productivity and motivational blog XpressPlanet.com. Speaking of OracleMine.com, I will try my best to share knowledge on technologies in as simple and understandable manner as possible. You can also contribute your knowledge on OracleMine by writing to us at firstname.lastname@example.org. Again I appreciate your visit. Hope to see you again and again!