oracle flashback

官方说明文档 http://www.oracle.com/technology/global/cn/deploy/availability/htdocs/Flashback_Overview.htm

1. Overview of Flashback Features
Oracle Database has a group of features, known collectively as flashback, that provide ways to view past states of database objects or to return database objects to a previous state without using point-in-time media recovery.

You can use flashback features of the database to do the following:

      Perform queries that return past data.
   
      Perform queries that return metadata that shows a detailed history of changes to the database.

      Recover tables or rows to a previous point in time.

Flashback features use the Automatic Undo Management system to obtain metadata and historical data for transactions. They rely on undo data, which are records of the effects of individual transactions. For example, if a user executes an UPDATE statement to change a salary from 1000 to 1100, then Oracle would store the value 1000 in the undo data.

Undo data is persistent and survives a database shutdown. By using flashback features, you can employ undo data to query past data or recover from logical corruptions. Besides using it in flashback operations, Oracle Database uses undo data to perform the following actions:

      Roll back active transactions

      Recover terminated transactions by using database or process recovery

      Provide read consistency for SQL queries
2. Views
V$RECOVERY_FILE_DEST

DBA_RECYCLEBIN

3. Parameters
.db_recovery_file_dest            --闪回文件保存路径

.db_recovery_file_dest_size       --闪回文件大小

.db_flashback_retention_target    --控制flashback log的保留时间的参数
4. options
SQl>select flashback_on from v$database;

FLASHBACK_ON
-----------
NO

SQL>shutdown immediate;

SQL>startup mount;

SQL>alter database flashback on;     --开启闪回

SQL>alter tablespace <tablespace_name> flashbackup on;  --开启针对某一tablespace的闪回

SQL>alter database open;

SQL>alter database flashback off;    --关闭闪回
4.1. flashback database(闪回数据库)
对应flashback database来说,一般用来处理误删除了user或者一些错误的数据操作

example

SQL>drop user *** cascade;
SQL>shutdown abort;
SQL>startup mount;         --Signifies that the database can only be mounted and opened by the current instance (it
                                      cannot be opened simultaneously by multiple instances). Cannot be used with SHARED, PARALLEL,
                                      or NOMOUNT. If no mounting option is specified, EXCLUSIVE is assigned by default.
SQL>flashback database to timestamp(to_date(’2009-05-13 00:35:50′, ‘yyyy-mm-dd hh24:mi:ss’));
SQL>alter database open read only;

--如果不正确,还可以执行
SQL>shutdown abort
SQL>startup mount;
SQL>recover database until time '2009-05-13:00:40:00';
SQL>alter database open read only;

--最后
SQL>shutdown abort;
SQL>startup mount;
SQL>flashback database to timestamp to_timestamp('2009-05-13 00:37:00','yyyy-mm-dd hh24:mi:ss');
SQL>alter database open read only;
SQL>shutdown abort;
SQL>alter database open resetlogs;

SQL>flashback database to scn 5342420;     --闪回到某一SCN号状态

SQL>alter database open resetlogs;

4.2. flashback drop(闪回删除)
用来恢复被误drop掉的表的恢复,同时还能恢复所有索引以及触发器的授权。此外,唯一约束、主键约束与非空约束也将被恢复。外键约束则不可以恢复。

SQL>flashback table 'table name' to before drop

SQL>flashback table <table_name> to before drop rename to <new_name>

注:用户要有falshback any table的系统权限或者是某一个表的flashback权限。有table的row Movement权限

4.2.1. example
SQL>create user dropper identified by dropper;
SQL>grant resource,connect to dropper;
SQL>conn dropper/dropper
SQL>create table names(vname varchar2(10));
SQL>create index name_idx on names(vname);
SQL>alter table names add (constraint name_u unique(vname));
SQL>insert into names values('john');
SQL>commit;

SQL>select object_name,object_type from user_objects;
OBJECT_NAME      OBJECT_TYPE
-----------------------------------------------
NAMES            TABLE
NAME_IDX         INDEX

--drop table
SQL>drop table names;
SQL> select object_name,object_type from user_objects;

OBJECT_NAME                     OBJECT_TYPE
-----------------------------------------------------------------------------
BIN$lcoRJePGDObgQKjAgrAcMg==$0  TABLE

BIN$lcoRJePFDObgQKjAgrAcMg==$0  INDEX

--查询回收站
SQL>select object_name,original_name,type from user_recyclebin;

OBJECT_NAME                    ORIGINAL_NAME                    TYPE
------------------------------ -------------------------------- -------------------------

BIN$lcoRJePFDObgQKjAgrAcMg==$0 NAME_IDX                         INDEX

BIN$lcoRJePGDObgQKjAgrAcMg==$0 NAMES                            TABLE

SQL>flashback table names to before drop;

--重命名索引
SQL>alter index "BIN$lcoRJePFDObgQKjAgrAcMg==$0" rename to name_idex;
--重命名约束
SQL>alter table names rename constraint "*********************" to name_u;

--重新查询user_objects 确认恢复正确

4.2.2. example 2
如果误操作删除多表(存在外键约束),需要一起恢复
SQL>flashback table emp,dept to timestamp to_timestamp();
4.3. flashback table(闪回表更新)
SQL>alter table 'tablename' enable row movement;

SQL>flashback table 'tablename' to timestamp to_date((’2009-05-13 00:35:50′, ‘yyyy-mm-dd hh24:mi:ss’));

注:这里是利用undo表做恢复的基准,所以有关undo的参数undo_management和undo_retention对这个有影响

4.4. flashback query(闪回查询)
--视图

--需要权限 SELECT ANY TRANSACTION

SQL>DESC FLASHBACK_TRANSACTION_QUERY

--参数

UNDO_RETENTION

SQL>conn scott/tiger
SQL>create table rates(
  2>CURRENCY VARCHAR2(4),
  3>RATE     NUMBER(15,10));

SQL>insert into rates values ('EURO',1.1012);
SQL>commit;
SQL>update rates set rate = 1.1014;
SQL>commit;
SQL>update rates set rate = 1.1013;
SQL>commit;
SQL>delete rates;
SQL>commit;
SQL>insert into rates values ('EURO',1.1016);
SQL>commit;
SQL>update rates set rate = 1.1011;
SQL>commit;

SQL> select versions_startscn,versions_endscn,versions_starttime , versions_endtime, versions_xid,
  2  versions_operation, rate
  3  from rates versions between timestamp minvalue and maxvalue
  4  order by VERSIONS_STARTTIME
  5  ;

VERSIONS_STARTTIME     VERSIONS_ENDTIME        VERSIONS_XID         V       RATE

---------------------------------------------------------------------------
24-NOV-10 11.05.40 AM  24-NOV-10 11.06.48 AM   0600120039010000     I     1.1012

24-NOV-10 11.06.48 AM  24-NOV-10 11.07.16 AM   02001B0035010000     U     1.1014

24-NOV-10 11.07.16 AM  24-NOV-10 11.07.22 AM   050002004F010000     U     1.1013

24-NOV-10 11.07.22 AM                          0600070039010000     D     1.1013

24-NOV-10 11.07.28 AM  24-NOV-10 11.07.34 AM   08000C0033010000     I     1.1016

24-NOV-10 11.07.34 AM                          090026005F010000     U     1.1011

6 rows selected.

SQL>SELECT UNDO_SQL
  2>FROM FLASHBACK_TRANSACTION_QUERY
  3>WHERE XID = '0600070039010000';

--需要找出某一时刻的精确rate值
SQL> select rate,versions_starttime,versions_endtime
  2  from rates 
  3  versions between timestamp to_date('2010-11-24 11:07:26','yyyy-mm-dd hh24:mi:ss')
  4  and to_date('2010-11-24 11:07:27','yyyy-mm-dd hh24:mi:ss');

SQl>select rate, versions_starttime, versions_endtime
 2>from rates versions
 3>between scn *** and *****;

--使用MAXVALUE or MINVALUE 可以显示一段时间的变更
SQL> select rate,versions_starttime,versions_endtime
  2  from rates 
  3  versions between timestamp to_date('2010-11-24 11:07:26','yyyy-mm-dd hh24:mi:ss')
  4  and maxvalue;
4.5. 回收站管理
--视图

user_recyclebin

dba_recyclebin



SQL>show recyclebin;

SQL>selecct owner,original_name,type,droptime,can_undrop,space from dba_recyclebin;

你可能感兴趣的:(oracle,sql)