SQL: deleting tables with prefix

How to delete my tables who all have the prefix myprefix_?

Note: need to execute it in phpMyAdmin

106783 次浏览

你可以用 MySQL 的一个命令来做到这一点:

drop table myprefix_1, myprefix_2, myprefix_3;

You'll probably have to build the table list dynamically in code though.

An alternative approach would be to use the general purpose routine library for MySQL 5.

You cannot do it with just a single MySQL command, however you can use MySQL to construct the statement for you:

在 MySQL shell 中或通过 PHPMyAdmin,使用以下查询

SELECT CONCAT( 'DROP TABLE ', GROUP_CONCAT(table_name) , ';' )
AS statement FROM information_schema.tables
WHERE table_name LIKE 'myprefix_%';

这将生成一个 DROP 语句,您可以复制并执行该语句以删除表。

编辑: 这里有一个免责声明——上面生成的语句将删除所有前缀为该前缀的数据库中的所有表。如果希望将其限制在特定的数据库中,请将查询修改为如下所示,并将 database _ name 替换为您自己的 database _ name:

SELECT CONCAT( 'DROP TABLE ', GROUP_CONCAT(table_name) , ';' )
AS statement FROM information_schema.tables
WHERE table_schema = 'database_name' AND table_name LIKE 'myprefix_%';
show tables like 'prefix_%';

将结果复制并粘贴到文本编辑器或将查询输出到文件中,使用一些搜索并替换以删除不需要的格式,并用逗号替换 \n 把一个 ;的结束,并添加下降表的前面。

you'll get something that looks like this:

drop table myprefix_1, myprefix_2, myprefix_3;

@ andre-miller 解决方案是不错,但是还有更好更专业的解决方案,可以帮助你一次性完成所有任务。仍然需要不止一个命令,但是这个解决方案允许您使用 SQL 进行自动构建。

SET @tbls = (SELECT GROUP_CONCAT(TABLE_NAME)
FROM information_schema.TABLES
WHERE TABLE_NAME LIKE 'myprefix_%');
PREPARE stmt FROM 'DROP TABLE @tbls';
EXECUTE stmt USING @tbls;
DEALLOCATE PREPARE stmt;

注意: 这段代码是平台相关的,它是针对 MySQL 的,但是可以肯定的是,它可以在 Postgre,Oracle 和 MS SQL 中实现,只需要做一些细微的改变。

SELECT CONCAT("DROP TABLE ", table_name, ";")
FROM information_schema.tables
WHERE table_schema = "DATABASE_NAME"
AND table_name LIKE "PREFIX_TABLE_NAME%";

早期的一些答案非常好。 我把他们的想法和一些 来自网上其他答案的观点。

我需要删除以‘ temp _’开头的所有表 经过几次迭代之后,我得到了这段代码:

-- Set up variable to delete ALL tables starting with 'temp_'
SET GROUP_CONCAT_MAX_LEN=10000;
SET @tbls = (SELECT GROUP_CONCAT(TABLE_NAME)
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'my_database'
AND TABLE_NAME LIKE 'temp_%');
SET @delStmt = CONCAT('DROP TABLE ',  @tbls);
-- SELECT @delStmt;
PREPARE stmt FROM @delStmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

我希望这对其他 MySQL/PHP 程序员有用。

我发现准备好的语句对于我来说有点棘手,但是当您有很多表时,设置 GROUP_CONCAT_MAX_LEN是必不可少的。这导致了一个简单的从 Mysql命令行剪切和粘贴的三步过程,对我来说非常有效:

SET GROUP_CONCAT_MAX_LEN=10000;
SELECT CONCAT( 'DROP TABLE ', GROUP_CONCAT(table_name) , ';' )
AS statement FROM information_schema.tables
WHERE table_name LIKE 'myprefix_%';

然后仔细剪切并粘贴生成的长 放下语句。

我只是想把我使用的 SQL 发布出来——它是前三个答案的混合体:

SET GROUP_CONCAT_MAX_LEN=10000;


SET @del = (
SELECT      CONCAT('DROP TABLE ', GROUP_CONCAT(TABLE_NAME), ';')
FROM        information_schema.TABLES


WHERE       TABLE_SCHEMA = 'database_name'
AND         TABLE_NAME LIKE 'prefix_%'
);


PREPARE stmt FROM @del;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

我通过编辑查询成功地删除了表格,如下所示

SET GROUP_CONCAT_MAX_LEN=10000;
SET FOREIGN_KEY_CHECKS = 0;
SET @tbls = (SELECT GROUP_CONCAT(CONCAT('`', TABLE_NAME, '`'))
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'pandora'
AND TABLE_NAME LIKE 'temp_%');
SET @delStmt = CONCAT('DROP TABLE ',  @tbls);
-- SELECT @delStmt;
PREPARE stmt FROM @delStmt;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
SET FOREIGN_KEY_CHECKS = 1;

Just another solution using 集团 _ CONCAT so it will execute one drop query like
DROP TABLE table1,table2,. .

SET @Drop_Stm = CONCAT('DROP TABLE ', (
SELECT GROUP_CONCAT(TABLE_NAME) AS All_Tables FROM information_schema.tables
WHERE TABLE_NAME LIKE 'prefix_%' AND TABLE_SCHEMA = 'database_name'
));
PREPARE Stm FROM @Drop_Stm;
EXECUTE Stm;
DEALLOCATE PREPARE Stm;