How to check if a table exists in MySQL and then delete it in batches

How to check if a table exists in MySQL and then delete it in batches

1. I searched for a long time on the Internet but couldn't find whether the judgment table existed, so I deleted it vaguely. But in the end, we found a roundabout way to save the country.

Requirement: Delete the table with the suffix _ to be deleted.

1. The first step is to find all such tables and assemble the SQL to delete the table

Select CONCAT( 'DROP TABLE IF EXISTS ', table_name, ';' ) 
FROM information_schema.tables 
Where table_name LIKE '%_to be deleted';

Execution Results

2. Step 2: Execute the delete statements one by one. If you are capable, you can write a script to execute something.

3. SQL parsing

3-1: concat string concatenation

3-2: DROP TABLE IF EXISTS equi_accp_info__to be deleted; -- Check if this table exists, and delete it if it exists

Supplementary knowledge: mysql determines whether a table field exists and then modifies it

I won't say much nonsense, let's just look at the code~

-- Check if the thresholdMin field exists in the vrv_paw_rule table. If it does not exist, add it. If it does exist, modify the field type DELIMITER ??
DROP PROCEDURE IF EXISTS schema_change??
CREATE PROCEDURE schema_change()
BEGIN
IF NOT EXISTS (SELECT * FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name = 'vrv_paw_rule' AND column_name = 'thresholdMin') THEN
  ALTER TABLE vrv_paw_rule ADD COLUMN thresholdMin BIGINT;
ELSE  
  ALTER TABLE vrv_paw_rule MODIFY COLUMN thresholdMin BIGINT;
END IF; 
END??
DELIMITER ;

CALL schema_change();

delimit command:

It tells the MySQL interpreter whether the command has ended and whether MySQL can execute it, that is, change the input terminator.

By default, the delimiter is a semicolon ";".

In the command line client, if a line of commands ends with a semicolon,

Then after pressing Enter, MySQL will execute the command.

But sometimes, you don't want MySQL to do this. Because you may enter more statements and the statements may contain semicolons.

By default, it is not possible to wait until the user has entered all of the statements before executing the entire statement.

Because mysql will automatically execute once it encounters a semicolon.

In this case, you can use delimiter and replace the delimiter with other symbols, such as // or $$.

At this time, the role of delimiter is to simply encapsulate the entire small statement.

This command is mostly used to define subroutines, trigger programs, and other MySQL built-in programs.

The above operation of MySQL to determine whether a table exists and then delete it in batches is all the content that the editor shares with you. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • Why the table file size remains unchanged after deleting data in MySQL
  • Three ways to delete a table in MySQL (summary)
  • 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

<<:  JavaScript to achieve the idea of ​​​​snake game

>>:  Detailed explanation of the latest IDEA process of quickly deploying and running Docker images

Recommend

MySQL index cardinality concept and usage examples

This article uses examples to explain the concept...

XHTML Web Page Tutorial

<br />This article is mainly to let beginner...

Detailed explanation of docker version es, milvus, minio startup commands

1. es startup command: docker run -itd -e TAKE_FI...

Basic knowledge of HTML: a preliminary understanding of web pages

HTML is the abbreviation of Hypertext Markup Langu...

Detailed analysis of MySQL master-slave replication

Preface: In MySQL, the master-slave architecture ...

Detailed explanation of how to reduce memory usage in MySql

Preface By default, MySQL will initialize a large...

Introduction to keyword design methods in web design

Many times, we ignore the setting of the web page ...

Web Design: When the Title Cannot Be Displayed Completely

<br />I just saw the newly revamped ChinaUI....

Graphical explanation of the underlying principle of JavaScript scope chain

Table of contents Preface Scope 1. What is scope?...

MySQL table field time setting default value

Application Scenario In the data table, the appli...

How to view nginx configuration file path and resource file path

View the nginx configuration file path Through ng...

React Router 5.1.0 uses useHistory to implement page jump navigation

Table of contents 1. Use the withRouter component...

Front-end JavaScript operation principle

Table of contents 1. What is a JavaScript engine?...

Detailed explanation of lazy loading and preloading of webpack

Table of contents Normal loading Lazy Loading Pre...