FLASHBACK TABLE command can be used to restore an earlier state of a table in the event of human or application error.Restoring a dropped table
The first use ofFLASHBACK TABLE is to retrieve a table from the recycle bin after it has been dropped.TheDROP TABLE dept;Table dropped.SELECT *
FROM dept;SQL Error: ORA-00942: table or view does not existSELECT object_name, original_name
FROM recyclebin;OBJECT_NAME ORIGINAL_NAME
------------------------------ --------------------------------
BIN$QzJJ24I1hx7gQAAK+wFg5Q==$0 DEPT1 rows selectedFLASHBACK TABLE dept TO BEFORE DROP;Flashback complete.SELECT *
FROM dept;DEPTNO DNAME LOC
---------------------- -------------- -------------
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON4 rows selected
RENAME TO clause can be used to give the table a new name during the restore. This is useful if a new table has been created with the same name since the old one was deleted.Any indexes or triggers associated with the table will still have their recycled names and will need to be re-instated manually. Also, views and procedures which rely on the table will have been invalidated and will need to be recompiled.FLASHBACK TABLE dept TO BEFORE DROP RENAME TO dept_before_drop;Flashback complete.
Restoring a table to an earlier state
FLASHBACK TABLE can also be used to revert the state of a table back to a specified time or SCN.Let's see this in action using the EMP table in the Oracle demo schema.
Suppose we wish to delete all entries for department 10 but we mistype the SQL and commit before we notice our mistake.SELECT deptno, count(*)
FROM emp
GROUP BY deptno;DEPTNO COUNT(*)
------------ --------------
10 3
20 5
30 63 rows selected
All the rows have been deleted and the transaction committed. But fear not, flashback technology was introduced to deal with just this kind of problem. We can restore the table back to the state it was in, say, one minute ago. Before we can do this, however, we must ensure that row movement is enabled on the table.DELETE FROM emp
WHERE deptno >= 10;14 rows deletedCOMMIT;Commit complete.SELECT deptno, count(*)
FROM emp
GROUP BY deptno;DEPTNO COUNT(*)
------------ --------------0 rows selected
The table has been 'rewound' to a prior point in time. All the rows that existed on the table one minute ago have been restored. It is as if the delete never happened.ALTER TABLE emp ENABLE ROW MOVEMENT;Table altered.FLASHBACK TABLE emp TO TIMESTAMP systimestamp - INTERVAL '1' MINUTE;Flashback complete.SELECT deptno, count(*)
FROM emp
GROUP BY deptno;DEPTNO COUNT(*)
------------ --------------
10 3
20 5
30 63 rows selected
Alternatively, we could restore to a particular SCN. To use this method we must know the exact SCN we wish to restore to, so we will need to record the value before the update is attempted.
SELECT current_scn
FROM v$database;CURRENT_SCN
----------------------
37798197031 rows selectedDELETE FROM emp
WHERE deptno >= 10;14 rows deletedCOMMIT;COMMIT succeeded.FLASHBACK TABLE emp TO SCN 3779819703;FLASHBACK TABLE succeeded.SELECT deptno, count(*)
FROM emp
GROUP BY deptno;DEPTNO COUNT(*)
------------ --------------
10 3
20 5
30 63 rows selected
Restore Point
Oracle 10g Release 2 introduced the concept of restore points. A restore point is a name which is associated with the SCN of the database at the time the restore point was created. We can reference a restore point name in theFLASHBACK TABLE command.For example.
If we run in to any problems during our updates, we can simply rewind one or more tables back to the restore point.CREATE RESTORE POINT before_changes;Restore point created.
DELETE FROM emp
WHERE deptno >= 10;14 rows deletedCOMMIT;COMMIT succeeded.FLASHBACK TABLE emp TO RESTORE POINT before_changes;FLASHBACK TABLE succeeded.SELECT deptno, count(*)
FROM emp
GROUP BY deptno;DEPTNO COUNT(*)
------------ --------------
10 3
20 5
30 63 rows selected
Restrictions
There are are some rules and restrictions to be aware of when usingFLASHBACK TABLE.- This feature is only available with Oracle Enterprise Edition.
- You will need the appropriate privileges (such as FLASHBACK ANY TABLE) to execute these commands. See the Oracle documentation for more details.
- Oracle may restore rows to a different location and allocate new rowids. Any references to the original rowids will no longer be valid.
- There must be sufficient redo data available in the database to restore the data. This can be guaranteed for a predetermined period of time if the
UNDO_RETENTIONinitialization parameter is used. - You cannot restore a table to an earlier state across any DDL operations that change the structure of the table (including
TRUNCATE). - Restore points were only introduced at 10g Release 2.
0 comments:
Post a Comment