基于postgresql查询某个字段属于哪个表的说明

时间:2021-05-23

一、场景还原

前端小林问我,它从前端的返回的某些字段,想知道那个表有这个字段,它想操作这个表……

二、sql语句

SELECT *FROM information_schema. COLUMNSWHERE COLUMN_NAME = 'storename';SELECT c.relname AS "表名",a.attname AS "字段名",c.oidFROM pg_class c LEFT JOIN pg_attribute a ON a.attrelid = c.oidWHERE c.relname NOT LIKE 'pg_%'-- AND c.relname = 'kx_store'AND a.attname = 'storename';

补充:postgresql数据库查询所有字段详细信息,包括描述

把里边的public改成相应模式名(默认为public,可不改)

table_name改成对应表名称

SELECT AA.*,BB.DESC FROM (SELECTA.ORDINAL_POSITION AS COL_NUM,A.COLUMN_NAME AS COL_NAME,A.DATA_TYPE AS COL_TYPE,A.CHARACTER_MAXIMUM_LENGTH AS COL_LEN,A.NUMERIC_PRECISION AS COL_PREC,A.DATETIME_PRECISION AS COL_TIME_PREC,A.IS_NULLABLE AS COL_NULLABLE,A.COLUMN_DEFAULT AS COL_DEFAULTFROM INFORMATION_SCHEMA.COLUMNS AWHERE A.TABLE_SCHEMA = 'public' AND A.TABLE_NAME = 'table_name' ) AS AA LEFT JOIN (SELECTOBJSUBID,DESCRIPTION AS DESCFROM PG_DESCRIPTIONWHERE OBJOID =( SELECT RELID FROM PG_STAT_ALL_TABLES WHERE SCHEMANAME = 'public' AND RELNAME = 'table_name') ) AS BB ON AA.COL_NUM = BB.OBJSUBID ORDER BY AA.COL_NUM;

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

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章