select * from pg_tables where schemaname ='reports' and tablename like '%enhancedreports03_0%' order by tablename;
schemaname | tablename |桌主| 表空间| hasindexes | hasrules | hastriggers ------------ + -------------------------------- + ---- ----------------- + -------------- + ------------ + ---- ------ + -------------报告| enhancedreports03_0_1970_01_01 | ss_agent_summarizer | rpt_data_tbs | t | f | f报告| enhancedreports03_0_2013_10_08 | ss_agent_summarizer | rpt_data_tbs | t | f | ˚F
检查约束条件: “enhancedreports03_0_1970_01_01_check”CHECK(0 :: double precision> = date_part('epoch':: text,'1970-01-01':: date)AND 0 :: double precision< date_part('epoch':: text, '1970年1月2日' ::日期))
我需要删除两个表中的约束。所以我尝试了一个动态查询来生成结果集......
select 'ALTER TABLE '||schemaname ||'.'||tablename||' DROP CONSTRAINT '||conname||'' from pg_tables a, pg_constraint b
where schemaname ='reports' and tablename like '%enhancedreports03_0%' and conname like '%enhancedreports03_0_%' order by tablename;
?柱?
ALTER TABLE reports.enhancedreports03_0_1970_01_01 DROP CONSTRAINT
enhancedreports03_0_1970_01_01_check ALTER TABLE
reports.enhancedreports03_0_1970_01_01 DROP CONSTRAINT
enhancedreports03_0_2013_10_08_check ALTER TABLE
reports.enhancedreports03_0_1970_01_01 DROP CONSTRAINT
enhancedreports03_0_2013_10_08_check ALTER TABLE
reports.enhancedreports03_0_1970_01_01 DROP CONSTRAINT
enhancedreports03_0_2013_10_08_check ALTER TABLE
reports.enhancedreports03_0_2013_10_08 DROP CONSTRAINT
enhancedreports03_0_1970_01_01_check ALTER TABLE
reports.enhancedreports03_0_2013_10_08 DROP CONSTRAINT
enhancedreports03_0_2013_10_08_check ALTER TABLE
reports.enhancedreports03_0_2013_10_08 DROP CONSTRAINT
enhancedreports03_0_2013_10_08_check ALTER TABLE
reports.enhancedreports03_0_2013_10_08 DROP CONSTRAINT
enhancedreports03_0_2013_10_08_check
(8 rows)
我需要结果集像这样
ALTER TABLE reports.enhancedreports03_0_1970_01_01 DROP CONSTRAINT enhancedreports03_0_1970_01_01_check
ALTER TABLE reports.enhancedreports03_0_2013_10_08 DROP CONSTRAINT enhancedreports03_0_2013_10_08_check
提前致谢 阿卜杜勒
答案 0 :(得分:0)
我已经在我的系统中检查了你的代码,它工作得非常好并且放弃你想要的,所以我最后添加了一个;
希望这对你有帮助。
select 'ALTER TABLE '||schemaname ||'.'||tablename||' DROP CONSTRAINT '||conname||';' from pg_tables a, pg_constraint b
where schemaname ='reports' and tablename like '%enhancedreports03_0%' and conname like '%enhancedreports03_0_%' order by tablename;
答案 1 :(得分:0)
尝试查询information_schema.table_constraints
,您可以直接在其中查询表格:
select
*
from
information_schema.table_constraints
where
table_name like 'enhancedreports03_0_%%'
order by
table_name;
您的查询存在的问题是,由于pg_tables
和pg_constraint
之间缺少联接,您正在创建交叉联接。
最简单的方法是使用:
select
'ALTER TABLE '||table_schema ||'.'||table_name||
' DROP CONSTRAINT '||constraint_name||';'
from
information_schema.table_constraints
where
table_schema ='reports'
and table_name like '%enhancedreports03_0%'
and constraint_name like '%enhancedreports03_0_%'
order by
table_name;
如果您想使用pg目录,则必须正确连接表:
select
relname,
conname
from
pg_class pcl
join pg_constraint pco on (pcl.oid = pco.conrelid)
where
relname like 'enhancedreports03_0_%'