How to drop all MySQL tables from the command-line?

29

1

Usually I open Terminal.app and connect to a remote MySQL database.

Then I use this command to drop a table:

mysql> drop table [table name];

But what I need is the command line to drop all tables in the database.

If I use:

mysql> drop database [database name];

I'll destroy the database completely and I won't be able to create tables again. Am I right?

chefnelone

Posted 2011-07-08T11:40:19.960

Reputation: 525

Answers

28

You can drop the database then immediately recreate it:

mysql> drop database [database name];
mysql> create database [database name];

Or you could use a script to drop each table in the database.

William Jackson

Posted 2011-07-08T11:40:19.960

Reputation: 7 646

1

it's totally unsafe, see the comments http://stackoverflow.com/a/12403742/1713660

– vladkras – 2015-10-29T08:01:30.450

ok, just to be sure: I connect to remote database in Terminal with this line: /Applications/MAMP/Library/bin/mysql -h 80.54.554.10 -u adm_user -p my_db. After dropping and creating the database as you said will I need to change this connection values or they remain the same? – chefnelone – 2011-07-08T11:57:38.197

1@chef - Dropping all tables and dropping the whole database does not have the same effect. If you drop only the tables any database wide permissions will remain, not so if you drop/recreate the database. – Nifle – 2011-07-08T21:16:14.643

10@chefnelone : Be careful !!! Dropping a database drops stored procedures and user-defined views as well. – RolandoMySQLDBA – 2011-07-09T01:03:15.770

Quite clear for me. I'll go with the script to delete each table. – chefnelone – 2011-07-11T07:31:12.470

10

You can try the following command:

mysqldump --no-data --add-drop-table DB_NAME | grep ^DROP | mysql -v DB_NAME

Or:

mysql --silent --skip-column-names -e "SHOW TABLES" DB_NAME | xargs -L1 -I% echo 'DROP TABLE `%`;' | mysql -v DB_NAME

Where DB_NAME is your database name. Database credentials you can specify either in ~/.my.cnf or adding them to the command (e.g. -uroot -proot).

This method has some advantages over dropping and creating the database in case your database user doesn't have permission to drop it.

kenorb

Posted 2011-07-08T11:40:19.960

Reputation: 16 795

1Confirmed second example, awesome. Add db credentials as additional flags before --silent and -v / --verbose respectively like: --user=username --password=password --host=host.name – here – 2015-05-20T03:41:18.230

Good idea. But have some problem when there is FOREIGN KEY. I add | sort -r to reverse line, but still not compatible some db. – Fancyoung – 2015-12-03T06:45:17.373

2

mysql -u USERHERE -pPASSWORDHERE --silent --skip-column-names -e "SHOW TABLES" DATABASENAMEHERE | xargs -L1 -I% echo 'SET FOREIGN_KEY_CHECKS = 0; DROP TABLE%; SET FOREIGN_KEY_CHECKS = 1;' | mysql -u USERHERE -pPASSWORDHERE -v DATABASENAMEHERE

sveilleux2

Posted 2011-07-08T11:40:19.960

Reputation: 121

2Welcome to Super User!  While this may answer the question, it would be a better answer if you could explain why it does so.  We’re looking for comprehensive, high-quality answers that provide some explanation and context.  Don’t just give a one-line answer; explain why your answer is right, ideally with citations.  Answers that don’t include explanations may be removed.  Also, check your formatting. – G-Man Says 'Reinstate Monica' – 2015-05-29T01:20:10.497

Not forget set foreign_key_checks = 1 after drop tables: see http://stackoverflow.com/a/2873991/4306855

mysql -u USERHERE -pPASSWORDHERE --silent --skip-column-names -e "SHOW TABLES" DATABASENAMEHERE | xargs -L1 -I% echo 'SET FOREIGN_KEY_CHECKS = 0; DROP TABLE%;SET FOREIGN_KEY_CHECKS = 1;' | mysql -u USERHERE -pPASSWORDHERE -v DATABASENAMEHERE

– toto21 – 2015-11-14T14:10:14.207