A, delete all data tables for the specified database
#!/bin/bash# Delete all tables in MySQL # example: # Usage:./Script user Password dbnane# Usage:./script User Password Dbnane server-ip# Usage:./script user Password Dbnane mysql.nixcraft.inch# ---------------------------------------------------Muser=" $"Mpass=" $"MDB=" $"Mhost="localhost" [ "$4"!=""] && mhost="$4"# Set command path MySQL=$(whichMySQL) AWK=$(which awk) GREP=$(which grep) # Helpif[! $#-ge3 ] Then Echo "Usage: $ {mysql-user-name} {Mysql-user-password} {mysql-database-name} [Host-name]" Echo "Drops All tables from a MySQL"Exit1fi# Connect MYSQL database $mysql-U $MUSER-p$mpass-h $MHOST-E"Use $MDB"&>/dev/NULLif[$?-ne0 ] Then Echo "Error-Invalid user name or password, unable to connect to MySQL database"Exit2fiTABLES=$ ($MYSQL-u $MUSER-p$mpass-h $MHOST $MDB-E'Show Tables'| $AWK'{print $}'| $GREP-V'^tables' ) # MakeSure tables exitsif["$TABLES"=="" ] Then Echo "Error-$MDB No related tables found in the database"Exit3fi# Let us Doit forTinch$TABLES Do Echo "Deleting $t table from $MDB database ..."$MYSQL-U $MUSER-p$mpass-h $MHOST $MDB-E"drop table $t" Done
B, clear the contents of the data table (save table structure)
#!/bin/bash# Delete all tables in MySQL # example: # Usage:./Script user Password dbnane# Usage:./script User Password Dbnane server-ip# Usage:./script user Password Dbnane mysql.nixcraft.inch# ---------------------------------------------------Muser=" $"Mpass=" $"MDB=" $"Mhost="localhost" [ "$4"!=""] && mhost="$4"# Set command path MySQL=$(whichMySQL) AWK=$(which awk) GREP=$(which grep) # Helpif[! $#-ge3 ] Then Echo "Usage: $ {mysql-user-name} {Mysql-user-password} {mysql-database-name} [Host-name]" Echo "Drops All tables from a MySQL"Exit1fi# Connect MYSQL database $mysql-U $MUSER-p$mpass-h $MHOST-E"Use $MDB"&>/dev/NULLif[$?-ne0 ] Then Echo "Error-Invalid user name or password, unable to connect to MySQL database"Exit2fiTABLES=$ ($MYSQL-u $MUSER-p$mpass-h $MHOST $MDB-E'Show Tables'| $AWK'{print $}'| $GREP-V'^tables' ) # MakeSure tables exitsif["$TABLES"=="" ] Then Echo "Error-$MDB No related tables found in the database"Exit3fi# Let us Doit forTinch$TABLES Do Echo "Truncate $t table from $MDB database ..."$MYSQL-U $MUSER-p$mpass-h $MHOST $MDB-E"TRUNCATE TABLE $t" Done
Shell script to delete the data table and empty the contents of the data table (save table structure)