Oracle Flashback Recyclebin 是 Oracle 数据库中的一个重要功能,它允许用户恢复被删除的对象(如表、索引等)到一个较早的状态。这个功能在 Oracle 10g 及以上版本中引入,主要目的是为了减少由于误操作导致的数据丢失问题。以下是对 Oracle Flashback Recyclebin 的详细解释:
一、功能概述
- 回收站(Recycle Bin):当执行 DROP TABLE、DROP INDEX 等删除操作时,如果不带 PURGE 选项,被删除的对象不会被永久删除,而是被移动到回收站中。回收站是一个逻辑上的区域,不是物理上从表空间中划分出来的。
- Flashback Drop:通过 Flashback Drop,用户可以从回收站中恢复被删除的对象。这类似于撤销(Undo)操作,但针对的是整个对象而非单个记录。
二、使用场景
- 误删除恢复:当用户不小心删除了重要的表或索引时,可以通过 Flashback Recyclebin 快速恢复。
- 空间管理:当表空间不足时,Oracle 会根据先入先出的原则自动清除回收站中的对象,以释放空间。
三、操作步骤
-
查看回收站中的对象:
sqlHR@ORCL> SELECT * FROM RECYCLEBIN; -- OR HR@ORCL> show recyclebin;
这条语句会列出当前用户回收站中的所有对象,包括它们的原始名称、回收站中的名称、对象类型以及删除时间等信息。
-
从回收站中恢复对象:
-
如果要恢复的对象在当前模式中不存在同名对象,可以直接使用:
sqlFLASHBACK TABLE object_name TO BEFORE DROP;
其中
object_name
是要恢复对象的原始名称。 -
如果存在同名对象,可以使用
RENAME TO
子句指定一个新的名称:sqlFLASHBACK TABLE object_name TO BEFORE DROP RENAME TO new_object_name; HR@ORCL> flashback table "BIN$HbOl4zDe8sLgY+9QqMABpw==$0" to before drop rename to t_flash_recyclebin; HR@ORCL> select * from T_FLASH_RECYCLEBIN;
-
闪回后可以使用
RENAME TO
子句指定一个新的约束名称:sqlHR@ORCL> select table_name,constraint_name from user_constraints where table_name='T_FLASH_RECYCLEBIN'; TABLE_NAME CONSTRAINT_NAME ------------------------------ -------------------------------------------------------------- T_FLASH_RECYCLEBIN BIN$HbOl4zDU8sLgY+9QqMABpw==$0 T_FLASH_RECYCLEBIN BIN$HbOl4zDV8sLgY+9QqMABpw==$0 T_FLASH_RECYCLEBIN BIN$HbOl4zDW8sLgY+9QqMABpw==$0 T_FLASH_RECYCLEBIN BIN$HbOl4zDX8sLgY+9QqMABpw==$0 T_FLASH_RECYCLEBIN BIN$HbOl4zDY8sLgY+9QqMABpw==$0 T_FLASH_RECYCLEBIN BIN$HbOl4zDZ8sLgY+9QqMABpw==$0 6 rows selected.
sqlHR@ORCL> alter table T_FLASH_RECYCLEBIN rename constraint "BIN$HbOl4zDU8sLgY+9QqMABpw==$0" to T_FLASH_RECYCLEBIN_EMPLOYEE_ID_NOTNULL; Table altered. HR@ORCL> alter table T_FLASH_RECYCLEBIN rename constraint "BIN$HbOl4zDV8sLgY+9QqMABpw==$0" to T_FLASH_RECYCLEBIN_START_DATE_NOTNULL; Table altered. HR@ORCL> alter table T_FLASH_RECYCLEBIN rename constraint "BIN$HbOl4zDW8sLgY+9QqMABpw==$0" to T_FLASH_RECYCLEBIN_END_DATE_NOTNULL; Table altered. HR@ORCL> alter table T_FLASH_RECYCLEBIN rename constraint "BIN$HbOl4zDX8sLgY+9QqMABpw==$0" to T_FLASH_RECYCLEBIN_JOB_ID_NOTNULL; Table altered. HR@ORCL> alter table T_FLASH_RECYCLEBIN rename constraint "BIN$HbOl4zDY8sLgY+9QqMABpw==$0" to T_FLASH_RECYCLEBIN_DATE_INTERVAL; Table altered. HR@ORCL> select CONSTRAINT_NAME,constraint_type as c_type,SEARCH_CONDITION from user_constraints where table_name='T_FLASH_RECYCLEBIN'; CONSTRAINT_NAME C SEARCH_CONDITION ---------------------------------------- - -------------------------------------------------------------------------------- T_FLASH_RECYCLEBIN_EMPLOYEE_ID_NOTNULL C "EMPLOYEE_ID" IS NOT NULL T_FLASH_RECYCLEBIN_START_DATE_NOTNULL C "START_DATE" IS NOT NULL T_FLASH_RECYCLEBIN_END_DATE_NOTNULL C "END_DATE" IS NOT NULL T_FLASH_RECYCLEBIN_JOB_ID_NOTNULL C "JOB_ID" IS NOT NULL T_FLASH_RECYCLEBIN_DATE_INTERVAL C end_date > start_date BIN$HbOl4zDZ8sLgY+9QqMABpw==$0 P 6 rows selected. HR@ORCL> alter table T_FLASH_RECYCLEBIN rename constraint "BIN$HbOl4zDZ8sLgY+9QqMABpw==$0" to PK_T_FLASH_RECYCLEBIN_EMP_ID_ST_DATE; Table altered. HR@ORCL> select CONSTRAINT_NAME,constraint_type as c_type,SEARCH_CONDITION from user_constraints where table_name='T_FLASH_RECYCLEBIN'; CONSTRAINT_NAME C SEARCH_CONDITION ---------------------------------------- - -------------------------------------------------------------------------------- T_FLASH_RECYCLEBIN_EMPLOYEE_ID_NOTNULL C "EMPLOYEE_ID" IS NOT NULL T_FLASH_RECYCLEBIN_START_DATE_NOTNULL C "START_DATE" IS NOT NULL T_FLASH_RECYCLEBIN_END_DATE_NOTNULL C "END_DATE" IS NOT NULL T_FLASH_RECYCLEBIN_JOB_ID_NOTNULL C "JOB_ID" IS NOT NULL T_FLASH_RECYCLEBIN_DATE_INTERVAL C end_date > start_date PK_T_FLASH_RECYCLEBIN_EMP_ID_ST_DATE P 6 rows selected.
-
闪回后可以使用
RENAME TO
子句指定一个新的索引名称:sqlFLASHBACK TABLE object_name TO BEFORE DROP RENAME TO new_object_name;
查询索引名称及索引字段信息并rename索引名称
sqlHR@ORCL> select table_name,index_name from user_indexes where table_name='T_FLASH_RECYCLEBIN'; TABLE_NAME INDEX_NAME ------------------------------ -------------------------------------------------- T_FLASH_RECYCLEBIN BIN$HbOl4zDd8sLgY+9QqMABpw==$0 T_FLASH_RECYCLEBIN BIN$HbOl4zDc8sLgY+9QqMABpw==$0 T_FLASH_RECYCLEBIN BIN$HbOl4zDb8sLgY+9QqMABpw==$0 T_FLASH_RECYCLEBIN BIN$HbOl4zDa8sLgY+9QqMABpw==$0 HR@ORCL> col column_name format a30 HR@ORCL> select table_name,index_name,column_name from user_ind_columns where table_name='T_FLASH_RECYCLEBIN'; TABLE_NAME INDEX_NAME COLUMN_NAME ------------------------------ -------------------------------------------------- ------------------------------ T_FLASH_RECYCLEBIN BIN$HbOl4zDd8sLgY+9QqMABpw==$0 EMPLOYEE_ID T_FLASH_RECYCLEBIN BIN$HbOl4zDd8sLgY+9QqMABpw==$0 START_DATE T_FLASH_RECYCLEBIN BIN$HbOl4zDc8sLgY+9QqMABpw==$0 JOB_ID T_FLASH_RECYCLEBIN BIN$HbOl4zDb8sLgY+9QqMABpw==$0 EMPLOYEE_ID T_FLASH_RECYCLEBIN BIN$HbOl4zDa8sLgY+9QqMABpw==$0 DEPARTMENT_ID HR@ORCL> alter index "BIN$HbOl4zDd8sLgY+9QqMABpw==$0" rename to INDX_T_FLASH_RECYCLEBIN_EMPID_STARTDATE; Index altered. HR@ORCL> select table_name,index_name,column_name from user_ind_columns where table_name='T_FLASH_RECYCLEBIN'; TABLE_NAME INDEX_NAME COLUMN_NAME ------------------------------ -------------------------------------------------- ------------------------------ T_FLASH_RECYCLEBIN INDX_T_FLASH_RECYCLEBIN_EMPID_STARTDATE EMPLOYEE_ID T_FLASH_RECYCLEBIN INDX_T_FLASH_RECYCLEBIN_EMPID_STARTDATE START_DATE T_FLASH_RECYCLEBIN BIN$HbOl4zDc8sLgY+9QqMABpw==$0 JOB_ID T_FLASH_RECYCLEBIN BIN$HbOl4zDb8sLgY+9QqMABpw==$0 EMPLOYEE_ID T_FLASH_RECYCLEBIN BIN$HbOl4zDa8sLgY+9QqMABpw==$0 DEPARTMENT_ID -- 在Oracle数据库中,要查看表的主键字段,你可以通过查询数据字典视图来实现 HR@ORCL> select uc.table_name,cc.constraint_name,cc.column_name,uc.constraint_type from user_cons_columns cc inner join user_constraints uc ON cc.constraint_name = uc.constraint_name WHERE uc.constraint_type = 'P' AND uc.table_name = 'T_FLASH_RECYCLEBIN'; TABLE_NAME CONSTRAINT_NAME COLUMN_NAME C ------------------------------ ---------------------------------------- ------------------------------ - T_FLASH_RECYCLEBIN PK_T_FLASH_RECYCLEBIN_EMP_ID_ST_DATE EMPLOYEE_ID P T_FLASH_RECYCLEBIN PK_T_FLASH_RECYCLEBIN_EMP_ID_ST_DATE START_DATE P HR@ORCL> alter index "BIN$HbOl4zDc8sLgY+9QqMABpw==$0" rename to INDX_T_FLASH_RECYCLEBIN_JOB_ID; Index altered. HR@ORCL> alter index "BIN$HbOl4zDb8sLgY+9QqMABpw==$0" rename to INDX_T_FLASH_RECYCLEBIN_EMPLOYEE_ID; Index altered. HR@ORCL> alter index "BIN$HbOl4zDa8sLgY+9QqMABpw==$0" rename to INDX_T_FLASH_RECYCLEBIN_DEPARTMENT_ID; Index altered. HR@ORCL> select table_name,index_name,column_name from user_ind_columns where table_name='T_FLASH_RECYCLEBIN'; TABLE_NAME INDEX_NAME COLUMN_NAME ------------------------------ -------------------------------------------------- ------------------------------ T_FLASH_RECYCLEBIN INDX_T_FLASH_RECYCLEBIN_EMPID_STARTDATE EMPLOYEE_ID T_FLASH_RECYCLEBIN INDX_T_FLASH_RECYCLEBIN_EMPID_STARTDATE START_DATE T_FLASH_RECYCLEBIN INDX_T_FLASH_RECYCLEBIN_JOB_ID JOB_ID T_FLASH_RECYCLEBIN INDX_T_FLASH_RECYCLEBIN_EMPLOYEE_ID EMPLOYEE_ID T_FLASH_RECYCLEBIN INDX_T_FLASH_RECYCLEBIN_DEPARTMENT_ID DEPARTMENT_ID
-
-
清空回收站:
-
清空当前用户的回收站:
sqlPURGE RECYCLEBIN;
-
清空指定表空间的回收站中指定用户的对象(需要 DBA 权限):
sqlPURGE TABLESPACE tablespace_name USER user_name;
-
永久删除对象而不放入回收站:
sqlDROP TABLE table_name PURGE;
-
四、注意事项
- 权限:执行 Flashback Recyclebin 相关的操作需要足够的权限,如恢复其他用户的对象可能需要 DBA 权限。
- 时间戳和SCN:虽然 Flashback Recyclebin 主要用于恢复被删除的对象,但 Oracle 还提供了 Flashback Table 到特定时间戳或 SCN 的功能,这可以进一步细化恢复操作。
- 归档日志:Flashback Database(将整个数据库恢复到过去的某个时间点或SCN)依赖于归档日志,而 Flashback Recyclebin 主要依赖回收站中的信息,不需要归档日志。
五、总结
Oracle Flashback Recyclebin 是一个强大的功能,它为用户提供了从误删除中恢复对象的能力。通过合理使用这一功能,可以大大降低数据丢失的风险,提高数据库的安全性和可用性。