当前位置:   article > 正文

PostgreSql数据库查询所有的表以及表信息/列信息_pg数据库c接口读取sql查询的列信息

pg数据库c接口读取sql查询的列信息

PostgreSql查询所有的表以及注释信息

select relname as table_name,cast(obj_description(relfilenode,'pg_class') as varchar) as table_comment from pg_class c 
where relkind = 'r' and relname not like 'pg_%' and relname not like 'sql_%' order by relname

 

PostgreSql查询表信息/列信息(列ID/列名/数据类型/长度/精度/是否可以为null/默认值/是否自增/是否是主键/列描述)

根据自身需要的字段 增加减

select ordinal_position as Colorder,column_name as ColumnName,data_type as TypeName,
coalesce(character_maximum_length,numeric_precision,-1) as Length,numeric_scale as Scale,
case is_nullable when 'NO' then 0 else 1 end as CanNull,column_default as DefaultVal,
case  when position('nextval' in column_default)>0 then 1 else 0 end as IsIdentity, 
case when b.pk_name is null then false else true end as IsPK,c.DeText,d.type
from information_schema.columns 
left join (
    select pg_attr.attname as colname,pg_constraint.conname as pk_name,
        format_type(pg_attr.atttypid,pg_attr.atttypmod) as type
         from pg_constraint  
    inner join pg_class on pg_constraint.conrelid = pg_class.oid 
    inner join pg_attribute pg_attr on pg_attr.attrelid = pg_class.oid and  pg_attr.attnum = pg_constraint.conkey[1] 
    inner join pg_type on pg_type.oid = pg_attr.atttypid
    where pg_class.relname = 'tb_en_coal_quality_info' and pg_constraint.contype='p' 
) b on b.colname = information_schema.columns.column_name
left join (
    select pg_attr.attname as colname,
        format_type(pg_attr.atttypid,pg_attr.atttypmod) as type
        from pg_class  
    inner join pg_attribute pg_attr on pg_attr.attrelid = pg_class.oid 
    where pg_class.relname = 'tb_en_coal_quality_info' 
) d on d.colname = information_schema.columns.column_name
left join (
    select attname,description as DeText from pg_class
    left join pg_attribute pg_attr on pg_attr.attrelid= pg_class.oid
    left join pg_description pg_desc on pg_desc.objoid = pg_attr.attrelid and pg_desc.objsubid=pg_attr.attnum
    where pg_attr.attnum>0 and pg_attr.attrelid=pg_class.oid and pg_class.relname='tb_en_coal_quality_info'
)c on c.attname = information_schema.columns.column_name
where table_schema='public' and table_name='tb_en_coal_quality_info' order by ordinal_position asc

本文内容由网友自发贡献,转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/464397
推荐阅读
相关标签
  

闽ICP备14008679号