postgresql 实现获取所有表名,字段名,字段类型,注释
导读:收集整理的这篇文章主要介绍了postgresql 实现获取所有表名,字段名,字段类型,注释,觉得挺不错的,现在分享给大家,也给大家做个参考。 获取表名及注释:select relname...
收集整理的这篇文章主要介绍了postgresql 实现获取所有表名,字段名,字段类型,注释,觉得挺不错的,现在分享给大家,也给大家做个参考。 获取表名及注释:
select relname as tabname,cast(obj_description(relfilenode,'pg_class') as vArchar) as comment From pg_class c where relkind = 'r' and relname not like 'pg_%' and relname not like 'SQL_%' order by relname
过滤掉分表:
加条件 and relchecks=0 即可
获取字段名、类型、注释、是否为空:
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补充:PostgreSQL查询表主键及注释内容
网上关于pgSql获取表主键的内容都是千篇一律,并且对于存在多主键的场景不支持。
附上测试后可获取多个主键字段值的SQL
SELECT string_agg(DISTINCT t3.attname,',') AS PrimaryKeyColumn ,t4.tablename AS tableName , string_agg(cast(obj_description(relfilenode,'pg_class') as VARchar),'') as commentFROM pg_constraint t1 INNER JOIN pg_class t2 ON t1.conrelid = t2.oid INNER JOIN pg_attribute t3 ON t3.attrelid = t2.oid AND array_posITion(t1.conkey,t3.attnum) is not null INNER JOIN pg_tables t4 on t4.tablename = t2.relname INNER JOIN pg_index t5 ON t5.indrelid = t2.oid AND t3.attnum = ANY (t5.indkey) LEFT JOIN pg_description t6 on t6.objoid=t3.attrelid and t6.objsubid=t3.attnumWHERE t1.contype = 'p' AND length(t3.attname) >
0 AND t2.oid = '表名' :: regclass group by t4.tablename目前只找到了获取指定表的主键信息,对于批量获取没有找到。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
您可能感兴趣的文章:- postgresql varchar字段regexp_replace正则替换操作
- 解决postgresql表中的字段名称包含特殊符号的问题
- PostgreSQL 实现查询表字段信息SQL脚本
- PostgreSQL 更新JSON,JSONB字段的操作
- postgresql 修改字段长度的操作
- postgresql 补齐空值、自定义查询字段并赋值操作
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: postgresql 实现获取所有表名,字段名,字段类型,注释
本文地址: https://pptw.com/jishu/633156.html
