Eliminare tutte le tabelle in un Database MySQL
Jump to navigation
Jump to search
Oneliner
Per cancellare tutte le tabelle di un database mysql di nome MYDATABASENAME (presuppone che le credenziali siano in ~/.my.cnf per mysql e mysqldump:
echo "SET FOREIGN_KEY_CHECKS = 0;" | (cat && mysqldump --add-drop-table --no-data MYDATABASENAME | grep 'DROP TABLE') | mysql MYDATABASENAME
A Step
First, disable foreign key check:
echo "SET FOREIGN_KEY_CHECKS = 0;" > ./temp.sql
Then dump the db with no data and drop all tables:
mysqldump --add-drop-table --no-data -u root -p db_name | grep 'DROP TABLE' >> ./temp.sql
Turn the foreign key check back on:
echo "SET FOREIGN_KEY_CHECKS = 1;" >> ./temp.sql
Now restore the db with the dump file:
mysql -u root -p db_name < ./temp.sql