Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

158
Views
Postgresql Constraints (display)

I need to display the list of integrity constraints of my solution by indicating the name of the constraint, its type and the detail of the constraint (message that explains what validates the constraint), all sorted by table name and constraint name.

This is what I tried :

SELECT constraint_name, constraint_type
FROM user_constraints
WHERE table_name = 'mytable'

How can I display all of that and also give a message that indicates what validates the constraint ?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

This is a comment that doesn't fit the comments section.

I wanted to document that tables have 11+1 types of constraints:

  1. Table PRIMARY KEY constraint.
  2. Table UNIQUE constraint.
  3. Table CHECK constraint.
  4. Table [imported] FOREIGN KEY constraint (aka reference).
  5. Table [exported] FOREIGN KEY constraint (aka referential action RESTRICT).
  6. Column NOT NULL constraint.
  7. Column PRIMARY KEY constraint.
  8. Column UNIQUE constraint.
  9. Column CHECK constraint.
  10. Column [imported] FOREIGN KEY constraint (aka reference).
  11. Column [exported] FOREIGN KEY constraint (aka referential action RESTRICT).
  12. The "Partial UNIQUE INDEX" pseudo constraint, when adding WHERE to the CREATE UNIQUE INDEX ... statement. This type of constraints is, however, debated since the SQL Standard does not include it. In practice all major database engines (Oracle, DB2, PostgreSQL, etc.) implement them (albeit using different strategies) and they are very efficiently at maintaining data quality.

Note: Some engines (e.g. Oracle) do not implement #7 to #11. For Oracle most constraints are recorded at the table level, even if you define them at the column level.

over 4 years ago · Santiago Trujillo Report

0

Please use below query (This query is tested on MS SQL, I hope you convert it for PostgreSQL):

Select self_objects.name 'Table_Name', C.*, (Select definition From sys.default_constraints Where object_id = C.object_id) As dk_definition,
(Select definition From sys.check_constraints Where object_id = C.object_id) As ck_definition,
(Select name From sys.objects Where object_id = D.referenced_object_id) As fk_table,
(Select name From sys.columns Where column_id = D.parent_column_id And object_id = D.parent_object_id) As fk_col
From sys.objects As C
join sys.objects self_objects on
self_objects.object_id = c.parent_object_id
and self_objects.type = 'U'
Left Join (Select * From sys.foreign_key_columns) As D On D.constraint_object_id = C.object_id 
order by self_objects.name
over 4 years ago · Santiago Trujillo Report

0

Please try below code.

SELECT
    "ns"."nspname" AS "table_schema",
    "t"."relname" AS "table_name",
    "cnst"."conname" AS "constraint_name", pg_get_constraintdef ( "cnst"."oid" ) AS "expression",
CASE
        "cnst"."contype" 
        WHEN 'p' THEN
        'PRIMARY' 
        WHEN 'u' THEN
        'UNIQUE' 
        WHEN 'c' THEN
        'CHECK' 
        WHEN 'x' THEN
        'EXCLUDE' 
    END AS "constraint_type",
    "a"."attname" AS "column_name"
FROM
    "pg_constraint" "cnst" 
    INNER JOIN "pg_class" "t" ON "t"."oid" = "cnst"."conrelid"
    INNER JOIN "pg_namespace" "ns" ON "ns"."oid" = "cnst"."connamespace"
    LEFT JOIN "pg_attribute" "a" ON "a"."attrelid" = "cnst"."conrelid" 
    AND "a"."attnum" = ANY ( "cnst"."conkey" )
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!