zoukankan      html  css  js  c++  java
  • Oracle 的闪回技术 --flashback

    1. 如何开启数据库闪回?

    SQL> shutdown immediate;

    ORA-01109: database not open

     

     

    Database dismounted.

    ORACLE instance shut down.

    SQL> startup mount;

    ORACLE instance started.

     

    Total System Global Area 1653518336 bytes

    Fixed Size                  2253784 bytes

    Variable Size            1207962664 bytes

    Database Buffers          436207616 bytes

    Redo Buffers                7094272 bytes

    Database mounted.

    SQL> alter database flashback on;--------------------------mout情况下开启闪回.

     

    Database altered.

     

    SQL> select flashback_on from v$database;------------------v$database视图中查询闪回是否开启

     

    FLASHBACK_ON

    ------------------

    YES

     

    SQL>

     

    1. 数据库的闪回
      1. 基于SCN的闪回

    SCN是数据库的顺序号,每个号都是数据库的一个状态,当知道具体的SCN号的时候就可以精确地闪回,可以通过日志来查看具体的SCN

    SQL>flashback database to scn 5540916;

    1. 基于时间的闪回

    如果知道大概的时间,也可以通过时间闪回

    Flashback database to '2:05 PM';

    • 需要性能开销、闪回日志磁盘空间的开销
    • 默认数据库的闪回技术是关闭的,建议开启.
    1. 表的闪回
      1. 删除表的闪回

    SQL> conn test/test

    Connected.

    SQL> select * from w;

     

             X

    ----------

             1

     

    SQL> drop table w;--------------------------------------------删除表,没有加purge参数,这时候表会放到回收站

     

    Table dropped.

     

    SQL> select * from w;

    select * from w

                  *

    ERROR at line 1:

    ORA-00942: table or view does not exist

     

     

    SQL> show recyclebin;-----------------------------------------------------查看回收站

    ORIGINAL NAME    RECYCLEBIN NAME                OBJECT TYPE DROP TIME

    ---------------- ------------------------------ ------------ -------------------

    W                BIN$UgO0wwwhHBTgU3ABqMDCXQ==$0 TABLE        2017-06-16:01:07:40

     

     

    SQL> flashback table w to before drop;--------------------------------使用flashback技术还原w

     

    Flashback complete.

     

    SQL> select * from w;

     

             X

    ----------

             1

     

    SQL> drop table w purge;--------------------------如果使用了purge表示表已经在回收站同时删除了,就不能恢复了

     

    Table dropped.

     

    SQL> show recyclebin;

     

    1. DML操作的闪回

    做了一个操作,并且提交了怎么办?

    SQL> select rowid,id,name from g;

     

    ROWID                      ID NAME

    ------------------ ---------- ----------

    AAAVW7AAEAAAAD8AAC          1 a

    AAAVW7AAEAAAAD8AAD          2 b

     

    SQL> select sysdate 时间, timestamp_to_scn(sysdate) SCN from dual;----------------------将系统时间转换为scn

     

    时间                      SCN

    ------------------ ----------

    16-JUN-17             1373003

     

    SQL> delete from g where id=2;

     

    1 row deleted.

     

    SQL> commit;

     

    Commit complete.

     

    SQL> select rowid,id,name from g;

     

    ROWID                      ID NAME

    ------------------ ---------- ----------

    AAAVW7AAEAAAAD8AAC          1 a

     

    SQL> alter table g enable row movement;-----------因为恢复了就不是以前那个位置了,所以flashback需要允许行迁移,在flashback中使用,当需要使用flashback table功能时,需要首先打开row movement的选项,否则使用该功能也会报错

     

     

    Table altered.

     

    SQL> flashback table g to scn 1373003;

     

    Flashback complete.

     

    SQL> select rowid,id,name from g;--------------------注意rowid已经不一样了

     

    ROWID                      ID NAME

    ------------------ ---------- ----------

    AAAVW7AAEAAAAD8AAA          1 a

    AAAVW7AAEAAAAD8AAB          2 b

    1. 事务的闪回
    2. 版本的闪回
    3. 查询的闪回
      1. 基于时间的查询闪回

    Select * from g as of timestamp sysdate-5/1440-----------当前时间-5,这里表示5分钟之前

    1. 基于SCN的查询闪回

    Select * from g as of scn 344197

  • 相关阅读:
    HDU 5650 异或
    HDU 5646
    HDU 5645
    P2075 [NOIP2012T5]借教室 区间更新+二分查找
    HDU 5641
    读写分离
    linux执行cmd之一
    html2image
    挂载引起的权限问题
    如何防止sql注入
  • 原文地址:https://www.cnblogs.com/thescentedpath/p/oracleflashback.html
Copyright © 2011-2022 走看看