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!