postgre drop table 根据表名 模糊删除 上面这种话sql你知道怎么写么?

image.png

postgre drop table whose name like "test%" 根据表名 模糊删除 上面这种话sql你知道怎么写么?

阅读 5.4k
2 个回答
postgres=# \dt public.ex*
         List of relations
 Schema |   Name    | Type  | Owner
--------+-----------+-------+-------
 public | express   | table | fei
 public | express_2 | table | fei
(2 rows)

postgres=# select 'drop table '||schemaname||'.'||tablename||';' from pg_tables where schemaname||'.'||tablename like '%public.ex%' \gexec
DROP TABLE
DROP TABLE

postgres=# \dt public.ex*
Did not find any relation named "public.ex*".
postgres=#

不支持 DROP 时模糊匹配。

你只能先查出来所有表名,然后挨个删除了。

SELECT DISTINCT(table_name) FROM information_schema.columns WHERE table_name LIKE 'test%';

也可以用 FOR LOOP + END LOOP 直接 SQL 里写循环,就是比较难受……

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进