主頁 > 知識庫 > Postgresql 實(shí)現(xiàn)查詢一個(gè)表/所有表的所有列名

Postgresql 實(shí)現(xiàn)查詢一個(gè)表/所有表的所有列名

熱門標(biāo)簽:合肥公司外呼系統(tǒng)運(yùn)營商 辦公外呼電話系統(tǒng) 重慶自動外呼系統(tǒng)定制 漯河外呼電話系統(tǒng) 打電話智能電銷機(jī)器人授權(quán) 外呼調(diào)研系統(tǒng) 美容工作室地圖標(biāo)注 地圖標(biāo)注和圖片名稱的區(qū)別 海豐有多少商家沒有地圖標(biāo)注

假設(shè)postgres有數(shù)據(jù)庫testdb

在testdb下執(zhí)行下列sql語句,得到所有表的信息

select * from information_schema.columns 
where table_schema='public' and table_name>'pg_stat_statements';

在testdb下執(zhí)行下列sql語句,得到所有表的列名

select column_name from information_schema.columns 
where table_schema='public' and table_name>'pg_stat_statements';

在testdb下執(zhí)行下列sql語句,得到表"table"的列名

select column_name from information_schema.columns 
where table_schema='public' and table_name='table';

補(bǔ)充:PostgreSql數(shù)據(jù)庫查詢表信息/列信息(列ID/列名/數(shù)據(jù)類型/長度/精度/是否可以為null/默認(rèn)值/是否自增/是否是主鍵/列描述)

查詢表信息(表名/表描述)

select a.relname as name , b.description as value from pg_class a 
left join (select * from pg_description where objsubid =0 ) b on a.oid = b.objoid
where a.relname in (select tablename from pg_tables where schemaname = 'public')
order by a.relname asc

查詢列信息(列ID/列名/數(shù)據(jù)類型/長度/精度/是否可以為null/默認(rèn)值/是否自增/是否是主鍵/列描述)

select ordinal_position as Colorder,column_name as ColumnName,data_type as TypeName,
coalesce(character_maximum_length,numeric_precision,-1) as Length,numeric_scale as Scale,
case is_nullable when 'NO' then 0 else 1 end as CanNull,column_default as DefaultVal,
case when position('nextval' in column_default)>0 then 1 else 0 end as IsIdentity, 
case when b.pk_name is null then 0 else 1 end as IsPK,c.DeText
from information_schema.columns 
left join (
 select pg_attr.attname as colname,pg_constraint.conname as pk_name from pg_constraint 
 inner join pg_class on pg_constraint.conrelid = pg_class.oid 
 inner join pg_attribute pg_attr on pg_attr.attrelid = pg_class.oid and pg_attr.attnum = pg_constraint.conkey[1] 
 inner join pg_type on pg_type.oid = pg_attr.atttypid
 where pg_class.relname = '表名' and pg_constraint.contype='p' 
) b on b.colname = information_schema.columns.column_name
left join (
 select attname,description as DeText from pg_class
 left join pg_attribute pg_attr on pg_attr.attrelid= pg_class.oid
 left join pg_description pg_desc on pg_desc.objoid = pg_attr.attrelid and pg_desc.objsubid=pg_attr.attnum
 where pg_attr.attnum>0 and pg_attr.attrelid=pg_class.oid and pg_class.relname='表名'
)c on c.attname = information_schema.columns.column_name
where table_schema='public' and table_name='表名' order by ordinal_position asc

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

您可能感興趣的文章:
  • 查詢PostgreSQL占多大內(nèi)存的操作
  • Postgresql通過查詢進(jìn)行更新的操作
  • postgresql 實(shí)現(xiàn)查詢某時(shí)間區(qū)間的所有日期案例
  • PostgreSQL 分頁查詢時(shí)間的2種比較方法小結(jié)
  • postgresql通過索引優(yōu)化查詢速度操作
  • postgresql查詢鎖表以及解除鎖表操作
  • postgresql數(shù)據(jù)庫根據(jù)年月查詢出本月的所有數(shù)據(jù)操作
  • PostgreSQL 實(shí)現(xiàn)查詢表字段信息SQL腳本

標(biāo)簽:來賓 晉城 株洲 珠海 蚌埠 錦州 烏海 衡陽

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Postgresql 實(shí)現(xiàn)查詢一個(gè)表/所有表的所有列名》,本文關(guān)鍵詞  Postgresql,實(shí)現(xiàn),查詢,一個(gè),;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Postgresql 實(shí)現(xiàn)查詢一個(gè)表/所有表的所有列名》相關(guān)的同類信息!
  • 本頁收集關(guān)于Postgresql 實(shí)現(xiàn)查詢一個(gè)表/所有表的所有列名的相關(guān)信息資訊供網(wǎng)民參考!
  • 企业400电话

    智能AI客服机器人
    15000

    在线订购

    合计11份范本:公司章程+合伙协议+出资协议+合作协议+股权转让协议+增资扩股协议+股权激励+股东会决议+董事会决议

    推薦文章