【问题标题】:Postgres How to drop all tables from a schema with "group" in name?Postgres如何从名称中带有“组”的模式中删除所有表?
【发布时间】:2020-11-03 22:29:57
【问题描述】:

我有一个 postgres 数据库模式,其中包含我需要删除的数十个表。我基本上需要删除我的模式(abcd)中名称中带有“组”一词的所有表。如果不手动进入并为每个脚本编写放置脚本,我该如何做到这一点?想知道在 postgres sql 中是否有一些简单的方法可以做到这一点?或者也许使用 python?

drop table abcd.group%;

【问题讨论】:

  • select * from pg_class where relnamespace = 'abcd'::regnamespace and relname ilike 'group%' 将为您提供以组开头的表格。如果您想在名称中的任何位置分组,则使用“%group%”。然后,您可以循环使用 Python 或 Postgres 函数。

标签: sql postgresql


【解决方案1】:

这可能不是最酷的方法,但您可以运行下面的查询,复制并粘贴输出并运行它。

SELECT 'DROP TABLE ' || schemaname || '.' || relname || ';' "statement"
FROM pg_catalog.pg_statio_user_tables
WHERE schemaname = 'abcd' AND relname ILIKE 'group%';

【讨论】:

  • 什么是“声明”?这是 postgres 中的特定命令吗?
  • 我可以留下一个没有名字的专栏。否则没有意义。
猜你喜欢
  • 2022-12-20
  • 1970-01-01
  • 1970-01-01
  • 2022-07-27
  • 2017-04-04
  • 1970-01-01
  • 2015-06-07
  • 1970-01-01
  • 2016-03-04
相关资源
最近更新 更多