Last active
August 29, 2015 14:03
-
-
Save lavoiesl/96aa8586b609daac2dad to your computer and use it in GitHub Desktop.
Drop all tables of a MySQL database
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Drop all tables of a MySQL database | |
# https://gist.github.com/lavoiesl/96aa8586b609daac2dad | |
if [ -z "$@" ]; then | |
echo "Usage: $0 database_name" >&2 | |
echo " or : $0 -uuser -ppassword -hlocalhost database_name" >&2 | |
exit 1 | |
fi | |
tables=$(mysql $@ -NB -e "SHOW TABLES;" | sed -E 's/^(.+)$/\`\1\`/') | |
if [ -z "${tables}" ]; then | |
echo "Database is empty" | |
exit | |
fi | |
joined=$(echo "${tables}" | tr "\n" ", " | sed 's/,$//') | |
echo "DROP TABLE $joined;" | |
echo | |
echo -n "Confirm (y/N) ? " | |
read confirm | |
if [ "${confirm}" = "y" ]; then | |
mysql $@ -e "SET foreign_key_checks = 0; DROP TABLE $joined; SET foreign_key_checks = 1;" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment