TRUNCATE ALL Tables

By admin

USE AT YOUR OWN RISK. Put this in a script, mysqltrunc.sh then run it….it will truncate all the data in all of your table

#!/usr/bin/env bash
# Written by David Burger

if [ $# -eq 2 ]; then
user=$1
dbname=$2
basecmd=”mysql -u ${user} -D ${dbname}”
elif [ $# -eq 3 ]; then
user=$1
PASS=$2
dbname=$3
basecmd=”mysql -u ${user} -p$PASS -D ${dbname}”
else
echo “usage: mysqltrunc user [pass] database” >&2
exit 1
fi

tables=$(${basecmd} -e “SHOW TABLES;” | grep -v “+–” | grep -v “Tables_in_${dbname}”)
if [ $? -ne 0 ]; then
echo “Unable to retrieve the table names.” >&2
exit 1
fi

cmd=”"

for table in ${tables}; do
cmd=”${cmd} TRUNCATE ${table};”
done

$(${basecmd} -e “${cmd}”)



categoriaMysql commentoNo Comments dataAugust 3rd, 2007

About... admin

This author published 340 posts in this site.

Share

FacebookTwitterEmailWindows LiveTechnoratiDeliciousDiggStumbleponMyspaceLikedin

Leave a comment