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。如有錯誤或未考慮完全的地方,望不吝賜教。
推薦閱讀:
- pgsql 實現用戶自定義表結構信息獲取
- postgresql 實現獲取所有表名,字段名,字段類型,註釋
- PostgreSQL 查找當前數據庫的所有表操作
- Postgresql 實現查詢一個表/所有表的所有列名
- PostgreSQL 實現將多行合並轉為列