search
HomeDatabaseMysql TutorialFlashbackTable闪回表

Oracle10g里面有个Recyclebin,删除时没加purge参数并非真正删除,而是先通过修改数据字典的方式,并将其改名放入Recycle Bin中。 从Recycle Bin 中恢复: 1.创建测试数据 SQL conn scott/oracle Connected. SQL create table temp(id int primary key); Tab

Oracle10g里面有个Recyclebin,删除时没加purge参数并非真正删除,而是先通过修改数据字典的方式,并将其改名放入Recycle Bin中。

从Recycle Bin 中恢复:

1.创建测试数据

SQL> conn scott/oracle
Connected.

SQL> create table temp(id int primary key);

Table created.

SQL> insert into temp values(1);

1 row created.

SQL> insert into temp values(2);

1 row created.

SQL> insert into temp values(3);

1 row created.
SQL> drop table temp;

Table dropped.

2.查看Recycle Bin

SQL> desc recyclebin;
Name Null? Type
----------------------------------------- -------- ----------------------------
OBJECT_NAME NOT NULL VARCHAR2(30)
ORIGINAL_NAME VARCHAR2(32)
OPERATION VARCHAR2(9)
TYPE VARCHAR2(25)
TS_NAME VARCHAR2(30)
CREATETIME VARCHAR2(19)
DROPTIME VARCHAR2(19)
DROPSCN NUMBER
PARTITION_NAME VARCHAR2(32)
CAN_UNDROP VARCHAR2(3)
CAN_PURGE VARCHAR2(3)
RELATED NOT NULL NUMBER
BASE_OBJECT NOT NULL NUMBER
PURGE_OBJECT NOT NULL NUMBER
SPACE NUMBER

SQL> select ORIGINAL_NAME,OBJECT_NAME from recyclebin;

ORIGINAL_NAME OBJECT_NAME
-------------------------------- ------------------------------
TEMP BIN$9fpGUw34tFvgQAB/AQAb+w==$0 //TEMP表
SYS_C006287 BIN$9fpGUw33tFvgQAB/AQAb+w==$0 //主键
SQL> select * from tab;

TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
EXPFULL TABLE
FLASH_TBL TABLE
TEST TABLE
BIN$9fpGUw34tFvgQAB/AQAb+w==$0 TABLE
3.恢复表
SQL> flashback table temp to before drop;

Flashback complete.

SQL> select * from tab;

TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
EXPFULL TABLE
TEMP TABLE
FLASH_TBL TABLE
TEST TABLE

SQL> select * from temp;

ID
----------
1
2
3


SQL> desc user_indexes;
Name Null? Type
----------------------------------------- -------- ----------------------------
INDEX_NAME NOT NULL VARCHAR2(30)
INDEX_TYPE VARCHAR2(27)
TABLE_OWNER NOT NULL VARCHAR2(30)
TABLE_NAME NOT NULL VARCHAR2(30)
TABLE_TYPE VARCHAR2(11)
UNIQUENESS VARCHAR2(9)
COMPRESSION VARCHAR2(8)
PREFIX_LENGTH NUMBER
TABLESPACE_NAME VARCHAR2(30)
INI_TRANS NUMBER
MAX_TRANS NUMBER
INITIAL_EXTENT NUMBER
NEXT_EXTENT NUMBER
MIN_EXTENTS NUMBER
MAX_EXTENTS NUMBER
PCT_INCREASE NUMBER
PCT_THRESHOLD NUMBER
INCLUDE_COLUMN NUMBER
FREELISTS NUMBER
FREELIST_GROUPS NUMBER
PCT_FREE NUMBER
LOGGING VARCHAR2(3)
BLEVEL NUMBER
LEAF_BLOCKS NUMBER
DISTINCT_KEYS NUMBER
AVG_LEAF_BLOCKS_PER_KEY NUMBER
AVG_DATA_BLOCKS_PER_KEY NUMBER
CLUSTERING_FACTOR NUMBER
STATUS VARCHAR2(8)
NUM_ROWS NUMBER
SAMPLE_SIZE NUMBER
LAST_ANALYZED DATE
DEGREE VARCHAR2(40)
INSTANCES VARCHAR2(40)
PARTITIONED VARCHAR2(3)
TEMPORARY VARCHAR2(1)
GENERATED VARCHAR2(1)
SECONDARY VARCHAR2(1)
BUFFER_POOL VARCHAR2(7)
USER_STATS VARCHAR2(3)
DURATION VARCHAR2(15)
PCT_DIRECT_ACCESS NUMBER
ITYP_OWNER VARCHAR2(30)
ITYP_NAME VARCHAR2(30)
PARAMETERS VARCHAR2(1000)
GLOBAL_STATS VARCHAR2(3)
DOMIDX_STATUS VARCHAR2(12)
DOMIDX_OPSTATUS VARCHAR2(6)
FUNCIDX_STATUS VARCHAR2(8)
JOIN_INDEX VARCHAR2(3)
IOT_REDUNDANT_PKEY_ELIM VARCHAR2(3)
DROPPED VARCHAR2(3)
SQL> select table_name,index_name from user_indexes where table_name='TEMP';

TABLE_NAME INDEX_NAME
------------------------------ ------------------------------
TEMP BIN$9fpGUw33tFvgQAB/AQAb+w==$0

4.手动修改索引
SQL> alter index "BIN$9fpGUw33tFvgQAB/AQAb+w==$0" rename to SYS_C006287;

Index altered.
复杂点的表恢复:
SQL> select * from tab;

TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
EXPFULL TABLE
TEMP TABLE
FLASH_TBL TABLE
TEST TABLE

8 rows selected.

SQL> drop table temp;

Table dropped.

SQL> create table temp as select * from flash_tbl;

Table created.

SQL> flashback table temp to before drop;
flashback table temp to before drop
*
ERROR at line 1:
ORA-38312: original name is used by an existing object
SQL> flashback table temp to before drop rename to old_temp; 命名冲突,重命名……

Flashback complete.

SQL> select * from tab;

TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
EXPFULL TABLE
OLD_TEMP TABLE
FLASH_TBL TABLE
TEST TABLE
TEMP TABLE

9 rows selected.


SQL> select * from old_temp;

ID
----------
1
2
3
从多次删除中恢复:

SQL> create table temp_old as select * from temp;

Table created.

SQL> drop table temp;

Table dropped.

SQL> alter table temp_old rename to temp;

Table altered.

SQL> drop table temp;

Table dropped.

SQL> create table temp (id int);

Table created.

SQL> drop table temp;

Table dropped.
SQL> select droptime,original_name,object_name from recyclebin;

DROPTIME ORIGINAL_NAME
------------------- --------------------------------
OBJECT_NAME
------------------------------
2014-04-01:04:48:58 TEMP
BIN$9fm3y2lF6oHgQAB/AQAeMQ==$0

2014-04-01:04:49:26 TEMP
BIN$9fm3y2lG6oHgQAB/AQAeMQ==$0

2014-04-01:04:46:26 TEMP
BIN$9fm3y2lE6oHgQAB/AQAeMQ==$0
SQL> flashback table temp to before drop;

Flashback complete.

SQL> select droptime,original_name,object_name from recyclebin;

DROPTIME ORIGINAL_NAME
------------------- --------------------------------
OBJECT_NAME
------------------------------
2014-04-01:04:48:58 TEMP
BIN$9fm3y2lF6oHgQAB/AQAeMQ==$0

2014-04-01:04:46:26 TEMP
BIN$9fm3y2lE6oHgQAB/AQAeMQ==$0

从时间可以看出越后删除的越先被恢复,即倒着恢复。

SQL> select droptime,original_name,object_name from recyclebin;

DROPTIME ORIGINAL_NAME
------------------- --------------------------------
OBJECT_NAME
------------------------------
2014-04-01:05:04:31 TEMP3
BIN$9fm3y2lI6oHgQAB/AQAeMQ==$0

2014-04-01:05:04:27 TEMP2
BIN$9fm3y2lH6oHgQAB/AQAeMQ==$0

当然可以直接指定对象名称来恢复:
SQL> flashback table "BIN$9fm3y2lI6oHgQAB/AQAeMQ==$0" to before drop;

Flashback complete.

SQL> select * from tab;

TNAME TABTYPE CLUSTERID
------------------------------ ------- ----------
DEPT TABLE
EMP TABLE
BONUS TABLE
SALGRADE TABLE
EXPFULL TABLE
BIN$9fm3y2lH6oHgQAB/AQAeMQ==$0 TABLE
FLASH_TBL TABLE
TEST TABLE
TEMP3 TABLE
TEMP TABLE

10 rows selected.
从UNDO表空间恢复:
SQL> select * from flash_tbl;

ID V
---------- -
10 I
11 J
12 K
13 L
14 M
15 N
117 P
118 Q
119 R
120 S
6 E

ID V
---------- -
7 F
8 G
9 H
300 r
500 t

16 rows selected.

SQL> select dbms_flashback.get_system_change_number from dual;

GET_SYSTEM_CHANGE_NUMBER
------------------------
1227868

SQL> delete flash_tbl where id=7;

1 row deleted.

SQL> insert into flash_tbl values(25,'r');

1 row created.

SQL> commit;

Commit complete.

SQL> select * from flash_tbl;

ID V
---------- -
10 I
11 J
12 K
13 L
14 M
15 N
117 P
118 Q
119 R
120 S
25 r

ID V
---------- -
6 E
8 G
9 H
300 r
500 t

16 rows selected.

基于SCN的查询:
SQL> select * from flash_tbl as of scn 1227868;

ID V
---------- -
10 I
11 J
12 K
13 L
14 M
15 N
117 P
118 Q
119 R
120 S
6 E

ID V
---------- -
7 F
8 G
9 H
300 r
500 t

16 rows selected.
SQL> flashback table flash_tbl to scn 1227868;
flashback table flash_tbl to scn 1227868
*
ERROR at line 1:
ORA-08189: cannot flashback the table because row movement is not enabled


SQL> oerr ora 08189
SP2-0734: unknown command beginning "oerr ora 0..." - rest of line ignored.
SQL> host oerr ora 08189
08189, 00000, "cannot flashback the table because row movement is not enabled"
// *Cause: An attempt was made to perform Flashback Table operation on a table for
// which row movement has not been enabled. Because the Flashback Table
// does not preserve the rowids, it is necessary that row
// movement be enabled on the table.
// *Action: Enable row movement on the table

未开启行移动
SQL> alter table flash_tbl enable row movement;

Table altered.
SQL> flashback table flash_tbl to scn 1227868;

Flashback complete.

SQL> select * from flash_tbl;

ID V
---------- -
10 I
11 J
12 K
13 L
14 M
15 N
117 P
118 Q
119 R
120 S
6 E

ID V
---------- -
7 F
8 G
9 H
300 r
500 t

16 rows selected.

恢复成功……

SCN:在10g中,系统平均每3秒产生一次系统时间与SCN的匹配并存入SYS.SMON_SCN_TIME表,若使用AS OF TIMESTAMP 查询UNDO中的数据,实际获取的数据是以指定的时间对应的SCN时的数据为基准。

例如:SCN TIME

123 2013-04-01 20:25:00

125 2013-04-01 20:30:00

当通过AS OF TIMESTAMP查询2013-04-01 20:25:00或2013-04-01 20:29:59这段时间内任何时间,Oracle都会将其匹配为SCN:123到UNDO表空间中查找。就是说在这个时间段内,查询返回的数据都是2013-04-01 20:25:00这个时间对应的SCN的数据。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you alter a table in MySQL using the ALTER TABLE statement?How do you alter a table in MySQL using the ALTER TABLE statement?Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

How do I configure SSL/TLS encryption for MySQL connections?How do I configure SSL/TLS encryption for MySQL connections?Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

How do you handle large datasets in MySQL?How do you handle large datasets in MySQL?Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)?Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you drop a table in MySQL using the DROP TABLE statement?How do you drop a table in MySQL using the DROP TABLE statement?Mar 19, 2025 pm 03:52 PM

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

How do you represent relationships using foreign keys?How do you represent relationships using foreign keys?Mar 19, 2025 pm 03:48 PM

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

How do you create indexes on JSON columns?How do you create indexes on JSON columns?Mar 21, 2025 pm 12:13 PM

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)?How do I secure MySQL against common vulnerabilities (SQL injection, brute-force attacks)?Mar 18, 2025 pm 12:00 PM

Article discusses securing MySQL against SQL injection and brute-force attacks using prepared statements, input validation, and strong password policies.(159 characters)

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft