Oracle principle: flashback

table of Contents

1. Flashback query method in 9i

Second, the 10g flashback version query method

Three, 10g flashback transaction query method and recovery data

Four, 10g flashback meter

Five, 10g flashback delete

Six, 10g flashback database


1. Flashback query method in 9i

      You can query forward according to the time point or SCN (System Change Number) to obtain the data before modification. Flashback Query relies on the previous mirroring of the data stored in the rollback segment, and the retention time of the previous mirror by setting the undo_retention parameter.

     select ...   as of scn |timestamp

For example, there are 8 rows of data in the existing table:

In the case that the retention time of the rollback segment is still in effect, knowing the exact time you want to restore to

delete salary_tbl where employer_nm like '雇佣者导入1';
commit ;
select * from salary_tbl  as of timestamp to_date('20200723 22:45:30','yyyymmdd hh24:mi:ss');

Checkpoint query: need to estimate the value of the checkpoint

select current_scn from v$database;  --查询当前检查点
delete salary_tbl where employer_nm like '雇佣者导入1';
commit ;
select * from salary_tbl  as of scn 8681060;

Use Flashback Query to recover data

create table salary_tbl_bak as select * from salary_tbl  as of timestamp to_date('20200723 22:45:30','yyyymmdd hh24:mi:ss');
truncate table salary_tbl;
insert into salary_tbl select * from salary_tbl_bak;
drop table salary_tbl_bak;

Second, the 10g flashback version query method

10g provides a flashback version that can be queried in different versions within a time period.

select  ...  from ... versions between 

Among them, select can choose fake and inferior, query ID, SCN number, etc.

select versions_startTime,versions_endtime,versions_xid,versions_operation,
                                      salary_tbl.employer_nm,
                                      salary_tbl.department,
                                      salary_tbl.salary,
                                      salary_tbl.leader_nm from  salary_tbl  versions between timestamp minvalue and maxvalue
                                      

Where version_xid represents the thing Id, version_operation can see the type of operation

Three, 10g flashback transaction query method and recovery data

    alter database add supplemental log data; ----Enable minimum additional log

select * from flashback_transaction_query where TABLE_NAME='SALARY_TBL' order by start_timestamp desc;

The role of this view is as follows

comment on table SYS.FLASHBACK_TRANSACTION_QUERY is 'Description of the flashback transaction query view';
comment on column SYS.FLASHBACK_TRANSACTION_QUERY.XID is 'Transaction identifier';
comment on column SYS.FLASHBACK_TRANSACTION_QUERY.START_SCN is 'Transaction start SCN';
comment on column SYS.FLASHBACK_TRANSACTION_QUERY.START_TIMESTAMP is 'Transaction start timestamp';
comment on column SYS.FLASHBACK_TRANSACTION_QUERY.COMMIT_SCN is 'Transaction commit SCN';
comment on column SYS.FLASHBACK_TRANSACTION_QUERY.COMMIT_TIMESTAMP is 'Transaction commit timestamp';
comment on column SYS.FLASHBACK_TRANSACTION_QUERY.LOGON_USER is 'Logon user for transaction';
comment on column SYS.FLASHBACK_TRANSACTION_QUERY.UNDO_CHANGE# is '1-based undo change number';
comment on column SYS.FLASHBACK_TRANSACTION_QUERY.OPERATION is 'forward operation for this undo';
comment on column SYS.FLASHBACK_TRANSACTION_QUERY.TABLE_NAME is 'table name to which this undo applies';
comment on column SYS.FLASHBACK_TRANSACTION_QUERY.TABLE_OWNER is 'owner of table to which this undo applies';
comment on column SYS.FLASHBACK_TRANSACTION_QUERY.ROW_ID is 'rowid to which this undo applies';
comment on column SYS.FLASHBACK_TRANSACTION_QUERY.UNDO_SQL is 'SQL corresponding to this undo';

Special attention: UNDO_SQL is the SQL for transaction rollback; as long as you sort the order according to COMMIT_TIMESTAMP, you can know the history of submitted transactions.

Want to restore the data of any table: First filter by TABLE_NAME, sort by COMMIT_TIMESTAMP, and execute UNDO_SQL one by one on which position you want to restore

 

Four, 10g flashback meter

 10g can roll back the data in the table to a certain moment, or SCN. The premise is that row migration must be enabled

    alter  table  salary_tbl enable row movement;

Then you can perform a table flashback.

flashback table table_name  to timestamp|SCN


flashback table salary_tbl to timestamp timestamp'2020-7-23 22:00:00'

 

Five, 10g flashback delete

Oracle also has something like the recycle bin, which is used to store data that has been deleted. Enable the recycle bin, the recycle bin only puts the deleted tables of ordinary users, so the SYS user, SYSTEM tablespace will not be placed here;

alter session set recylcebin=on

>>show recyclebin 

flashback table <tablename> to before drop;    --闪回删除

drop table <tablename> purge; -------- Completely delete

purge recyclebin ;---------Empty the recycle bin

-----查询原来的表名和回收站情况
select * from tab where TABTYPE='TABLE';
select * from recyclebin;
--------删除一张表------------
create table salary_tbl_bak as select * from salary_tbl;
drop table  salary_tbl_bak;
------闪回----------
flashback table salary_tbl_bak to before drop;

In Oracle, if the same table is repeatedly deleted, then the flashback table is the most recently deleted table or you can directly select <"table name in recyclebin"> double quotes

 

Six, 10g flashback database

If there is a physical error in the database, such as a damaged hard disk, then Flashback Database is not applicable here. If there is a logical error in the database, you can use Flashback Database to recover and roll back. The previous flashbacks used the rollback segment flashback, while the flashback database used the log to return.

1. The database needs to be in archive mode

  >>shutdown immediate;

  >>startup mount

 >>alter database archivelog

 >> alter database open

2. Configure the flash recovery area

   show parameter DB_RECOVE

3. Configure flashback retention time (unit: minutes)

show parameter db_flashback_reten

4. In the mount state, enable the database flashback alter database flashback on;

At this time, you can see the files needed for flashback in the directory under the db_recovery_file_dest parameter value;

5. Flash back to the database in the mount state

 flashback database  to timestamp | scn

There is a table, the time at this time is select systimestamp from dual; ---- 26-July-20 03.14.24.153000 PM+08:00

Execute drop table salary_tbl;

Guess you like

Origin blog.csdn.net/superSmart_Dong/article/details/107549595