简体   繁体   中英

Query Constraint Clauses With Schema and Table (Postgres)

I am trying to query the constraint clauses along with schema and table in postgres. I've gotten as far as identifying information_schema.check_constraints as a useful table. The problem is that doing

select *
from information_schema.check_constraints

Results in constraint_catalog , constraint_schema , constraint_name , check_clause . The check_clause is what I want and this table also gives me the constraint_schema . However, it does not give the table that this constraint is defined on. In my current database, I have constraints with the same name defined on different tables within the same schema (which is in it of itself perhaps poor design but what I need to deal with). Is it possible to get the table name here as well?

select
  conname,
  connamespace::regnamespace as schemaname,
  conrelid::regclass as tablename,
  consrc as checkclause,
  pg_get_constraintdef(oid) as definition
from
  pg_constraint
where
  contype = 'c'
  and conrelid <> 0; -- to get only table constraints

About pg_constraint

About Object Identifier Types

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM