Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop all tables in database without dropping the database itself?

Tags:

I would like to delete all the tables from database, but not deleting the database itself. Is it possible ? I'm just looking for shorter way than removing the database and create it again. Thanks !

like image 538
Misha Moroshko Avatar asked Aug 16 '10 12:08

Misha Moroshko


People also ask

How delete all tables from MySQL database?

Connect to the target database. Select all tables from the left sidebar. Right-click and choose delete, or simply hit delete button. Press Cmd + S to commit changes to the server.

Can you drop a database with tables?

Removing a Table from Database. You can use the DROP TABLE statement to easily delete the database tables that you no longer need. The DROP TABLE statement permanently erase all data from the table, as well as the metadata that defines the table in the data dictionary.

How do I drop all tables in a schema?

You can select all the available tables from the right sidebar, right click and choose Delete.. , or press Delete key to drop all.


2 Answers

The shortest is to re-create database. but if you don't want to...

This is for MySQL/PHP. Not tested but something like that.

$mysqli = new mysqli("host", "my_user", "my_password", "database"); $mysqli->query('SET foreign_key_checks = 0'); if ($result = $mysqli->query("SHOW TABLES")) {     while($row = $result->fetch_array(MYSQLI_NUM))     {         $mysqli->query('DROP TABLE IF EXISTS '.$row[0]);     } }  $mysqli->query('SET foreign_key_checks = 1'); $mysqli->close(); 
like image 70
Tomasz Struczyński Avatar answered Sep 28 '22 22:09

Tomasz Struczyński


There is no simple way to do this. Either you'll need to know what the tables are in advance:

//edit you can get this information using the query SHOW TABLE STATUS

$tables = array('users','otherdata'); foreach($tables as $table){   db.execute("DROP TABLE "+$table); } 

or you can drop the database and re-create it empty (it's really not that much effort!):

db.execute('DROP DATABASE SITEDATA'); db.execute('CREATE DATABASE SITEDATA'); 
like image 39
fredley Avatar answered Sep 28 '22 22:09

fredley