ITPub博客

首页 > 数据库 > Oracle > oracle闪回特性

oracle闪回特性

原创 Oracle 作者:sky850623 时间:2014-01-24 18:01:15 0 删除 编辑

一、闪回查询
1)查询emp1表

20:55:03 SQL> select * from emp1;

     EMPNO ENAME      JOB              MGR HIREDATE            SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------ ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80           800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81          1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81          1250        500         30
      7566 JONES      MANAGER         7839 02-APR-81          2975                    20
      7654 MARTIN     SALESMAN        7698 28-SEP-81          1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81          2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81          2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87          3000                    20
      7839 KING       PRESIDENT            17-NOV-81          5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81          1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87          1100                    20

     EMPNO ENAME      JOB              MGR HIREDATE            SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------ ---------- ---------- ----------
      7900 JAMES      CLERK           7698 03-DEC-81           950                    30
      7902 FORD       ANALYST         7566 03-DEC-81          3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82          1300                    10

14 rows selected.
2)删除一条记录

20:55:55 SQL> delete from emp1 where ename='JONES';

1 row deleted.

20:56:05 SQL> commit;

Commit complete.
3)再次查询

20:56:07 SQL> select * from emp1;

     EMPNO ENAME      JOB              MGR HIREDATE            SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------ ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80           800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81          1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81          1250        500         30
      7654 MARTIN     SALESMAN        7698 28-SEP-81          1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81          2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81          2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87          3000                    20
      7839 KING       PRESIDENT            17-NOV-81          5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81          1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87          1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81           950                    30

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

13 rows selected.
4)恢复删除的记录

20:58:41 SQL> insert into emp1(select *from emp as of timestamp to_timestamp('2014-1-23 20:54:00','yyyy-mm-dd hh24:mi:ss') where ename='JONES');

1 row created.
20:59:11 SQL> commit;

Commit complete.

20:59:18 SQL> select * from emp1;

     EMPNO ENAME      JOB              MGR HIREDATE            SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------ ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80           800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81          1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81          1250        500         30
      7654 MARTIN     SALESMAN        7698 28-SEP-81          1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81          2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81          2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87          3000                    20
      7839 KING       PRESIDENT            17-NOV-81          5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81          1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87          1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81           950                    30

     EMPNO ENAME      JOB              MGR HIREDATE            SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------ ---------- ---------- ----------
      7902 FORD       ANALYST         7566 03-DEC-81          3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82          1300                    10
      7566 JONES      MANAGER         7839 02-APR-81          2975                    20

14 rows selected.
二、闪回表
a)删除一条记录

21:01:02 SQL> select *from emp1;

     EMPNO ENAME      JOB              MGR HIREDATE            SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------ ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80           800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81          1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81          1250        500         30
      7654 MARTIN     SALESMAN        7698 28-SEP-81          1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81          2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81          2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87          3000                    20
      7839 KING       PRESIDENT            17-NOV-81          5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81          1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87          1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81           950                    30

     EMPNO ENAME      JOB              MGR HIREDATE            SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------ ---------- ---------- ----------
      7902 FORD       ANALYST         7566 03-DEC-81          3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82          1300                    10
      7566 JONES      MANAGER         7839 02-APR-81          2975                    20

14 rows selected.

21:11:50 SQL> delete from emp1 where empno=7566;  --删除一行

1 row deleted.

21:12:08 SQL> commit;

Commit complete.

21:12:10 SQL> select *from emp1;

     EMPNO ENAME      JOB              MGR HIREDATE            SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------ ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80           800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81          1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81          1250        500         30
      7654 MARTIN     SALESMAN        7698 28-SEP-81          1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81          2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81          2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87          3000                    20
      7839 KING       PRESIDENT            17-NOV-81          5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81          1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87          1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81           950                    30

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

13 rows selected.
2)使用闪回表恢复

21:12:16 SQL> flashback table emp1 to timestamp to_timestamp('2014-1-23 21:10:00');   --没有启用行移动,不能使用flashback table进行恢复
flashback table emp1 to timestamp to_timestamp('2014-1-23 21:10:00')
                *
ERROR at line 1:
ORA-08189: cannot flashback the table because row movement is not enabled


21:14:45 SQL> select row_movement from user_tables where table_name='EMP1';

ROW_MOVE
--------
DISABLED

21:15:08 SQL> alter table emp1 enable row movement;   --启用行移动

Table altered.

21:15:37 SQL> select row_movement from user_tables where table_name='EMP1';

ROW_MOVE
--------
ENABLED


21:15:43 SQL> flashback table emp1 to timestamp to_timestamp('2014-1-23 21:10:00','yyyy-mm-dd hh24:mi:ss');   --闪回表

Flashback complete.

21:16:26 SQL> select * from emp1;

     EMPNO ENAME      JOB              MGR HIREDATE            SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------ ---------- ---------- ----------
      7369 SMITH      CLERK           7902 17-DEC-80           800                    20
      7499 ALLEN      SALESMAN        7698 20-FEB-81          1600        300         30
      7521 WARD       SALESMAN        7698 22-FEB-81          1250        500         30
      7654 MARTIN     SALESMAN        7698 28-SEP-81          1250       1400         30
      7698 BLAKE      MANAGER         7839 01-MAY-81          2850                    30
      7782 CLARK      MANAGER         7839 09-JUN-81          2450                    10
      7788 SCOTT      ANALYST         7566 19-APR-87          3000                    20
      7839 KING       PRESIDENT            17-NOV-81          5000                    10
      7844 TURNER     SALESMAN        7698 08-SEP-81          1500          0         30
      7876 ADAMS      CLERK           7788 23-MAY-87          1100                    20
      7900 JAMES      CLERK           7698 03-DEC-81           950                    30

     EMPNO ENAME      JOB              MGR HIREDATE            SAL       COMM     DEPTNO
---------- ---------- --------- ---------- ------------ ---------- ---------- ----------
      7902 FORD       ANALYST         7566 03-DEC-81          3000                    20
      7934 MILLER     CLERK           7782 23-JAN-82          1300                    10
      7566 JONES      MANAGER         7839 02-APR-81          2975                    20

14 rows selected.
三、闪回drop
 1) 要使用闪回drop,要先启用recyclebin为on,默认为on,查看recyclebin参数值:

SQL> show parameter recycleb

NAME                                 TYPE       VALUE
------------------------------------ ---------- ----------
recyclebin                           string     on

SQL> alter system set recyclebin=off;   --关闭recyclebin

System altered.

SQL> show parameter recycleb

NAME                                 TYPE        VALUE
------------------------------------ ----------- -------------
recyclebin                           string      OFF

关闭后删除的对象不会放在recyclebin当中

SQL> drop table s;

Table dropped.

SQL> show recyclebin;
ORIGINAL NAME    RECYCLEBIN NAME                OBJECT TYPE  DROP TIME
---------------- ------------------------------ ------------ -------------------
T                BIN$rq6W8XMORSC+tI/adKNm7w==$0 TABLE        2013-11-18:21:29:15

没有s表.

SQL> alter system set recyclebin=on;   --启用recyclebin

System altered.

 

SQL> drop table t2;

Table dropped.

SQL> show recyclebin;
ORIGINAL NAME    RECYCLEBIN NAME                OBJECT TYPE  DROP TIME
---------------- ------------------------------ ------------ -------------------
T                BIN$rq6W8XMORSC+tI/adKNm7w==$0 TABLE        2013-11-18:21:29:15
T2               BIN$4fnu/CPsRIeeFe4rPxP6yA==$0 TABLE        2014-01-25:12:48:00

可以在recycle中查询到删除的t2表
2)查询recyclebin中的对象
SQL> show recyclebin;
ORIGINAL NAME    RECYCLEBIN NAME                OBJECT TYPE  DROP TIME
---------------- ------------------------------ ------------ -------------------
T                BIN$rq6W8XMORSC+tI/adKNm7w==$0 TABLE        2013-11-18:21:29:15
或者使用user_recyclebin这个视图来查询
3)查询删除的t2表

SQL> select * from "BIN$4fnu/CPsRIeeFe4rPxP6yA==$0";

        ID        ID2
---------- ----------
         1          2
         2          3
         3          4
         4          5
         5          6
         6          7
         7          8
         8          9
         9         10
        10         11
        11         12

4)使用flashback table进行恢复

SQL> flashback table "BIN$4fnu/CPsRIeeFe4rPxP6yA==$0" to before drop;

Flashback complete.

5)t2表已恢复
 

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

上一篇: oracle闪回特性
下一篇: expdp使用
请登录后发表评论 登录
全部评论

注册时间:2013-05-30

  • 博文量
    154
  • 访问量
    468939