【Oracle】delete表后commit后怎么找回,方法

有些时候,不小心删除了一些需要的表,而且数据库不能停止,只能一直运行下去,这样的话很麻烦


下面介绍的方法就是删除表后通过时间戳后者scn找回删除的数据


模拟实验环境:

创建一个新表

SQL> create table www as select * from hr.employees;


Table created.


查看新表是否有数据
SQL> select count(*) from www;


  COUNT(*)
----------
       107

查看当前的时间,这里时间或者scn有一个就行。后面会有通过scn恢复的案例

SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;


TO_CHAR(SYSDATE,'YY
-------------------
2017-11-13 17:34:45


查找时间戳之前是否有数据

SQL> select count(*) from www as of timestamp to_timestamp('2017-11-13 17:30:45','yyyy-mm-dd hh24:mi:ss');
select count(*) from www as of timestamp to_timestamp('2017-11-13 17:30:45','yyyy-mm-dd hh24:mi:ss')
                     *
ERROR at line 1:
ORA-01466: unable to read data - table definition has changed


查询下这个时间段的数据是存在的
SQL> select count(*) from www as of timestamp to_timestamp('2017-11-13 17:34:45','yyyy-mm-dd hh24:mi:ss');


  COUNT(*)
----------
       107


删除表:

SQL> delete from  www;


107 rows deleted.


SQL> commit;


Commit complete.


查看www表是没有数据的

SQL> select * from www;


no rows selected


当前时间查看也是没有数据了

SQL> select count(*) from www as of timestamp to_timestamp('2017-11-13 17:37:45','yyyy-mm-dd hh24:mi:ss');


  COUNT(*)
----------
         0


我们需要这样做,查看下删除之前的时间,根据删除之前的时间戳找到www表中的数据

SQL>  select count(*) from www as of timestamp to_timestamp('2017-11-13 17:34:45','yyyy-mm-dd hh24:mi:ss');


  COUNT(*)
----------
       107


查看时间戳发现有数据存在,说明数据可以恢复

SQL> insert into www select * from www as of timestamp to_timestamp('2017-11-13 17:34:45','yyyy-mm-dd hh24:mi:ss');


107 rows created.



SQL> commit;


Commit complete.

数据恢复成功

SQL> select count(*) from www;


  COUNT(*)
----------
       107


同理,scn也是可以恢复的


SQL> select current_scn from v$database;


CURRENT_SCN
-----------
    3698087


SQL> create table www as select * from hr.employees;


Table created.


SQL> select count(*) from www;


  COUNT(*)
----------
       107

查看创建表之后的scn

SQL> select current_scn from v$database;


CURRENT_SCN
-----------
    3698135


SQL> delete from www;


107 rows deleted.


表中的数据已经没有了

SQL> select count(*) from www;


  COUNT(*)
----------
         0

SQL> commit;


Commit complete.


通过创建表之后的scn号来查询后,找到了相关数据

SQL> select count(*) from www as of scn  3698135;


  COUNT(*)
----------
       107


将数据插入到表中即可

SQL> insert into www select * from www as of scn 3698135;

107 rows created.


SQL> commit;


Commit complete.

完成恢复。

同理scn和时间戳可以互相转换
scn-->timestamp

SQL> select scn_to_timestamp(3698087) scn from dual;


SCN
---------------------------------------------------------------------------
13-NOV-17 05.43.11.000000000 PM


timestamp-->scn

SQL> select timestamp_to_scn(to_timestamp('2017-11-13 17:34:45','yyyy-mm-dd hh24-mi-ss')) scn  from dual;


       SCN
----------
   3697660





























posted @   zclinux  阅读(245)  评论(0编辑  收藏  举报
编辑推荐:
· .NET制作智能桌面机器人:结合BotSharp智能体框架开发语音交互
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
阅读排行:
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(四):结合BotSharp
· 一个基于 .NET 开源免费的异地组网和内网穿透工具
· 《HelloGitHub》第 108 期
· Windows桌面应用自动更新解决方案SharpUpdater5发布
· 我的家庭实验室服务器集群硬件清单
点击右上角即可分享
微信分享提示