Oracle的闪回特性之恢复truncate删除表的数据

Oracle的闪回特性之恢复truncate删除表的数据

SQL> show parameter flashback

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_flashback_retention_target        integer     1440

flashback功能默认为1440分钟,也就一天时间


数据库闪回状态可以从vdatabaseSQL>selectdbid,name,flashbackon,currentscnfromvdatabase;

      DBID NAME      FLASHBACK_ON       CURRENT_SCN
---------- --------- ------------------ -----------
2274326636 RISENET   NO                      997660


flashback_on为NO,也就是没有开flashback功能,当truncate删除数据时,无法使用flashback


启动flashback_on(需要数据库在mount状态下打开)

SQL> shutdown immediate;
数据库已经关闭。
已经卸载数据库。
ORACLE 例程已经关闭。
SQL> startup mount
ORACLE 例程已经启动。

Total System Global Area  272629760 bytes
Fixed Size                  1248476 bytes
Variable Size             109052708 bytes
Database Buffers          155189248 bytes
Redo Buffers                7139328 bytes
数据库装载完毕。
SQL> alter database flashback on;

数据库已更改。

SQL> select dbid,name,flashback_on,current_scn from v$database;

      DBID NAME      FLASHBACK_ON       CURRENT_SCN
---------- --------- ------------------ -----------
2274326636 RISENET   YES                          0

SQL> alter database open;

数据库已更改。

 

SQL> connect scott/mzl
已连接。
SQL> select table_name from user_tables;

TABLE_NAME
------------------------------
SALGRADE
TEST
DEPT_COPY
DEPT_RECOVER
DEPT

SQL> select * from dept;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        50 support        denver
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     beijing

SQL> truncate table dept;

表被截断。

SQL> select * from dept;

未选定行


因为truncate是DDL语句,不是DML语句,所以不能闪回查询as of 来恢复

恢复数据:

SQL> connect sys/mzl as sysdba
已连接。
SQL> shutdown immediate
数据库已经关闭。
已经卸载数据库。
ORACLE 例程已经关闭。
SQL> startup mount
ORACLE 例程已经启动。

Total System Global Area  272629760 bytes
Fixed Size                  1248476 bytes
Variable Size             117441316 bytes
Database Buffers          146800640 bytes
Redo Buffers                7139328 bytes
数据库装载完毕。
SQL> flash database to timestamp
SP2-0734: 未知的命令开头 "flash data..." - 忽略了剩余的行。
SQL> flashback database to timestamp
  2  to_timestamp('2008-06-08 18:22:33','yyyy-mm-dd hh24:mi:ss');

闪回完成。

SQL> alter database open read only;

数据库已更改。

SQL> select * from scott.dept;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        50 support        denver
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     beijing
如果数据恢复不够理想,可以关闭数据库继续进行恢复。 如果用'alter database open resetlogs'打开,在想关闭数据库用flashback恢复就不行了
SQL> shutdown immediate
数据库已经关闭。
已经卸载数据库。
ORACLE 例程已经关闭。
SQL> startup
ORACLE 例程已经启动。

Total System Global Area  272629760 bytes
Fixed Size                  1248476 bytes
Variable Size             117441316 bytes
Database Buffers          146800640 bytes
Redo Buffers                7139328 bytes
数据库装载完毕。
ORA-01589: 要打开数据库则必须使用 RESETLOGS 或 NORESETLOGS 选项


SQL> alter database open resetlogs;

数据库已更改。

SQL> select * from scott.dept;

    DEPTNO DNAME          LOC
---------- -------------- -------------
        50 support        denver
        10 ACCOUNTING     NEW YORK
        20 RESEARCH       DALLAS
        30 SALES          CHICAGO
        40 OPERATIONS     beijing

一旦resetlogs之后,将不能再flashback至resetlogs之前的时间点。

出处:http://blog.163.com/card_2005/blog/static/117810368200910654544146/

posted on   jack_Meng  阅读(6501)  评论(0编辑  收藏  举报

编辑推荐:
· 没有源码,如何修改代码逻辑?
· 一个奇形怪状的面试题:Bean中的CHM要不要加volatile?
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
阅读排行:
· 微软正式发布.NET 10 Preview 1:开启下一代开发框架新篇章
· DeepSeek “源神”启动!「GitHub 热点速览」
· C# 集成 DeepSeek 模型实现 AI 私有化(本地部署与 API 调用教程)
· DeepSeek R1 简明指南:架构、训练、本地部署及硬件要求
· NetPad:一个.NET开源、跨平台的C#编辑器

导航

< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8
点击右上角即可分享
微信分享提示

喜欢请打赏

扫描二维码打赏

支付宝打赏

主题色彩