当前位置: 代码网 > it编程>数据库>MsSqlserver > PostgreSQL如何查询表结构和索引信息

PostgreSQL如何查询表结构和索引信息

2024年12月29日 MsSqlserver 我要评论
前言postgresql的表一般都是建立在public这个schema下的,假如现在有个数据表t_student,可以用以下几种方式来查询表结构和索引信息。使用\d元命令查看表字段信息和索引信息在cm

前言

postgresql的表一般都是建立在public这个schema下的,

假如现在有个数据表t_student,可以用以下几种方式来查询表结构和索引信息。

使用\d元命令查看表字段信息和索引信息

在cmd界面使用psql连接db后

输入\d加上表名即可:

\d t_student

通过系统数据字典查询表结构

select
col.table_schema,
col.table_name,
col.ordinal_position,
col.column_name,
col.data_type,
col.character_maximum_length,
col.numeric_precision,
col.numeric_scale,
col.is_nullable,
col.column_default,
des.description
from
information_schema.columns col left join pg_description des on
col.table_name::regclass = des.objoid
and col.ordinal_position = des.objsubid
where
table_schema = 'public'
and table_name = 't_student'
order by
ordinal_position;

或者简单点:

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

通过系统数据字典查询索引信息

select
a.schemaname,
a.tablename,
a.indexname,
a.tablespace,
a.indexdef,
b.amname,
c.indexrelid,
c.indnatts,
c.indisunique,
c.indisprimary,
c.indisclustered,
d.description
from
pg_am b left join pg_class f on
b.oid = f.relam left join pg_stat_all_indexes e on
f.oid = e.indexrelid left join pg_index c on
e.indexrelid = c.indexrelid left outer join pg_description d on
c.indexrelid = d.objoid,
pg_indexes a
where
a.schemaname = e.schemaname
and a.tablename = e.relname
and a.indexname = e.indexrelname
and e.schemaname = 'public'
and e.relname = 't_student';

查询所有的表名

select
n.nspname,
relname
from
pg_class c,
pg_namespace n
where
c.relnamespace = n.oid
and nspname = 'public'
and relkind = 'r'
order by
relname;

可视化工具dbeaver

对于上述的sql语句只需要修改要查询的table name,可以根据需要自行修改想要查询的column。

如果是通过dbeaver来连接数据库,还可以直接在当前的数据库实例下打开schema里的public选项,接着选中table,选中你想查看的表,可以很直观地看到该表的各种信息:column、index等等。

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com