码迷,mamicode.com
首页 > 数据库 > 详细

PostgreSql 查询表结构和说明

时间:2019-01-19 22:12:48      阅读:589      评论:0      收藏:0      [点我收藏+]

标签:ble   comment   postgresq   bsp   ike   for   elk   att   结构   

select 
(select relname from  pg_class where oid=a.attrelid) relname ,
(select relname||--||(select description  from pg_description where objoid=oid and objsubid=0) as comment from pg_class where oid=a.attrelid) as table_name,

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 主键约束,

(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 唯一约束,

(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 外键约束,

(case when a.attnotnull=true then Y else N end) as nullable,

col_description(a.attrelid,a.attnum) as comment

from pg_attribute a

where attstattarget=-1 and attrelid in (select oid from pg_class where relname in(select relname from pg_class where relkind =r and relname not like pg_% ))

order by relname,a.attnum;

 

PostgreSql 查询表结构和说明

标签:ble   comment   postgresq   bsp   ike   for   elk   att   结构   

原文地址:https://www.cnblogs.com/hzy168/p/10293408.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!