欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

oracle误删数据表还原的二种方法(oracle还原)

程序员文章站 2022-07-20 14:47:56
一、如果是刚刚删除,那么有两方法: 首先用show parameter undo;命令查看当时的数据库参数undo_retention设置。 显示如下:复制代码 代码如...

一、如果是刚刚删除,那么有两方法:

首先用show parameter undo;命令查看当时的数据库参数undo_retention设置。

显示如下:

复制代码 代码如下:

undo_management   string   auto

undo_retention  integer 10800

undo_suppress_errors  boolean  false

undo_tablespace   string   undotbs1

undo_retention(保持力),10800单位是秒。即3个小时。

修改默认的undo_retention参数设置:

复制代码 代码如下:

alter system set undo_retention=10800 scope=both;

方法1,通过oracle提供的回闪功能:

复制代码 代码如下:

exec dbms_flashback.enable_at_time(to_date('2007-07-23 10:21:00','yyyy-mm-dd hh24:mi:ss'));

set serveroutput on

declare r_temp hr.job_history%rowtype;

cursor c_temp is select * from hr.job_history;

begin

open c_temp;

dbms_flashback.disable;

loop

fetch c_temp into r_temp;

exit when c_temp%notfound;

insert into hr.job_history(employee_id,job_id,start_date,end_date) values (r_temp.employee_id,r_temp.job_id,r_temp.start_date,r_temp.end_date);

commit;

end loop;

close c_temp;

end;

方法2,insert into hr.job_history

复制代码 代码如下:

select * from hr.job_history as of timestamp to_timestamp('2007-07-23 10:20:00', 'yyyy-mm-dd hh24:mi:ss');

这种方法简单,容易掌握,功能和上面的一样时间为你误操作之前的时间,最好是离误操作比较近的,因为oracle保存在回滚保持段里的数据时间有一定的时间限制由undo_retention 这个参数值决定。

二、如果是删除一段时间了,但你有比较新的数据库备份,就通过备份来恢复。新建一个库,把备份还原上去,导出表数据,再导入到现在用的库中去。

三、如果删除一段时间了,并且无备份,但是数据在写入表的时候同时会写入其它一些关联表的话,那么就尝试通过写sql语句从其它表取数据出来insert到被删除的表中。

四、恢复到备份表中

复制代码 代码如下:

create table tablename_bak
as
select * from tablename as of timestamp to_timestamp('20081126 103435','yyyymmdd hh24miss');