Oracle 培训笔记 9.4 闪回

1、准备环境

1.1 还原一个干净的数据库
1.2 创建一个用户

create tablespace tbs
datafile 'E:\oracle\product\10.2.0\oradata\mydb\tbs.dbf' size 100M;

create user u1 identified by u1 default tablespace tbs
quota unlimited on tbs;

grant create session,create table to u1;
grant execute on dbms_flashback to u1;
grant create view,create synonym ,create trigger to u1;
grant select on flashback_transaction_query to u1;

2、闪回
用u1用户登录

2.1 闪回查询(借助undo)
create table t01(id number);
insert into t01 values(100);
commit;

select dbms_flashback.get_system_change_number from dual;
199722

insert into t01 values(200);
commit;

select * from t01;

select * from t01 as of scn 199722;
select * from t01 as of timestamp to_timestamp('2011-09-04 09:41:20','yyyy-mm-dd hh24:mi:ss');

2.2 闪回数据(借助undo)
delete from t01;
commit;

select * from t01;

alter table t01 enable row movement;
flashback table t01 to scn 199722;

select * from t01;

truncate table t01;


2.3 闪回删除
insert into t01 values(100);
commit;

drop table t01;
select * from recyclebin;

flashback table t01 to before drop;
select * from t01;

drop table t01 purge;


练习1: 删除多个表
create table t01(id number);
insert into t01 values(100);
commit;
drop table t01;

create table t01(d date);
insert into t01 values(sysdate);
commit;
drop table t01;

select * from recyclebin;

--清除表,清除的最早的
purge table t01;

--闪回表,闪回最近的
flashback table t01 to before drop;
select * from t01;

flashback table t01 to before drop rename to t02;
select * from t02;


练习2:删除再闪回表,相关对象状态
drop table t01 purge;
drop table t02 purge;

--删除表,观察约束和相关对象状态(同义词和视图没有删除)
create table dept(deptid number primary key,deptname varchar2(20));
create table emp(empid number primary key,
empname varchar2(20),deptid number references dept(deptid));

create view deptv as select * from dept;
create synonym depts for dept;

create trigger deptt after update on dept for each row
begin
null;
end;

select * from user_objects;
select * from user_constraints;

drop table emp;
drop table dept;

select * from user_objects;
select * from user_constraints;

purge recyclebin;
select * from user_objects;
select * from user_constraints;


--删除表再闪回,观察约束和相关对象状态
create table dept(deptid number primary key,deptname varchar2(20));
create table emp(empid number primary key,
empname varchar2(20),deptid number references dept(deptid));

create view deptv as select * from dept;
create synonym depts for dept;

create trigger deptt after update on dept for each row
begin
null;
end;

select * from user_objects;
select * from user_constraints;

drop table emp;
drop table dept;

flashback table emp to before drop;
flashback table dept to before drop;

select * from user_objects;
select * from user_constraints;

alter trigger "BIN$pVw3vW2mR3qCagTrP+luRg==$0" rename to deptt;
select * from user_indexes;
alter index "BIN$Wfr41wYxRHG3sKlZROUgMQ==$0" rename to emp_idx;
alter index "BIN$TZPIRHm9QxOJQ9y7xrz8nA==$0" rename to dept_idx;

--删除表再闪回,观察约束
create table t01(id number unique,name varchar2(20) not null,
gender number check(gender in (1,2)));

select * from user_constraints where table_name='T01';

drop table t01;
flashback table t01 to before drop;


--创建表
drop table t01 purge;
create table t01(idd number primary key ,id number unique,name varchar2(20) not null,
gender number check(gender in (1,2)));

create table t02 as select * from t01;
select * from user_constraints where table_name='T02';
select * from user_constraints where table_name='T01';


2.4 闪回版本查询
select * from user_objects;
drop table emp purge;
drop table dept purge;
drop table t01 purge;
drop table t02 purge;
drop synonym depts;
drop view deptv;

create table t01( id number);

insert into t01 values(100);
commit;

insert into t01 values(200);
insert into t01 values(300);
commit;

insert into t01 values(400);
commit;

select versions_startscn,versions_endscn,
versions_starttime,versions_endtime,
versions_xid,id
from t01 versions between scn minvalue and maxvalue;

2.5 闪回事务
0900020081000000
用sys用户查询
select * from flashback_transaction_query where xid='0900020081000000';
delete from "U1"."T01" where ROWID = 'AAAC4IAAFAAAAAMAAC';
delete from "U1"."T01" where ROWID = 'AAAC4IAAFAAAAAMAAB';
commit;

select * from u1.t01;

2.6 闪回数据库
以sysdba权限的用户操作


1)开启数据库的闪回特性
SQL> shutdown immediate
SQL> startup mount

查询数据库是否闪回
SQL> select flashback_on from v$database;

FLASHBACK_ON
------------------
NO

置成归档模式
SQL> alter database archivelog;
启动闪回特性
SQL> alter database flashback on;

设置闪回数据保留时间
SQL> show parameter target
NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
db_flashback_retention_target        integer     1440

alter system set db_flashback_retention_target=720;

打开数据库
SQL> alter database open;

2、查看视图
select * from v$flashback_database_stat;(统计信息)
select * from v$flashback_database_log;

select * from v$flash_recovery_area_usage;
select * from v$recovery_file_dest;

3、模拟数据变化
drop table t01 purge;
create table t01(id number) tablespace users;
insert into t01 values(100);
commit;
select dbms_flashback.get_system_change_number from dual; //267568

insert into t01 values(200);
commit;
select dbms_flashback.get_system_change_number from dual; //267576

insert into t01 values(300);
commit;
select dbms_flashback.get_system_change_number from dual; //267592

insert into t01 values(400);
commit;
select dbms_flashback.get_system_change_number from dual; //267601

4、闪回
第1次闪回
SQL> shutdown immediate
SQL> startup mount
SQL> flashback database to scn 267576;
SQL> alter database open resetlogs;
SQL> select * from t01;

        ID
----------
       100
       200

第2次闪回(比第1次靠后)    
SQL> shutdown immediate
SQL> startup mount
SQL> flashback database to scn 267592;
SQL> alter database open resetlogs;
SQL> select * from t01;

        ID
----------
       100
       200

第3次闪回(比第1次靠前)           
SQL> shutdown immediate
SQL> startup mount
SQL> flashback database to scn 267568;
SQL> alter database open resetlogs;
SQL> select * from t01;

        ID
----------
       100

flashback database to timestamp xxxx;
create restore point a;
select * from v$restore_point;
flashback database to restore point a;


你可能感兴趣的:(Oracle 培训笔记 9.4 闪回)