Shell script to delete the data table and empty the contents of the data table (save table structure)

Source: Internet
Author: User
Tags mdb database

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)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.