简体   繁体   中英

How to drop all table in MySQL?

I don't want to drop database,

because I'm not hosting the website on my own machine,

drop the database will require create it again,and many settings.

Is there a command in MySQL that can be used to delete all tables in a specific database?

EDIT

Everything I can do is within a phpMyAdmin

You were talking about doing this in phpMyAdmin.

I just had to do this and I'm not sure what version you are using but in the version I have if you scroll to the bottom of the table list you can click "Check All" and then in the drop down next to it that has "With Selected:" you can select "Drop" and it empties all the tables.

mysqldump -u[USERNAME] -p[PASSWORD] --add-drop-table --no-data [DATABASE] | grep ^DROP | mysql -u[USERNAME] -p[PASSWORD] [DATABASE]

Here there are more methods to drop all tables without dropping the database.

You can also generate a sql file from the information_schema database:

Select concat('DROP TABLE database_name.', table_name,';') from information_schema.TABLES where table_schema='database_name';

This will give an output like:

DROP TABLE database_name.table1;
DROP TABLE database_name.table2;
[...]
DROP TABLE database_name.tableN;

I'm not aware of anything direct to suit your needs. You might try doing the following:

Run this to show your drop commands:

mysqldump -u[USERNAME] -p[PASSWORD] --add-drop-table --no-data [DATABASE] | 
grep ^DROP


Nex you can do this to actually execute the drop commands:

mysqldump -u[USERNAME] -p[PASSWORD] --add-drop-table --no-data [DATABASE] | 
grep ^DROP | 
mysql -u[USERNAME] -p[PASSWORD] [DATABASE]
$q=mysql_query("SHOW TABLES FROM ".SQL_DATABASE_NAME);

while($r=mysql_fetch_assoc($q)){
    mysql_query("DROP TABLE ".$r["Tables_in_".SQL_DATABASE_NAME]); 
}

it work for me!

如果您的表名只有简单的名称(没有空格或其他特殊字符),则此查询可以为您构建整个 drop 命令:

select concat('drop table ', group_concat(table_name), ';') from information_schema.tables where table_schema='yourdbname';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM