当前位置:网站首页>1.delete
1.delete
2022-06-12 16:21:00 【JAVA Chinese community - Leige】
stay MySQL in , The total number of deleted methods is 3 Kind of :delete、truncate、drop, The usage and usage scenarios of the three are completely different , Let's look at it in detail .
1.delete
detele Can be used to delete some or all of the data in a table , Its usage syntax is as follows :
delete from table_name [where...] [order by...] [limit...]PS:[] The commands in are optional , Can be omitted .
If we want to delete the top math scores in the student list 3 Students , You can use the following SQL:
delete from student order by math desc limit 3;1.1 delete Realization principle
stay InnoDB In the engine ,delete The operation does not really delete the data , Instead, the data is marked for deletion , Marked as deleted , We can do this by MySQL Set to non auto submit mode , To test and verify . Settings for non auto submit mode SQL as follows :
set autocommit=0;After that, a data delete Delete the , And then use rollback Rollback operation , Finally, verify whether the data we deleted before still exists , If the data still exists, it means delete The data is not really deleted , It just identifies the data as deleted , verification SQL And execution results are shown in the figure below :
1.2 About the auto increment column
stay InnoDB In the engine , Used delete After deleting all the data , The auto increment column is not reset to the initial value , We can use the following command to verify :
2.truncate
truncate Execution effect and delete similar , It is also used to delete all row data in the table , Its usage syntax is as follows :
truncate [table] table_nametruncate In use and delete The big difference ,delete You can use conditional expressions to delete some data , and truncate Cannot add conditional expression , So it can only delete all row data , For example, the following truncate Added where An error will be reported after the command :
2.1 truncate Realization principle
truncate It seems that only row data has been deleted , But it is DDL sentence , That is to say Data Definition Language Data definition language , It is used to maintain the structure instructions of stored data , So this is also the same as delete Commands are different ,delete The statement belongs to DML,Data Manipulation Language Data manipulation language , Used to manipulate data . Why? truncate Only the row data is deleted , Column data was not deleted ( Data such as fields and indexes ) nevertheless DDL Language ? This is because truncate In essence, a new table structure is created , Delete the original table , So it belongs to DDL Language , Instead of DML Language .
2.2 Reset auto increment column
truncate stay InnoDB The auto incrementing column will be reset in the engine , The following command shows :
3.drop
drop Unlike the previous two commands, which only delete the row data of the table ,drop The row data of the entire table and the table structure will be deleted , Its syntax is as follows :
DROP [TEMPORARY] TABLE [IF EXISTS] tbl_name [,tbl_name]among TEMPORARY It means temporary table , In general, this command will be ignored .
drop An example is as follows :
Differences among the three
- Data recovery :delete Can recover deleted data , and truncate and drop Cannot recover deleted data .
- In terms of execution speed :drop > truncate > delete.
- Delete data :drop Delete the entire table , Contains row data and fields 、 Index and other data , and truncate and drop Only row data is deleted .
- Add conditional aspect :delete have access to where Expression to add query criteria , and truncate and drop Can not add where Query criteria .
- Reset auto incrementing aspect : stay InnoDB In the engine ,truncate Auto incrementing columns can be reset , and delete Auto incrementing columns cannot be reset .
summary
delete、truncate Can be used to delete row data in a table , and drop Is to delete the whole table , The deleted data contains all row data and fields 、 Index and other data , among delete Deleted data can be recovered , and truncate and drop Is irrecoverable , But in terms of execution efficiency , The latter two deletion methods have great advantages , Therefore, select the corresponding delete command according to the actual scene , Of course truncate and drop You should also be careful when using the deletion method of these unrecoverable data .
It's up to you to judge right and wrong , Disdain is to listen to people , Gain or loss is more important than number . Interview collection :https://gitee.com/mydb/interview
边栏推荐
- Learning record [email protected] understand canvas
- Sha6 of D to large integer
- 小程序:如何在插件中获取用户手机号
- C packing and unpacking
- C regular expression
- MySQL系列之分库分表学习笔记
- Multimix:从医学图像中进行的少量监督,可解释的多任务学习
- 盒马,最能代表未来的零售
- The market share of packaged drinking water has been the first for eight consecutive years. How does this brand DTC continue to grow?
- acwing 801. 二进制中1的个数(位运算)
猜你喜欢

acwing796 子矩阵的和

Gopher to rust hot eye grammar ranking

leetcode-54. Spiral matrix JS

Let's talk about events. Listen to those things. - Part one

Project training of Software College of Shandong University rendering engine system basic renderer (VII)

acwing794 高精度除法

Scanpy (VI) analysis and visualization of spatial transcriptome data

The C Programming Language(第 2 版) 笔记 / 8 UNIX 系统接口 / 8.7 实例(存储分配程序)

Applet: how to get the user's mobile number in the plug-in

generate pivot data 0
随机推荐
Let's talk about events. Listen to those things. - Part one
Project training of Software College of Shandong University rendering engine system point cloud processing (10)
3/6 线性系统的时域分析法(上)
Interview: hashcode() and equals()
使用 .NET 升级助手将NET Core 3.1项目升级为.NET 6
acwing 高精度乘法
超详细干货!Docker+PXC+Haproxy搭建高可用强一致性的MySQL集群
33-【go】Golang sync.WaitGroup的用法—保证go协程执行完毕,主协程才退出
<山东大学项目实训>渲染引擎系统(八-完)
33-【go】Golang sync. Usage of waitgroup - ensure that the go process is completed before the main process exits
5-5配置Mysql复制 基于日志点的复制
In 2021, China's lottery sales generally maintained a rapid growth, and the monthly sales generally tended to be stable [figure]
小程序:如何在插件中获取用户手机号
Analysis of China's cargo transport volume, cargo transport turnover and port cargo in 2021 [figure]
[thinking about the process of structure optimization] how to build the evaluation ability of the impact of technical solutions
acwing 802. 区间和 (离散化)
GloVe词嵌入(IMDB电影评论情感预测项目实战)
C packing and unpacking
puppeteer入门之 BrowserContext 类
pbootcms的if判断失效直接显示标签怎么回事?