23

Possible Duplicate:
Truncate all tables in a MySQL database in one command?

I need to delete, or drop, all the tables in a MySQL database with single command without knowing the tablenames. Is this possible?

0

2 Answers 2

40
drop database YOUR_DATABASE;
/* this will delete all the tables for this database */

create database YOUR_DATABASE;
/* added back the database namespace */
Sign up to request clarification or add additional context in comments.

9 Comments

requires the specific right to drop it though
alright, but that is not using single command :)
phpadmin not allowed me to use drop, anything else.
For the longest time, I thought this would reset permissions for some reason! ;_; all the wasted minutes.
I do this by phpmyadmin (I check foreign key check false) .Because You cannot always get permission to create database. Espesially, in shared hosting enviornment. So in my view, what to do is to select all tables from phpmyadmin and drop it. Disclaimer: May this answer OP looking for. But not suggestible as a general solution to all.
|
2

Rather long, but try this command (after replacing the obvious things):

mysql --user=YOUR_USERNAME --password=YOUR_PASSWORD -BNe "show tables" YOUR_DBSCHEMA_NAME | tr '\n' ',' | sed -e 's/,$//' | awk '{print "SET FOREIGN_KEY_CHECKS = 0;DROP TABLE IF EXISTS " $1 ";SET FOREIGN_KEY_CHECKS = 1;"}' | mysql --user=YOUR_USERNAME --password=YOUR_PASSWORD YOUR_DBSCHEMA_NAME

1 Comment

Don't know why you got downvoted (I don't think that should be allowed without a comment!), but this is great and better than other linked answers as it deals with foreign key checks too. Thanks.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.