介绍
怎么在postgresql中获取所有的表名和字段名?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。
<强>获取表名及注释:强>
select relname as tabname,演员(obj_description (relfilenode & # 39; pg_class& # 39;), as varchar), as comment 得到pg_class c where relkind =, & # 39; " # 39;,以及relname not like & # 39; pg_ % & # 39;,以及relname not like & # 39; sql_ % & # 39;, order by relname
<>强过滤掉分表:强>
加条件<代码>和relchecks=0> 代码即可
<强>获取字段名、类型,注释,是否为空:强>
SELECT col_description (a.attrelid a.attnum), as 评论,format_type (a.atttypid a.atttypmod), as 类型,a.attname as 名字,,a.attnotnull as notnull 得到pg_class as c, pg_attribute as a where c.relname =, & # 39;表名& # 39;,以及a.attrelid =, c.oid 以及a.attnum> 0
<强>补充:PostgreSQL查询表主键及注释内容强>
网上关于pgSql获取表主键的内容都是千篇一律,并且对于存在多主键的场景不支持。
附上测试后可获取多个主键字段值的SQL
选择 ,string_agg (DISTINCT t3.attname & # 39; & # 39;), AS primaryKeyColumn ,,t4.tablename AS 表名 ,,string_agg (cast (obj_description (relfilenode & # 39; pg_class& # 39;), as varchar), & # 39; & # 39;), as 发表评论 从 pg_constraint t1 ,INNER JOIN  pg_class t2 提醒t1.conrelid =t2.oid ,INNER JOIN  pg_attribute t3 提醒t3.attrelid =, t2.oid 以及array_position (t1.conkey t3.attnum), is not null ,INNER JOIN  pg_tables t4 提醒t4.tablename =t2.relname ,INNER JOIN  pg_index t5 提醒t5.indrelid =, t2.oid 以及t3.attnum =, ANY (t5.indkey) ,LEFT JOIN  pg_description t6 提醒t6.objoid=t3.attrelid 以及t6.objsubid=t3.attnum WHERE t1.contype =, & # 39; p # 39; ,,以及长度(t3.attname),在0 以及才能t2.oid =, & # 39;表名& # 39;,::regclass group by t4。表
看完上述内容,你们掌握怎么在postgresql中获取所有的表名和字段名的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注行业资讯频道,感谢各位的阅读!