--flashback 功能
flashback database:将整个数据库倒退到一个特定的时刻。
flashback table:将表返回到过去的一个状态。
flashback drop:撤销 drop table 命令并恢复被删除的表。
flashback query、flashback version query、flashback transaction query:检索过去某一时刻(或时间间隔)的数据。
flashback transaction backout:撤销一个事务以及它依赖的所有事务。
flashback data archive:存储对一个表所做更改的历史,可用它来构造旧版本数据的查询和用于审计用途。
--闪回查询技术
闪回查询
闪回版本查询
闪回事务查询
--闪回错误操作技术
闪回表
闪回删除
闪回数据库
--使用条件
undo_management 参数必须是auto
闪回功能开启
启用回收站
应该为撤销数据指定 retention guarantee 子句。仅设置较大的 undo_retention 值并不能保证 Oracle 不丢弃未过期的撤销数据。
alter tablespace undotbs1 retention guarantee;
为保证数据一致性,在使用任何种类的闪回操作前,应该发出一条commit或者rollback语句。
--查看闪回功能是否开启
select flashback_on from v$database;
开启闪回功能(mount状态下)
alter database flashback on;
--查看回收站是否启用
show parameter recyclebin;
开启回收站功能
alter system set recyclebin=on;
--查询最大事务时间
select maxquerylen from v$undostat
order by maxquerylen;
--闪回查询权限
grant flashback on employees to ikki;
或者
grant flashback any table to ikki;
--闪回查询
select 列名
from 表名
as of scn|timestamp 表达式
where 条件
sql>set time on;
14:02:58 SQL> select empno,sal
14:03:11 2 from emp
14:03:16 3 as of timestamp sysdate-1/24 (一个小时前的,1天除24)
14:03:53 4 where empno=7844;
14:04:01 SQL> select empno,sal
14:08:43 2 from emp
14:08:47 3 as of timestamp to_timestamp('2012-7-28 14:01:20','yyyy-mm-dd hh24:mi:ss')
14:09:56 4 where empno=7844;
select scn,to_char(time_dp,'yyyy-mm-dd hh24:mi:ss') time_dp
from smon_scn_time
select scn_to_timestamp(2335368) from dual
select timestamp_to_scn(to_date('2012-7-28 14:01:26','yyyy-mm-dd hh24:mi:ss')) from dual
14:35:39 SQL> select current_scn from v$database;
2335368
14:39:55 SQL> select empno,sal from scott.emp
14:40:53 2 where empno=7844;
7844 4000
create bak_table as select * from table_name as of timestamp|scn;
insert into bak_table select * from table_name as of timestamp|scn;
--闪回版本查询
select 列名
from 表名
versions between scn|timestamp
minvalue|表达式 and maxvalue|表达式
where 条件
versions_starttime
versions_startscn
versions_endtime
versions_endscn
versions_xid
versions_operation(i,d,u)
update scott.emp set sal=6000 where empno=7844;
update scott.emp set sal=6500 where empno=7844;
update scott.emp set sal=7000 where empno=7844;
commit;
update scott.emp set sal=7500 where empno=7844;
commit;
set linesize 600
col starttime format a30
col endtime format a30
col operation format a10
select versions_xid xid,versions_starttime starttime,versions_endtime endtime,
versions_operation operation,sal 表字段
from scott.emp
versions between timestamp minvalue and maxvalue
where empno=7844
order by starttime;
select versions_xid xid,versions_startscn startscn,versions_endscn endscn,
versions_operation operation,sal
from scott.emp
versions between scn minvalue and maxvalue
where empno=7844
order by startscn;
闪回版本查询的一些限制:
1)只能用此特性来查询实际的表,不能查询视图。
2)不能跨DDL操作应用versions子句。
3)查询将忽略纯粹的行物理更改,如段收缩中发生的行物理更改。
4)如果处理外部表或临时表,则不能使用此特性。
--闪回事务查询
set pause on;
select versions_xid,sal
from scott.emp
versions between scn minvalue and maxvalue
where empno=7844;
select operation,undo_sql
from flashback_transaction_query
where xid=HEXTORAW('04001e002e010000');
--闪回表(不适合system表空间, 不能跨DDL操作)
flashback table 表名 to
scn|timestamp 表达式
[enable|disable triggers]
用户具有flashback any table权限
用户具有对象权限
合理设置undo_retention参数值
闪回表前先执行下面的语句:
alter table t enable row movement;
这是因为闪回操作是使用DML操作来完成工作,这些DML操作更改了影响到行的ID,因此你必须保证已经启用用于闪回表特性的表中的行移动。
闪回表命令:
flashback table t to scn 675371;
或者
flashback table t to timestamp
to_timestamp('20131121 15:35:00','yyyymmdd hh24:mi:ss');
闪回表的一些限制:
1)不能闪回SYS拥有的表,恢复对象或远程表。
2)不能将表闪回到涉及表结构更改(如修改或删除列、截取一个表、添加约束、执行与分区有关的操作如添加或删除一个分区)
3)flashback语句涉及单个事务,闪回操作要么完全成功,要么完全失败。如果闪回操作涉及多个表,所有的表都必须闪回或者不闪回。
4)如果在闪回操作中Oracle发现违背了约束,则放弃操作,使表保留原来的状态。
5)如果收缩一个表或更改一个表的任意非存储属性(除了pctfeee、inittrans和maxtrans等属性),则不能闪回到做出这些更改前的时刻。
--闪回删除(只适合本地管理,非系统表空间的表)
flashback table 表名 to before drop[rename to 表名1]
show parameter recyclebin
alter system set recyclebin=on
select object_name,original_name,type from user_recyclebin;
--清除回收站
purge [table 表名]|[index 索引名]|
[recyclebin|dba_recyclebin]|
[tablespace 表空间名 [user 用户名]]
purge recyclebin
--闪回数据库
flashback [standby] database 数据名
to [scn|timestamp 表达式]
[before scn|timestamp 表达式]
归档模式
设置了闪回恢复区
启用了flashback database特性
(mount状态)alter database flashback on;
db_flashback_retention_target 设置闪回日志保留时间
不适用于:
已还原或重建控制文件
已删除了表空间
已收缩了数据文件
SQL> select oldest_flashback_scn,oldest_flashback_time,retention_target
2 from v$flashback_database_log;
OLDEST_FLASHBACK_SCN OLDEST_FLASH RETENTION_TARGET
-------------------- ------------ ----------------
2327829 28-JUL-12 1440
alter database to scn 2327829;
alter database open resetlogs;
select * from v$flashback_database_logfile
闪回表
alter table test.tbs enable row movement;
flashback table test.tbs to timestamp to_timestamp('2012-8-4 09:39:22','yyyy-mm-dd hh24:mi:ss');
select object_name,original_name,type from user_recyclebin;
select object_name,original_name,type from dba_recyclebin;
show recyclebin
select OBJECT_NAME,OBJECT_TYPE from user_objects;
alter index rename "BIN$xmfpWkYzIJPgQKjAJQARMA==$0" to "idx_a"
v$flashback_database_log
--闪回数据归档
11:30:46 SYS@dbtest> create tablespace fla
11:30:58 2 datafile '/u01/app/oracle/oradata/fla01.dbf' size 10m;
11:31:50 SYS@dbtest> create flashback archive default flaarea
11:32:37 2 tablespace fla quota 8m
11:32:55 3 retention 1 year;
11:33:05 SYS@dbtest> create tablespace fla1
11:35:47 2 datafile '/u01/app/oracle/oradata/fla101.dbf' size 20m;
11:36:17 SYS@dbtest> alter flashback archive flaarea
11:36:37 2 add tablespace fla1
11:36:47 3 quota 10m;
11:36:53 SYS@dbtest> alter flashback archive flaarea
11:38:24 2 modify retention 2 year;
11:38:37 SYS@dbtest> alter flashback archive flaarea
11:39:06 2 modify tablespace fla1
11:39:16 3 quota 12 m;
11:39:24 SYS@dbtest> alter flashback archive flaarea
11:39:39 2 set default;
11:39:44 SYS@dbtest> create tablespace fla2
11:41:44 2 datafile '/u01/app/oracle/oradata/fla201.dbf'
11:42:09 3 size 10m;
11:42:15 SYS@dbtest> create flashback archive flaarea1
11:42:31 2 tablespace fla2
11:42:36 3 quota 5m
11:42:42 4 retention 1 year;
--手动删除闪回归档区的数据
alter flashback archive flaarea
purge before timestamp to_timestamp('2012-8-4 15:00:00','yyyy-mm-dd hh24:mi:ss')
--授予用户scott闪回区管理权限和flaarea区上的闪回权限
grant flashback archive administer to scott
grant flashback archive on flaarea to scott
grant flashback archive on flaarea1 to scott
--指定表到闪回归档区
create table t1(c1 int) flashback archive
create table t2(c1 int) flashback archive flaarea1
create table t3(c1 int)
select * from t3 as of timestamp to_timestamp()
ORA-01548: active rollback segment '_SYSSMU2_2232571081$' found, terminate
dropping tablespace
--清除特定scn之前的数据
alter flashback archive flaarea purge before scn 728969
--将指定的表不再设置数据归档
alter table t1 no flashback archive
--删除数据归档区
drop flashback archive flaarea
dba_flashback_archive
dba_flashback_archive_ts --表空间
dba_flashback_archive_tables
user_flashback_archive
user_flashback_archive_tables
--rman下的闪回
(mount下)
rman> flashback database to time="to_date('2012-8-4 15:00:00','yyyy-mm-dd hh24:mi:ss')";
rman> flashback database to scn=273533;
rman> flashback database to sequence=3 thread=1;