21

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 !

Misha Moroshko
  • 148,413
  • 200
  • 467
  • 700

10 Answers10

39

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();
Tomasz Struczyński
  • 3,135
  • 19
  • 28
5

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');
fredley
  • 29,323
  • 39
  • 131
  • 223
  • If there are other things in the database (Stored Procedures, Functions, and User Permissions associated with these things) dropping the database may not be the thing the OP wants. – Raj More Aug 16 '10 at 12:59
  • As fredley said, if you dont need anything but the empty database just drop it and recreate it. – Chris Aug 16 '10 at 13:00
  • If you can't do a DROP/CREATE, the first method is the only way, you can't drop all tables in one command. – fredley Aug 16 '10 at 13:03
  • But you might have right on this database, and have no rights to create a new one... – IcanDivideBy0 Aug 16 '10 at 13:04
  • 1
    You wouldn 't really have to know the tables, you could query these from the meta data (`select table_name from information_schema.tables where table_type='BASE TABLE' and table_schema='yourdbname'`). – wimvds Aug 16 '10 at 13:04
  • @wimvds - digging around in information_schema is a bad idea, I would very much advise against it. – fredley Aug 16 '10 at 13:06
  • it is really slow to use `information_sheme` here... prefer `SHOW TABLE STATUS` – IcanDivideBy0 Aug 16 '10 at 13:06
  • @fredley: And why would that be a bad idea? AFAIK it's part of SQL-92, so it's the standard way of doing things, as opposed to using the `SHOW whatever` queries which are MySQL-only. – wimvds Aug 16 '10 at 13:09
  • @wimvds, first answer was posted with MySQL tag, next did you ever had a look at `information_schema` queries performances? – IcanDivideBy0 Aug 16 '10 at 13:30
  • Why would you care about performance for this use case? It's not like you're going to delete all tables every single day. It doesn't hurt to do something according to the standard once in a while. And I just tried the queries, both after a fresh start of MySQL Query Browser, for a remote system : my query takes 0.0015s to complete while `SHOW TABLE STATUS` takes 0.0137s, so maybe, just maybe, you're wrong in assuming that `SHOW whatever` is faster... – wimvds Aug 16 '10 at 13:45
  • Repeated it several times now, timing varies from 0.0013s to 0.0050s for information_schema, always above 0.0120s for `SHOW TABLE STATUS` (quote logical actually, since that does more then dumping table data). `SHOW TABLES` also varies between 0.0020s-0.0050s. So there's no performance gain using the non-standard queries. – wimvds Aug 16 '10 at 13:51
2

I needed to drop all tables except a couple from an inadvertent dump.

A PHP function to drop all tables except some (adapted from here), for anyone else who might need:

<?php
$mysqli = new mysqli( "localhost", "user", 'password', "database");
function drop_all_tables($exceptions_array, $conn) {
    $exceptions_string="('" ;
    foreach ($exceptions_array as $table) {
        $exceptions_string .=$table . "','";
    }
    $exceptions_string=rtrim($exceptions_string, ",'");
    $exceptions_string .="')" ;
    $sql="SELECT CONCAT('DROP TABLE ', TABLE_NAME, '; ')
         FROM information_schema.tables
         WHERE table_schema = DATABASE() AND table_name NOT IN $exceptions_string";
    $result=$ conn->query($sql);
    while($row = $result->fetch_array(MYSQLI_NUM)) {
        $conn->query($row[0]);
    }
}

//drop_all_tables(array("table1","table2","table3","table4"), $mysqli);
?>
Community
  • 1
  • 1
Abdul
  • 997
  • 2
  • 19
  • 37
2

You'd have to drop every table in the db separately, so dropping the database and recreating it will actually be the shortest route (and the fastest one for that matter).

wimvds
  • 12,553
  • 2
  • 38
  • 41
2

When I had to do this in Oracle, I would write a select statement that would generate the drop table statements for me. Something to the effect of:

Select 'DROP TABLE ' || table_name || ';' from user_tables;

I could then pipe the output of the select statement to a file. After I ran this, I would have a file that would drop all my tables for me. It would look something like:

DROP TABLE TABLE1;

DROP TABLE TABLE2;

DROP TABLE TABLE3;

etc...

Not a mysql expert, but I would imagine it would have a similar facility to both select all tables for a schema, as well as direct output from a SQL statement to a file.

Brett McCann
  • 2,429
  • 2
  • 26
  • 44
  • You may have to repeatedly apply such a script because the presence of foreign key constraints may prevent tables from being dropped. – Brian Hooper Aug 16 '10 at 13:12
  • Absolutely. It could take several passes. – Brett McCann Aug 16 '10 at 14:18
  • Hmm, not necessarily. If you delete all tables in one transaction you can defer constraint checks (`SET CONSTRAINTS ALL DEFERRED;`) - in Oracle. In MySQL you can temporarily disable foreign key checks (as in my solution - `SET foreign_key_checks = 0;`) This can at least reduce number of phases needed. – Tomasz Struczyński Mar 16 '12 at 09:27
2

Use SHOW TABLE STATUS to get all tables in your database, then loop over result and drop them one by one.

IcanDivideBy0
  • 1,509
  • 9
  • 16
2

There are some solutions here in comments: http://dev.mysql.com/doc/refman/5.1/en/drop-table.html

Vladislav Rastrusny
  • 27,954
  • 23
  • 85
  • 152
0

A procedural way to do this is as follows:

$query_disable_checks = 'SET foreign_key_checks = 0';

$query_result = mysqli_query($connect, $query_disable_checks);

// Get the first table
$show_query = 'Show tables';
$query_result = mysqli_query($connect, $show_query);
$row = mysqli_fetch_array($query_result);

while ($row) {
  $query = 'DROP TABLE IF EXISTS ' . $row[0];
  $query_result = mysqli_query($connect, $query);

  // Getting the next table
  $show_query = 'Show tables';
  $query_result = mysqli_query($connect, $show_query);
  $row = mysqli_fetch_array($query_result);
}

Here $connect is just the connection made with mysqli_connect();.

R. Gurung
  • 805
  • 1
  • 8
  • 28
-1

The single line query to drop all tables, as below:

$dbConnection = mysqli_connect("hostname", "username", "password", "database_name");
$dbConnection->query('SET foreign_key_checks = 0');

$qry_drop = "DROP TABLE IF EXISTS buildings, business, computer, education, fashion, feelings, food, health, industry, music, nature, people, places, religion, science, sports, transportation, travel";            
$dbConnection->query($qry_drop);

$mysqli->query('SET foreign_key_checks = 1');
$mysqli->close();
user9453341
  • 239
  • 4
  • 8
-1

You can execute this. Just add more tables if I missed any

drop table wp_commentmeta;
drop table wp_comments;
drop table wp_links;
drop table wp_options;
drop table wp_postmeta;
drop table wp_posts;
drop table wp_term_relationships;
drop table wp_term_taxonomy;
drop table wp_termmeta;
drop table wp_terms;
drop table wp_usermeta;
drop table wp_users;
Abdullah Aman
  • 724
  • 6
  • 10