一、Fashback Query闪回查询:Books-->APP-->Application Developer's Guide - Fundamentals-->Flashback
1、应用Flashback Query查询过去的数据
select * from t1 as of scn 44545454;
select * from t1 as of timestamp to_timestamp('2012-09-28:09:20:13','yyyy-mm-dd:hh24:mi:SS');
select * from t1 as of timestamp sysdate-5/1440;
--select to_char(sysdate,'yyyy-mm-dd:hh24:mi:ss') sys_time,to_char(scn_to_timestamp(current_scn),'yyyy-mm-dd:hh24:mi:ss') scn_to_time,timestamp_to_scn(sysdate) date_to_scn,dbms_flashback.get_system_change_number scn1,current_scn scn2 from v$database;
2、应用Flashback Query查询操作的事务
(1)使用Flashback Version Query 查询记录修改版本
select current_scn from v$database;
DML操作........
insert into t1 values(20,'gyj20');
commit;
update t1 set name='gyj200000' where id=20;
commit;
delete from t1 where id=20;
commit;
select current_scn from v$database;
SELECT ID,NAME,VERSIONS_STARTSCN STARTSCN,VERSIONS_ENDSCN ENDSCN,VERSIONS_OPERATION OPERATION,VERSIONS_XID XID
FROM T1 VERSIONS BETWEEN SCN 2942841 AND 2942947;
--VERSIONS_STARTSCN:该条记录操作时的SCN,如果为空,表示该行记录是在查询范围外创建的
--VERSIONS_ENDSCN:该条记录失效时的SCN,如果为空,说明记录在这段时间无操作,或者已经被删数,配合VERSIONS_OPERATION
--VERSIONS_OPERATION:I表示insert、D表示delete、U表示update
--VERSIONS_XID:该操作的事务ID
(2)使用Flashback Transaction Query 查询事务信息(可以做审计)
alter database add supplemental log data; //Oracle11g禁用了supplemental logging
select current_scn from v$database;
DML操作........
select current_scn from v$database;
select xid,commit_scn,commit_timestamp,operation,undo_sql
from flashback_transaction_query q where q.xid in (select
versions_xid from t1 versions between SCN 2943656 and 2943676);
SQL> DESC FLASHBACK_TRANSACTION_QUERY --能获得这个事务执行时的很多信息,包括UNDO语句
Name Null? Type
----------------- -------- ------------
XID RAW(8) --事务ID,对应Versions Query中的VERSIONS_XID
START_SCN NUMBER --事务开始时的SCN
START_TIMESTAMP DATE --事务开始时间
COMMIT_SCN NUMBER --事务提交时的SCN,该列为空的话,说明事务为活动事务
COMMIT_TIMESTAMP DATE --事务提交时间
LOGON_USER VARCHAR2(30) --操作用户
UNDO_CHANGE# NUMBER --UNDO SCN
OPERATION VARCHAR2(32) --执行操作,有几个值:Delete、Insert、Update、B、UNKNOWN
TABLE_NAME VARCHAR2(256)--DML操作对象的表名
TABLE_OWNER VARCHAR2(32) --表的属主
ROW_ID VARCHAR2(19) --DML操作记录的行地址
UNDO_SQL VARCHAR2(4000)--撤销该操作对应的SQL
二、Flashbackup Table闪回表:Books-->ADM-->Administrator's Guide-->flashback
1、从undo表空间恢复
select row_movement from user_tables where table_name='T1';
alter table t1 enable row movement;
select * from t1;
select current_scn from v$database;
delete from t1;
commit;
select * from t1;
select * from t as of scn 9999;
flashback table t1 to scn 9999;
flashback table t1 to timestamp sysdate-5/1440;
--create temporary tablespace temp2 tempfile '/u01/app/oracle/oradata/bxdb/temp02.dbf' size 50M;
--alter user gyj temporary tablespace temp2;
2、利用recyclebin,drop闪回
drop table t1;
select object_name,original_name,droptime from recyclebin;
flashback table t1 to before drop;
--flashback table t1 to before drop rename to t1_old;
实验:多个名为t1的表恢复会是什么顺序???
回收站清除是按什么规则的??????
恢复表时索引也同时被恢复,但索引名称是????
drop table t1 purge; --真正删除表
三、数据库闪回配置:Books-->BAK-->Backup and Recovery Advanced User's Guide-->flashback
v$flashback_database_log
1、闪回区的大小
db_recovery_file_dest
db_recovery_file_dest_size
2、闪回日志保留时间
db_flashback_retention_target
3、设闪回
startup mount
alter database flashback on;
alter database open;
4、闪回操作要在mount下做
一般在备库中做完测试,用闪回恢复到测度前状态
flashbackup database TO TIMESTAMP(SYSDATE-1/24);
alter database open;
5、建闪回点
create restore point 恢复点名字 [guarantee flashback database]
drop restore point 恢复点名字
flashback database to restore point re_data;
6、*场景1:
show parameter db_recover
arcive log list;
select flashback_on,force_logging from v$database;
alter database force logging;
select dbms_flashback.get_system_change_number from dual;
drop table t1 purge;
shutdown immediate;
startup mount;
flashback database to restore point re_data;
--flashback database to scn 1511225;
alter database open resetlogs;
select * from t1;