作者推荐:【Java代码生成神器】自动化生成Java实体类、代码、增删改查功能!
原文:
——————————————————————————————————————
网上找了一圈也没有一个支持多主键查询的,自己参考资料写了一个,可以查询字段名,类型,长度,主键(支持多主键),备注等信息
select a.attnum,
a.attname columnName,
t.typname columnType,
coalesce(d.character_maximum_length, d.numeric_precision) columnLength,
d.numeric_scale numericScaleLength,
(a.attnotnull is false) isNullable,
d.column_default defaultValue,
col_description(a.attrelid, a.attnum) as columnComment,
case when length(b.attname) > 0 then 'PRI' end as columnKey
from pg_class c
left join pg_namespace p on c.relnamespace = p.oid,
pg_attribute a,
pg_type t,
information_schema.columns d
left join (select pg_attribute.attname
from pg_index,
pg_class,
pg_attribute
where pg_class.oid = '表名' :: regclass
and pg_index.indrelid = pg_class.oid
and pg_attribute.attrelid = pg_class.oid
and pg_attribute.attnum = any (pg_index.indkey)) b on d.column_name = b.attname
where a.attrelid = c.oid
and a.atttypid = t.oid
and a.attnum > 0
and c.relname = d.table_name
and d.column_name = a.attname
and c.relname = '表名'
and d.table_schema = ?
and p.nspname = ?