Home >Database >Mysql Tutorial >Can I Truncate All Tables in a MySQL Database with a Single Command?

Can I Truncate All Tables in a MySQL Database with a Single Command?

DDD
DDDOriginal
2024-12-10 19:27:12742browse

Can I Truncate All Tables in a MySQL Database with a Single Command?

Truncating All Tables in a MySQL Database with a Single Command

The question arises: is it possible to truncate all tables in a MySQL database in a single operation?

Absolutely! Here's a solution that combines both drop and truncate commands to achieve your objective:

Drop Tables

The following command drops (i.e., removes) all tables in a database:

mysql -Nse 'show tables' DATABASE_NAME | while read table; do mysql -e "drop table $table" DATABASE_NAME; done

Truncate Tables

Alternatively, you can use a truncate command to empty all tables instead of dropping them:

mysql -Nse 'show tables' DATABASE_NAME | while read table; do mysql -e "truncate table $table" DATABASE_NAME; done

These commands will effectively purge all tables from the specified database in one swift command.

The above is the detailed content of Can I Truncate All Tables in a MySQL Database with a Single Command?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn