oracle delete from table 误删数据的恢复:

oracle delete from table 误删数据的恢复:
select log_mode,open_mode,flashback_on from v$database;        --查看闪回功能是否开启(NO 表示未开启;YES 表示启用)
--开启闪回
shutdown immediate
startup mount;
alter database flashback on;    --开启闪回
alter database flashback off;    --关闭闪回
alter database open;    


创建测试表:
sqlplus / as sysdba
create tablespace test datafile '/u01/app/oracle/oradata/orcl/test.dbf' size 1024m autoextend on next 10m;
create user c##test identified by test;
grant dba to c##test;
ALTER USER c##test DEFAULT TABLESPACE test;
ALTER USER c##test TEMPORARY TABLESPACE TEMP;
conn c##test/test
create table t (ID int,NAME varchar2(20));    
insert into t values(1,'ZhangSan');
select * from c##test.t;

create table t3 as select * from t;

set time on;    --开启时间显示功能


select * from t3;
delete from t3;        --删除表中的数据(只是删除了数据,但表还是存在的)


--查询表 t3 在 2018-12-27 13:08:28 这个时间点的数据(这张表是使用 delete from t3 删除的)
select * from t3 as of timestamp to_timestamp('2018-12-27 13:50:40', 'yyyy-mm-dd HH24:mi:ss') ;
--发现用时间点查询的有数据,但用select * from t3;查询是没有数据的


--如上:知道了表 t3 在 2018-12-27 13:50:40 的数据之后,我们就可以很容易的把表数据恢复到2018-12-27 13:50:40这个时间点了
--第一种方法,通过查询闪回把2018-12-27 13:50:40这个时点之前的数据拿出来然后插入到当前 t3 表中
insert into t3 select * from t3 as of timestamp to_timestamp('2018-12-27 13:50:40', 'yyyy-mm-dd HH24:mi:ss');
--第二种方法:先找出从person表中删除的记录,然后再把它们插到person表中,Sql语句如下:
insert into t3 select * from t3 as of timestamp to_timestamp('2018-12-27 13:50:40', 'yyyy-mm-dd HH24:mi:ss') p where not exists(select * from t3 where id=p.id);

猜你喜欢

转载自blog.csdn.net/yaoshixian/article/details/85279730