PostgreSQL 数据字典表——查询列属性

作者:moocbaby

日期:2019-2-12

新年快乐,开工大吉!

表名用途
pg_attribute列信息
pg_class表信息
pg_type数据类型
pg_constraint约束,如主键

-----------------------------------------------------
# pg_attribute 列信息
## attrelid 表名,关联 pg_class.oid 
## atttypid 数据类型,关联 pg_type.oid
## attlen 数据长度,同 pg_type.typlen
## attname 列名
## attnum 列序号
## attnotnull 列是否空,true:not null;false:null

# pg_class 表信息
## relname 表名


# pg_type 数据类型 
## typename 数据类型名

# pg_constraint 约束
## contype 'p'主键
## conkey 是一个数组,如{1}或者{1,2},可以用unnest(conkey)函数将数组分开,然后关联pg_attribute.attnum 获取键对应的列
## conrelid 关联 pg_class.oid

样例sql(未测试),
如查询某表的表名、列、序号、是否主键、是否非空以及数据类型,数据类型长度

select c.relname,a.attname,a.attnum,case when conkey is not null then 'PK' else '' end,case a.attnotnull when TRUE then 'not null' ELSE '' END, d.typename, pg_catalog.format_type(a.atttypeid,a.atttypmod) from pg_attribute a inner join pg_class c on a.attrelid=c.oid left join pg_type d on a.atttypid=d.oid left join (select conrelid, UNNEST(conkey) conkey from pg_constraint where contype='p') b on  c.oid=b.conrelid and a.attnum=b.conkey where c.relname='xx' and a.attnum>0
order by a.attnum;
 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值