Home >Database >Mysql Tutorial >How Can I Quickly Truncate or Drop All Tables in a MySQL Database Using a Single Command?
Truncating or dropping tables in a large MySQL database can be a tedious task when performed manually. Fortunately, there are methods to execute this operation with a single command. Let's explore the options available.
To drop all the tables in a database, use the following command:
mysql -Nse 'show tables' DATABASE_NAME | while read table; do mysql -e "drop table $table" DATABASE_NAME; done
This command executes the following steps:
Truncating a table empties it without deleting its structure, unlike dropping it. To truncate all tables in a database, use this command:
mysql -Nse 'show tables' DATABASE_NAME | while read table; do mysql -e "truncate table $table" DATABASE_NAME; done
This command follows the same steps as for dropping tables but uses truncate table instead of drop table to preserve table structures.
The above is the detailed content of How Can I Quickly Truncate or Drop All Tables in a MySQL Database Using a Single Command?. For more information, please follow other related articles on the PHP Chinese website!