您需要通过运行以下查询检索表名:
原文链接:https://www.f2er.com/postgresql/193604.htmlSELECT * FROM information_schema.constraint_table_usage WHERE table_name = 'your_table'
或者,您可以使用pg_constraint来检索此信息
select n.nspname as schema_name,t.relname as table_name,c.conname as constraint_name from pg_constraint c join pg_class t on c.conrelid = t.oid join pg_namespace n on t.relnamespace = n.oid where t.relname = 'your_table_name';
然后可以运行所需的ALTER TABLE语句:
ALTER TABLE your_table DROP CONSTRAINT constraint_name;
当然你可以让查询返回完整的alter语句:
SELECT 'ALTER TABLE '||table_name||' DROP CONSTRAINT '||constraint_name||';' FROM information_schema.constraint_table_usage WHERE table_name in ('your_table','other_table')
如果有多个具有相同表的模式,请不要忘记在WHERE子句(和ALTER语句)中包含table_schema。