美文网首页
pg 查询指定表元数据(表结构、字段、数据类型、主键、非空、默认

pg 查询指定表元数据(表结构、字段、数据类型、主键、非空、默认

作者: 花雨归来 | 来源:发表于2022-12-22 02:09 被阅读0次

注:array_position函数在postgresql 9.5版本及以后才有。

中文版(适用初学)

select
    c.relname as 表名,
    cast(obj_description(c.oid) as varchar) as 表注释,
    a.attnum as 字段序号,
    a.attname as 字段名,
    d.description as 字段注释,
    concat_ws('', t.typname, replace(substring(format_type(a.atttypid, a.atttypmod) from '\(.*\)'), ',0', '')) as 字段类型,
    case
        when a.attnum = any(con.conkey) then '是'
    end as 是否主键,
    case
        when array_position(con.conkey, a.attnum) is not null then concat(array_position(con.conkey, a.attnum))
    end as 主键位置,
    con.conkey 主键所在字段序号,
    case
        when a.attnotnull is true then '是'
        else '否'
    end as 非空,
    case
        when position('::' in col.column_default) > 0 then replace(substring(col.column_default from '.*::'), '::', '')
        else col.column_default
    end as 默认值,
    col.is_identity as 是否自增
from
    pg_class c left join pg_constraint con
        on con.conrelid = c."oid" and con.contype = 'p',
    pg_type t,
    information_schema."columns" col,
    pg_attribute a left join pg_description d
        on d.objoid = a.attrelid and d.objsubid = a.attnum
where
    a.attnum > 0
    and a.attrelid = c.oid
    and a.atttypid = t.oid
    and col.table_name = c.relname
    and col.column_name = a.attname
    and col.table_schema = '你的schema'
    and c.relname = '你的表名'
order by
    主键位置,
    字段序号;

英文版(适合写代码)

select
    c.relname as table_name,
    cast(obj_description(c.oid) as varchar) as table_desc,
    a.attnum as field_seq,
    a.attname as field_name,
    d.description as field_desc,
    concat_ws('', t.typname, replace(substring(format_type(a.atttypid, a.atttypmod) from '\(.*\)'), ',0', '')) as field_type,
    case
        when a.attnum = any(con.conkey) then '是'
        end as is_pk,
    case
        when array_position(con.conkey, a.attnum) is not null then concat(array_position(con.conkey, a.attnum))
        end as at_pk_index,
    con.conkey as pk_field_seqs,
    case
        when a.attnotnull is true then '是'
        else '否'
        end as not_null,
    case
        when position('::' in col.column_default) > 0 then replace(substring(col.column_default from '.*::'), '::', '')
        else col.column_default
        end as default_value,
    col.is_identity as is_identity
from
    pg_class c left join pg_constraint con
        on con.conrelid = c."oid" and con.contype = 'p',
    pg_type t,
    information_schema."columns" col,
    pg_attribute a left join pg_description d
        on d.objoid = a.attrelid and d.objsubid = a.attnum
where
      a.attnum > 0
  and a.attrelid = c.oid
  and a.atttypid = t.oid
  and col.table_name = c.relname
  and col.column_name = a.attname
  and col.table_schema = '{schema}'
  and c.relname = '{table_name}'
order by
    at_pk_index,
    field_seq;

相关文章

  • 表的约束

    表的约束 单字段主键 字段名 数据类型 PRIMARY KEY 非空约束 字段名 数据类型 NOT NULL;

  • Mysql查询表结构、索引导出excel及索引查询后的列名含义

    MySQL查询表结构、索引导出excel 导出表结构(列名,数据类型,字段类型,长度,是否为空,默认值,备注) 在...

  • 修改MySQL表中的字段属性

    登录数据库 查询所有数据表 查询表的字段信息 1.修改某个表的字段类型及指定为空或非空 2.修改某个表的字段名称及...

  • 2022-01-26 gorm库使用批量查询FindInBatc

    1.必须指定表结构体的主键 2.表结构体的数据类型必须与数据库表一致

  • SQLite中常用的SQL语句

    创建表:creat table 表名 (字段名 字段数据类型 是否为主键, 字段名 字段数据类型, 字段名 字段数...

  • 10《MySQL 教程》MySQL 设计数据表

    一个数据表主要包含信息有 : 表名、主键、字段、数据类型、索引,本节主要介绍表的命名规范、字段命名、字段的数据类型...

  • MySQL查看表结构简单命令

    简单描述表结构,字段类型desc 表名称;显示表结构,字段类型,主键,是否为空等属性,但不显示外键。 查询表中列的...

  • mysql表结构修改、约束(二)

    知识要点: 表结构操作 非空约束 唯一约束 主键约束 自增长 默认约束 表结构操作 ( alter table...

  • mysql 常用语句

    查询数据库下所有表名、表注释 查询数据库下字典表下所有字段名、数据类型、字段注释 查询数据库下所有表下所有字段名、...

  • Day16 MySQL基础

    1、DDL 常用数据类型创建表 查看表结构 查看表创建语句 删除表 创建表指定字符集和引擎 修改字段类型 添加字段...

网友评论

      本文标题:pg 查询指定表元数据(表结构、字段、数据类型、主键、非空、默认

      本文链接:https://www.haomeiwen.com/subject/bhkvqdtx.html