Take the 2-minute tour ×
Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

I need to drop all the tables in a database without knowing their names beforehand. The typical procedure is to drop and then recreate the database but this is not an option. What is the best way to do it?

share|improve this question
1  
Probably should have asked this beforehand, but better late than never I suppose: What OS? –  Jeff Snider Nov 6 '09 at 14:00
    
Unix/Linux like OS. –  Angel Chiang Jan 5 '12 at 22:38

5 Answers 5

up vote 16 down vote accepted

There is a simpler bash one-liner using mysqldump (from Thingy Ma Jig Blog).

mysqldump -u[USERNAME] -p[PASSWORD] --add-drop-table --no-data [DATABASE] | grep ^DROP | mysql -u[USERNAME] -p[PASSWORD] [DATABASE]

If you're getting this error:

ERROR 1217 (23000) at line 1: Cannot delete or update a parent row: 
    a foreign key constraint fails

Try the following:

(echo 'SET foreign_key_checks = 0;'; 
 (mysqldump -u[USERNAME] -p[PASSWORD] --add-drop-table --no-data [DATABASE] | 
     grep ^DROP); 
 echo 'SET foreign_key_checks = 1;') | \
    mysql -u[USERNAME] -p[PASSWORD] -b [DATABASE]

Now it ignores the constraints.

share|improve this answer

You should use the information_schema tables to fetch the metadata about the database, and then drop the tables listed there.

share|improve this answer

You could try a quick shell script to do it too:

#!/bin/bash

IFS=$'\n'
for table in `mysql <databaseName> -N -e 'show tables'`; do
    echo mysql <databaseName> -e "drop table $table"
done

Remove echo after you've checked that it's going to do what you expect.

share|improve this answer
    
By the way, why do you write '$' before IFS's value? Why doesn't it look like "IFS='\n'" ? –  kolypto Nov 6 '09 at 15:13
    
@o_OTync Using $'' causes bash to interpret backslashed sequences instead of taking them literally. '\n' would contain exactly \n, just like that, which $IFS would understand to be splitting on \ and n characters. $'\n' would contain a newline character, 0x0D. Take a look here for more info: gnu.org/software/bash/manual/bashref.html#ANSI_002dC-Quoting. –  Jeff Snider Nov 6 '09 at 15:41
    
I think this is simpler than using the INFORMATION_SCHEMA. –  Angel Chiang Nov 10 '09 at 13:05

There was an entry on the Xaprb blog recently which covers this nicely.

It includes why using INFORMATION_SCHEMA isn't always a good thing and references a tool from those lovely people at Maatkit.

Try the following with mk-find:

mk-find '<database>' --printf 'DROP TABLE %D.%N'

If you're happy with the results then:

mk-find '<database>' --exec 'DROP TABLE %D.%N'
share|improve this answer

If you have filesystem access, then just rm -rf databasename/* :).

The Drop database statement does the same... Excerpt from the MySQL manual:

The DROP DATABASE statement removes from the given database directory those files and directories that MySQL itself may create during normal operation:

share|improve this answer
1  
Extremely bad idea for any non-trivial setup as MySQL is not expecting these files to suddenly disappear and could be in the middle of any number of operations, transactions, replication, locks... –  bot403 Jan 5 '12 at 22:53
    
Would you consider revising your answer to include the proper commands and procedure then? –  bot403 Jan 6 '12 at 19:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.