Tuesday, September 27, 2016

Delete all tables from database using command line

We can drop all tables of a database using an GUI Interface like PHPMYADMIN. But sometimes in some scenarios we do not have access to the GUI Interface like PHPMYADMIN but only tool like command line or command prompt.

In Such scenarios we can drop all the tables of a specific database using the command line. Following are the statements which can be used on the command line to drop all the database tables from the database with ease.

SET FOREIGN_KEY_CHECKS = 0; 

SET @tables = NULL;
SELECT GROUP_CONCAT(table_schema, '.', table_name) INTO @tables
  FROM information_schema.tables 
  WHERE table_schema = 'database_name'; -- specify DB name here.

SET @tables = CONCAT('DROP TABLE ', @tables);
PREPARE stmt FROM @tables;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;

That's it all tables from the database will be deleted.

No comments:

Post a Comment

MS SQL : How to identify fragmentation in your indexes?

Almost all of us know what fragmentation in SQL indexes are and how it can affect the performance. For those who are new Index fragmentation...