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

相关文章

oracle数据库索引失效的问题及解决

《oracle数据库索引失效的问题及解决》本文总结了在Oracle数据库中索引失效的一些常见场景,包括使用isnull、isnotnull、!=、、、函数处理、like前置%查询以及范围索引和等值索引... 目录oracle数据库索引失效问题场景环境索引失效情况及验证结论一结论二结论三结论四结论五总结ora

SpringCloud集成AlloyDB的示例代码

《SpringCloud集成AlloyDB的示例代码》AlloyDB是GoogleCloud提供的一种高度可扩展、强性能的关系型数据库服务,它兼容PostgreSQL,并提供了更快的查询性能... 目录1.AlloyDBjavascript是什么?AlloyDB 的工作原理2.搭建测试环境3.代码工程1.

Java中ArrayList的8种浅拷贝方式示例代码

《Java中ArrayList的8种浅拷贝方式示例代码》:本文主要介绍Java中ArrayList的8种浅拷贝方式的相关资料,讲解了Java中ArrayList的浅拷贝概念,并详细分享了八种实现浅... 目录引言什么是浅拷贝?ArrayList 浅拷贝的重要性方法一:使用构造函数方法二:使用 addAll(

Golang使用etcd构建分布式锁的示例分享

《Golang使用etcd构建分布式锁的示例分享》在本教程中,我们将学习如何使用Go和etcd构建分布式锁系统,分布式锁系统对于管理对分布式系统中共享资源的并发访问至关重要,它有助于维护一致性,防止竞... 目录引言环境准备新建Go项目实现加锁和解锁功能测试分布式锁重构实现失败重试总结引言我们将使用Go作

Oracle Expdp按条件导出指定表数据的方法实例

《OracleExpdp按条件导出指定表数据的方法实例》:本文主要介绍Oracle的expdp数据泵方式导出特定机构和时间范围的数据,并通过parfile文件进行条件限制和配置,文中通过代码介绍... 目录1.场景描述 2.方案分析3.实验验证 3.1 parfile文件3.2 expdp命令导出4.总结

JAVA利用顺序表实现“杨辉三角”的思路及代码示例

《JAVA利用顺序表实现“杨辉三角”的思路及代码示例》杨辉三角形是中国古代数学的杰出研究成果之一,是我国北宋数学家贾宪于1050年首先发现并使用的,:本文主要介绍JAVA利用顺序表实现杨辉三角的思... 目录一:“杨辉三角”题目链接二:题解代码:三:题解思路:总结一:“杨辉三角”题目链接题目链接:点击这里

SpringBoot使用注解集成Redis缓存的示例代码

《SpringBoot使用注解集成Redis缓存的示例代码》:本文主要介绍在SpringBoot中使用注解集成Redis缓存的步骤,包括添加依赖、创建相关配置类、需要缓存数据的类(Tes... 目录一、创建 Caching 配置类二、创建需要缓存数据的类三、测试方法Spring Boot 熟悉后,集成一个外

Springboot使用RabbitMQ实现关闭超时订单(示例详解)

《Springboot使用RabbitMQ实现关闭超时订单(示例详解)》介绍了如何在SpringBoot项目中使用RabbitMQ实现订单的延时处理和超时关闭,通过配置RabbitMQ的交换机、队列和... 目录1.maven中引入rabbitmq的依赖:2.application.yml中进行rabbit

Python绘制土地利用和土地覆盖类型图示例详解

《Python绘制土地利用和土地覆盖类型图示例详解》本文介绍了如何使用Python绘制土地利用和土地覆盖类型图,并提供了详细的代码示例,通过安装所需的库,准备地理数据,使用geopandas和matp... 目录一、所需库的安装二、数据准备三、绘制土地利用和土地覆盖类型图四、代码解释五、其他可视化形式1.

opencv实现像素统计的示例代码

《opencv实现像素统计的示例代码》本文介绍了OpenCV中统计图像像素信息的常用方法和函数,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一... 目录1. 统计像素值的基本信息2. 统计像素值的直方图3. 统计像素值的总和4. 统计非零像素的数量