MySQL uses the truncate command to quickly clear all tables in a database

MySQL uses the truncate command to quickly clear all tables in a database

1. Execute the select statement first to generate all truncate statements

Statement format:

select CONCAT('truncate TABLE ',table_schema,'.',TABLE_NAME, ';') from INFORMATION_SCHEMA.TABLES where table_schema in ('数据库1','数据库2');

Take the database named dbname as an example and execute the select statement:

mysql> select CONCAT('truncate TABLE ',table_schema,'.',TABLE_NAME, ';') from INFORMATION_SCHEMA.TABLES where table_schema in ('dbname'); 
+------------------------------------------------------------+ 
| CONCAT('truncate TABLE ',table_schema,'.',TABLE_NAME, ';') | 
+------------------------------------------------------------+ 
| truncate TABLE dbname.ABOUTUSINFO; | 
| truncate TABLE dbname.ABUTMENT; | 
| truncate TABLE dbname.ABUTMENTFILE; | 
| truncate TABLE dbname.ACHVORG; | 
| truncate TABLE dbname.WORKFLOWNODE; | 
| truncate TABLE dbname.ZONESERVICE; | 
| truncate TABLE dbname.ZONESERVICEFILE; | 
+------------------------------------------------------------+ 
7 rows in set 

mysql> 

2. Replace the “|” before and after each truncate statement with a blank character

Use a text editor (such as Notepad++) to replace the "|" before and after each truncate statement with a blank character to facilitate copying and executing multiple statements at once.

Before replacement:

After replacement:

3. Copy the truncate statement to the mysql command line and execute it

Copy the truncate statement to the mysql command line for execution. You can copy multiple statements at a time for execution.

mysql> truncate TABLE dbname.ZONESERVICE;  
Query OK, 0 rows affected 
mysql> 

This will clear all tables in the database. It's simple~

Comparison between truncate, drop, and delete

As mentioned above, truncate is very similar to delete and drop. In fact, there are still significant differences between these three. The following is a brief comparison of the similarities and differences between the three.

  • truncate and drop are DDL statements and cannot be rolled back after execution; delete is a DML statement and can be rolled back.
  • truncate can only be applied to tables; delete and drop can be applied to tables, views, etc.
  • Truncate will clear all rows in the table, but the table structure and its constraints, indexes, etc. remain unchanged; drop will delete the table structure and the constraints, indexes, etc. on which it depends.
  • Truncate resets the table's auto-increment value; delete does not.
  • Truncate does not activate delete triggers associated with the table; delete does.
  • After truncate, the space occupied by the table and index will be restored to the initial size; the delete operation will not reduce the space occupied by the table or index, and the drop statement will release all the space occupied by the table.

This is the end of this article about how to use MySQL truncate command to quickly clear all tables in a database. For more information about MySQL truncate to clear database tables, please search 123WORDPRESS.COM's previous articles or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • When Navicat Premium connects to the database, the error message appears: 2003 Can't connect to MySQL server on''localhost''(10061)
  • Detailed tutorial on installing MySQL database on Alibaba Cloud Server
  • Notes on the MySQL database backup process
  • Detailed explanation of how to connect to MySQL database using Java in IntelliJ IDEA
  • MySql sets the specified user database view query permissions
  • Jmeter implements MySQL database testing based on JDBC requests
  • Implementation of Python connecting to MySQL database and reading data
  • Python operating MySQL database
  • How to migrate local mysql to server database

<<:  Implementation of Nginx filtering access logs of static resource files

>>:  Vue realizes the function of uploading photos on PC

Recommend

Detailed steps to upgrade mysql8.0.11 to mysql8.0.17 under win2008

Upgrade background: In order to solve the vulnera...

RHCE installs Apache and accesses IP with a browser

1. at is configured to write "This is a at t...

Vue implements graphic verification code

This article example shares the specific code of ...

HTML+CSS+JS sample code to imitate the brightness adjustment effect of win10

HTML+CSS+JS imitates win10 brightness adjustment ...

A brief discussion on the principle of js QR code scanning login

Table of contents The essence of QR code login Un...

Analysis of Linux boot system methods

This article describes how to boot the Linux syst...

Do you know why vue data is a function?

Official website explanation: When a component is...

Native JS music player

This article example shares the specific code of ...

Django2.* + Mysql5.7 development environment integration tutorial diagram

environment: MAC_OS 10.12 Python 3.6 mysql 5.7.25...

How to use and limit props in react

The props of the component (props is an object) F...

html+css+js to realize the function of photo preview and upload picture

Preface: When we are making web pages, we often n...

MySQL 5.7 installation and configuration tutorial under CentOS7 64 bit

Installation environment: CentOS7 64-bit MINI ver...