【備份恢復】 閃回技術之閃回刪除

不一樣的天空w發表於2016-10-17

閃回刪除
語法:
FLASHBACK TABLE [schema.]<table_name> {BEFORE DROP [RENAME TO
table_new_name]}
回收站中表名的含義:(格式: BIN$globalUID$version
BIN: 表示 RECYCLEBIN
globalUID: 是一個全域性唯一的, 24 個位元組,該標識與原物件名沒有任何關係
version: 指資料庫分配的版本號。

 

閃回刪除利用的是recyclebin回收站,閃回刪除特指是drop操作,drop後的表實際上是存放在recyclebin裡的,還是可以對刪除後的表進行查詢操作(基於回收站);但是注意:當表空間用滿或設定為自動擴充套件了,但對於要擴張表空間首先會侵佔回收站的空間,這就導致回收站內的資料徹底丟失!!!!
回收站採用後進先出原則;假設當t表被drop後,有建了一新的同名t表,則flashback table就必須要重新命名rename回收站的老t表了;
當然也可以指定回收站內的表明(特指在回收站內新生成的名字)進行flashback table操作;


一:測試閃回刪除

使用閃回刪除前提啟動閃回回收站:

SYS@ORA11GR2>show parameter recyclebin

 

NAME                                 TYPE        VALUE

------------------------------------ ----------- ------------------------------

recyclebin                           string      on

(預設狀態下,回收站是啟用的/on的狀態)

1) 準備環境(建立測試表 t,索引 idx_t,觸發器 trg_t

——在xx使用者下建立測試表

XX@ORA11GR2>create table t(x number(2),d date);

 

Table created.

 

XX@ORA11GR2>create unique index idx_t on t(x);

 

Index created.

 

XX@ORA11GR2>create or replace trigger trg_t

  2  before insert on t

  3  for each row

  4  begin

  5  if :new.d is null then

  6  :new.d:=sysdate;

  7  end if;

  8  end;

  9  /

 

Trigger created.

 

2刪除測試表t,驗證已進入回收站

XX@ORA11GR2>drop table t;

 

Table dropped.

 

XX@ORA11GR2>select tname,tabtype from tab;

 

TNAME                          TABTYPE

------------------------------ -------

BIN$Pa549j+PX+LgUwEAAH+ELA==$0 TABLE

SYS_FBA_DDL_COLMAP_89859       TABLE

SYS_FBA_HIST_89859             TABLE

SYS_FBA_TCRV_89859             TABLE

T1                             TABLE

T2                             TABLE

 

6 rows selected.

 

——檢視回收站內的表: 

XX@ORA11GR2>show recyclebin;

ORIGINAL NAME    RECYCLEBIN NAME                OBJECT TYPE  DROP TIME

---------------- ------------------------------ ------------ -------------------

T                BIN$Pa549j+PX+LgUwEAAH+ELA==$0 TABLE        2016-09-30:07:02:31

 

——只看到了表,沒有看到表關聯的索引、觸發器,再利用user_recyclebin查詢:

XX@ORA11GR2>desc user_recyclebin

XX@ORA11GR2>select ORIGINAL_NAME,OBJECT_NAME,TYPE,DROPTIME from user_recyclebin;

 

ORIGINAL_N OBJECT_NAME                    TYPE     DROPTIME

---------- ------------------------------ -------- -------------------

IDX_T      BIN$Pa549j+NX+LgUwEAAH+ELA==$0 INDEX    2016-09-30:07:02:31

TRG_T      BIN$Pa549j+OX+LgUwEAAH+ELA==$0 TRIGGER  2016-09-30:07:02:31

T          BIN$Pa549j+PX+LgUwEAAH+ELA==$0 TABLE    2016-09-30:07:02:31

 注 有一定許可權的話,也可以檢視 dba_recyclebin 這個檢視)

 

3) 對刪除的表進行閃回,閃回成功後,回收站已經沒有測試表的記錄

XX@ORA11GR2>flashback table t to before drop;

 

Flashback complete.

 

驗證:

XX@ORA11GR2>select ORIGINAL_NAME,OBJECT_NAME,TYPE,DROPTIME from user_recyclebin;

 

no rows selected

 

4) 檢視閃回的表相關物件的狀態
透過以下查詢,發現以下兩個問題: 1:觸發器的狀態為失效的; 2:物件名稱,除表名以外,其他的都已不是原來的名稱。

XX@ORA11GR2>col OBJECT_NAME for a30

XX@ORA11GR2>select object_name,object_type,status from user_objects;

 

OBJECT_NAME                    OBJECT_TYPE         STATUS

------------------------------ ------------------- -------

BIN$Pa549j+NX+LgUwEAAH+ELA==$0 INDEX               VALID

BIN$Pa549j+OX+LgUwEAAH+ELA==$0 TRIGGER             INVALID

T                                              TABLE               VALID

SYS_FBA_TCRV_IDX_89859         INDEX               VALID

SYS_FBA_TCRV_89859             TABLE               VALID

SYS_FBA_HIST_89859             TABLE               VALID

SYS_FBA_HIST_89859             TABLE PARTITION     VALID

SYS_FBA_DDL_COLMAP_89859       TABLE               VALID

T2                             TABLE               VALID

T1                             TABLE               VALID

 

10 rows selected.

 

5) 重新編譯觸發器,並重新命名觸發器

——先重新編譯:

XX@ORA11GR2>alter trigger "BIN$Pa549j+OX+LgUwEAAH+ELA==$0" compile;

 

Trigger altered.

 

——再重新命名:

XX@ORA11GR2>alter trigger "BIN$Pa549j+OX+LgUwEAAH+ELA==$0" rename to trg_t;

 

Trigger altered.

 

驗證:

XX@ORA11GR2>select object_name,object_type,status from user_objects;

 

OBJECT_NAME                    OBJECT_TYPE         STATUS

------------------------------ ------------------- -------

TRG_T                          TRIGGER             VALID

BIN$Pa549j+NX+LgUwEAAH+ELA==$0 INDEX               VALID

T                              TABLE               VALID

SYS_FBA_TCRV_IDX_89859         INDEX               VALID

SYS_FBA_TCRV_89859             TABLE               VALID

SYS_FBA_HIST_89859             TABLE               VALID

SYS_FBA_HIST_89859             TABLE PARTITION     VALID

SYS_FBA_DDL_COLMAP_89859       TABLE               VALID

T2                             TABLE               VALID

T1                             TABLE               VALID

 

10 rows selected.

 

6)再次刪t除表:(這是實驗刪除表進回收站,而後使用者xx又建了一個同名的t表的閃回表的操作)

XX@ORA11GR2>drop table t;

 

Table dropped.

 

XX@ORA11GR2>select original_name,object_name,type from user_recyclebin;

 

ORIGINAL_NAME                    OBJECT_NAME                    TYPE

-------------------------------- ------------------------------ --------

BIN$Pa549j+NX+LgUwEAAH+ELA==$0   BIN$Pa549j+QX+LgUwEAAH+ELA==$1 INDEX

TRG_T                            BIN$Pa549j+RX+LgUwEAAH+ELA==$0 TRIGGER

T                                BIN$Pa549j+SX+LgUwEAAH+ELA==$0 TABLE

 

——建立一張新的 t 表(包含兩個欄位,目的是將 t 的物件名稱佔用上)

XX@ORA11GR2>create table t(x int,y int);

 

Table created.

 

XX@ORA11GR2>

 

——閃回 t 表(包含一個欄位的那個表,即回收站中的物件)由於物件 t 已經存在,直接閃回會報錯,閃回刪除提供 rename 功能,即閃回後重新命名

XX@ORA11GR2>flashback table t to before  drop;

flashback table t to before  drop

*

ERROR at line 1:

ORA-38312: original name is used by an existing object

 

 --解決(重新命名):

XX@ORA11GR2>flashback table t to before drop rename to t_old;

 

Flashback complete.

 

XX@ORA11GR2>select * from tab where tname like'T%';

 

TNAME                          TABTYPE  CLUSTERID

------------------------------ ------- ----------

T_OLD                          TABLE

T2                             TABLE

T1                             TABLE

T                              TABLE

 

驗證回收站:(無顯示,即為無表在回收站內)

XX@ORA11GR2>show recyclebin;

XX@ORA11GR2>

(此時需要注意重新閃回回來的表t_old的觸發器狀態還是invalid(查user_objects),需要重新編譯後再重新命名)

 

 

二:實驗回收站之 後進先出原則:
如回收站中存在兩個(以上) 相同的源表名,則閃回時總是閃回最近的版本,如果閃回特定
的表,需要指定該表在回收站中的名稱。 測試如下:
1.建立表 hehe,建立三次,刪除三次,第一次一個欄位,第二次兩個欄位,第三次三個欄位

XX@ORA11GR2>conn scott/tiger

Connected.


SCOTT@ORA11GR2>create table hehe(x1 int);

 

Table created.

 

SCOTT@ORA11GR2>drop table hehe;

 

Table dropped.

 

SCOTT@ORA11GR2>create table hehe(x1 int,x2 int);

 

Table created.

 

SCOTT@ORA11GR2>drop table hehe;

 

Table dropped.

 

SCOTT@ORA11GR2>create table hehe(x1 int,x2 int,x3 int);

 

Table created.

 

SCOTT@ORA11GR2>drop table hehe;

 

Table dropped.

 

2.檢視使用者scott的回收站:

SCOTT@ORA11GR2>show recyclebin;

ORIGINAL NAME    RECYCLEBIN NAME                OBJECT TYPE  DROP TIME

---------------- ------------------------------ ------------ -------------------

HEHE             BIN$Pa8DJnKCasvgUwEAAH+7Hg==$0 TABLE        2016-09-30:07:43:02

HEHE             BIN$Pa8DJnKBasvgUwEAAH+7Hg==$0 TABLE        2016-09-30:07:41:40

HEHE             BIN$Pa8DJnKAasvgUwEAAH+7Hg==$0 TABLE        2016-09-30:07:41:10

或者:

SCOTT@ORA11GR2>SELECT original_name, object_name FROM user_recyclebin where original_name='HEHE';

 

3.閃回 hehe 表,我們檢視返回後的 hehe 表,發現為最後刪除的、包含三個欄位的 hehe

SCOTT@ORA11GR2>flashback table hehe to before drop;

 

Flashback complete.

 

SCOTT@ORA11GR2>desc hehe

 Name                                      Null?    Type

 ----------------------------------------- -------- ----------------------------

 X1                                                 NUMBER(38)

 X2                                                 NUMBER(38)

 X3                                                 NUMBER(38)

 

(最後進回收站的,閃回時最先出,即後進先出原則)

 

4.現在回收站中還有兩個‘ hehe’表,要求閃回包含一個欄位的 hehe 表,並且重新命名為 haha

SCOTT@ORA11GR2>show recyclebin

ORIGINAL NAME    RECYCLEBIN NAME                OBJECT TYPE  DROP TIME

---------------- ------------------------------ ------------ -------------------

HEHE             BIN$Pa8DJnKBasvgUwEAAH+7Hg==$0 TABLE        2016-09-30:07:41:40

HEHE             BIN$Pa8DJnKAasvgUwEAAH+7Hg==$0 TABLE        2016-09-30:07:41:10

 

SCOTT@ORA11GR2>desc "BIN$Pa8DJnKAasvgUwEAAH+7Hg==$0"

 Name                                      Null?    Type

 ----------------------------------------- -------- ----------------------------

 X1                                                 NUMBER(38)

 

(要閃回的就是它,對於回收站內的表,一樣可以查詢select * from "BIN$Pa8DJnKAasvgUwEAAH+7Hg==$0";但是不能對回收站的表進行DDL/DML操作)


——進行閃回操作

SCOTT@ORA11GR2>flashback table "BIN$Pa8DJnKAasvgUwEAAH+7Hg==$0" to before drop rename to haha;

 

Flashback complete.

驗證(回收成功):

SCOTT@ORA11GR2>desc haha;

 Name                                      Null?    Type

 ----------------------------------------- -------- ----------------------------

 X1                                                 NUMBER(38)

 

 

三: 清空回收站
1) 整個表空間的回收站內容全部清空:
SCOTT@ORA11GR2>purge tablespace users;

 

Tablespace purged.

 

2) 清空某個表空間上某個使用者回收站中的物件
SCOTT@ORA11GR2>purge tablespace users user scott;

 

Tablespace purged.

 

3) 清空當前使用者下回收站:
SCOTT@ORA11GR2>purge recyclebin;

 

Recyclebin purged.

 

4) DBA身份登入時可以清空所有表空間上回收站
SYS@ORA11GR2>purge dba_recyclebin;

 

DBA Recyclebin purged.

 

SYS@ORA11GR2>purge recyclebin;

 

Recyclebin purged.

 

回收站注意事項
ü 閃回刪除不適用於:
- 駐留在 SYSTEM 表空間中的表
- 使用細粒度級審計或虛擬專用資料庫的表
- 駐留在字典管理表空間中的表
- 已清除的表,無論是手動清除的,還是在空間壓力下自動清除的
ü 以下相關物件不受保護:
- 點陣圖聯接索引
- 實體化檢視日誌
- 引用完整性約束條件
- 在刪除表前刪除的索引
1) 使用 system 表空間的表,刪除不進入回收站

SYS@ORA11GR2>conn xx/oracle

Connected.

XX@ORA11GR2>

XX@ORA11GR2>create table ha tablespace system as select 1 as x from dual;

 

Table created.

 

XX@ORA11GR2>select table_name,tablespace_name from user_tables;

 

TABLE_NAME                     TABLESPACE_NAME

------------------------------ ------------------------------

T1                             TS_USERS

T2                             TS_USERS

SYS_FBA_DDL_COLMAP_89859                       TS_USERS

SYS_FBA_TCRV_89859                         TS_USERS

T_OLD                             TS_USERS

T                              TS_USERS

HA                             SYSTEM

SYS_FBA_HIST_89859

 

8 rows selected.

 

XX@ORA11GR2>drop table ha;

 

Table dropped.

 

XX@ORA11GR2>show recyclebin;

XX@ORA11GR2>

或者:

XX@ORA11GR2>select original_name,object_name,type,droptime from user_recyclebin;

 

no rows selected

 

不進回收站也就意味著無法閃回刪除的使用system表空間的ha表;

 

2) 使用 vpd(Virtual Private Database)的表,刪除不進入回收站

XX@ORA11GR2>create table vpd_tab(id number(5)) tablespace users;

 

Table created.

 

XX@ORA11GR2>insert into vpd_tab values(1);

1 row created.

 

XX@ORA11GR2>insert into vpd_tab values(100);

 

1 row created.

 

XX@ORA11GR2>commit;

 

Commit complete.

 

XX@ORA11GR2>create or replace function fun_vpd_tab_lq(p_schema in varchar2,p_object in varchar2)

  2  return varchar2 is

  3  begin

  4  return 'id<100';

  5  end;

  6  /

 

Function created.

 

XX@ORA11GR2>conn / as sysdba

Connected.

SYS@ORA11GR2>begin

  2  dbms_rls.add_policy (object_schema => 'xx',         

  3  object_name => 'vpd_tab',                           

  4  policy_name => 'p_vpd_tab_lq',                      

  5  function_schema => 'xx',                            

  6  policy_function => 'fun_vpd_tab_lq');               

  7  end;                                                

  8  /                                                    

 

PL/SQL procedure successfully completed.

 

SYS@ORA11GR2>select * from xx.vpd_tab;

 

        ID

----------

         1

       100

 

SYS@ORA11GR2>conn xx/oracle

Connected.

XX@ORA11GR2>select * from vpd_tab;

 

        ID

----------

         1

 

XX@ORA11GR2>select table_name,tablespace_name from user_tables where table_name='VPD_TAB';

 

TABLE_NAME                     TABLESPACE_NAME

------------------------------ ------------------------------

VPD_TAB                        USERS

 

XX@ORA11GR2>drop table vpd_tab;

 

Table dropped.

 

——查詢回收站:

XX@ORA11GR2>show recyclebin;

XX@ORA11GR2>

或者:

XX@ORA11GR2>select original_name,object_name,type,droptime from user_recyclebin;

 
no rows selected

回收站不適用vpd(Virtual Private Database)的表!!!!!!!!!!!!!!!!!!!

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/31397003/viewspace-2126606/,如需轉載,請註明出處,否則將追究法律責任。

相關文章