[英]How to drop all views in Postgres
I have created lot of tables and views in path (called it parlgov)我在路径中创建了很多表和视图(称为 parlgov)
I want to either delete all the tables and views in this Schema (or called path) or preferable to only delete all the view and leave the tables.我想删除此架构(或称为路径)中的所有表和视图,或者最好只删除所有视图并保留表。
I have tried manually delete by drop view view's name which is not efficient.我曾尝试通过下拉视图视图的名称手动删除,但效率不高。
I am wondering if there are better ways to do it.我想知道是否有更好的方法来做到这一点。
You can do that with a simple anonymous PL/pgSQL block:你可以用一个简单的匿名 PL/pgSQL 块来做到这一点:
do
$$
declare
l_rec record;
l_stmt text;
begin
for l_rec in (select schemaname, viewname
from pg_views
where schemaname = 'parlgov')
loop
l_stmt := format('drop view if exists %I.%I cascade', l_rec.schemaname, l_rec.viewname);
raise notice 'Dropping %', l_stmt;
execute l_stmt;
end loop;
end;
$$
;
select 'DROP VIEW '||viewname||';' FROM pg_views where schemaname='parlgov';
SELECT 'DROP VIEW ' || table_name || ';'
FROM information_schema.views
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
AND table_name !~ '^pg_';
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.