PG数据库中查询数据表字段
select
distinct c.relname as table_name,
cast(obj_description(c.oid) as varchar) as table_desc,
a.attnum as field_seq,
a.attname as field_name,
d.description as field_desc,
concat_ws('', t.typname, replace(substring(format_type(a.atttypid, a.atttypmod) from '\(.*\)'), ',0', '')) as field_type,
case
when a.attnum = any(con.conkey) then '是'
end as is_pk,
case
when array_position(con.conkey, a.attnum) is not null then concat(array_position(con.conkey, a.attnum))
end as at_pk_index,
con.conkey as pk_field_seqs,
case
when a.attnotnull is true then '是'
else '否'
end as not_null,
case
when position('::' in col.column_default) > 0 then replace(substring(col.column_default from '.*::'), '::', '')
else col.column_default
end as default_value,
col.is_identity as is_identity
from
pg_class c left join pg_constraint con
on con.conrelid = c."oid" and con.contype = 'p',
pg_type t,
information_schema."columns" col,
pg_attribute a left join pg_description d
on d.objoid = a.attrelid and d.objsubid = a.attnum
where
a.attnum > 0
and a.attrelid = c.oid
and a.atttypid = t.oid
and col.table_name = c.relname
and col.column_name = a.attname
and col.table_schema = '{schema_name}'
-- 过滤主键字段
and a.attnum = any(con.conkey)
-- and c.relname = '{table_name}'
order by
at_pk_index,
field_seq;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· .NET Core 中如何实现缓存的预热?
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
· 【译】Visual Studio 中新的强大生产力特性