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;

 

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM