Oracle Flashback示例集锦

2023-12-10 00:01
文章标签 oracle 示例 集锦 flashback

本文主要是介绍Oracle Flashback示例集锦,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Flashback Table

本例参考Rewind a Table Using Oracle Flashback Table

Flashback Table,Flashback Query和Flashback Drop的示例合集可参见这里
首先获得当前的时间,或当前的SCN,以便后续回退使用

-- 获取SCN法1
select DBMS_FLASHBACK.GET_SYSTEM_CHANGE_NUMBER from dual;-- 获取SCN法2
select current_scn from v$database;-- 获取当前时间
alter session set nls_date_format='dd-mm-yy hh24:mi:ss';
select sysdate from dual;

输出如下:

GET_SYSTEM_CHANGE_NUMBER
------------------------942616CURRENT_SCN
-----------9426162SYSDATE
-----------------
09-01-23 15:03:33

以SYS登入,并修改数据:

connect / as sysdba
alter session set container=orclpdb1;
alter table hr.regions enable row movement;
select * from hr.regions;REGION_ID REGION_NAME
---------- -------------------------1 Europe2 Americas3 Asia4 Middle East and Africaupdate hr.regions set region_name = 'ORACLE';
commit;
select * from hr.regions;REGION_ID REGION_NAME
---------- -------------------------1 ORACLE2 ORACLE3 ORACLE4 ORACLE

此时,可以采用以下几种方法恢复:

-- 恢复到5分钟前
flashback table hr.regions to timestamp sysdate - 5/24/60;-- 恢复到指定时间点
flashback table hr.regions to timestamp to_timestamp('09-01-23 15:03:33','dd-mm-yy hh24:mi:ss');-- 恢复到指定的SCN
flashback table hr.regions to scn 9426162

也可以用类似的语法查询之前的数据:

select * from hr.regions as of timestamp to_timestamp('09-01-23 15:03:33','dd-mm-yy hh24:mi:ss');
select * from hr.regions as of 9426162;
select * from hr.regions as of timestamp sysdate - 1/24/60;

Flashback Drop

本例参考[https://docs.oracle.com/en/database/oracle/oracle-database/tutorial-rec-flashback/index.html?opt-release-19c](Recover a Dropped Table Using Oracle Flashback Drop)。

LiveSQL中的例子可参见这里。

以SYS用户登录并执行以下:

connect / as sysdba
alter session set container=orclpdb1;
create table hr.regions_hist as select * from hr.regions; 
select * from hr.regions_hist;REGION_ID REGION_NAME
---------- -------------------------1 Europe2 Americas3 Asia4 Middle East and Africadrop table hr.regions_hist;
select * from hr.regions_hist;
select * from hr.regions_hist*
ERROR at line 1:
ORA-00942: table or view does not existflashback table hr.regions_hist to before drop;
select * from hr.regions_hist;REGION_ID REGION_NAME
---------- -------------------------1 Europe2 Americas3 Asia4 Middle East and Africatruncate table hr.regions_hist;Table truncated.flashback table hr.regions_hist to before drop;
flashback table hr.regions_hist to before drop
*
ERROR at line 1:
ORA-38305: object not in RECYCLE BINdrop table hr.regions_hist purge;

Flashback Transaction

本例参考Backing Out Transactions with Flashback

LiveSQL中的示例可参考这里。
什么是Flashback Transaction Back-Out?

Flashback Transaction Back-Out is a logical recovery option to roll back a target transaction and its dependent transactions while the database remains online. A dependent transaction is related by either a write-after-write (WAW) relationship, in which a transaction modifies the same data that was changed by the target transaction, or a primary key constraint relationship, in which a transaction re-inserts the same primary key value that was deleted by the target transaction. Flashback Transaction utilizes undo and the redo generated for undo blocks, to create and execute a compensating transaction for reverting the affected data back to its original state.

Flashback Transaction要求启用归档。如果没有启用,请运行以下脚本:

echo "******************************************* "
echo "For demo purposes ONLY:"
echo "  * Unlock HR account"
echo "  * Enable ARCHIVELOG mode for database"
echo ""
echo "The script may appear to hang at the SQL prompt"
echo "when the database is shutting down and being"
echo "opened. Wait a few minutes and it should progress."
echo "******************************************* "
sqlplus "/ as sysdba" << EOFALTER USER HR IDENTIFIED BY hr ACCOUNT UNLOCK;
ALTER SYSTEM SET db_recovery_file_dest_size = 10737418240 SCOPE=BOTH;shutdown immediate
startup mount
alter database archivelog;
alter database open;
archive log list
exit
EOF

本实验的初始化需要运行以下脚本:

REM "******************************************* "
REM "For demo purposes ONLY:"
REM "  * Setup for Flashback Transaction"
REM "Execute script as SYSDBA" set echo on
set serveroutput on
set term on
set lines 200
set pause on/*== Set up the HR database account for this OBE ==*/ALTER DATABASE ADD SUPPLEMENTAL LOG DATA;
ALTER DATABASE ADD SUPPLEMENTAL LOG DATA (PRIMARY KEY) COLUMNS;ALTER SESSION SET CONTAINER = orclpdb1;
GRANT EXECUTE ON dbms_flashback TO hr;
GRANT select any transaction TO hr;
pause Press [Enter] to continue.../*== Create test data for flashback transaction ==*/
connect hr@orclpdb1;/*== Test transaction 1 ==*/
INSERT INTO hr.regions VALUES (10,'Pole');
INSERT INTO hr.regions VALUES (20,'Moon');
INSERT INTO hr.regions VALUES (30,'Venus');
INSERT INTO hr.regions VALUES (40,'Mars');
INSERT INTO hr.regions VALUES (50,'Saturn');
COMMIT;
pause Press [Enter] to continue.../*== Test transaction 2 ==*/
/*== Region 10 and 20 has a WAW dependency on transaction 1 ==*/
UPDATE hr.regions SET region_name='Two Poles'  WHERE region_id = 10;
UPDATE hr.regions SET region_name='Many Moons' WHERE region_id = 20;
COMMIT;
pause Press [Enter] to continue.../*== Test transaction 3 ==*/
/*== Region 10 has a WAW dependency on transaction 1 and 2 ==*/
/*== Region 40 and 50 has a WAW dependency on transaction 1 ==*/
UPDATE hr.regions SET region_name='No star'  WHERE region_id = 10;
UPDATE hr.regions SET region_name='Red star' WHERE region_id = 40;
UPDATE hr.regions SET region_name='Big star' WHERE region_id = 50;
COMMIT;
pause Press [Enter] to continue.../*== Test transaction 4 ==*/
/*== Region 30 has a WAW dependency on transaction 1 ==*/
UPDATE hr.regions SET region_name='Still called Venus' WHERE region_id = 30;
COMMIT;pause Press [Enter] to continue...connect / as sysdba
ALTER SYSTEM ARCHIVE LOG CURRENT;prompt "Setup for Flashback Transaction completed"
pause Press [Enter] to continue...exit

查看当前数据:

select * from hr.regions;REGION_ID REGION_NAME
---------- -------------------------10 No star20 Many Moons30 Still called Venus40 Red star50 Big star1 Europe2 Americas3 Asia4 Middle East and Africa9 rows selected.

查询和regions表相关的事务:

set lines 140
set pages 9999
col VERSIONS_STARTTIME for a22
col VERSIONS_ENDTIME for a22
select region_id, region_name, versions_xid, versions_startscn, versions_starttime, versions_endscn, versions_endtime, versions_operation from hr.regions versions between scn minvalue and maxvalue;REGION_ID REGION_NAME               VERSIONS_XID     VERSIONS_STARTSCN VERSIONS_STARTTIME   VERSIONS_ENDSCN VERSIONS_ENDTIME     V
---------- ------------------------- ---------------- ----------------- -------------------- --------------- -------------------- -30 Still called Venus        05001900BE030000           9430052 09-JAN-23 03.26.31 P                                      UM50 Big star                  03001400EE030000           9430048 09-JAN-23 03.26.22 P                                      UM40 Red star                  03001400EE030000           9430048 09-JAN-23 03.26.22 P                                      UM10 No star                   03001400EE030000           9430048 09-JAN-23 03.26.22 P                                      UM20 Many Moons                02001600C0030000           9430043 09-JAN-23 03.26.10 P                                      UM10 Two Poles                 02001600C0030000           9430043 09-JAN-23 03.26.10 P         9430048 09-JAN-23 03.26.22 P UM                                    M50 Saturn                    09000B00BB030000           9430037 09-JAN-23 03.25.53 P         9430048 09-JAN-23 03.26.22 P IM                                    M40 Mars                      09000B00BB030000           9430037 09-JAN-23 03.25.53 P         9430048 09-JAN-23 03.26.22 P IM                                    M30 Venus                     09000B00BB030000           9430037 09-JAN-23 03.25.53 P         9430052 09-JAN-23 03.26.31 P IM                                    M20 Moon                      09000B00BB030000           9430037 09-JAN-23 03.25.53 P         9430043 09-JAN-23 03.26.10 P IM                                    M10 Pole                      09000B00BB030000           9430037 09-JAN-23 03.25.53 P         9430043 09-JAN-23 03.26.10 P IM                                    M1 Europe2 Americas3 Asia4 Middle East and Africa15 rows selected.

可查询行的所有修改历史。以上结果按时间降序排列,因此需从底部读起。

以上minvalue和maxvalue实际为scn.minvalue和scn.maxvalue,是常数。也可以查询某事件范围,如between timestamp(systimestamp - 1/24) and timestamp
依赖于伪列(pseudocolumns ); 伪列不是ISO标准。以上VERSIONS_XID,VERSIONS_STARTSCN ,VERSIONS_ENDSCN,VERSIONS_OPERATION都是伪列。最著名的伪列是ROWID。

Flashback Data Archive

本例参考(以后再写)

其它参考

  • Oracle 10g: Recovering from Human Error Using Flashback
Object LevelScenario ExamplesFlashback TechnologyDepends OnAffectes Data
DatabaseTruncate table; undesired multitable changes madeDatabaseFlashback logsTRUE
TableDrop tableDropRecycle binTRUE
Update with the wrong WHERE clauseTableUndo dataTRUE
Compare current data with data from the pastQueryUndo dataFALSE
Compare versions of rowsVersion QueryUndo dataFALSE
TransactionInvestigate and back out suspect transactionsTransaction QueryUndo/redo from archive logsTRUE
Table and TransactionAudit, compliance, historical reports, ILMData Archival (Temporal)TablespaceFALSE

这篇关于Oracle Flashback示例集锦的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/475473

相关文章

Java中StopWatch的使用示例详解

《Java中StopWatch的使用示例详解》stopWatch是org.springframework.util包下的一个工具类,使用它可直观的输出代码执行耗时,以及执行时间百分比,这篇文章主要介绍... 目录stopWatch 是org.springframework.util 包下的一个工具类,使用它

Spring Boot 3.4.3 基于 Spring WebFlux 实现 SSE 功能(代码示例)

《SpringBoot3.4.3基于SpringWebFlux实现SSE功能(代码示例)》SpringBoot3.4.3结合SpringWebFlux实现SSE功能,为实时数据推送提供... 目录1. SSE 简介1.1 什么是 SSE?1.2 SSE 的优点1.3 适用场景2. Spring WebFlu

springboot security快速使用示例详解

《springbootsecurity快速使用示例详解》:本文主要介绍springbootsecurity快速使用示例,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝... 目录创www.chinasem.cn建spring boot项目生成脚手架配置依赖接口示例代码项目结构启用s

golang 日志log与logrus示例详解

《golang日志log与logrus示例详解》log是Go语言标准库中一个简单的日志库,本文给大家介绍golang日志log与logrus示例详解,感兴趣的朋友一起看看吧... 目录一、Go 标准库 log 详解1. 功能特点2. 常用函数3. 示例代码4. 优势和局限二、第三方库 logrus 详解1.

SpringBoot实现MD5加盐算法的示例代码

《SpringBoot实现MD5加盐算法的示例代码》加盐算法是一种用于增强密码安全性的技术,本文主要介绍了SpringBoot实现MD5加盐算法的示例代码,文中通过示例代码介绍的非常详细,对大家的学习... 目录一、什么是加盐算法二、如何实现加盐算法2.1 加盐算法代码实现2.2 注册页面中进行密码加盐2.

Redis 中的热点键和数据倾斜示例详解

《Redis中的热点键和数据倾斜示例详解》热点键是指在Redis中被频繁访问的特定键,这些键由于其高访问频率,可能导致Redis服务器的性能问题,尤其是在高并发场景下,本文给大家介绍Redis中的热... 目录Redis 中的热点键和数据倾斜热点键(Hot Key)定义特点应对策略示例数据倾斜(Data S

JavaScript Array.from及其相关用法详解(示例演示)

《JavaScriptArray.from及其相关用法详解(示例演示)》Array.from方法是ES6引入的一个静态方法,用于从类数组对象或可迭代对象创建一个新的数组实例,本文将详细介绍Array... 目录一、Array.from 方法概述1. 方法介绍2. 示例演示二、结合实际场景的使用1. 初始化二

C#中的 StreamReader/StreamWriter 使用示例详解

《C#中的StreamReader/StreamWriter使用示例详解》在C#开发中,StreamReader和StreamWriter是处理文本文件的核心类,属于System.IO命名空间,本... 目录前言一、什么是 StreamReader 和 StreamWriter?1. 定义2. 特点3. 用

Java中&和&&以及|和||的区别、应用场景和代码示例

《Java中&和&&以及|和||的区别、应用场景和代码示例》:本文主要介绍Java中的逻辑运算符&、&&、|和||的区别,包括它们在布尔和整数类型上的应用,文中通过代码介绍的非常详细,需要的朋友可... 目录前言1. & 和 &&代码示例2. | 和 ||代码示例3. 为什么要使用 & 和 | 而不是总是使

Java强制转化示例代码详解

《Java强制转化示例代码详解》:本文主要介绍Java编程语言中的类型转换,包括基本类型之间的强制类型转换和引用类型的强制类型转换,文中通过代码介绍的非常详细,需要的朋友可以参考下... 目录引入基本类型强制转换1.数字之间2.数字字符之间引入引用类型的强制转换总结引入在Java编程语言中,类型转换(无论