首页 > 数据库 >【openGauss、PostgreSQL】openGauss、PostgreSQL数据库通用查表字段信息脚本-v20240620-2216

【openGauss、PostgreSQL】openGauss、PostgreSQL数据库通用查表字段信息脚本-v20240620-2216

时间:2024-06-23 21:57:22浏览次数:3  
标签:PostgreSQL 2216 data when table openGauss type ic name

【openGauss、PostgreSQL】openGauss、PostgreSQL数据库通用查表字段信息脚本-v20240620-2216


openGauss、PostgreSQL数据库通用查表字段信息脚本-v20240620-2216

此脚本,openGauss、PostgreSQL都可执行

/* openGauss、PostgreSQL数据库通用查表字段信息脚本-v20240619-1503 */
drop view if exists tab_info_v;
create view tab_info_v as 
select case
         when substr(version(), 0, 1) = '(' 
           then substring(substr(version(), 2, length(version())) from '^[^ ]+')
         else substring(version() from '^[^ ]+')
       end as database_type
      ,ic.table_schema as schema
      ,ic.table_name
      -- ,c.oid
      ,tab_com.description as table_comment
      ,ic.ordinal_position as column_num
      ,ic.column_name
      ,case
         when ic.data_type = 'bigint' 
           then 'INT8'
         when ic.data_type in ('nvarchar2') 
           then upper(ic.data_type) || '(' || ic.character_maximum_length || ')'
         when ic.data_type in ('character varying') 
           then 'VARCHAR(' || ic.character_maximum_length || ')'
         when ic.data_type = 'timestamp without time zone'
           then 'TIMESTAMP(' || ic.datetime_precision || ')'
         when ic.data_type = 'time without time zone'
           then 'TIME(' || ic.datetime_precision || ')'
         when ic.data_type = 'numeric' 
           then 'NUMERIC(' || ic.numeric_precision || ',' || ic.numeric_scale || ')'
         when ic.data_type = 'text' 
           then 'TEXT'
         when ic.data_type = 'date' 
           then 'DATE'
         else ic.data_type
       end as data_type
      ,col_com.description as column_comment
      ,ic.column_default
      ,ic.is_nullable
      -- 查表的主键详情
      /* 
      ,case when (SELECT count(*)
                    FROM information_schema.table_constraints AS tc
                    JOIN information_schema.key_column_usage  AS kcu 
                      ON tc.constraint_name = kcu.constraint_name
                   WHERE tc.constraint_type = 'PRIMARY KEY'
                     AND tc.table_name      = ic.table_name
                     and kcu.column_name    = ic.column_name
                     ) > 0 then 'Y' 
         else NULL::text
       END AS pkey
       */
      ,case 
         when pc.conname is null then null::text
         else 'Y'
       end as pkey
  from information_schema.columns ic
  join pg_class c
    on ic.table_name = c.relname
  join pg_namespace n
    on c.relnamespace = n.oid
  left join pg_description tab_com
    on tab_com.objoid = c.oid
   and tab_com.objsubid = 0
  left join pg_description col_com
    on col_com.objoid = c.oid
   and col_com.objsubid = ic.ordinal_position
  left join (
    SELECT conname, conrelid , unnest(conkey) as column_num
      FROM pg_constraint) as pc
    on pc.conrelid = c.oid
   and pc.column_num = ic.ordinal_position
 where ic.table_catalog = CURRENT_CATALOG
   and ic.table_schema = CURRENT_SCHEMA
   and ic.table_name !~ '^act\_'
   and n.nspname = CURRENT_USER
   and c.relkind in ('r','p')
   -- 把分区表过滤掉
   -- 查结尾不是"p+4位数字"的,例如:p2023
   and ic.table_name !~* '^.*[p][0-9]{4}$'
   -- 查结尾不是"p+5位数字"的,例如:p20231
   and ic.table_name !~* '^.*[p][0-9]{5}$'
   -- 查结尾6位字符不是"others"的
   and ic.table_name !~* '^.*[others]{6}$'
   -- 查结尾5位字符不是"other"的
   and ic.table_name !~* '^.*[other]{5}$'
   -- 查结尾不是数字或者不是_bak的表名
   and (ic.table_name !~ '^.*[0-9]{1}.*$' and ic.table_name !~ '^.*\_bak$')
 order by ic.table_name, ic.ordinal_position;

-- select * from tab_info_v;

标签:PostgreSQL,2216,data,when,table,openGauss,type,ic,name
From: https://blog.csdn.net/tttzzzqqq2018/article/details/139745700

相关文章

  • python连接mysql、sqlserver、oracle、postgresql数据库进行封装
    python连接mysql、sqlserver、oracle、postgresql数据库进行封装python连接mysql、sqlserver、oracle、postgresql数据库进行封装详解一、引言二、python连接MySQL数据库进行封装三、python连接SQLServer数据库进行封装四、Python连接Oracle数据库进行封装五、Python连......
  • PostgreSQL——入门到精通(小白必看)
    PostgreSQL是一个高度可扩展的开源对象关系数据库管理系统(ORDBMS),它以其稳定性、强大的功能和对SQL标准的严格遵守而闻名。第一部分:PostgreSQL简介和安装1.1PostgreSQL概述定义:PostgreSQL是一个高度可扩展的开源对象关系数据库系统,支持丰富的数据类型和并发控制机制。优势:强......
  • 探索PostgreSQL的JSON宝石:深入掌握JSON数据处理
    探索PostgreSQL的JSON宝石:深入掌握JSON数据处理引言在数据驱动的世界中,JSON已成为数据交换的事实标准。PostgreSQL,作为一款领先的关系型数据库管理系统,通过其强大的JSON支持,为开发者提供了丰富的工具来存储、查询和处理JSON数据。本文将深入探讨PostgreSQL中的JSON特性,引......
  • 【机器学习】在【R语言】中的应用:结合【PostgreSQL数据库】的【金融行业信用评分模型
    目录1.数据库和数据集的选择1.准备工作2.PostgreSQL安装与配置3.R和RStudio安装与配置2.数据导入和预处理1.连接数据库并导入数据1.连接数据库2.数据检查和清洗1.数据标准化2.拆分训练集和测试集3.特征工程1.生成新特征2.特征选择4.模型训练和评估1.逻辑回归2.......
  • PostgreSQL的存储结构介绍
    PostgreSQL的存储结构介绍PostgreSQL是一个先进的开源关系型数据库管理系统,其存储结构设计非常复杂且高效。了解其存储结构有助于数据库管理员和开发人员更好地优化数据库的性能、设计数据模型以及进行故障排除。以下是PostgreSQL存储结构的详细概述。1.数据文件结构......
  • PostgreSQL中 FETCH FIRST ... WITH TIES 是查询结果中限制返回的行数
    在PostgreSQL中,FETCHFIRST…WITHTIES是一个在查询结果中限制返回的行数,但同时确保与最后一行具有相同排序值的所有行都被包括进来的子句。这通常与ORDERBY子句一起使用。当您使用FETCHFIRSTnROWSONLY时,您只会得到前n个排序后的行。但是,如果您希望包括与......
  • 全是坑!!从 MySQL 到 PostgreSQL【送源码】
    0、前言原项目框架SpringBoot+ MybatisPlus +Mysql1、切换流程1.1、项目引入postgresql驱动包由于我们要连接新的数据库,理所当然的要引入该数据库的驱动包,这与mysql驱动包类似<dependency>    <groupId>org.postgresql</groupId>    <artifactId>postgresq......
  • postgresql简单使用
    postgresql.conf#系统、数据库配置文件pg_hha.conf#客户机连接文件pg_ctl和pgsql一、pg_ctl服务管理命令pg_ctl是PostgreSQL提供的控制服务启动、停止、重启等操作的命令行工具。常用选项和示例命令 选项 描述 示例启动PostgreSQL服务 start 启动数据库服......
  • 安装openGauss操作步骤
    操作步骤以root或普通用户登录待安装openGauss的任意主机,并按规划创建存放安装包的目录。mkdir-p/opt/software/openGauss 说明:不建议把安装包的存放目录规划到openGauss用户的根目录或其子目录下,可能导致权限问题。2.将安装包“openGauss-x.x.x-openEuler-64bit-......
  • 为了保证openGauss的正确安装,请首先对主机环境进行配置
    初始化安装环境为了保证openGauss的正确安装,请首先对主机环境进行配置。准备安装用户及环境手工建立互信配置操作系统参数准备安装用户及环境创建完openGauss配置文件后,在执行安装前,为了后续能以最小权限进行安装及openGauss管理操作,保证系统安全性,需要运行安装前置脚本gs_......