主页 > 知识库 > pgsql 实现用户自定义表结构信息获取

pgsql 实现用户自定义表结构信息获取

热门标签:办公外呼电话系统 美容工作室地图标注 地图标注和图片名称的区别 海丰有多少商家没有地图标注 外呼调研系统 合肥公司外呼系统运营商 打电话智能电销机器人授权 漯河外呼电话系统 重庆自动外呼系统定制

1. 获取表中普通信息:如字段名,字段类型等

SELECT column_name, data_type, ordinal_position, is_nullable 
FROM information_schema."columns"
WHERE "table_name"='TABLE-NAME' -- 将 'TABLE-NAME' 换成自己的表

2.获取所有的表和视图

SELECT table_name, table_type FROM INFORMATION_SCHEMA.tables WHERE table_schema='public' AND table_type IN ('BASE TABLE','VIEW')

3.获取约束注释

SELECT obj_description(oid, 'pg_constraint') AS d FROM pg_constraint WHERE conname = constraint_name;

4.获取表的约束

-- conname 约束名称
-- contype 约束类型(p:主键, f:外键, c: 检查约束, u:唯一约束)
-- conkey 约束字段
-- confkey 外键字段
-- consrc 检查约束条件
-- confreltable 外键字段引用的表
SELECT conname, contype, conkey, consrc, 
  (SELECT array_agg(column_name::text) FROM INFORMATION_SCHEMA.COLUMNS WHERE ordinal_position = any(conkey) AND table_name= 'TABLE-NAME') AS conkey, 
  (SELECT column_name FROM INFORMATION_SCHEMA.COLUMNS WHERE ordinal_position = any(confkey) AND table_name='TABLE-NAME') AS confkey, 
  (SELECT relname FROM pg_class WHERE oid = confrelid) AS confreltable
FROM pg_constraint WHERE conrelid=(SELECT oid FROM pg_class WHERE relname ='TABLE-NAME'); -- 将 'TABLE-NAME' 换成自己的表

5.获取表的触发器

SELECT trigger_name, event_manipulation, event_object_table, action_statement, action_orientation, action_timing FROM INFORMATION_SCHEMA.TRIGGERS;

6.获取字段的注释

--table_oid 表的oid
--col_position 字段的位置
SELECT col_description(table_oid, col_position);

补充:查询PostgreSQL库中所有表的表结构信息SQL

我就废话不多说了,大家还是直接看代码吧~

select
(select relname as comment from pg_class where oid=a.attrelid) as table_name,
 row_number() over(partition by (select relname as comment from pg_class where oid=a.attrelid) order by a.attnum),
a.attname as column_name,
format_type(a.atttypid,a.atttypmod) as data_type,
(case when atttypmod-4>0 then atttypmod-4 else 0 end)data_length,
(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='p')>0 then '是' else '否' end) as 主键约束,
(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='u')>0 then '是' else '否' end) as 唯一约束,
(case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='f')>0 then '是' else '否' end) as 外键约束,
(case when a.attnotnull=true then '是' else '否' end) as nullable,
col_description(a.attrelid,a.attnum) as comment
from pg_attribute a
where attstattarget=-1 and attrelid in (select oid from pg_class where relname in(select relname from pg_class where relkind ='r' and relname 
in 
(select tablename from pg_tables where tablename not like 'pg_%' and tablename not like 'sql_%' and schemaname not in(XXXX) and tablename not in(XXXX)
))
order by table_name,a.attnum;

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

您可能感兴趣的文章:
  • pgsql之pg_stat_replication的使用详解
  • pgsql 如何删除仍有活动链接的数据库
  • pgsql的UUID生成函数实例
  • pgsql 如何手动触发归档
  • pgsql锁表后kill进程的操作
  • PGSQL 实现把字符串转换成double类型(to_number())
  • pgsql之create user与create role的区别介绍

标签:株洲 来宾 珠海 锦州 乌海 衡阳 蚌埠 晋城

巨人网络通讯声明:本文标题《pgsql 实现用户自定义表结构信息获取》,本文关键词  pgsql,实现,用户,自定义,;如发现本文内容存在版权问题,烦请提供相关信息告之我们,我们将及时沟通与处理。本站内容系统采集于网络,涉及言论、版权与本站无关。
  • 相关文章
  • 下面列出与本文章《pgsql 实现用户自定义表结构信息获取》相关的同类信息!
  • 本页收集关于pgsql 实现用户自定义表结构信息获取的相关信息资讯供网民参考!
  • 推荐文章