欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

PostgreSQL表结构【仅需要写数据库文档时使用】

程序员文章站 2021-12-23 21:07:24
...

 原文:点这里

select
    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 'Y' else 'N' end) as 主键约束,
    (case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='u')>0 then 'Y' else 'N' end) as 唯一约束,
    (case when (select count(*) from pg_constraint where conrelid = a.attrelid and conkey[1]=attnum and contype='f')>0 then 'Y' else 'N' end) as 外键约束,
    (case when a.attnotnull=true then 'Y' else 'N' end) as nullable,
col_description(a.attrelid,a.attnum) as comment
from pg_attribute a
where attstattarget=-1 and attrelid = (select oid from pg_class where relname ='table_name'); -- 表名