Oracle10g新特性之flashback table
Flashback table功能可以将表恢复到以前的某一个时刻的状态,因为flashback table也是利用到undo信息,所以在进行flashback table的时候,必须保存其undo信息存在。
--flashback table对于SYS用户下的对象无效
SQL> conn sys/sys@ora10g as sysdba;
已连接。
SQL> create table tt(name varchar2(10));
表已创建。
SQL> insert into tt values ('one');
已创建 1 行。
SQL> commit;
提交完成。
SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
会话已更改。
SQL> select sysdate from dual;
SYSDATE
-------------------
2007-05-16 15:38:39
SQL> truncate table tt;
表已截掉。
SQL> alter table tt enable row movement;
表已更改。
SQL> flashback table tt to TIMESTAMP to_timestamp('2007-05-16 15:38:39','yyyy-mm-dd hh24:mi:ss');
flashback table tt to TIMESTAMP to_timestamp('2007-05-16 15:38:39','yyyy-mm-dd hh24:mi:ss')
*
第 1 行出现错误:
ORA-08185: 用户 SYS 不支持闪回
--我们用另外一个用户尝试
SQL> grant flashback any table to scott;
授权成功。
SQL> grant select on v_$database to scott;
授权成功。
SQL> conn scott/scott@ora10g;
已连接。
SQL> create table tt(name varchar2(10));
表已创建。
SQL> insert into tt values ('one');
已创建 1 行。
SQL> commit;
提交完成。
SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';
会话已更改。
SQL> select sysdate from dual;
SYSDATE
-------------------
2007-05-16 15:47:50
SQL> truncate table tt;
表被截断。
SQL> alter table tt enable row movement;
表已更改。
SQL> flashback table tt to TIMESTAMP to_timestamp('2007-05-16 15:47:50','yyyy-mm-dd hh24:mi:ss');
flashback table tt to TIMESTAMP to_timestamp('2007-05-16 15:47:50','yyyy-mm-dd hh24:mi:ss')
*
第 1 行出现错误:
ORA-01466: 无法读取数据 - 表定义已更改
可以看到truncate 是无法进行恢复的,因为表进行truncate之后并不产生undo信息。
SQL> insert into tt values ('one');
已创建 1 行。
SQL> commit;
提交完成。
SQL> insert into tt values ('two');
已创建 1 行。
SQL> commit;
提交完成。
SQL> select sysdate from dual;
SYSDATE
-------------------
2007-05-16 15:49:51
SQL> delete from tt;
已删除2行。
SQL> commit;
提交完成。
--我们先通过时间进行恢复
SQL> flashback table tt to TIMESTAMP to_timestamp('2007-05-16 15:49:51','yyyy-mm-dd hh24:mi:ss');
闪回完成。
SQL> select * from tt;
NAME
----------
one
two
SQL> select current_scn from v$database;
select current_scn from v$database
*
第 1 行出现错误:
ORA-00942: 表或视图不存在
SQL> select current_scn from v$database;
CURRENT_SCN
-----------
594467
SQL> delete from tt;
已删除2行。
SQL> commit;
提交完成。
--也可以通过SCN进行恢复
SQL> flashback table tt to scn 594467;
闪回完成。
SQL> select * from tt;
NAME
----------
one
two
SQL> select current_scn from v$database;
CURRENT_SCN
-----------
595036
SQL> insert into tt values ('three');
已创建 1 行。
SQL> commit;
提交完成。
SQL> select * from tt;
NAME
----------
one
two
three
SQL> create or replace trigger flashback_test
2 after insert or update or Delete on tt
3 for each Row
4 Begin
5 null;
6 end;
7 /
触发器已创建
SQL> select trigger_name,status from user_triggers where trigger_name='FLASHBACK
_TEST';
TRIGGER_NAME STATUS
------------------------------ --------
FLASHBACK_TEST ENABLED
SQL> flashback table tt to scn 595036;
闪回完成。
SQL> select * from tt;
NAME
----------
one
two
SQL> select trigger_name,status from user_triggers where trigger_name='FLASHBACK_TEST';
TRIGGER_NAME STATUS
------------------------------ --------
FLASHBACK_TEST ENABLED