MYSQL drop all tables in database in linux

Taken from http://knaddison.com/technology/mysql-drop-all-tables-database-using-single-command-line-command#comment-3588.

#!/bin/sh

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

Save code above in a file, say, mysql-drop-tables. Then change the permission to excecuteable

chmod +x mysql-delete-tables

Then run it

mysql-drop-tables dbuser dbpassword dbname

Here’s fhe file if you’re too lazy to copy paste
http://dl.dropbox.com/u/112837/scripts/mysql-drop-tables

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: