views:

81

answers:

6

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 !

+3  A: 

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
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
As fredley said, if you dont need anything but the empty database just drop it and recreate it.
Chris
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
But you might have right on this database, and have no rights to create a new one...
Tarentrulle
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
@wimvds - digging around in information_schema is a bad idea, I would very much advise against it.
fredley
it is really slow to use `information_sheme` here... prefer `SHOW TABLE STATUS`
Tarentrulle
Yes, SHOW TABLE STATUS would work
fredley
@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
@wimvds, first answer was posted with MySQL tag, next did you ever had a look at `information_schema` queries performances?
Tarentrulle
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
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
A: 

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
A: 

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
You may have to repeatedly apply such a script because the presence of foreign key constraints may prevent tables from being dropped.
Brian Hooper
Absolutely. It could take several passes.
Brett McCann
A: 

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

Tarentrulle
+2  A: 

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
nice to thing at foreign_key
Tarentrulle
A: 

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

FractalizeR