Postgresql 表结构、列名相关信息查询

查询数据库拥有的表数量

sql 复制代码
select count(*)
from pg_class c join pg_namespace n on relnamespace = n.oid where relkind = 'r' and n.nspname = 'public' ;

查询所有表名及注释

sql 复制代码
-- 方法一
select relname, reltuples as rw,obj_description(c.oid)
from pg_class c join pg_namespace n on relnamespace = n.oid where relkind = 'r' and n.nspname = 'public';

-- 方法二 加条件 and relchecks=0是过滤掉分表
SELECT relname as tabname,
    cast(obj_description(relfilenode,'pg_class') as varchar) as comment 
FROM pg_class c 
WHERE relchecks=0 
AND relkind = 'r' 
AND relname not like 'pg_%' 
AND relname not like 'sql_%'
order by relname;

查询字段名、类型、注释、是否为空

sql 复制代码
SELECT 
    col_description(a.attrelid,a.attnum) as comment,
    format_type(a.atttypid,a.atttypmod) as type,
    a.attname as name, 
    a.attnotnull as notnull   
FROM pg_class as c,pg_attribute as a 
where c.relname = '表名' 
and a.attrelid = c.oid 
and a.attnum>0;

查询表字段(详细版):列名、备注、类型、是否为空、长度、是否为主键

sql 复制代码
select a.attname as colname,	col_description(a.attrelid,a.attnum) as remarks,t.typname,a.attnotnull as notnull ,
(case when atttypmod-4>0 then atttypmod-4 else 0 end) len,
(case  when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='p')>0  
	then 'Y' else 'N' end) as primarykey
from pg_attribute a left join pg_class c on a.attrelid = c.oid
left join pg_type t on a.atttypid = t.oid
where	a.attnum >= 0 and c.relname = '表名'
order by c.relname desc,	a.attnum asc

查询表和表字段(表名和列的详细信息的融合)

sql 复制代码
select ROW_NUMBER() over(ORDER bY c.relname ,	a.attnum asc ) AS	 num,
 c.relname as 英文表名,
 de.description  as 中文表名,
a.attname as 英文字段名称,	
col_description(a.attrelid,a.attnum) as 中文字段名称,
t.typname 字段类型,
(case when atttypmod-4>0 then atttypmod-4 else 0 end) 字段长度,
'0' 字段小数位数,
(case when a.attnotnull='t' THEN	'是' ELSE	'否' END )  字段是否非空,
(case  when (select count(*) from pg_constraint where conrelid = a.attrelid 
and conkey[1]=attnum and contype='p')>0  	then '是' else '否' end)  字段是否主键,
'该字段为'||col_description(a.attrelid,a.attnum) as 字段描述
from pg_attribute a 
left join pg_class c on a.attrelid = c.oid
left join pg_type t on a.atttypid = t.oid
left outer join pg_namespace pn on c.relnamespace = pn.oid
left join ( select pd.*
    from pg_description pd
    where 1=1
        and pd.objsubid = 0 ) de on c.oid = de.objoid 
where	a.attnum >= 0 
and relchecks=0 
and relkind in ('r','v','m','f','p')
and pn.nspname not in ('pg_catalog','information_schema') 
        and pn.nspname not like 'pg_toast%'
order by c.relname ,	a.attnum asc;
相关推荐
devmoon14 分钟前
在 Polkadot Runtime 中添加多个 Pallet 实例实战指南
java·开发语言·数据库·web3·区块链·波卡
认真的薛薛24 分钟前
数据库-sql语句
数据库·sql·oracle
爱学英语的程序员34 分钟前
面试官:你了解过哪些数据库?
java·数据库·spring boot·sql·mysql·mybatis
·云扬·2 小时前
MySQL Redo Log落盘机制深度解析
数据库·mysql
用户982863025682 小时前
pg内核实现细节
数据库
飞升不如收破烂~2 小时前
Redis 分布式锁+接口幂等性使用+当下流行的限流方案「落地实操」+用户连续点击两下按钮的解决方案自用总结
数据库·redis·分布式
workflower2 小时前
业务需求-假设场景
java·数据库·测试用例·集成测试·需求分析·模块测试·软件需求
亓才孓2 小时前
[JDBC]基于三层架构和MVC架构的JDBCTools
数据库
IT邦德2 小时前
RPM包快速安装Oracle26ai
数据库·oracle
Dovis(誓平步青云)2 小时前
《滑动窗口算法:从 “暴力遍历” 到 “线性高效” 的思维跃迁》
运维·服务器·数据库·算法