ITPub博客

首页 > Linux操作系统 > Linux操作系统 > 循序渐进oracle第8章:Oracle的闪回特性之恢复删除表的数据四种方法

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

原创 Linux操作系统 作者:mengzhaoliang 时间:2008-06-09 09:29:58 0 删除 编辑

/* 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博客 ” ,链接:https://blog.itpub.net/12778571/viewspace-343096/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
暂无介绍

注册时间:2008-01-30

  • 博文量
    336
  • 访问量
    3085695