• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

PostgreSQL 查找当前数据库的所有表操作

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

实现的功能类似MySQL:

show tables;

在 PostgreSQL 中需要写:

select * from pg_tables where schemaname = 'public';

返回结果类似如下:

schemaname | tablename | tableowner | tablespace | hasindexes | hasrules | hastriggers | rowsecurity 
------------+-----------+------------+------------+------------+----------+-------------+-------------
 public  | deploy | postgres |   | t   | f  | f   | f
 public  | deploy2 | postgres |   | f   | f  | f   | f
(2 rows)

补充:PostgreSql 获取所有的表、视图、字段、 主键

PostgreSQL获取数据库中所有view名 视图:

SELECT viewname FROM pg_views 
WHERE  schemaname ='public' 

postgreSQL获取数据库中所有table名 表:

SELECT tablename FROM pg_tables 
WHERE tablename NOT LIKE 'pg%' 
AND tablename NOT LIKE 'sql_%'
ORDER BY tablename;

postgreSQL获取某个表tablename 所有字段名称 , 类型,备注,是否为空 等

SELECT col_description(a.attrelid,a.attnum) as comment,pg_type.typname as typename,a.attname as name, a.attnotnull as notnull
FROM pg_class as c,pg_attribute as a inner join pg_type on pg_type.oid = a.atttypid
where c.relname = 'tablename' and a.attrelid = c.oid and a.attnum>0

postgreSQL获取某个表tablename 的主键信息

select pg_attribute.attname as colname,pg_type.typname as typename,pg_constraint.conname as pk_name from 
pg_constraint inner join pg_class 
on pg_constraint.conrelid = pg_class.oid 
inner join pg_attribute on pg_attribute.attrelid = pg_class.oid 
and pg_attribute.attnum = pg_constraint.conkey[1]
inner join pg_type on pg_type.oid = pg_attribute.atttypid
where pg_class.relname = 'tablename' 
and pg_constraint.contype='p'

以上为个人经验,希望能给大家一个参考,也希望大家多多支持极客世界。如有错误或未考虑完全的地方,望不吝赐教。


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
PostgreSQL COALESCE使用方法代码解析发布时间:2022-02-08
下一篇:
Postgresql 实现查询一个表/所有表的所有列名发布时间:2022-02-08
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap