删除不在列表中的表?

发布于 2024-11-05 01:31:40 字数 91 浏览 0 评论 0原文

我需要删除大约 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

别闹i 2024-11-12 01:31:40

您可以获得一个列表,

SELECT CONCAT("DROP TABLE ", table_name, ";") 
FROM information_schema.TABLES 
WHERE table_schema = <whatever your db name is> 
AND table_name NOT IN (<your list>);

然后复制并粘贴!

You can get a list with this

SELECT CONCAT("DROP TABLE ", table_name, ";") 
FROM information_schema.TABLES 
WHERE table_schema = <whatever your db name is> 
AND table_name NOT IN (<your list>);

Then copy and paste!

浅沫记忆 2024-11-12 01:31:40

尝试此操作来获取 SQL DROP 语句的结果集:

SELECT CONCAT('DROP TABLE ', TABLE_NAME , ';')
FROM INFORMATION_SCHEMA.TABLES

WHERE TABLE_SCHEMA='YourDatabase'
AND TABLE_NAME NOT IN ('Table1', 'Table2');

复制并粘贴结果,您将获得 20,000 个 DROP 语句。

Try this to get a resultset of SQL DROP statements:

SELECT CONCAT('DROP TABLE ', TABLE_NAME , ';')
FROM INFORMATION_SCHEMA.TABLES

WHERE TABLE_SCHEMA='YourDatabase'
AND TABLE_NAME NOT IN ('Table1', 'Table2');

Copy and paste the results, and you've got 20,000-n DROP statements.

人生戏 2024-11-12 01:31:40

您需要运行 select from information_schema.tables 并创建一个游标,该游标将迭代结果并执行适当的删除(使用 if 语句)。这是因为 drop 语句不支持选择/过滤或其他选项。 (除非在过去的两三年里发生了一些变化)

当你执行 select 语句时,你可以使用类似的东西:

WHERE table_name NOT IN ('ssss','dddd');

另一件事是:为什么你的数据库中有 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:

WHERE table_name NOT IN ('ssss','dddd');

Another thing is: why do you have 20k tables in your database?????

So尛奶瓶 2024-11-12 01:31:40

我建议您采用以下方法

  • 提取所有表的完整列表,每行一个表,使用 SHOW TABLES (或从 information_schema.TABLES
  • 打开列表文本编辑器
  • 删除您不想从列表中删除的表
  • 使用宏或搜索/替换将每一行变成DROP TABLE命令

现在您有一个可以运行的SQL脚本针对数据库

如果您是 Linux shell 忍者,可能需要使用命令行工具,例如 uniqxargs 等来操作文件。使用电子表格可能是处理这个问题的另一种方法。

I would suggest that you take the following approach

  • extract a full list of all of the tables one table per line with SHOW TABLES (or from information_schema.TABLES)
  • open the list in a text editor
  • remove the tables that you don't want to remove from the list
  • use a macro or search/replace to turn each line into a DROP TABLE command

Now 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.

和我恋爱吧 2024-11-12 01:31:40

如果您可以摆脱您希望在短时间内保持不可用的表的情况,我将建议采用完全不同的方法。

  • 备份要保留的表
  • 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.

  • Backup the tables that you want to keep
  • DROP DATABASE the database with all of the tables in
  • Restore the tables that you want to keep from the backups
相思碎 2024-11-12 01:31:40

我今天遇到了完全相同的问题。这是跳过表 ABC、DEF 和 XYZ 的一种方法:

mysql -Nu USER --password=PASSWORD DATABASE -e 'show tables' | \
perl -ne 'BEGIN { print "SET FOREIGN_KEY_CHECKS=0;\n" };
    !/^(ABC|DEF|XYZ)$/ && s/^(.*)/DROP TABLE `\1`;/ && print' | \
mysql -u USER --password=PASSWORD DATABASE

那么这到底是什么?

  1. 第一个 mysql 调用获取所有没有标题行的表(-N 选项)
  2. mysql 的输出通过管道传输到 Perl
  3. Perl 脚本添加第一行以跳过外键检查,因为我们要删除表
  4. 然后脚本添加 DROP TABLE X对于每个表,跳过表 ABC、DEF 和 XYZ
  5. Perl 的输出转到 mysql,修改相同的数据库

I ran into this exact same problem today. Here is one approach for skipping tables ABC, DEF and XYZ:

mysql -Nu USER --password=PASSWORD DATABASE -e 'show tables' | \
perl -ne 'BEGIN { print "SET FOREIGN_KEY_CHECKS=0;\n" };
    !/^(ABC|DEF|XYZ)$/ && s/^(.*)/DROP TABLE `\1`;/ && print' | \
mysql -u USER --password=PASSWORD DATABASE

So what the devil is all that?

  1. The first mysql call gets all the tables without the header row (-N option)
  2. The output of mysql is piped to Perl
  3. The Perl script adds a first line to skip foreign key checks since we are dropping the tables
  4. The script then adds DROP TABLE X for each table, skipping the tables ABC, DEF and XYZ
  5. The output from Perl goes to mysql, modifying the same database
影子是时光的心 2024-11-12 01:31:40

有一个更简单的方法。在命令行中的一行中键入:

mysql -u root -p[password] --skip-column-names [database] -e "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='[database]' AND TABLE_NAME NOT IN ('[table 1]', '[table 2]',..'[table n]');" | cut -f1 | sed -r 's/(.*)/DROP TABLE IF EXISTS \1;/' | mysql -u root -p[password] [database]

您可能会收到两个有关在命令中包含密码的警告。如果这可能是一个问题,您可以清除命令历史记录以“删除证据”:-)

history -c

There is a much easier way. In one line, on your command line type:

mysql -u root -p[password] --skip-column-names [database] -e "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA='[database]' AND TABLE_NAME NOT IN ('[table 1]', '[table 2]',..'[table n]');" | cut -f1 | sed -r 's/(.*)/DROP TABLE IF EXISTS \1;/' | mysql -u root -p[password] [database]

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' :-)

history -c
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文