SQL:删除带有前缀的表
如何删除我的所有带有 myprefix_
前缀的表?
注意:需要在phpMyAdmin中执行
How to delete my tables who all have the prefix myprefix_
?
Note: need to execute it in phpMyAdmin
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
复制结果并将其粘贴到文本编辑器中或将查询输出到文件中,使用一些搜索和替换来删除不需要的格式并将
\n
替换为逗号在末尾添加
;
并在前面添加 drop table。你会得到类似这样的东西:
copy the results and paste them into a text editor or output the query to a file, use a few search and replaces to remove unwanted formatting and replace
\n
with a commaput a
;
on the end and add drop table to the front.you'll get something that looks like this:
@andre-miller 解决方案很好,但还有更好的和稍微的更专业,将帮助您一次性执行所有操作。仍然需要多个命令,但这一解决方案将允许您使用 SQL 进行自动构建。
注意:此代码与平台相关,它适用于 MySQL,但可以肯定的是,只需稍作更改即可适用于 Postgre、Oracle 和 MS SQL。
@andre-miller solution is good but there is even better and slightly more professional that will help you execute all in one go. Still will need more than one command but this solution will allow you to use the SQL for automated builds.
Note: this code is platform dependant, it's for MySQL but for sure it could be implemented for Postgre, Oracle and MS SQL with slight changes.
我通过编辑查询成功删除表,如下所示
I drop table successfully by edit query to like this
这是使用 GROUP_CONCAT 的另一种解决方案,因此它将执行一个删除查询,例如
删除表表1,表2,..
Just another solution using GROUP_CONCAT so it will execute one drop query like
DROP TABLE table1,table2,..
您可以使用 MySQL 通过一个命令来完成此操作:
不过,您可能必须在代码中动态构建表列表。
另一种方法是使用 通用例程库 对于 MySQL 5。
You can do that in one command with MySQL:
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.
我只是想发布我使用的确切 SQL - 它是前 3 个答案的混合体:
I just wanted to post the exact SQL I used - it's something of a mixture of the top 3 answers:
我发现准备好的语句对我来说有点棘手,但是当您有很多表时,设置 GROUP_CONCAT_MAX_LEN 是必不可少的。这导致了一个简单的三步过程,从mysql命令行进行剪切和粘贴,这对我来说非常有用:
然后小心地剪切并粘贴生成的长DROP陈述。
I found that the prepared statements were a little tricky to get working for me but setting the
GROUP_CONCAT_MAX_LEN
was essential when you have a lot of tables. This resulted in a simple three-step process with cut-and paste from the mysql command line that worked great for me:Then carefully cut-and-paste the resulting long DROP statement.
您不能仅使用单个 MySQL 命令来完成此操作,但是您可以使用 MySQL 为您构建语句:
在 MySQL shell 中或通过 PHPMyAdmin,使用以下查询
这将生成一个 DROP 语句,您可以复制并执行该语句来删除桌子。
编辑:这里的免责声明 - 上面生成的语句将删除所有数据库中具有该前缀的所有表。如果您想将其限制为特定数据库,请将查询修改为如下所示,并将database_name替换为您自己的database_name:
You cannot do it with just a single MySQL command, however you can use MySQL to construct the statement for you:
In the MySQL shell or through PHPMyAdmin, use the following query
This will generate a DROP statement which you can than copy and execute to drop the tables.
EDIT: A disclaimer here - the statement generated above will drop all tables in all databases with that prefix. If you want to limit it to a specific database, modify the query to look like this and replace database_name with your own database_name:
之前的一些回答都说得非常好。
我将他们的想法与一些人结合起来
网络上其他答案的概念。
我需要删除所有以“temp_”开头的表
经过几次迭代后,我想出了这段代码:
我希望这对其他 MySQL/PHP 程序员有用。
Some of the earlier answers were very good.
I have pulled together their ideas with some
notions from other answers on the web.
I needed to delete all tables starting with 'temp_'
After a few iterations I came up with this block of code:
I hope this is useful to other MySQL/PHP programmers.