学习数据库时,我们只是以学习的态度,考虑如何使用数据库命令语句,并未想过工作中,如果误操作一下,都可能导致无可挽回的损失。当我在工作中真正遇到这些问题时,我开始寻找答案。
今天主要以oracle数据库为例,介绍关于表中数据删除的解决办法。(不考虑全库备份和利用归档日志)
删除表中数据有三种方法:
·delete(删除一条记录)
·drop或truncate删除表格中数据
1.delete误删除的解决方法
原理:
利用oracle提供的闪回方法,如果在删除数据后还没做大量的操作(只要保证被删除数据的块没被覆写),就可以利用闪回方式直接找回删除的数据
具体步骤为:
*确定删除数据的时间(在删除数据之前的时间就行,不过最好是删除数据的时间点)
*用以下语句找出删除的数据:select * from 表名 as of timestamp to_timestamp('删除时间点','yyyy-mm-dd hh24:mi:ss')
*把删除的数据重新插入原表:
insert into 表名 (select * from 表名 as of timestamp to_timestamp('删除时间点','yyyy-mm-dd hh24:mi:ss'));注意要保证主键不重复。
如果表结构没有发生改变,还可以直接使用闪回整个表的方式来恢复数据。
具体步骤为:
表闪回要求用户必须要有flash any table权限
--开启行移动功能
·alter table 表名 enable row movement
--恢复表数据
·flashback table 表名 to timestamp to_timestamp(删除时间点','yyyy-mm-dd hh24:mi:ss')
--关闭行移动功能 ( 千万别忘记 )
·alter table 表名 disable row movement
2.drop误删除的解决方法
原理:由于oracle在删除表时,没有直接清空表所占的块,oracle把这些已删除的表的信息放到了一个虚拟容器“回收站”中,而只是对该表的数据块做了可以被覆写的标志,所以在块未被重新使用前还可以恢复。
具体步骤:
*查询这个“回收站”或者查询user_table视图来查找已被删除的表:
· select table_name,dropped from user_tables
· select object_name,original_name,type,droptime from user_recyclebin
在以上信息中,表名都是被重命名过的,字段table_name或者object_name就是删除后在回收站中的存放表名
*如果还能记住表名,则可以用下面语句直接恢复:
flashback table 原表名 to before drop
如果记不住了,也可以直接使用回收站的表名进行恢复,然后再重命名,参照以下语句:
flashback table "回收站中的表名(如:Bin$DSbdfd4rdfdfdfegdfsf==$0)" to before drop rename to 新表名
oracle的闪回功能除了以上基本功能外,还可以闪回整个数据库:
使用数据库闪回功能,可以使数据库回到过去某一状态, 语法如下:
SQL>alter database flashback on
SQL>flashback database to scn SCNNO;
SQL>flashback database to timestamp to_timestamp('2007-2-12 12:00:00','yyyy-mm-dd hh24:mi:ss');
总结:
oracle提供以上机制保证了安全操作,但同时也代来了另外一个问题,就是空间占用,由于以上机制的运行,使用drop一个表或者delete数据后,空间不会自
动回收,对于一些确定不使用的表,删除时要同时回收空间,可以有以下2种方式:
1、采用truncate方式进行截断。(但不能进行数据回恢复了)
2、在drop时加上purge选项:drop table 表名 purge
该选项还有以下用途:
也可以通过删除recyclebin区域来永久性删除表 ,原始删除表drop table emp cascade constraints
purge table emp;
删除当前用户的回收站:
purge recyclebin;
删除全体用户在回收站的数据:
purge dba_recyclebin
更多相关教程请访问 MySQL视频教程

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Dreamweaver Mac version
Visual web development tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.