删除不在列表中的表?
我需要删除大约 20k 个表。我知道不要删除的表的名称。
如果我有表名“a,b,c,d,e,f,g ...”,如何删除不在列表“a,b,c”中的所有表?
I need to drop about 20k tables. I know the names of the tables that are NOT to be dropped.
If I have table names "a,b,c,d,e,f,g...", how can I drop all tables that are not in the list "a,b,c"?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可以获得一个列表,
然后复制并粘贴!
You can get a list with this
Then copy and paste!
尝试此操作来获取 SQL DROP 语句的结果集:
复制并粘贴结果,您将获得 20,000 个 DROP 语句。
Try this to get a resultset of SQL DROP statements:
Copy and paste the results, and you've got 20,000-n DROP statements.
您需要运行 select from information_schema.tables 并创建一个游标,该游标将迭代结果并执行适当的删除(使用 if 语句)。这是因为 drop 语句不支持选择/过滤或其他选项。 (除非在过去的两三年里发生了一些变化)
当你执行 select 语句时,你可以使用类似的东西:
另一件事是:为什么你的数据库中有 20k 个表???
You need to run select from information_schema.tables and create a cursor that will iterate through the results and perform the appropriate drops (use if statement). This is because drop statement does not support selecting/filtering or other options. (unless something has changed during the last two or three years)
When you're performing the select statement you can use something like:
Another thing is: why do you have 20k tables in your database?????
我建议您采用以下方法
SHOW TABLES
(或从information_schema.TABLES
)DROP TABLE
命令现在您有一个可以运行的SQL脚本针对数据库
如果您是 Linux shell 忍者,可能需要使用命令行工具,例如
uniq
、xargs
等来操作文件。使用电子表格可能是处理这个问题的另一种方法。I would suggest that you take the following approach
SHOW TABLES
(or frominformation_schema.TABLES
)DROP TABLE
commandNow you have an SQL script that you can run against the database
If you're a linux shell ninja might want to use commandline tools like
uniq
,xargs
, etc to manipulate the files. Using a spreadsheet might be another way to deal with it.如果您可以摆脱您希望在短时间内保持不可用的表的情况,我将建议采用完全不同的方法。
DROP DATABASE
数据库以及从I'm going to suggest a completely different approach if you can get away with the tables that you want to keep being made unavailable for a short period of time.
DROP DATABASE
the database with all of the tables in我今天遇到了完全相同的问题。这是跳过表 ABC、DEF 和 XYZ 的一种方法:
那么这到底是什么?
I ran into this exact same problem today. Here is one approach for skipping tables ABC, DEF and XYZ:
So what the devil is all that?
有一个更简单的方法。在命令行中的一行中键入:
您可能会收到两个有关在命令中包含密码的警告。如果这可能是一个问题,您可以清除命令历史记录以“删除证据”:-)
There is a much easier way. In one line, on your command line type:
You may get two warnings about including passwords in a command. If this may be a problem, you can clear the command history to 'remove the evidence' :-)