select *FROM (
from (
select
pgc.contype as constraint_type,
ccu.table_schema as table_schema,
kcu.table_name as table_name,
case when (pgc.contype = 'f') then kcu.column_name else ccu.column_name end as column_name,
case when (pgc.contype = 'f') then ccu.table_name else (null) end as reference_table,
case when (pgc.contype = 'f') then ccu.column_name else (null) end as reference_col,
case when (pgc.contype = 'p') then 'yes' else 'no' end as auto_inc,
case when (pgc.contype = 'p') then 'no' else 'yes' end as is_nullable,
'integer' as data_type,
'0' as numeric_scale,
'32' as numeric_precision
from
pg_constraint as pgc
join pg_namespace nsp on nsp.oid = pgc.connamespace
join pg_class cls on pgc.conrelid = cls.oid
join information_schema.key_column_usage kcu on kcu.constraint_name = pgc.conname
left join information_schema.constraint_column_usage ccu on pgc.conname = ccu.constraint_name
and nsp.nspname = ccu.constraint_schema
union
select
null as constraint_type ,
table_schema,
table_name,
column_name,
null as refrence_table,
null as refrence_col,
'no' as auto_inc,
is_nullable,
data_type,
numeric_scale,
numeric_precision
from information_schema.columns cols
where
table_schema = 'public'
and concat(table_name, column_name) not in(
select concat(kcu.table_name, kcu.column_name)
from
pg_constraint as pgc
join pg_namespace nsp on nsp.oid = pgc.connamespace
join pg_class cls on pgc.conrelid = cls.oid
join information_schema.key_column_usage kcu on kcu.constraint_name = pgc.conname
left join information_schema.constraint_column_usage ccu on pgc.conname = ccu.constraint_name
and nsp.nspname = ccu.constraint_schema
)
) as foo
order by table_name asc, column_name
可以通过 检索约束
pg_catalog.pg_constraint
。替换
<schema name>
为您的架构<table name>
的名称和您的表的名称。在
psql
命令行中,此信息在表格中,通过\d+
命令获得。d+
还通知NOT NULL
约束,表中不存在的东西pg_catalog.pg_constraint
。一个例子:这里需要注意的是,您不会以这种方式获得所有约束的名称。
这是 PostgreSQL 特定的答案。它还将检索所有列及其关系: