PostgreSQL 查询数据表、视图信息

复制代码
--获得指定schema范围内的所有表和视图的列表,可指定一个排除表前缀模式
复制代码
with param as (select 'public,iit' as schema_name,'db2g%' as exclude_pattern)
    ,base_info as (
        --获得所有基表
        select pg_namespace.nspname as schema_name, a.relname as tbl_name ,'TBL' as tbl_type, b.description as tbl_comment
        from pg_class a
            join pg_namespace on a.relnamespace=pg_namespace.oid
            left join (select * from pg_description where objsubid =0 ) b
                on a.oid = b.objoid
        where a.relkind='r'
             and a.relname not like (select exclude_pattern from param)
             and pg_namespace.nspname in (select regexp_split_to_table(schema_name,',') from param)
        union all
        ---获取所有视图
        SELECT schemaname as schema_name, viewname as tbl_name,'VW' as tbl_type, null as tbl_comment
        FROM pg_views
        WHERE schemaname in (select regexp_split_to_table(schema_name,',') from param)
    )
select * from base_info;
复制代码
--获得指定schema范围内的所有表和视图的数据列信息,可指定一个排除表前缀模式
复制代码
with param as (select 'public,iit' as schema_name,'db2g%' as exclude_pattern),
     base_info as (
         select table_schema
              , table_name
              , ordinal_position                                                      as Colorder
              , column_name                                                           as ColumnName
              , data_type                                                             as TypeName
              , coalesce(character_maximum_length, numeric_precision, -1)             as Length
              , numeric_scale                                                         as Scale
              , case is_nullable when 'NO' then 0 else 1 end                          as CanNull
              , column_default                                                        as DefaultVal
              , case when position('nextval' in column_default) > 0 then 1 else 0 end as IsIdentity
              , case when b.pk_name is null then 0 else 1 end                         as IsPK
              , c.DeText
         from information_schema.columns
                  left join (
                             select pg_attr.attname as colname
                                    ,pg_constraint.conname as pk_name
                                    ,pg_class.relname as tblname
                                    ,pg_namespace.nspname as schema_name
                             from pg_constraint
                                      join pg_class on pg_constraint.conrelid = pg_class.oid
                                      join pg_attribute pg_attr on pg_attr.attrelid = pg_class.oid
                                            and pg_attr.attnum = pg_constraint.conkey[1]
                                      join pg_type on pg_type.oid = pg_attr.atttypid
                                      join pg_namespace on pg_constraint.connamespace=pg_namespace.oid
                             where pg_constraint.contype = 'p'
                            ) b on b.colname = information_schema.columns.column_name
                                    and b.tblname=information_schema.columns.table_name
                                    and b.schema_name=information_schema.columns.table_schema
                  left join (
                             select attname, description as DeText, pg_class.relname as tblname,pg_namespace.nspname as schema_name
                             from pg_class
                                      join pg_namespace on pg_class.relnamespace=pg_namespace.oid
                                      left join pg_attribute pg_attr on pg_attr.attrelid = pg_class.oid
                                      left join pg_description pg_desc on pg_desc.objoid = pg_attr.attrelid
                                 and pg_desc.objsubid = pg_attr.attnum
                             where pg_attr.attnum > 0
                               and pg_attr.attrelid = pg_class.oid
                         ) c on c.attname = information_schema.columns.column_name
                                and c.tblname=information_schema.columns.table_name
                                and c.schema_name=information_schema.columns.table_schema
         where table_schema in (select regexp_split_to_table(schema_name,',') from param)
              and table_name not like (select exclude_pattern from param)
         order by table_name,ordinal_position
     )
select * from base_info;
复制代码
--查询指定模式下的表和视图

with param as (select 'public' as schema_name,'db2g%' as exclude_pattern)
--获得所有基表
select a.relname as tbl_name ,'TBL' as tbl_type, b.description as tbl_comment
from pg_class a
    left join (select *
                from pg_description where objsubid =0 ) b
        on a.oid = b.objoid
where a.relname in (select tablename
                    from pg_tables
                    where schemaname = (select schema_name from param))
     and a.relname not like (select exclude_pattern from param)
union all
---获取所有视图
SELECT viewname as tbl_name,'VW' as tbl_type, null as tbl_comment
FROM pg_views
WHERE schemaname =(select schema_name from param)
order by tbl_name asc;

--查询指定数据基表的列信息

复制代码
with param as (select 'emp' as tblname),
     base_info as (
         select ordinal_position                                                      as Colorder
              , column_name                                                           as ColumnName
              , data_type                                                             as TypeName
              , coalesce(character_maximum_length, numeric_precision, -1)             as Length
              , numeric_scale                                                         as Scale
              , case is_nullable when 'NO' then 0 else 1 end                          as CanNull
              , column_default                                                        as DefaultVal
              , case when position('nextval' in column_default) > 0 then 1 else 0 end as IsIdentity
              , case when b.pk_name is null then 0 else 1 end                         as IsPK
              , c.DeText
         from information_schema.columns
                  left join (
                             select pg_attr.attname as colname, pg_constraint.conname as pk_name
                             from pg_constraint
                                      join pg_class on pg_constraint.conrelid = pg_class.oid
                                      join pg_attribute pg_attr on pg_attr.attrelid = pg_class.oid
                                 and pg_attr.attnum = pg_constraint.conkey[1]
                                      join pg_type on pg_type.oid = pg_attr.atttypid
                             where pg_class.relname = (select tblname from param)
                               and pg_constraint.contype = 'p'
                            ) b on b.colname = information_schema.columns.column_name
                  left join (
             select attname, description as DeText
             from pg_class
                      left join pg_attribute pg_attr on pg_attr.attrelid = pg_class.oid
                      left join pg_description pg_desc on pg_desc.objoid = pg_attr.attrelid
                 and pg_desc.objsubid = pg_attr.attnum
             where pg_attr.attnum > 0
               and pg_attr.attrelid = pg_class.oid
               and pg_class.relname = (select tblname from param)
         ) c on c.attname = information_schema.columns.column_name
         where table_schema = 'public'
           and table_name = (select tblname from param)
         order by ordinal_position asc
     )
select * from base_info
相关推荐
小蒜学长28 分钟前
springboot餐厅信息管理系统设计(代码+数据库+LW)
java·数据库·spring boot·后端
Justin_1931 分钟前
mysql数据库高级特性(一)
数据库·mysql
邂逅you1 小时前
用python操作mysql之pymysql库基本操作
数据库·python·mysql
心 一1 小时前
接口安全测试实战:从数据库错误泄露看如何构建安全防线
数据库·安全
点灯小铭1 小时前
基于单片机的PID调节脉动真空灭菌器上位机远程监控设计
数据库·单片机·嵌入式硬件·毕业设计·课程设计
小高Baby@1 小时前
Redis Key的设计
数据库·redis·缓存
白鲸开源2 小时前
最佳实践:基于Apache SeaTunnel从MySQL同步到PostgreSQL
大数据·mysql·postgresql
q_19132846952 小时前
基于RuoYi框架+Mysql的汽车进销存后台管理系统
数据库·vue.js·spring boot·mysql·汽车·个人开发·若依
wuyunhang1234563 小时前
MySQL----锁
数据库·mysql
悟能不能悟3 小时前
springboot在DTO使用service,怎么写
java·数据库·spring boot