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
相关推荐
FIN技术铺1 小时前
问:数据库的六种锁机制实践总结?
数据库·sql·oracle
程序员劝退师_2 小时前
优惠券秒杀的背后原理
java·数据库
Gauss松鼠会2 小时前
GaussDB全密态数据库等值查询
数据库·oracle·gaussdb
JSUITDLWXL2 小时前
在Oracle数据中更新整个对象和更新对象的某几个字段时,他们的锁是相同的吗
数据库·oracle
杏花春雨江南2 小时前
ddl/dml/dcl
数据库·oracle
Matrix702 小时前
HBase理论_HBase架构组件介绍
大数据·数据库·hbase
不太灵光的程序员2 小时前
【HBase分布式数据库】第七章 数据的导入导出 importtsv导入数据
数据库·分布式·hbase
Mephisto.java2 小时前
【大数据学习 | HBASE高级】region split机制和策略
数据库·学习·hbase
Lucifer三思而后行2 小时前
YashanDB YAC 入门指南与技术详解
数据库·后端
大气层煮月亮2 小时前
python调用MySql详细步骤
数据库·mysql