闪回 错误的DML 操作

在oracle10g版本之前,如果用户对一个表进行了错误的DML操作并且提交了这些操作,而且之前用户也没备份,要恢复该表到操作之前的状态,就要对这个表进行不完全回复。oracle10g中只要使用一条命令就可以了!oracle是利用还原段中的数据来执行这一恢复的。

提交DML操作之后,该操作所使用的还原段就可以被其他的操作使用了,为了保证在进行闪回操作时这些数据仍然在还原段中,可能要重新设置undoretention的数值,单位为妙,表示事务提交的数据至少在还原段里停留的时间。

实验如下:

SQL> create table test as select * from  emp;--首先创建一个test表用于实验。

表已创建。

SQL> select *  from test;

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7369 SMITH      CLERK           7902 17-12月-80            800           
        20                                                                     
                                                                               
      7499 ALLEN      SALESMAN        7698 20-2月 -81           1600        300
        30                                                                     
                                                                               
      7521 WARD       SALESMAN        7698 22-2月 -81           1250        500
        30                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7566 JONES      MANAGER         7839 02-4月 -81           2975           
        20                                                                     
                                                                               
      7654 MARTIN     SALESMAN        7698 28-9月 -81           1250       1400
        30                                                                     
                                                                               
      7698 BLAKE      MANAGER         7839 01-5月 -81           2850           
        30                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7782 CLARK      MANAGER         7839 09-6月 -81           2450           
        10                                                                     
                                                                               
      7788 SCOTT      ANALYST         7566 19-4月 -87           3000           
        20                                                                     
                                                                               
      7839 KING       PRESIDENT            17-11月-81           5000           
        10                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7844 TURNER     SALESMAN        7698 08-9月 -81           1500          0
        30                                                                     
                                                                               
      7876 ADAMS      CLERK           7788 23-5月 -87           1100           
        20                                                                     
                                                                               
      7900 JAMES      CLERK           7698 03-12月-81            950           
        30                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7902 FORD       ANALYST         7566 03-12月-81           3000           
        20                                                                     
                                                                               
      7934 MILLER     CLERK           7782 23-1月 -82           1300           
        10                                                                     
                                                                               

已选择14行。

SQL> conn system/yang --
已连接。

2  更改undo_retention的值。
SQL> show parameter undo_retention

NAME                                 TYPE        VALUE                         
------------------------------------ ----------- ------------------------------
undo_retention                       integer     900                           
SQL> alter system set undo_retention =7200 ;

系统已更改。

SQL> show parameter undo_retention

NAME                                 TYPE        VALUE                         
------------------------------------ ----------- ------------------------------
undo_retention                       integer     7200         

3 对test表进行DML操作                 

SQL> update test set sal =9999;

已更新14行。

4。接下来,使用带有version子句的查询语句获取刚刚所做的DML操作的version_ID 和其他相关信息。versions_xid,-- 事务号,minvalue 最小值, maxvalue 最大值

SQL> select versions_xid,empno,ename,sal
  2  from test
  3  versions between scn minvalue and maxvalue
  4  where empno =7900;

VERSIONS_XID          EMPNO ENAME             SAL                              
---------------- ---------- ---------- ----------                              
                       7900 JAMES             950        

由于还没有提交DML操作,所以versionid 为空。进行提交。                      

SQL> commit;

提交完成。

SQL> select versions_xid,empno,ename,sal
  2  from test
  3  versions between scn minvalue and maxvalue
  4  where empno =7900;

VERSIONS_XID          EMPNO ENAME             SAL                              
---------------- ---------- ---------- ----------                              
0800050012030000       7900 JAMES            9999                              
                       7900 JAMES             950         

此时 versionid 已经 有值了,为了使用闪回功能,我们要进入到system用户下。                     

SQL> conn system/yang
已连接。
SQL> col operation for a10
SQL> col undo_sql for a80
SQL> select operation,undo_sql
  2  from flashback_transaction_query
  3  where xid = hextoraw('0800050012030000');

OPERATION                                                                      
----------                                                                     
UNDO_SQL                                                                       
--------------------------------------------------------------------------------
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '1300' where ROWID = 'AAARWCAAEAAAAGMAAN';   
                                                                               
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '3000' where ROWID = 'AAARWCAAEAAAAGMAAM';   
                                                                               
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '950' where ROWID = 'AAARWCAAEAAAAGMAAL';    
                                                                               

OPERATION                                                                      
----------                                                                     
UNDO_SQL                                                                       
--------------------------------------------------------------------------------
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '1100' where ROWID = 'AAARWCAAEAAAAGMAAK';   
                                                                               
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '1500' where ROWID = 'AAARWCAAEAAAAGMAAJ';   
                                                                               
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '5000' where ROWID = 'AAARWCAAEAAAAGMAAI';   
                                                                               

OPERATION                                                                      
----------                                                                     
UNDO_SQL                                                                       
--------------------------------------------------------------------------------
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '3000' where ROWID = 'AAARWCAAEAAAAGMAAH';   
                                                                               
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '1250' where ROWID = 'AAARWCAAEAAAAGMAAC';   
                                                                               
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '2975' where ROWID = 'AAARWCAAEAAAAGMAAD';   
                                                                               

OPERATION                                                                      
----------                                                                     
UNDO_SQL                                                                       
--------------------------------------------------------------------------------
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '1250' where ROWID = 'AAARWCAAEAAAAGMAAE';   
                                                                               
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '2850' where ROWID = 'AAARWCAAEAAAAGMAAF';   
                                                                               
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '2450' where ROWID = 'AAARWCAAEAAAAGMAAG';   
                                                                               

OPERATION                                                                      
----------                                                                     
UNDO_SQL                                                                       
--------------------------------------------------------------------------------
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '1600' where ROWID = 'AAARWCAAEAAAAGMAAB';   
                                                                               
UPDATE                                                                         
update "SCOTT"."TEST" set "SAL" = '800' where ROWID = 'AAARWCAAEAAAAGMAAA';    
                                                                               
BEGIN                                                                          
                                                                               
                                                                               

已选择15行。

查询test表的内容,看见所有的工资都变为了9999

SQL> select * from scott.test ;

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7369 SMITH      CLERK           7902 17-12月-80           9999           
        20                                                                     
                                                                               
      7499 ALLEN      SALESMAN        7698 20-2月 -81           9999        300
        30                                                                     
                                                                               
      7521 WARD       SALESMAN        7698 22-2月 -81           9999        500
        30                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7566 JONES      MANAGER         7839 02-4月 -81           9999           
        20                                                                     
                                                                               
      7654 MARTIN     SALESMAN        7698 28-9月 -81           9999       1400
        30                                                                     
                                                                               
      7698 BLAKE      MANAGER         7839 01-5月 -81           9999           
        30                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7782 CLARK      MANAGER         7839 09-6月 -81           9999           
        10                                                                     
                                                                               
      7788 SCOTT      ANALYST         7566 19-4月 -87           9999           
        20                                                                     
                                                                               
      7839 KING       PRESIDENT            17-11月-81           9999           
        10                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7844 TURNER     SALESMAN        7698 08-9月 -81           9999          0
        30                                                                     
                                                                               
      7876 ADAMS      CLERK           7788 23-5月 -87           9999           
        20                                                                     
                                                                               
      7900 JAMES      CLERK           7698 03-12月-81           9999           
        30                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7902 FORD       ANALYST         7566 03-12月-81           9999           
        20                                                                     
                                                                               
      7934 MILLER     CLERK           7782 23-1月 -82           9999           
        10                                                                     
                                                                               

已选择14行。

6 如果要对刚才的操作进行恢复,则先要使用下面的查询获得DML事务所对应的SCN号

SQL> select operation,START_SCN
  2  from flashback_transaction_query
  3  where xid=hextoraw('0800050012030000');

OPERATION   START_SCN                                                          
---------- ----------                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          

OPERATION   START_SCN                                                          
---------- ----------                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
UPDATE        1371887                                                          
BEGIN         1371887                                                          

已选择15行。

---------------这个是当前的SCN号-------------

SQL> select current_scn from v$database;

CURRENT_SCN                                                                    
-----------                                                                    
    1372969                                                                    

SQL> flashback table scott.test to SCN 1371887;
flashback table scott.test to SCN 1371887
                      *
第 1 行出现错误:
ORA-08189: 因为未启用行移动功能, 不能闪回表

出现此错误是因为在闪回一个表之前,要先将该表的行移动功能打开,oracle默认是关闭的。
SQL> alter table scott.test enable row movement;

表已更改。

执行闪回操作。

SQL> flashback table scott.test to SCN 1371887;

闪回完成。

SQL> select * from scott.test;

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7369 SMITH      CLERK           7902 17-12月-80            800           
        20                                                                     
                                                                               
      7499 ALLEN      SALESMAN        7698 20-2月 -81           1600        300
        30                                                                     
                                                                               
      7521 WARD       SALESMAN        7698 22-2月 -81           1250        500
        30                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7566 JONES      MANAGER         7839 02-4月 -81           2975           
        20                                                                     
                                                                               
      7654 MARTIN     SALESMAN        7698 28-9月 -81           1250       1400
        30                                                                     
                                                                               
      7698 BLAKE      MANAGER         7839 01-5月 -81           2850           
        30                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7782 CLARK      MANAGER         7839 09-6月 -81           2450           
        10                                                                     
                                                                               
      7788 SCOTT      ANALYST         7566 19-4月 -87           3000           
        20                                                                     
                                                                               
      7839 KING       PRESIDENT            17-11月-81           5000           
        10                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7844 TURNER     SALESMAN        7698 08-9月 -81           1500          0
        30                                                                     
                                                                               
      7876 ADAMS      CLERK           7788 23-5月 -87           1100           
        20                                                                     
                                                                               
      7900 JAMES      CLERK           7698 03-12月-81            950           
        30                                                                     
                                                                               

     EMPNO ENAME      JOB              MGR HIREDATE              SAL       COMM
---------- ---------- --------- ---------- -------------- ---------- ----------
    DEPTNO                                                                     
----------                                                                     
      7902 FORD       ANALYST         7566 03-12月-81           3000           
        20                                                                     
                                                                               
      7934 MILLER     CLERK           7782 23-1月 -82           1300           
        10                                                                     
                                                                               

已选择14行。

成功。。。。。。

时间: 2024-09-29 02:26:19

闪回 错误的DML 操作的相关文章

Oracle学习(二十三) 闪回操作(flashback)

Oracle的闪回功能可以在对数据库进行不完全恢复的情况下,对某一个指定的表进行恢复.闪回数据 库是进行时间点恢复的新方法,它能够快速将Oracle恢复到以前的时间,以更正由于逻辑数据损坏或用 户错误而引起的问题.当需要恢复时,可以将数据库恢复到错误前的时间点,并且只恢复改变的数据块 . Oracle中的闪回操作包括以下4种: (1)查询闪回:查询过去某个指定时间.指定实体的数据,恢复错误的数据库更新.删除等. (2)表闪回:使表返回到过去的某一时间的状态,恢复表.取消对表进行的修改. (3)删

用Oracle闪回功能恢复偶然丢失的数据

[导读]本文提出了闪回功能的原理,描述了利用Oracle 9i或Oracle 10g的闪回功能进行恢复偶然丢失数据的方法. 前言 人为的错误是数据库系统失败的重要原因之一,根据调查约40%的系统问题是操作失误或者用户错误引起的,这些人为的错误又特别难以避免.传统上当发生数据丢失.数据错误问题时,解决的主要方法就是数据的导入/导出.备份/恢复技术.这些方法都需要发生数据错误之前有一个正确的备份,才能进行恢复.恢复时不取决于错误程度,而只取决于备份/恢复策略.这种方法既耗时又使数据库系统不能提供服务

PostgreSQL flashback(闪回) 功能实现与介绍

标签 PostgreSQL , 脏读 , 事务 , flashback , 闪回 , drop , truncate , dml 背景 闪回的需求往往是救命的需求,因为通常情况下数据库正常运行是不需要闪回的,往往是出现了误操作,被攻击,被注入后,数据库的数据被删除或恶意纂改并且纂改的事务已提交,也就是说纂改已经被持久化了. 这种情况下需要闪回来救命,回到被破坏前的状态. 闪回的目标分为两种: DML闪回和DDL闪回. DML闪回指对INSET, UPDATE, DELETE操作的闪回.DDL闪回

Oracle 闪回特性(Flashback Version、Flashback Transaction)

--========================================================== -- Oracle 闪回特性(Flashback Version.Flashback Transaction) --==========================================================         Oracle闪回特性为数据的快速回复某一对象的特定数据提供了更多的便利.前面介绍了闪回的几种特性,包括flashback dat

闪回的用途与实战(闪回表,闪回删除,闪回重名删除,闪回版本查询)

闪回可以做的操作有如下几种类型:1.当数据错误删除,并且提交时(flashback table)2.当错误删除了一张表drop table(flashback drop)3.通过闪回获取表的历史记录(flashback version query)4.当错误地执行了一个事务,并且提交了(flashback transaction query)5.将整个数据库进行恢复. 使用闪回条件:1.切换到管理员上. 假设开始登录的用户名和密码是MISPS 如果是连接远程的,连接sqlplus的方式是: sq

【DG】利用闪回数据库(flashback)修复Failover后的DG环境

利用闪回数据库(flashback)修复Failover后的DG环境   1.1  BLOG文档结构图       1.2  前言部分 1.2.1  导读和注意事项 各位技术爱好者,看完本文后,你可以掌握如下的技能,也可以学到一些其它你所不知道的知识,~O(∩_∩)O~: ① Failover后DG环境的恢复方法(重点) ② DG的基本维护操作 ③ GC客户端软件的安装 ④ 利用GC快速搭建一套DG环境 ⑤ Failover和Switchover的区别 ⑥ 其它维护操作   Tips: ① 本文

Oracle 10g中的闪回查询操作实例

与Oracle 9i相比Oracle 10g的Flashback有了非常大的改进,在Orcle 10g之前,SMON_SCN_TIME由SMON来获取和记录信息的,每5分钟记录一次,从Oracle 10g开始,LGWR首先会在SGA中记录SCN与时间的映射关系(由于LGWR至少每3秒就会被激活一次,所以现在SMON_SCN_TIME能够支持大于3秒的闪回),SMON则定期检查SGA是否内存中的映射大于磁盘上的,如果有就刷新纪录到磁盘, 而且从普通的Flashback Query发展到了多种形式,

oracle 可以做闪回的操作有哪些

问题描述 请问下ORACLE可以做闪回的操作有哪些? 解决方案 闪回数据库闪回表(一种是删除表,一种是执行了错误的操作)闪回查询

Oracle 9i中的一个闪回查询操作实例

在利用闪回功能前需要确认: 1.用户有对dbms_flashback包有执行权限! 2.进行闪回查询必须设置自动回滚段管理,在init.ora设置参数UNDO_MANAGEMENT=AUTO,参数UNDO_RETENTION=n,决定了能往前闪回的最大时间,值越大就需要越多Undo空间. Oracle 9i中闪回查询操作实例 查看Oracle中Delete和Commit操作的流程分析 例:Oracle 9i的Flashback Query操作. (1)创建闪回查询用户 SQL> create u