承接国内外服务器租用托管、定制开发、网站代运营、网站seo优化托管接单、网站代更新,新老站点皆可!!咨询QQ:3787320601

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

管理员 2023-06-26 08:28:42 互联网圈 9 ℃ 0 评论 2385字 收藏

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

实现的功能类似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’

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有毛病或未斟酌完全的地方,望不吝赐教。

文章来源:丸子建站

文章标题:PostgreSQL 查找当前数据库的所有表操作

https://www.wanzijz.com/view/59267.html

相关文章

Related articles

X

截屏,微信识别二维码

微信号:weimawl

(点击微信号复制,添加好友)

打开微信