PostgreSQL 查詢表中所有字段,類型和注釋

如果你想看查詢所有表,以及表的 oid 請查看另一篇文章
PostgreSQL 查詢所有表和注釋

查詢某個表的所有字段及其類型和注釋,有2種方式
請注意,也可以通過其他表關(guān)聯(lián)查出,筆者的方法僅保證能使用,但是并不一定是最優(yōu)查詢

通過表名查詢

select c.column_name, c.is_nullable, c.udt_name, col_description(a.attrelid, a.attnum) as comment
from pg_attribute as a
         left join information_schema.columns as c
                   on c.table_name = 'bg_user_record' and a.attname = c.column_name
where a.attrelid = (select oid from pg_class where relname = 'bg_user_record')
  and a.attnum > 0
  and a.atttypid > 0;

結(jié)果展示


image.png

通過oid查詢

select c.column_name, c.is_nullable, c.udt_name, col_description(a.attrelid, a.attnum) as comment
from pg_attribute as a
         left join information_schema.columns as c
                   on c.table_name = (select relname from pg_class where oid = 16529) and a.attname = c.column_name
where a.attrelid = 16529
  and a.attnum > 0
  and a.atttypid > 0;

結(jié)果展示

image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容