當數據被錯誤修改時,如何還原。通過undo回滾段,不用開啓閃回功能,有如下幾種辦法:
oracle的兩個函數scn_to_timestamp和timestamp_to_scn
SQL> select timestamp_to_scn(sysdate) from dual;
TIMESTAMP_TO_SCN(SYSDATE)
-------------------------
3289230
SQL> select to_char(scn_to_timestamp(3289211),'yyyy-mm-dd hh24:mi:ss') from dual;
TO_CHAR(SCN_TO_TIMESTAMP(3289211),'YYY
--------------------------------------
2012-02-29 04:06:14
附上修改session顯示格式的語句
alter session set nls_date_format ='yyyy-mm-dd hh24:mi:ss';
一.利用回滾段信息閃回查詢,和對錶進行閃回
查看當前時間與scn號對應的關係
SQL> select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') now ,to_char(dbms_flashback.get_system_change_number) scn from dual;
NOW
--------------------------------------
SCN
--------------------------------------------------------------------------------
2011-11-24 13:59:45
1186296
通過timestamp閃回查詢數據
select * from e as of timestamp to_timestamp('2011-11-24 12:10:00','yyyy-mm-dd hh24:mi:ss');
通過scn閃回查詢數據
SQL> select * from e as of scn 1186000;
將表中的數據閃回到某一時刻
先開啓行移動
SQL> alter table e enable row movement;
Table altered.
再進行表的閃回
SQL> flashback table e to scn 1186800;
Flashback complete.
或
SQL> flashback table e to timestamp to_timestamp('2011-11-24 12:10:00','yyyy-mm-dd hh24:mi:ss');
Flashback complete.
如執行時報沒有權限則授予此功能需要的權限
ant execute on dbms_flashback to 用戶名;
二.Versions between版本檢查
可以查看指定時間段內undo表空間(默認保存900秒)中記錄的不同版本(只含被提交的)
注:保存時間可通過show parameter undo_retention查看,單位爲秒
在標準查詢後添加
versions between timestamp[或scn] 時間1 and 時間2
即可
該功能包含以下僞列
versions_startscn 開始scn號
versions_starttime 開始時間
versions_endscn 結束scn號
versions_endtime 結束時間
versions_xid 該操作的事務ID
versions_operation 執行的操作
範例:
select ename,sal,versions_startscn,versions_endscn,versions_operation, versions_xid from e
versions between timestamp to_timestamp('2011-11-24 14:15:00','yyyy-mm-dd hh24-mi-ss')
and to_timestamp('2011-11-24 14:20:19','yyyy-mm-dd hh24-mi-ss')
或
select ename,sal,versions_startscn,versions_endscn,versions_operation, versions_xid from e
versions between scn 1187395 and 1187538
三.查看回滾段的回滾語句(可以根據上面的方法查到的XID號,查找還原語句)
select undo_sql,to_char(start_timestamp,'yyyy-mm-dd hh:mi:ss') starttime,
to_char(commit_timestamp,'yyyy-mm-dd hh:mi:ss') committime
from flashback_transaction_query
where table_name='E'
and XID= '03000400CB020000'
/