首页 > 数据库 > PostgreSQL

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

admin PostgreSQL 2022-02-10 16:13:20 postgresql   查询某个字段"

一、场景还原

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

二、sql语句

SELECT *
FROM information_schema. COLUMNS
WHERE COLUMN_NAME = 'storename';

SELECT c.relname AS "表名",a.attname AS "字段名",c.oid
FROM pg_class c 
LEFT JOIN pg_attribute a ON a.attrelid = c.oid
WHERE c.relname NOT LIKE 'pg_%'
-- AND c.relname = 'kx_store'
AND a.attname = 'storename';

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

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

table_name改成对应表名称

SELECT AA.*,BB.DESC FROM
 (
SELECT
A.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_DEFAULT
FROM INFORMATION_SCHEMA.COLUMNS A
WHERE A.TABLE_SCHEMA = 'public' AND A.TABLE_NAME = 'table_name'
 )
 AS AA LEFT JOIN
 (
SELECT
OBJSUBID,
DESCRIPTION AS DESC
FROM PG_DESCRIPTION
WHERE 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;

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

版权声明

本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。
本文地址:/shujuku/PostgreSQL/106536.html

留言与评论(共有 0 条评论)
   
验证码:

潘少俊衡

| 桂ICP备2023010378号-4

Powered By EmpireCMS

爱享小站

中德益农

谷姐神农

环亚肥料

使用手机软件扫描微信二维码

关注我们可获取更多热点资讯

感谢潘少俊衡友情技术支持