Beruflich Dokumente
Kultur Dokumente
Flashback Database allows the DBA to roll back a table, set of tables or the entire database to a
previous point-in-time.
First of all change the undo_retention parameter of your database.
SQL>show parameter undo_retention;
# The default value is 900 seconds.
SQL>alter system set undo_retention=2400 scope=both;
# I am changing it to 40 minutes which equals 2400 seconds.
Also enable the retention guarantee parameter for
your database'e undo tablespace.
SQL>show parameter tablespace;
# My current undo tablespace is named as UNDOTBS1
SQL>select tablespace_name, retention from dba_tablespaces
where tablespace_name = 'UNDOTBS1';
#shows NOGUARANTEE which means retention guarantee
#is not enabled.
SQL>alter tablespace undotbs1 retention guarantee;
#enable the retention guarantee with the above command.
SQL>select tablespace_name, retention from dba_tablespaces
where tablespace_name = 'UNDOTBS1';
# shows GUARANTEE
FLASHBACK QUERY
In oracle 10g, the "as of" clause is available in a select query to retrieve the state of a table as of a
given timestamp or SCN.
Unlock the users to work with.
SQL>alter user hr identified by hr account unlock;
SQL>alter user scott identified by tiger account unlock;
SQL>grant insert, update, delete, select
on hr.employees to scott;
SQL>grant insert, update, delete, select
on hr.departments to scott;
SQL>grant flashback on hr.employees to scott;
SQL>grant flashback on hr.departments to scott;
SQL>grant flashback on hr.employees to hr;
SQL>grant flashback on hr.departments to hr;
SQL>grant select any transaction to scott;
SQL>grant create any table to scott;
SQL>grant drop any table to scott;
SQL>grant select any table to scott;
SQL>grant select on v_$database to scott;
SQL>grant select on v_$database to hr;
SQL>connect scott/tiger;
#Connect to the database as scott.
SQL>select current_scn from v$database;
#check the current scn number before making any changes.
481166
SQL>delete from hr.employees where employee_id in (195,196);
#delete employees which are no longer working in the company.
SQL>commit;
SQL>SELECT * FROM HR.EMPLOYEES
as of timestamp systimestamp - interval '5' minute
where hr.employees.employee_id not in
(select employee_id from hr.employees);
#displays the list of records that were deleted 5 minutes ago.
SQL>create table hr.employees_deleted as
SELECT * FROM HR.EMPLOYEES
as of timestamp systimestamp - interval '5' minute
where hr.employees.employee_id not in
(select employee_id from hr.employees);
SQL>commit;
Check the ending SCN number.
SQL> select dbms_flashback.get_system_change_number from dual;
1673495
Now by using Flashback Version Query, the HR user can see the entire history of changes between
specified timestamps or SCNs.
Now let us see the changes made for the two employees with IDs
195 and 196.
SQL>select versions_startscn startscn, versions_endscn endscn,
versions_xid xid, versions_operation oper,
employee_id empid, last_name name, manager_id mgrid, salary sal
from hr.employees
versions between scn 1673400 and 1673495
where employee_id in (195,196);
Meaning of Pseudocolumn:
VERSIONS_START{SCN|TIME} The starting SCN or timestamp when the
change was made to the row.
VERSIONS_END{SCN|TIME} The ending SCN or timesamp.
VERSIONS_XID The transaction ID of the transaction that created
the row.
VERSIONS_OPERATION The operation that was performed on the row
(I=insert, D=Delete, U=Update).
FLASHBACK TRANSACTION QUERY
After identifying the above changes, we can use Flashback Transaction Query to identify any other
changes that were made by the transaction containing the inappropriate changes. once identified, all
changes within the transaction can be reversed as a group to maintain referential integrity.
It is recommended that the supplemental log data should be enabled in the database from the very
beginning.
If it is not enabled, then enable it by the following sql command.
SQL> alter database add supplemental log data;
Suppose if the last update query to the employees table needs to be reversed in the FLASHBACK
QUERY section we will query the FLASHBACK_TRANSACTION_QUERY.
SQL>select start_scn, commit_scn, logon_user,
operation, table_name, undo_sql
from flashback_transaction_query
where xid = hextoraw('04000F0097030000');
In the undo_sql column it will show a sql entry which can be used to reverse the effects of the
original transaction.
update "HR"."EMPLOYEES" set "SALARY" = '2800' where ROWID =
'AAAMAEAAFAAAABYABc';
In order to restore the changes execute the above command in the
SQL prompt.
SQL>update "HR"."EMPLOYEES" set "SALARY" = '2800' where ROWID =
'AAAMAEAAFAAAABYABc';
SQL> commit;
FLASHBACK DATABASE
The flashback database command returns the database to a past time or SCN, providing a fast
alternative to performing incomplete database recovery. Database can be flashed back to
scn ot timestamp.
After a flashback database operation in order to have write access to the flashed back database, you
must reopen it with an "alter database open resetlogs" command.
To enable flashback database make sure your database is in archivelog mode after confirming do the
following.
SQL>shutdown immediate;
SQL>startup mount exclusive;
# flashback database command demands that the database should be
# mounted in exclusive mode.
SQL>alter database flashback on;
SQL>alter database open;
How to Enable Flashback:-Step 1: Verify the Database in flash back mode and the retention_target.
SQL> SELECT flashback_on, log_mode
FROM v$database;
FLASHBACK_ON LOG_MODE
------------------ -----------NO ARCHIVELOG
Step 2:-- shutdown the database
SQL> shutdown immediate;
Step 3:-- Enable the Archive log and Set the DB_FLASHBACK_RETENTION_TARGET,
DB_RECOVERY_FILE_DEST_SIZE and DB_RECOVERY_FILE_DEST.
Flash Recovery Area contains the Flashback Logs, Redo Archive logs, backups files by RMAN and
copies of control files. The destination and the size of the recovery area are setup using the
db_recovery_file_dest and b_recovery_file_dest_size initializatin parameters.
SQL> startup mount;
SQL> alter database archivelog;
SQL> alter system set db_recovery_file_dest=<location> scope=both;
SQL> alter system set db_recovery_file_dest_size=2G scope=both;
SQL> alter system set db_flashback_retention_target=600 ;---- In Minutes.
Step 4:-- On the Flash back and open the database.
SQL> alter database flashback on;
SQL> alter database open;
Step 05: Now Verify the Database flashback mode.
SQL> SELECT flashback_on, log_mode
FROM v$database;
FLASHBACK_ON LOG_MODE
------------------ -----------YES ARCHIVELOG
How to Recover Database from Flashback recovery area:Step 01: Find the Current SCN and Flashback time.
SQL> SELECT current_scn
2 FROM v$database;
CURRENT_SCN
----------1143033
SQL> SELECT oldest_flashback_scn,oldest_flashback_time
2 FROM gv$flashback_database_log;
OLDEST_FLASHBACK_SCN OLDEST_FL
-------------------- --------1141575 23-Feb-12
Step 02: Grant flashback to the user.
GRANT flashback any table TO <user_name>;
Step 03: Shutdown the database and start in exclusive mode
SQL> SHUTDOWN immediate;
SQL> startup mount exclusive;
Step 04: Be sure to substitute your SCN and issue the following command
SQL> FLASHBACK DATABASE TO SCN <SCN Number>;
Flashback complete.
Or
If restore point create by the user
FLASHBACK DATABASE TO RESTORE POINT <RESTORE POINT>;
Or
if flashback using TIMESTAMP
FLASHBACK DATABASE TO TIMESTAMP Timestamp 2012-02-23 14:00:00;
Step 05: Now open database using resetlogs
SQL> alter database open resetlogs;