postgreSQL-如何查数据库表、字段以及字段类型、注释等信息?

之前从网上也搜索了一些关于postgreSQL的系统表含义以及如何查表相关信息,但是都没有一个完整的内容,所以自己将找到的一些内容作了下整合,大家可以根据自己需要再对sql进行调整。

--1.查询对象字段、注释和字段类型
select  t.schemaname 
    ,c.relname object_name
    ,d.description object_comment
    --,c.reltuples count
    --,c.relkind object_type --r = 普通表,i = 索引,S = 序列,v = 视图, c = 复合类型,s = 特殊,t = TOAST表
    --,c.relhaspkey primary_key --f:假,t-:真
    ,a.attname column_name
    ,db.description column_comment
    ,s.attrnums distributed_column
    --,case when s.attrnums is not null then 'is distributed key' end distributed_column
    --,a.attnum column_seq
    ,case p.typname 
        when 'int8' then 'bigint'
        when 'int4' then 'integer'
        when 'int2' then 'smallint'
        when 'timestamp' then 'timestamp without time zone'
        when 'date' then 'date'
        when 'varchar' then 'character varying('||a.atttypmod - 4||')'
        when 'numeric' then  'numeric('||(a.atttypmod - 4) / 65536||','||(atttypmod - 4) % 65536||')'
    end column_type
    ,at.adsrc AS column_default
        ,CASE
         WHEN a.attnotnull THEN
          'not null'
         ELSE
          ''
       END AS column_not_null
    --,p.typname
    --,p.typlen
    --,a.atttypmod - 4 as column_length
 from pg_tables t
inner join pg_class c on t.tablename=c.relname
left join pg_description d on c.oid=d.objoid and d.objsubid=0
left join pg_description db on c.oid=db.objoid and db.objsubid>0
left join pg_attribute a on c.oid=a.attrelid and db.objsubid=a.attnum
left join ( select a.attrnums[i.i] attrnums,b.attname,a.localoid  
   from gp_distribution_policy a,  
        (select generate_series(1,10)) i(i),  --预计表分布键的个数为10个
        pg_attribute b   
  where a.attrnums[i.i] is not null   
    and a.localoid=b.attrelid   
    and a.attrnums[i.i]=b.attnum ) s on c.oid=s.localoid and a.attnum=s.attrnums
left join pg_type p on a.atttypid=p.oid
left join pg_attrdef at on c.oid=at.adrelid and a.attnum=at.adnum  
where upper(c.relname) = upper('bas_add_sub_rep') --表名
order by a.attnum;

 

 

 

转载于:https://www.cnblogs.com/eva-wu/p/3455404.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值