Wednesday, May 03, 2006

Snippet://Oracle/RecycleBin

Delete With Purge
DROP TABLE table_name PURGE;
DROP INDEX index_name PURGE;
DROP TABLESPACE tablespace_name PURGE;

NOTE: You can’t roll back a PURGE statement

Lookup Recycle Bin
SELECT * FROM USER_RECYCLEBIN;

Recycle Bin Naming Convention
BIN$unique_id$version
unique_id is an unique 26 character across all Oracle
version is a random number set by set by the database

Recover Recycle Bin
FLASHBACK TABLE "BIN$04LhcpnoanfgMAAAAAANPw==$0" TO BEFORE DROP RENAME TO new_table_name;
FLASHBACK TABLE table_name TO BEFORE DROP RENAME TO new_table_name1;
FLASHBACK TABLE table_name TO BEFORE DROP RENAME TO new_table_name2;

which will reinstate the first version of the table to new_table_name1 and the second versions to new_table_name2. The values of the column COL1 in new_table_name1 and new_table_name2 will be 1 and 2 respectively.

Purge Recycle Bin
PURGE RECYCLEBIN; -- Purge Current User Recycle Bin
PURGE USER_RECYCLEBIN; -- Purge Current User Recycle Bin
PURGE DBA_RECYCLEBIN; -- As DBA, purge all

Be Warned...

The un-drop feature brings the table back to its original name, but not the associated objects like indexes and triggers, which are left with the recycled names. Sources such as views and procedures defined on the table are not recompiled and remain in the invalid state. These old names must be retrieved manually and then applied to the flashed-back table.

The information is kept in the view named USER_RECYCLEBIN. Before flashing-back the table, use the following query to retrieve the old names.

SELECT OBJECT_NAME, ORIGINAL_NAME, TYPE
FROM USER_RECYCLEBIN
WHERE BASE_OBJECT = (SELECT BASE_OBJECT FROM USER_RECYCLEBIN
WHERE ORIGINAL_NAME = 'RECYCLETEST')
AND ORIGINAL_NAME != 'RECYCLETEST';

OBJECT_NAME ORIGINAL_N TYPE
------------------------------ ---------- --------
BIN$04LhcpnianfgMAAAAAANPw==$0 IN_RT_01 INDEX
BIN$04LhcpnganfgMAAAAAANPw==$0 TR_RT TRIGGER

After the table is flashed-back, the indexes and triggers on the table RECYCLETEST will be named as shown in the OBJECT_NAME column. From the above query, you can use the original name to rename the objects as follows:

ALTER INDEX "BIN$04LhcpnianfgMAAAAAANPw==$0" RENAME TO IN_RT_01;
ALTER TRIGGER "BIN$04LhcpnganfgMAAAAAANPw==$0" RENAME TO TR_RT;

One notable exception is the bitmap indexes. When they are dropped, they are not placed in the recycle bin--hence they are not retrievable. The constraint names are also not retrievable from the view. They have to be renamed from other sources.

No comments: