在Oracle的数据库中,若是不当心删除数据,该如何恢复数据呢?数据库
有两种方法 :scn 方法和时间戳方法spa
1、恢复删除数据的SQL语法(建议用时间戳).net
一、经过scn恢复删除且已提交的数据3d
1)得到当前数据库的scn号code
select current_scn from v$database; (切换到sys用户或system用户查询) blog
查询到的scn号为:1499223flash
2)查询当前scn号以前的scnit
select * from 表名 as of scn 1499220; (肯定删除的数据是否存在,若是存在,则恢复数据;若是不是,则继续缩小scn号)table
3)恢复删除且已提交的数据class
flashback table 表名 to scn 1499220;
二、经过时间恢复删除且已提交的数据
1)查询当前系统时间
select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') from dual;
2)查询删除数据的时间点的数据
select * from 表名 as of timestamp to_timestamp('2018-10-09 15:29:00','yyyy-mm-dd hh24:mi:ss'); (若是不是,则继续缩小范围)
3)恢复删除且已提交的数据
--开启行移动功能(解决执行如下语句报错问题)
alter table 表名 enable row movement;
--恢复某个时间点的数据
flashback table 表名 to timestamp to_timestamp('2018-10-09 15:29:00','yyyy-mm-dd hh24:mi:ss');
--关闭行移动功能
alter table 表名 disable row movement;
2、恢复删除数据的实例(方法)
一、查询删除数据前表数据
--查询删除前表的数据--- select * from Dxc_Goods;
二、执行删除数据操做(132,133),并查看
--执行删除操做:132,133-- delete from Dxc_GOODS where MID in(132,133); --提交(模拟误删操做) commit;
查看结果
三、恢复删除并提交的数据 (指定删除时间点,保证这个是执行删除以前的时间)
--开启行移动功能(解决执行如下语句报错问题) alter table Dxc_Goods enable row movement; --恢复某个时间点的数据 flashback table Dxc_Goods to timestamp to_timestamp('2019-07-24 18:00:00','yyyy-mm-dd hh24:mi:ss'); --关闭行移动功能 alter table Dxc_Goods disable row movement;
执行后,查询数据(132,133数据已恢复)
PS:
参考网址:https://blog.csdn.net/qq_36460189/article/details/82983732