利用flashback query 恢复表数据

flashback query可以查询过去某个时间点对象的状态,从而可以利用此来进行恢复数据

1 准备测试数据

用普通用户创建一个表,表中插入部分数据:
SQL> show user
USER is "KEL"
SQL> create table flqy(id int,old int);

Table created.

SQL> insert into flqy values (1,20);

1 row created.

SQL> insert into flqy values (2,40);

1 row created.

SQL> commit;

Commit complete.

查看当前系统时间,用来作为查询的基点:
SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss';

Session altered.

SQL> select sysdate from dual;

SYSDATE
-------------------
2014-07-27 20:23:37

2 模拟用户误删除数据

SQL> delete from flqy ;

2 rows deleted.

SQL> commit;

Commit complete.

SQL> select * from flqy;

no rows selected

3 查询过去时间表数据

SQL> select * from flqy as of timestamp to_timestamp('2014-07-27 20:23:37','yyyy-mm-dd hh24:mi:ss');

	ID	  OLD
---------- ----------
	 1	   20
	 2	   40

4 利用flashback query恢复数据

SQL> insert into flqy select * from flqy as of timestamp to_timestamp('2014-07-27 20:23:37','yyyy-mm-dd hh24:mi:ss');

2 rows created.

SQL> commit;

Commit complete.

5 利用flashback query来进行另外一种恢复数据(仅恢复一列数据)

SQL> update flqy f1 set f1.old = (select old from flqy as of timestamp to_timestamp('2014-07-27 20:23:37','yyyy-mm-dd hh24:mi:ss') f2 where f1.id=f2.id);

2 rows updated.

SQL> commit;

Commit complete.

flashback query只能看到某一个时间点的对象的状态,而不能看到某个时间段内的状态变化。

你可能感兴趣的:(闪回,Oracle,flashback,timestamp)