2
Icons Used
Hands on
Questions Tools Exercise
Coding Test Your
Reference
Standards Understanding
A Welcome
Try it Out Contacts
Break
3
Oracle 10g SQL Session 19:
Overview
Introduction:
As part of its read consistency model, Oracle displays
data that has been committed to the database. A
flashback query is used to see the data as it existed
prior to the commit.
4
Oracle 10g SQL Session 19:
Objective
Objective:
After completing this session, you will be able to:
» Describe a Flashback Query
» Describe a Flashback using SCN or Timestamp
» Describe a Flashback Transaction
» Describe a Flashback Table
» Describe a Flashback Row History
» Describe the Recycle bin
5
Flashback Query
As part of its read consistency model, Oracle displays data that
has been committed to the database.
A flashback query is used to see the data as it existed prior to the
commit.
You can use the result of the flashback query to restore the result
of the data.
To support flashback query the database should use system
managed undo.
System managed undo is a feature introduced in Oracle 9i
to automate management of rollback segments.
The DBA must create an undo tablespace, enable Automatic undo
management , and establish an undo retention time window.
To use some features of flashback queries, you must have the
EXECUTE privilege on the DBMS_FLASHBACK package.
6
Time–Based Flashback
Before delete
SELECT COUNT(*) FROM product_dim
COUNT(*)
-----------
10
7
Time–Based Flashback (Contd.)
DELETE FROM product_dim;
COMMIT;
After delete
COUNT(*)
-----------
0
8
Time–Based Flashback (Contd.)
COUNT(*)
-----------
9
Time–Based Flashback (Contd.)
Example:
CREATE TABLE product_dim_old
10
SCN–Based Flashback
11
SCN–Based Flashback (Contd.)
12
SCN–Based Flashback (Contd.)
SELECT COUNT(*) FROM product_dim
AS OF SCN(:SCN_FLASH);
COUNT(*)
-----------
10
13
SCN–Based Flashback (Contd.)
Example:
To see the SCN associated with each row use ORA_ROWSCN,
which is a new feature introduced in Oracle 10g.
INSERT INTO product_dim (product_id, product_name)
Values(1009, ’FIXED – 48 Month’)
COMMIT;
PRODUCT_NAME ORA_ROWSCN
-------------- -------------
ARM 553531
FIXED 553531
FIXED – 48 Month 553853
14
SCN–Based Flashback (Contd.)
SELECT SCN_TO_TIMESTAMP(553853)
FROM dual;
SCN_TO_TIMESTAMP(553853)
-----------------------------------
20-FEB-04 03.11.28.00000000 PM
15
Flashback Table
16
Flashback Table (Contd.)
You must have either the FLASHBACK object privilege on the table
or the FLASHBACK ANY TABLE system privilege.
17
Flashback Table (Contd.)
18
Flashback Table (Contd.)
Flashback table using timestamp or SCN:
Before flashback the table to the time just prior to an wrong
update ,first we must enable the row movement for the table:
ALTER TABLE product_dim enable row movement;
You can use the TO SCN clause if you wish to specify an SCN
instead of a timestamp:
FLASHBACK TABLE product_dim TO SCN (720880)
19
Flashback Database
The flashback database command returns the database to a past
time or SCN providing a first alternative to performing
incomplete database recovery.
You must have the SYSDBA system privilege in order to use the
Flashback Database command.
Syntax:
FLASHBACK TABLE DATABASE TO timestamp (systimestamp – 5/1440)
20
Q&A
21
Test Your Understanding
22
Oracle 10g SQL Session 19:
Summary
As part of its read consistency model, Oracle displays data that
has been committed to the database.
A flashback query is used to see the data as it existed prior to the
commit.
To begin with SCN – Based Flashback, you must first.
know the SCN of your transaction. To get the latest change
number, issue a commit and then use the AS OF SCN clause of the
SELECT COMMAND.
SCN_TO_TIMESTAMP can be used to find out the latest timing on
which a change is made to a particular table.
The flashback table command restores an earlier state of a table
in the event of human or application error.
The flashback database command returns the database to a past
time or SCN providing a first alternative to performing
incomplete database recovery.
23
Oracle 10g SQL Session 19:
Source
Oracle Database 10g: The Complete Reference
Disclaimer: Parts of the content of this course is based on the materials available from the Web sites and
books listed above. The materials that can be accessed from linked sites are not maintained by
Cognizant Academy and we are not responsible for the contents thereof. All trademarks, service marks,
and trade names in this course are the marks of the respective owner(s).
24
You have completed the
Session 19 of
Oracle 10g SQL.