Forum Moderators: coopster

Message Too Old, No Replies

How to drop all tables in a database with a specific prefix?

         

irock

4:20 pm on Mar 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I would like to drop all 50 tables with a prefix (vb3*) in a mysql database. And I would like to do this in php my admin. Is this possible?

Thanks!

Birdman

4:28 pm on Mar 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



After you select your database, you should get a listing of all your tables. There should be checkboxes next to each one. Then at the bottom of the list there is a select dropdown to choose what to do with the selected items. DROP is one of the options.

irock

4:42 pm on Mar 5, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm... apparently phpmyadmin 2.2.0 doesn't support this feature. Any other way to do this?

Thx

Birdman

1:09 am on Mar 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmm, your host needs to upgrade :)

Anyhow, after doing some digging over at mysql.com, I found this page:

13.2.8 DROP TABLE Syntax [mysql.com]

The user comments imply that there is no wildcard or LIKE(regex) type syntax allowed on that command. Well, that bites but, you can easily run a quick PHP script to do it.

Something like this:

<?php

//assuming you're connected and have selected the db already

$result = mysql_query("SHOW TABLES");

for ($i = 0; $i < mysql_num_rows($result); $i++)
{
$tablename = mysql_result($result, $i, 0);
if (substr($tablename, 0, 3) == "vb3")
{
if (mysql_query("DROP TABLE ".$tablename)) print "DROPed ".tablename."!<br />";
}
}
?>

I tested this myself with an old table I didn't want but please back up your database before running it.