3

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 ?

5
  • Great question! Tables have 11+1 types of constraints. Writing the query to retrieve them all and also to render them in readable language is probably tedious albeit totally feasible. I'll pay attention to the answers since it may come in handy for me. Oct 1, 2021 at 0:21
  • Does this answer your question? How to list all constraints of a table in PostgreSQL?
    – Schwern
    Oct 1, 2021 at 0:23
  • How do you find the <schema name> ? @Schwern Oct 1, 2021 at 0:45
  • @fenton.raine If you don't know your schema you're using the default schema "public". See postgresqltutorial.com/postgresql-schema
    – Schwern
    Oct 1, 2021 at 0:52
  • @fenton.raine Do you mind if I start a bounty for this question? I would love to see a good answer for it. Oct 2, 2021 at 18:49

3 Answers 3

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.

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
0
+100

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" )
1
  • Your answer is not complete but it's a start. I didn't want the bounty go to waste so I assigned it to you. Oct 12, 2021 at 18:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.