主頁 > 知識庫 > postgresql 實現(xiàn)獲取所有表名,字段名,字段類型,注釋

postgresql 實現(xiàn)獲取所有表名,字段名,字段類型,注釋

熱門標簽:鐵路電話系統(tǒng) 服務外包 地方門戶網(wǎng)站 百度競價排名 網(wǎng)站排名優(yōu)化 Linux服務器 AI電銷 呼叫中心市場需求

獲取表名及注釋:

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

過濾掉分表:

加條件 and relchecks=0 即可

獲取字段名、類型、注釋、是否為空:

SELECT col_description(a.attrelid,a.attnum) as comment,format_type(a.atttypid,a.atttypmod) as type,a.attname as name, a.attnotnull as notnull 
FROM pg_class as c,pg_attribute as a where c.relname = '表名' and a.attrelid = c.oid and a.attnum>0

補充:PostgreSQL查詢表主鍵及注釋內(nèi)容

網(wǎng)上關于pgSql獲取表主鍵的內(nèi)容都是千篇一律,并且對于存在多主鍵的場景不支持。

附上測試后可獲取多個主鍵字段值的SQL

SELECT
 string_agg(DISTINCT t3.attname,',') AS primaryKeyColumn
 ,t4.tablename AS tableName
 , string_agg(cast(obj_description(relfilenode,'pg_class') as varchar),'') as comment
FROM
 pg_constraint t1
 INNER JOIN pg_class t2 ON t1.conrelid = t2.oid
 INNER JOIN pg_attribute t3 ON t3.attrelid = t2.oid AND array_position(t1.conkey,t3.attnum) is not null
 INNER JOIN pg_tables t4 on t4.tablename = t2.relname
 INNER JOIN pg_index t5 ON t5.indrelid = t2.oid AND t3.attnum = ANY (t5.indkey)
 LEFT JOIN pg_description t6 on t6.objoid=t3.attrelid and t6.objsubid=t3.attnum
WHERE t1.contype = 'p'
  AND length(t3.attname) > 0
  AND t2.oid = '表名' :: regclass
 group by t4.tablename

目前只找到了獲取指定表的主鍵信息,對于批量獲取沒有找到。

以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。

您可能感興趣的文章:
  • postgresql varchar字段regexp_replace正則替換操作
  • 解決postgresql表中的字段名稱包含特殊符號的問題
  • PostgreSQL 實現(xiàn)查詢表字段信息SQL腳本
  • PostgreSQL 更新JSON,JSONB字段的操作
  • postgresql 修改字段長度的操作
  • postgresql 補齊空值、自定義查詢字段并賦值操作

標簽:湖南 黃山 崇左 衡水 仙桃 湘潭 蘭州 銅川

巨人網(wǎng)絡通訊聲明:本文標題《postgresql 實現(xiàn)獲取所有表名,字段名,字段類型,注釋》,本文關鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266