闪回数据归档实验(Flashback Data Archive)
使用环境
--11g开始有此功能
使用环境
--11g开始有此功能
--12c需要数据库没有插件库或版本为12.1.0.2及以后的版本
实验
--创建表空间用来存放闪回数据归档
SYS@PROD1> create tablespace fda datafile '/u01/app/oracle/oradata/PROD1/fda01.dbf' size 10m autoextend on;
Tablespace created.
--建立闪回数据归档,并指定保留时间
SYS@PROD1> create flashback archive fda tablespace fda quota 10m retention 1 month;
Flashback archive created.
--赋予权限,可以通过sys直接赋予表FDA权限或者给予用户FDA权限
SYS@PROD1> alter table scott.emp flashback archive fda;
Table altered.
SYS@PROD1> grant flashback archive administer to scott;
Grant succeeded.
--通过视图查看是否打开闪回视图归档
SCOTT@PROD1> desc user_flashback_archive_tables
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
TABLE_NAME NOT NULL VARCHAR2(30)
OWNER_NAME NOT NULL VARCHAR2(30)
FLASHBACK_ARCHIVE_NAME NOT NULL VARCHAR2(255)
ARCHIVE_TABLE_NAME VARCHAR2(53)
STATUS VARCHAR2(13)
SCOTT@PROD1> select * from user_flashback_archive_tables where table_name='EMP';
TABLE_NAME OWNER_NAME
------------------------------ ------------------------------
FLASHBACK_ARCHIVE_NAME
----------------------------------------------------------------------------------------------------
ARCHIVE_TABLE_NAME STATUS
----------------------------------------------------- -------------
EMP SCOTT
FDA
SYS_FBA_HIST_75315 ENABLED
--即使截断表也能闪回查询记录
SCOTT@PROD1> select * from emp as of timestamp sysdate-1/1440;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7934 MILLER CLERK 7782 23-JAN-82 1301 10
7902 FORD ANALYST 7566 03-DEC-81 3001 20
7900 JAMES CLERK 7698 03-DEC-81 951 30
7876 ADAMS CLERK 7788 23-MAY-87 1101 20
7844 TURNER SALESMAN 7698 08-SEP-81 1501 0 30
7839 KING PRESIDENT 17-NOV-81 5001 10
7788 SCOTT ANALYST 7566 19-APR-87 3001 20
7782 CLARK MANAGER 7839 09-JUN-81 2451 10
7698 BLAKE MANAGER 7839 01-MAY-81 2851 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1251 1400 30
7566 JONES MANAGER 7839 02-APR-81 2976 20
7521 WARD SALESMAN 7698 22-FEB-81 1251 500 30
7499 ALLEN SALESMAN 7698 20-FEB-81 1601 300 30
7369 SMITH CLERK 7902 17-DEC-80 801 20
14 rows selected.
SCOTT@PROD1> truncate table emp;
Table truncated.
SCOTT@PROD1> select * from emp as of timestamp sysdate-1/1440;
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
---------- ---------- --------- ---------- --------- ---------- ---------- ----------
7934 MILLER CLERK 7782 23-JAN-82 1301 10
7902 FORD ANALYST 7566 03-DEC-81 3001 20
7900 JAMES CLERK 7698 03-DEC-81 951 30
7876 ADAMS CLERK 7788 23-MAY-87 1101 20
7844 TURNER SALESMAN 7698 08-SEP-81 1501 0 30
7839 KING PRESIDENT 17-NOV-81 5001 10
7788 SCOTT ANALYST 7566 19-APR-87 3001 20
7782 CLARK MANAGER 7839 09-JUN-81 2451 10
7698 BLAKE MANAGER 7839 01-MAY-81 2851 30
7654 MARTIN SALESMAN 7698 28-SEP-81 1251 1400 30
7566 JONES MANAGER 7839 02-APR-81 2976 20
7521 WARD SALESMAN 7698 22-FEB-81 1251 500 30
7499 ALLEN SALESMAN 7698 20-FEB-81 1601 300 30
7369 SMITH CLERK 7902 17-DEC-80 801 20
14 rows selected.
--直接插入就可恢复
SCOTT@PROD1> insert into emp select * from emp as of timestamp sysdate-1;
14 rows created.
--一旦表被放到FDA,将不允许直接把表删除,除非关闭FDA
SCOTT@PROD1> drop table emp;
drop table emp
*
ERROR at line 1:
ORA-55610: Invalid DDL statement on history-tracked table
SCOTT@PROD1> alter table emp no flashback archive;
Table altered.
SCOTT@PROD1> drop table emp;
Table dropped.