您好,登錄后才能下訂單哦!
本篇內容介紹了“ORACLE回收站的基本操作”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
回收站開啟和關閉分為會話級別和系統級別。會話級別的命令如下:
ALTER SESSION SET recyclebin = ON; ALTER SESSION SET recyclebin = OFF; |
系統級別的命令如下:
ALTER SYSTEM SET recyclebin = ON SCOPE = SPFILE; ALTER SYSTEM SET recyclebin = OFF SCOPE = SPFILE; |
在系統級別開啟和關閉回收站需要重啟數據庫使其生效。
可以通過如下命令查看回收站是否開啟:
SQL> SHOW PARAMETER recyclebin; |
可以通過上面三個同義詞查看回收站內容,其中RECYCLEBIN和USER_RECYCLEBIN相同,都是查看回收站中當前用戶的對象,而DBA_RECYCLEBIN是查看所有回收站的對象,需要DBA角色或相應權限才能訪問該視圖。查看三個同義詞的元數據:
SQL> select dbms_metadata.get_ddl('SYNONYM','RECYCLEBIN','PUBLIC') from dual;
DBMS_METADATA.GET_DDL('SYNONYM','RECYCLEBIN','PUBLIC') --------------------------------------------------------------------------------
CREATE OR REPLACE PUBLIC SYNONYM "RECYCLEBIN" FOR "SYS"." USER_RECYCLEBIN"
SQL> select dbms_metadata.get_ddl('SYNONYM','USER_RECYCLEBIN','PUBLIC') from dual;
DBMS_METADATA.GET_DDL('SYNONYM','USER_RECYCLEBIN','PUBLIC') --------------------------------------------------------------------------------
CREATE OR REPLACE PUBLIC SYNONYM "USER_RECYCLEBIN" FOR "S YS"."USER_RECYCLEBIN
SQL> select dbms_metadata.get_ddl('SYNONYM','DBA_RECYCLEBIN','PUBLIC') from dual;
DBMS_METADATA.GET_DDL('SYNONYM','DBA_RECYCLEBIN','PUBLIC') --------------------------------------------------------------------------------
CREATE OR REPLACE PUBLIC SYNONYM "DBA_RECYCLEBIN" FOR "SY S"."DBA_RECYCLEBIN" |
RECYCLEBIN和USER_RECYCLEBIN是同一個視圖SYS.USER_RECYCLEBIN的同義詞,DBA_RECYCLEBIN是SYS.DBA_RECYCLEBIN的同義詞。
查看視圖SYS.USER_RECYCLEBIN的定義:
CREATE OR REPLACE FORCE VIEW "SYS"."USER_RECYCLEBIN" ("OBJECT_NAME", "ORIGINAL_NAME", "OPERATION", "TYPE", "TS_NAME", "CREATETIME", "DROPTIME", "DROPSCN", "PARTITION_NAME", "CAN_UNDROP", "CAN_PURGE", "RELATED", "BASE_OBJECT", "PURGE_OBJECT", "SPACE") AS select o.name, r.original_name, decode(r.operation, 0, 'DROP', 1, 'TRUNCATE', 'UNDEFINED'), decode(r.type#, 1, 'TABLE', 2, 'INDEX', 3, 'INDEX', 4, 'NESTED TABLE', 5, 'LOB', 6, 'LOB INDEX', 7, 'DOMAIN INDEX', 8, 'IOT TOP INDEX', 9, 'IOT OVERFLOW SEGMENT', 10, 'IOT MAPPING TABLE', 11, 'TRIGGER', 12, 'CONSTRAINT', 13, 'Table Partition', 14, 'Table Composite Partition', 15, 'Index Partition', 16, 'Index Composite Partition', 17, 'LOB Partition', 18, 'LOB Composite Partition', 'UNDEFINED'), t.name, to_char(o.ctime, 'YYYY-MM-DD:HH24:MI:SS'), to_char(r.droptime, 'YYYY-MM-DD:HH24:MI:SS'), r.dropscn, r.partition_name, decode(bitand(r.flags, 4), 0, 'NO', 4, 'YES', 'NO'), decode(bitand(r.flags, 2), 0, 'NO', 2, 'YES', 'NO'), r.related, r.bo, r.purgeobj, r.space from sys."_CURRENT_EDITION_OBJ" o, sys.recyclebin$ r, sys.ts$ t where r.owner# = userenv('SCHEMAID') and o.obj# = r.obj# and r.ts# = t.ts#(+) |
查看視圖SYS.DBA_RECYCLEBIN的定義:
CREATE OR REPLACE FORCE VIEW "SYS"."DBA_RECYCLEBIN" ("OWNER", "OBJECT_NAME", "ORIGINAL_NAME", "OPERATION", "TYPE", "TS_NAME", "CREATETIME", "DROPTIME", "DROPSCN", "PARTITION_NAME", "CAN_UNDROP", "CAN_PURGE", "RELATED", "BASE_OBJECT", "PURGE_OBJECT", "SPACE") AS select u.name, o.name, r.original_name, decode(r.operation, 0, 'DROP', 1, 'TRUNCATE', 'UNDEFINED'), decode(r.type#, 1, 'TABLE', 2, 'INDEX', 3, 'INDEX', 4, 'NESTED TABLE', 5, 'LOB', 6, 'LOB INDEX', 7, 'DOMAIN INDEX', 8, 'IOT TOP INDEX', 9, 'IOT OVERFLOW SEGMENT', 10, 'IOT MAPPING TABLE', 11, 'TRIGGER', 12, 'CONSTRAINT', 13, 'Table Partition', 14, 'Table Composite Partition', 15, 'Index Partition', 16, 'Index Composite Partition', 17, 'LOB Partition', 18, 'LOB Composite Partition', 'UNDEFINED'), t.name, to_char(o.ctime, 'YYYY-MM-DD:HH24:MI:SS'), to_char(r.droptime, 'YYYY-MM-DD:HH24:MI:SS'), r.dropscn, r.partition_name, decode(bitand(r.flags, 4), 0, 'NO', 4, 'YES', 'NO'), decode(bitand(r.flags, 2), 0, 'NO', 2, 'YES', 'NO'), r.related, r.bo, r.purgeobj, r.space from sys."_CURRENT_EDITION_OBJ" o, sys.recyclebin$ r, sys.user$ u, sys.ts$ t where o.obj# = r.obj# and r.owner# = u.user# and r.ts# = t.ts#(+) |
比較上面兩個視圖的定義,可以看到USER_RECYCLEBIN的where條件中有謂詞條件“r.owner# = userenv('SCHEMAID')”,這就解釋了USER_RECYCLEBIN只能查看回收站中關于當前用戶的對象。
根據需要,可以使用USER_RECYCLEBIN(RECYCLEBIN)或DBA_RECYCLEBIN查看回收站中的對象。他們包含的字段除了DBA_RECYCLEBIN多了OWNER字段外其他的都是相同的,常查詢的幾個字段如下:
SELECT owner,object_name,original_name,type,ts_name,droptime FROM dba_recyclebin; |
或
SELECT object_name,original_name,type,ts_name,droptime FROM recyclebin; |
|
除了上述方法外,也可以用“SHOW recyclebin”命令查看當前用戶在回收站中的表:
SQL> show recyclebin; ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME ---------------- ------------------------------ ------------ ------------------- TESTBIN BIN$WEHQbKB6WXbgU990QAqDfQ==$0 TABLE 2017-09-03:11:44:41 |
這里值得說明的是,只有“DROP TABLE”語句才能將對象放到回收站,這里的對象包含了要刪除的表和表相關的其他對象,如索引、觸發器等。用視圖或同義詞查詢回收站的對象,是可以看到表類型以外的對象的,而“SHOW recyclebin”方法只能看到表對象。
首先我們來做一段測試。
測試前,我們先看看回收站中有什么:
SQL> show user USER is "TEST" SQL> SELECT object_name,original_name,type,droptime FROM recyclebin;
OBJECT_NAME ORIGINAL_NAME TYPE DROPTIME ------------------------------ ------------------- ------------- ------------------------- BIN$WEHQbKB6WXbgU990QAqDfQ==$0 TESTBIN TABLE 2017-09-03:11:44:41 |
回收站中有一個原表名為TESTBIN的對象。
創建一個表名為TESTBIN的表,并為其創建一個主鍵索引和一個普通索引
create table testbin ( id number(3), name varchar2(20), constraint pk_testbin primary key (id) using index (create unique index ind_pk_testbin on testbin(id))); create index ind_testbin_name on testbin(name); |
接下來,我們刪除索引ind_testbin_name,看看回收站中是否會增加這個索引的對象:
SQL> drop index ind_testbin_name;
Index dropped.
SQL> SELECT object_name,original_name,type,droptime FROM recyclebin;
OBJECT_NAME ORIGINAL_NAME TYPE DROPTIME ------------------------------ ------------------- ------------- ------------------------- BIN$WEHQbKB6WXbgU990QAqDfQ==$0 TESTBIN TABLE 2017-09-03:11:44:41 |
回收站中并沒有增加刪除的索引信息。重新創建索引已進行后面的測試
SQL> create index ind_testbin_name on testbin(name); |
接下來刪除表TESTBIN,看看與之關聯的對象是否會被放入回收站
SQL> drop table testbin;
Table dropped.
SQL> SELECT object_name,original_name,type,droptime FROM recyclebin;
OBJECT_NAME ORIGINAL_NAME TYPE DROPTIME ------------------------------ -------------------------------- ------------------------- ------------------- BIN$WEHQbKB6WXbgU990QAqDfQ==$0 TESTBIN TABLE 2017-09-03:11:44:41 BIN$WEHQbKB/WXbgU990QAqDfQ==$0 IND_TESTBIN_NAME INDEX 2017-09-03:12:20:34 BIN$WEHQbKCAWXbgU990QAqDfQ==$1 BIN$WEHQbKB8WXbgU990QAqDfQ==$0 INDEX 2017-09-03:12:20:34 BIN$WEHQbKCBWXbgU990QAqDfQ==$0 TESTBIN TABLE 2017-09-03:12:20:34 |
可以看到,回收站中對了三條記錄,分別是刪除的表和表的索引。也同時看到,有兩條記錄對應的original_name字段值為TESTBIN。
清理回收站分為四個級別:表級別、用戶級別、表空間級別、清空級別。
可以單獨清理回收站中某個表的信息,同時該表相關的對象也會被清理掉。命令如下:
SQL> PURGE TABLE testbin; 或 SQL> PURGE TABLE "BIN$WEHQbKCBWXbgU990QAqDfQ==$0"; |
非表的對象是不能單獨清理的:
SQL> PURGE TABLE IND_TESTBIN_NAME; PURGE TABLE IND_TESTBIN_NAME * ERROR at line 1: ORA-38307: object not in RECYCLE BIN |
下面是接著上面的測試一個測試:
SQL> PURGE TABLE testbin;
Table purged.
SQL> SELECT object_name,original_name,type,droptime FROM recyclebin;
OBJECT_NAME ORIGINAL_NAME TYPE DROPTIME ------------------------------ -------------------------------- ------------------------- ------------------- BIN$WEHQbKB/WXbgU990QAqDfQ==$0 IND_TESTBIN_NAME INDEX 2017-09-03:12:20:34 BIN$WEHQbKCAWXbgU990QAqDfQ==$1 BIN$WEHQbKB8WXbgU990QAqDfQ==$0 INDEX 2017-09-03:12:20:34 BIN$WEHQbKCBWXbgU990QAqDfQ==$0 TESTBIN TABLE 2017-09-03:12:20:34 |
測試發現,清理回收站中的信息時,如果對應了多條記錄,會刪除最早的一條記錄。再刪一次:
SQL> PURGE TABLE testbin;
Table purged.
SQL> SELECT object_name,original_name,type,droptime FROM recyclebin;
no rows selected |
表和表相關的索引都被清理了。
用戶級別清理回收站是指只清理回收站中當前用戶的對象。命令如下:
PURGE recyclebin; 或 PURGE user_recyclebin; |
測試如下:
向回收站中注入TEST用戶的記錄
SQL> show user USER is "TEST" SQL> create table testbin (col1 number);
Table created.
SQL> drop table testbin;
Table dropped.
SQL> SELECT object_name,original_name,type,droptime FROM recyclebin;
OBJECT_NAME ORIGINAL_NAME TYPE DROPTIME ------------------------------ -------------------------------- ------------------------- ------------------- BIN$WEHQbKCCWXbgU990QAqDfQ==$0 TESTBIN TABLE 2017-09-03:12:46:32 |
能否向回收站中注入SYS用戶的記錄?
SQL> show user USER is "SYS" SQL> create table sysbin (id number);
Table created.
SQL> drop table sysbin;
Table dropped.
SQL> SELECT object_name,original_name,type,droptime FROM recyclebin;
no rows selected |
測試表名,刪除SYS用戶的表,并不會將表放到回收站。經過測試,還發現SYSTEM用戶的表被刪除時也不會放到回收站。
創建新的用戶TEST2,并向回收站中注入TEST2的記錄
SQL> show user USER is "TEST2" SQL> create table test2bin(id number);
Table created.
SQL> drop table test2bin;
Table dropped.
SQL> SELECT object_name,original_name,type,droptime FROM recyclebin;
OBJECT_NAME ORIGINAL_NAME TYPE DROPTIME ------------------------------ -------------------------------- ------------------------- ------------------- BIN$WELbyCwFaGTgU990QAoh8w==$0 TEST2BIN TABLE 2017-09-03:12:59:27 |
查看DBA_RECYCLEBIN中的記錄
SQL> show user USER is "SYS" SQL> SELECT owner,object_name,original_name,type,droptime FROM dba_recyclebin;
OWNER OBJECT_NAME ORIGINAL_NAME TYPE DROPTIME ------------------------------ ------------------------------ -------------------------------- ------------------------- ------------------- TEST BIN$WEHQbKCCWXbgU990QAqDfQ==$0 TESTBIN TABLE 2017-09-03:12:46:32 TEST2 BIN$WELbyCwFaGTgU990QAoh8w==$0 TEST2BIN TABLE 2017-09-03:12:59:27 |
能查看到TEST和TEST2的記錄。
在TEST2用戶下清理回收站
SQL> show user USER is "TEST2" SQL> PURGE recyclebin;
Recyclebin purged.
SQL> SELECT object_name,original_name,type,droptime FROM recyclebin;
no rows selected |
清理完成后,TEST2下查看不到自己的記錄了。用DBA_RECYCLEBIN可以查看到除TEST2以外的其他用戶的記錄:
SQL> show user USER is "SYS" SQL> SELECT owner,object_name,original_name,type,droptime FROM dba_recyclebin;
OWNER OBJECT_NAME ORIGINAL_NAME TYPE DROPTIME ------------------------------ ------------------------------ -------------------------------- ------------------------- ------------------- TEST BIN$WEHQbKCCWXbgU990QAqDfQ==$0 TESTBIN TABLE 2017-09-03:12:46:32 |
表空間級別刪除回收站中記錄,需要知道表空間,命令如下:
PURGE TABLESPACE test; |
也可以刪除指定表空間下指定用戶的記錄,命令如下:
PURGE TABLESPACE test USER test; |
測試如下:
SQL> SELECT object_name,original_name,type,ts_name,droptime FROM dba_recyclebin;
OBJECT_NAME ORIGINAL_NAME TYPE TS_NAME DROPTIME ------------------------------ -------------------------------- ------------------------- ------------------------------ ------------------- BIN$WEHQbKCCWXbgU990QAqDfQ==$0 TESTBIN TABLE TEST 2017-09-03:12:46:32
SQL> PURGE TABLESPACE test;
Tablespace purged.
SQL> SELECT object_name,original_name,type,ts_name,droptime FROM dba_recyclebin;
no rows selected |
清空回收站
命令如下:
PURGE dba_recyclebin; |
該命令需要在SYSDBA用戶下執行。
從回收站中恢復表用到的命令為flashback table,如下:
flashback table testbin to before drop; |
測試如下:
創建對象并drop
SQL> show recyclebin; SQL> show user USER is "TEST" SQL> create table testbin(id number);
Table created.
SQL> insert into testbin values(1);
1 row created.
SQL> commit;
Commit complete.
SQL> drop table testbin;
Table dropped.
SQL> create table testbin ( id number(3), name varchar2(20), constraint pk_testbin primary key (id) using index (create unique index ind_pk_testbin on testbin(id))); 2 3 4 5
Table created.
SQL> create index ind_testbin_name on testbin(name);
Index created.
SQL> insert into testbin values(123,'test recyclebin');
1 row created.
SQL> commit;
Commit complete.
SQL> drop table testbin;
Table dropped. |
上面創建了兩個相同表名的表,并將它們都放到了回收站中,其中后放入回收站的表上有一個主鍵索引和普通索引。
查看用戶的回收站
SQL> show recyclebin; ORIGINAL NAME RECYCLEBIN NAME OBJECT TYPE DROP TIME ---------------- ------------------------------ ------------ ------------------- TESTBIN BIN$WEO4ydVndITgU990QApxdg==$0 TABLE 2017-09-03:14:03:50 TESTBIN BIN$WEO4ydVjdITgU990QApxdg==$0 TABLE 2017-09-03:14:01:14 SQL> SELECT object_name,original_name,type,droptime FROM recyclebin;
OBJECT_NAME ORIGINAL_NAME TYPE DROPTIME ------------------------------ -------------------------------- ------------------------- ------------------- BIN$WEO4ydVldITgU990QApxdg==$0 IND_TESTBIN_NAME INDEX 2017-09-03:14:03:50 BIN$WEO4ydVmdITgU990QApxdg==$0 IND_PK_TESTBIN INDEX 2017-09-03:14:03:50 BIN$WEO4ydVjdITgU990QApxdg==$0 TESTBIN TABLE 2017-09-03:14:01:14 BIN$WEO4ydVndITgU990QApxdg==$0 TESTBIN TABLE 2017-09-03:14:03:50 |
確認了創建的對象都已進入了回收站。接下來就要測試flashback table了。
第一次flashback table
從回收站中恢復表:
SQL> flashback table testbin to before drop;
Flashback complete. |
恢復之后,查看回收站:
SQL> SELECT object_name,original_name,type,droptime FROM recyclebin;
OBJECT_NAME ORIGINAL_NAME TYPE DROPTIME ------------------------------ -------------------------------- ------------------------- ------------------- BIN$WEO4ydVjdITgU990QApxdg==$0 TESTBIN TABLE 2017-09-03:14:01:14 |
只剩最早刪除的記錄了,最新的表和表的索引都已從回收站恢復,查看表記錄:
SQL> select * from testbin;
ID NAME ---------- -------------------- 123 test recyclebin |
第二次flashback table
如果想要恢復回收站中剩下的表TESTBIN,顯然會出現錯誤,因為相同的表已經存在了。測試如下:
SQL> flashback table testbin to before drop; flashback table testbin to before drop * ERROR at line 1: ORA-38312: original name is used by an existing object |
此時,如果想恢復這個表,需要將它rename。命令如下:
flashback table testbin to before drop rename to testbin_old; |
測試如下:
SQL> flashback table testbin to before drop rename to testbin_old;
Flashback complete.
SQL> select * from testbin_old;
ID ---------- 1 |
值得說明的是,能否flashback table取決于回收站中是否存在該記錄,與是否開啟了回收站無關。
“ORACLE回收站的基本操作”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。