Skip to main content

Drop tables of a database from command line (windows or linux). MySQL.

Drop tables of a database from command line (windows or linux). MySQL.
How to drop tables from command line with a mysql database.





--setting a session (not global since we probably don't have enough privileges) with an higher length. Change the number/length as needed.
SET SESSION group_concat_max_len = 90000;

--setting a variable with the group concatenated list of the tables (ex.: table1,tables2;). Change the MYTABLENAMETOCHANGE with your own table
SET @mytables = (SELECT GROUP_CONCAT(table_name) FROM information_schema.tables WHERE table_schema = 'MYTABLENAMETOCHANGE');

--setting a variable so that we can have a full DROP (if table exists) statement
SET @drptables = (SELECT CONCAT('DROP TABLE IF EXISTS ', @mytables, ';'));

-- preparing "droppingtables" the sql statement from the @drptables variable
PREPARE droppingtables FROM @drptables;

-- executing the sql statement "droppingtables"
EXECUTE droppingtables;

-- releasing "droppingtables"
DEALLOCATE PREPARE droppingtables;



Sample Windows Batch file with command line including password.
Use -p instead of --password=yourpassword if you want to input it by hand.
Remember to add the error checkings or you will end up with the non execution of the queries

drop_tables.bat
________________________________________________________
________________________________________________________


@echo off
SET MYSQLPATH=C:\mysql\bin\
SET MYUSR=cart
SET MYPASS=cart
SET MYDB=cart
SET MYMAXLEN=90000

%MYSQLPATH%\mysql -u%MYUSR% --password=%MYPASS% -D %MYDB% -e "SET SESSION group_concat_max_len = %MYMAXLEN%;SET @mytables = (SELECT GROUP_CONCAT(table_name) FROM information_schema.tables WHERE table_schema = '%MYDB%');SET @drptables = (SELECT CONCAT('DROP TABLE IF EXISTS ', @mytables, ';'));PREPARE droppingtables FROM @drptables;EXECUTE droppingtables;DEALLOCATE PREPARE droppingtables;"
________________________________________________________
________________________________________________________



 Sample Bash script for unix
 drop_tables.sh
________________________________________________________
________________________________________________________
#!/bin/sh
SET MYUSR="cart"
SET MYPASS="cart"
SET MYDB="cart"
SET MYMAXLEN="90000"
MYSQLEXE="$(which mysql)"


#checking if mysql client exists
if [ -z "$MYSQL" ]; then
echo "Error: MYSQL not found"
exit 1
fi


$MYSQLEXE -u$MYUSR --password=$MYPASS -D $MYDB -e "SET SESSION group_concat_max_len = $MYMAXLEN;SET @mytables = (SELECT GROUP_CONCAT(table_name) FROM information_schema.tables WHERE table_schema = '$MYDB');SET @drptables = (SELECT CONCAT('DROP TABLE IF EXISTS ', @mytables, ';'));PREPARE droppingtables FROM @drptables;EXECUTE droppingtables;DEALLOCATE PREPARE droppingtables;"
________________________________________________________
________________________________________________________

Comments

Popular posts from this blog

Moodle 3.8.1+ - path leak via errors in several files

Moodle 3.8.1+ ----------------------------------------------- File: admin/mailout-debugger.php #!/usr/bin/php Notice : Disabled. in \admin\mailout-debugger.php on line 73 File: admin/settings/appearance.php Notice : Undefined variable: hassiteconfig in \admin\settings\appearance.php on line 10 Fatal error : Uncaught Error: Call to undefined function has_any_capability() in \admin\settings\appearance.php:10 Stack trace: #0 {main} thrown in \admin\settings\appearance.php on line 10 File: admin/settings/badges.php Notice : Undefined variable: hassiteconfig in \admin\settings\badges.php on line 30 Fatal error : Uncaught Error: Call to undefined function has_any_capability() in \admin\settings\badges.php:30 Stack trace: #0 {main} thrown in \admin\settings\badges.php on line 30 File: admin/settings/courses.php Notice : Undefined variable: hassiteconfig in \admin\settings\courses.php on line 32 Fatal error : Uncaught Error: Call to undefined function

2022 - Remove (the too many) Ads from Memu launcher

Simple method Download from pureapk "MEmu Launcher2" ex: MEmu Launcher2_v6.0.9_apkpure.com Install "System app remover" (root) remove from system apps the "memu launcher 2" import the "purified" MEmu Launcher2 apk with the Memu utility ("apk" on the right toolbar) Longer method Install "Export Apk" Export the memu launcher2  Install purify https://github.com/echo-devim/purify/raw/master/Purify.apk use purify with the exported memu launcher 2 Install "System app remover" (root) remove from system apps the "memu launcher 2" import the "purified" MEmu Launcher2 apk with the Memu utility ("apk" on the right toolbar)