1. 程式人生 > 其它 >PostgresSql 中查詢庫中所有的表以及表中欄位、主鍵、唯一、外來鍵

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')