循序渐进oracle第8章:Oracle的闪回特性之恢复删除表的数据四种方法

本文介绍了Oracle数据库中恢复已删除表数据的四种方法:通过时间戳闪回、通过SCN闪回、使用闪回查询按时间恢复以及使用闪回查询按SCN恢复。文章详细展示了如何利用这些技术恢复误删的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

/* 2008/06/09
*环境:Windows XP +Oracle10.2.0.1
*非归档模式
*循序渐进oracle——数据库管理、优化与备份恢复
*循序渐进oracle第8章:Oracle的闪回特性之(flashback query)和(flashback table)恢复删除表的数据
*恢复删除表的四种方法
*/

C:\>sqlplus "/as sysdba"

SQL*Plus: Release 10.2.0.1.0 - Production on 星期一 6月 9 08:26:42 2008

Copyright (c) 1982, 2005, Oracle.  All rights reserved.


连接到:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

SQL> select instance_name,status from v$instance;

INSTANCE_NAME    STATUS
---------------- ------------
orcl             OPEN

非归档模式:
SQL> archive log list;
数据库日志模式             非存档模式
自动存档             禁用
存档终点            USE_DB_RECOVERY_FILE_DEST
最早的联机日志序列     1
当前日志序列           3
SQL>
SQL> show parameter flashback;

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_flashback_retention_target        integer     1440
SQL> select dbid,name,flashback_on,current_scn from v$database;

      DBID NAME      FLASHBACK_ON       CURRENT_SCN
---------- --------- ------------------ -----------
1184709774 ORCL      NO                      578449

SQL> alter user scott account unlock;

用户已更改。

SQL> connect scott/tiger
ERROR:
ORA-28001: the password has expired


更改 scott 的口令
新口令:
重新键入新口令:
口令已更改
已连接。

SQL> create table empcopy
  2  as
  3  select * from emp;

表已创建。

SQL> select * from emp;

SQL> delete emp where deptno<50;

已删除14行。

SQL> commit;

提交完成。


已经提交,怎么恢复:(flashback不支持sys用户进行DML操作)
方法一:
用flashback的时间闪回。需要flashback一个数据表,需要具有flashback any table的系统权限或者是

该表的flashback对象权限,同时具有该表的select,insert,delete,alter权限,由于flashback table技

术使用DML操作去恢复数据,不能保证rowid不变,所以在闪回之前还需要启用表的row movement特性。

SQL> alter table emp enable row movement;

表已更改。

SQL> flashback table emp to timestamp to_timestamp('2008-06-09 8:40:00','yyyy-mm
-dd hh24:mi:ss');

闪回完成。

SQL> select count(*) from emp;

  COUNT(*)
----------
        14

 


方法二:
用flashback的SCN闪回.

SQL> conn sys/mzl as sysdba
已连接。
SQL> select dbms_flashback.get_system_change_number from dual;

GET_SYSTEM_CHANGE_NUMBER
------------------------
                  580643

SQL> alter table scott.emp enable row movement;

表已更改。

SQL> flashback table scott.emp to scn 580000;

闪回完成。

***********************************************************************************

怎么确定没删数据前的scn呢?

可以查询v$archived_log视图来找时间对应的scn.

SQL> alter session set nls_date_format='yyyy-mm-dd hh24:mi:ss'

SQL> select name,first_change#,next_change#,first_time from v$archived_log;

或者:

SQL> select thread#,stamp,first_change#,next_change#,first_time from v$log_history;

**************************************************************************************

SQL> select count(*) from scott.emp;

  COUNT(*)
----------
        14

 

方法三:用闪回查询(flashback query)闪回,从oracle9i开始就可以用该功能,用闪回时间查询功能。
SQL> create table emp_recover
  2  as
  3  select * from emp as of timestamp to_timestamp('2008-06-09 9:00:00','yyyy-m
m-dd hh24:mi:ss');

表已创建。


SQL> select count(*) from emp_recover;

  COUNT(*)
----------
        14

SQL> delete from emp;

 

SQL> insert into emp
  2  select * from emp_recover;

已创建14行。

SQL> select count(*) from emp;

  COUNT(*)
----------
        14


方法四:用闪回查询(flashback query)闪回,从oracle9i开始就可以用该功能,用闪回SCN查询功能。
SQL> connect sys/mzl as sysdba
已连接。
SQL> select dbms_flashback.get_system_change_number "SCN" from dual;

       SCN
----------
    582257

SQL> drop table scott.emp_recover;

表已删除。

创建恢复表:
SQL> create table scott.emp_recover
  2  as
  3  select * from scott.emp where 1=0;

表已创建。

用变量多次输入最接近的scn:
SQL> select count(*) from scott.emp as of scn &scn;
输入 scn 的值:  582250
原值    1: select count(*) from scott.emp as of scn &scn
新值    1: select count(*) from scott.emp as of scn 582250

  COUNT(*)
----------
         0

SQL> select count(*) from scott.emp as of scn &scn;
输入 scn 的值:  582000
原值    1: select count(*) from scott.emp as of scn &scn
新值    1: select count(*) from scott.emp as of scn 582000

  COUNT(*)
----------
         0

SQL> select count(*) from scott.emp as of scn &scn;
输入 scn 的值:  581000
原值    1: select count(*) from scott.emp as of scn &scn
新值    1: select count(*) from scott.emp as of scn 581000

  COUNT(*)
----------
        14

SQL> insert into scott.emp_recover
  2  select * from scott.emp as of scn 581000;

已创建14行。

SQL> commit;

提交完成。

SQL> delete from scott.emp;

已删除0行。


SQL> insert into scott.emp
  2  select * from scott.emp_recover;

已创建14行。

SQL> select count(*) from scott.emp;

  COUNT(*)
----------
        14

 

注释:flashback不支持sys用户进行DML操作,所以最好不要用sys操作数据。

 

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12778571/viewspace-343096/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/12778571/viewspace-343096/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值