PostgresSql 中查询库中所有的表以及表中字段、主键、唯一、外键

查询库中所有表
select relname as TABLE_NAME ,col_description(c.oid, 0) as COMMENTS from pg_class c
where  relkind = 'r' and relname not like 'pg_%' and relname not like 'sql_%'  order by relname


查询表中所有字段、主键、唯一、外键、是否为空
select 'true' as list,'true' as edit,'false' as search,a.attname as column_name,format_type(a.atttypid,a.atttypmod) as data_type,
(case
when atttypmod-4>0 then atttypmod-4
else 0
end)data_length,
(case
when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='p')>0  then 'Y'
else 'N'
end) as P,
(case
when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='u')>0  then 'Y'
else 'N'
end) as U,
(case
when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='f')>0  then 'Y'
else 'N'
end) as R,
(case
when a.attnotnull=true  then 'Y'
else 'N'
end) as nullable,
col_description(a.attrelid,a.attnum) as comment,'XEditText' as control
from  pg_attribute a
where attstattarget=-1 and attrelid = (select oid from pg_class where relname ='userinfo')

猜你喜欢

转载自xiaoning-zhou.iteye.com/blog/1567611