Mysql delete data and data table method example

Mysql delete data and data table method example

It is very easy to delete data and tables in MySQL, but you need to be careful because all data will disappear once deleted.

Deleting Data

To delete data in a table, use the delete keyword.

Delete data with specified conditions

Delete the user with id 1 in the user table:

delete from User where id = 1;

Delete all data in the table

Delete all data in the table without changing the table structure.

For MyISAM, disk space will be released immediately, but InnoDB will not release disk space.

delete from User;

Free up disk space

optimize table User;

Delete a table

There are two ways to delete a data table:

  1. Delete the data and table structure in the data table
  2. Only delete the data in the table and keep the table structure

drop

Using the drop keyword will delete the entire table, leaving nothing behind.

drop table User;

truncate

The truncate keyword only deletes the data in the table and retains the table structure.

truncate table User;

Question for consideration: How to batch delete tables with the same prefix?

There is no direct command to implement drop table like 'wp_%' , but it can be spliced ​​through MySQL syntax.

-- Delete the table starting with "wp_":
SELECT CONCAT( 'drop table ', table_name, ';' ) AS statement
FROM information_schema.tables
WHERE table_schema = 'database_name' AND table_name LIKE 'wp_%';

Here, replace database_name with the name of the database and wp_ with the prefix of the tables that need to be deleted in batches.

Note that only the drop command can be used like this:

drop table if exists tablename`;

truncate can only be used like this:

truncate table `tp_trade`.`setids`;

Summarize

  • When you no longer need the table, use drop;
  • When you still want to keep the table but delete all records, use truncate;
  • When you want to delete some records, use delete.

This is the end of this article about Mysql deleting data and data tables. For more information about Mysql deleting data and data tables, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Summary of common MySQL table design errors
  • MySQL data table partitioning strategy and advantages and disadvantages analysis
  • MySQL advanced features - detailed explanation of the concept and mechanism of data table partitioning
  • How to construct a table index in MySQL
  • How to maintain MySQL indexes and data tables
  • Setting the engine MyISAM/InnoDB when creating a data table in MySQL
  • How to delete a MySQL table
  • About MYSQL, you need to know the data types and operation tables
  • Detailed explanation of creating a data table in MySQL and establishing primary and foreign key relationships
  • A simple method to merge and remove duplicate MySQL tables
  • How to design MySQL statistical data tables

<<:  Vue3 encapsulates the magnifying glass effect component of Jingdong product details page

>>:  Docker meets Intellij IDEA, Java development improves productivity tenfold

Recommend

Modify the style of HTML body in JS

Table of contents 1. Original Definition 2. JS op...

Detailed tutorial for installing influxdb in docker (performance test)

1. Prerequisites 1. The project has been deployed...

Node implements search box for fuzzy query

This article example shares the specific code for...

Better-scroll realizes the effect of linking menu and content

1. Basic use <!DOCTYPE html> <html lang=...

The process of deploying a project to another host using Jenkins

environment Hostname ip address Serve Jenkins 192...

How to install nginx in docker and configure access via https

1. Download the latest nginx docker image $ docke...

nginx automatically generates configuration files in docker container

When a company builds Docker automated deployment...

Tutorial on installing MYSQL8.X on Centos

MySQL installation (4, 5, 6 can be omitted) State...

Some front-end basics (html, css) encountered in practice

1. The div css mouse hand shape is cursor:pointer;...

Several commonly used single-page application website sharing

CSS3Please Take a look at this website yourself, ...

Solution to the problem of var in for loop

Preface var is a way to declare variables in ES5....