PostgreSQL 實現查詢表字段信息SQL腳本

查詢格式:

SELECT
  c.relname 表名稱,
 A.attname AS 字段名稱,
 col_description(A.attrelid,A.attnum) AS 註釋,
 format_type ( A.atttypid, A.atttypmod ) AS 類型,
 CASE WHEN A.attnotnull='f' THEN '否' ELSE '是' END AS 是否必填,
 a.attnum 序號
FROM
 pg_class AS c,
 pg_attribute AS a
WHERE
 A.attrelid = C.oid 
 AND A.attnum > 0
 ORDER BY c.relname,a.attnum;

查詢示例:

SELECT
 c.relname 表名稱,
 A.attname AS 字段名稱,
 split_part(col_description ( A.attrelid, A.attnum ),':',1) AS 註釋,
 format_type ( A.atttypid, A.atttypmod ) AS 類型,
 CASE WHEN A.attnotnull='f' THEN '否' ELSE '是' END AS 是否必填,
 a.attnum 序號
FROM
 pg_class AS c,
 pg_attribute AS a
WHERE
 c.relnamespace=16389
 AND c.relname not like 'v_%'
 AND c.relname not like 'pk_%'
 AND c.relname not like 'unidx%'
 AND c.relname not like '%_index'
 AND c.relname not like '%_seq'
 AND c.relname not like '%_pkey'
 AND A.attrelid = C.oid 
 AND A.attnum > 0
 ORDER BY c.relname,a.attnum;

查詢效果:

補充:postgresql 查詢某一個表中的所有字段

postgresql 查詢某一個表中的所有字段,也就是查詢所有的列名

select * from information_schema.columns
where table_schema='public' and table_name='表名稱 ';

以上為個人經驗,希望能給大傢一個參考,也希望大傢多多支持WalkonNet。如有錯誤或未考慮完全的地方,望不吝賜教。

推薦閱讀: