Three ways to delete a table in MySQL (summary)

Three ways to delete a table in MySQL (summary)

drop table

Drop directly deletes table information, which is the fastest, but data cannot be retrieved

For example, to delete the user table:

drop table user;

truncate (table)

truncate deletes table data but not its structure. It ranks second in speed but cannot be used with where.

For example, to delete the user table:

truncate table user;

delete from

delete deletes the data in the table without deleting the table structure. It is the slowest, but can be used with where to delete the specified rows.

For example, delete all data in the user table

delete from user;

Delete the specified record in the user table

delete from user where user_id = 1;

The difference between the three methods

Similarities

  • Truncate, delete without a where clause, and drop will delete the data in the table;
  • Both drop and truncate are DDL statements (data definition language) and will be automatically committed after execution;

Differences

  • Statement type: delete statement is database manipulation language (DML), truncate and drop are database definition language (DDL);
  • Efficiency: Generally speaking, drop > truncate > delete;
  • Whether to delete the table structure: truncate and delete only delete data but not the table structure. After deletion, truncate will rebuild the index (the id will start from 0 after the new data is inserted), while delete will not delete the index (the new inserted data will continue to increase after the index of the deleted data). The drop statement will delete the table structure including dependent constraints, triggers, indexes, etc.
  • Security: Drop and truncate do not record MySQL logs and cannot be rolled back, while delete will record MySQL logs and can be rolled back;
  • Return value: delete returns the number of deleted records after the operation, while truncate returns 0 or -1 (0 if successful, -1 if failed);

Quick Facts

Difference between delete and delete from

If you only delete one table, the effect is the same; if you need to join other tables, you need to use from

delete tb1 from tb1 m where id in (select id from tb2);

Usage Summary

  • When you want to delete the table structure, use drop;
  • If you want to keep the table structure but delete all records, use truncate;
  • If you want to keep the table structure but delete some records, use delete.

This concludes this article about three ways to delete a table in MySQL (summary). For more information about deleting a table in MySQL, please search for previous articles on 123WORDPRESS.COM or continue browsing the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to check if a table exists in MySQL and then delete it in batches
  • Why the table file size remains unchanged after deleting data in MySQL
  • How to find and delete duplicate records in MySQL
  • Detailed explanation of several practical solutions for quickly deleting large amounts of data (tens of millions) in MySQL
  • Specific method to delete mysql service
  • MySQL's method of dealing with duplicate data (preventing and deleting)
  • MySQL database operations (create, select, delete)
  • How to recover deleted MySQL 8.0.17 root account and password under Windows
  • How to completely delete the MySQL 8.0 service under Linux
  • MySQL table deletion operation implementation (differences between delete, truncate, and drop)
  • Troubleshooting the reasons why MySQL deleted records do not take effect

<<:  Analysis of Nginx Rewrite usage scenarios and configuration methods

>>:  Summary of 28 common JavaScript string methods and usage tips

Recommend

The difference between MySQL count(1), count(*), and count(field)

Table of contents 1. First look at COUNT 2. The d...

How to use MySQL binlog to restore accidentally deleted databases

Table of contents 1 View the current database con...

Detailed explanation of MySQL custom functions and stored procedures

Preface This article mainly introduces the releva...

MySQL storage engine basics

In the previous article, we talked about MySQL tr...

Implement MySQL read-write separation and load balancing based on OneProxy

Introduction Part 1: Written at the beginning One...

SVG button example code based on CSS animation

The specific code is as follows: <a href="...

W3C Tutorial (10): W3C XQuery Activities

XQuery is a language for extracting data from XML...

Detailed tutorial on installing ElasticSearch 6.x in docker

First, pull the image (or just create a container...

How to implement Linux deepin to delete redundant kernels

The previous article wrote about how to manually ...

HTML table tag tutorial (26): cell tag

The attributes of the <TD> tag are used to ...

Detailed explanation of Linux commands and file search

1. Perform file name search which (search for ...