The list of all the queries

Table constraints with the cardinality bigger than one

Query goal: Find constraints that involve more than one columns. Check as to whether the names follow a common style or not.
Query type: General (Overview of some aspect of the database.)
Query license: MIT License
Data source: system catalog only
SQL query: Click on query to copy it

WITH pkey_constraint_names AS (
select 
'PRIMARY KEY' AS type,
o.conname AS constraint_name,
np.nspname as schema_name,
c.relname as table_name, 
cardinality(o.conkey) as cardinality
from pg_constraint o inner join pg_class c on c.oid = o.conrelid 
INNER JOIN pg_namespace AS np ON c.relnamespace=np.oid
INNER JOIN pg_authid AS a ON np.nspowner=a.oid
WHERE (np.nspname='public' OR rolname<>'postgres')
and o.contype ='p'  and cardinality(o.conkey)>1),

ukey_constraint_names AS (
select 
'UNIQUE' AS type,
o.conname AS constraint_name,
np.nspname as schema_name,
c.relname as table_name, 
cardinality(o.conkey) as cardinality
from pg_constraint o inner join pg_class c on c.oid = o.conrelid 
INNER JOIN pg_namespace AS np ON c.relnamespace=np.oid
INNER JOIN pg_authid AS a ON np.nspowner=a.oid
WHERE (np.nspname='public' OR rolname<>'postgres')
and o.contype='u'  and cardinality(o.conkey)>1),

chk_constraint_names AS (select 
'CHECK' AS type,
o.conname AS constraint_name,
np.nspname as schema_name,
c.relname as table_name, 
cardinality(o.conkey) as cardinality
from pg_constraint o inner join pg_class c on c.oid = o.conrelid
INNER JOIN pg_namespace AS np ON c.relnamespace=np.oid
INNER JOIN pg_authid AS a ON np.nspowner=a.oid
WHERE (np.nspname='public' OR rolname<>'postgres')
and o.contype = 'c'  and cardinality(o.conkey)>1),

fk_constraint_names AS (select 
'FOREIGN KEY' AS type,
o.conname AS constraint_name,
np.nspname as schema_name,
c.relname as table_name, 
cardinality(o.conkey) as cardinality
from pg_constraint o inner join pg_class c on c.oid = o.conrelid
INNER JOIN pg_namespace AS np ON c.relnamespace=np.oid
INNER JOIN pg_authid AS a ON np.nspowner=a.oid
WHERE (np.nspname='public' OR rolname<>'postgres')
and o.contype = 'f'  and cardinality(o.conkey)>1),

exclude_constraint_names AS (select 
'EXCLUDE' AS type,
o.conname AS constraint_name,
np.nspname as schema_name,
c.relname as table_name, 
cardinality(o.conkey) as cardinality
from pg_constraint o inner join pg_class c on c.oid = o.conrelid
INNER JOIN pg_namespace AS np ON c.relnamespace=np.oid
INNER JOIN pg_authid AS a ON np.nspowner=a.oid
WHERE (np.nspname='public' OR rolname<>'postgres')
and o.contype = 'x'  and cardinality(o.conkey)>1)

SELECT cardinality, type, schema_name, table_name, constraint_name
FROM pkey_constraint_names
UNION SELECT cardinality, type, schema_name, table_name, constraint_name
FROM ukey_constraint_names
UNION SELECT cardinality, type, schema_name, table_name, constraint_name
FROM chk_constraint_names
UNION SELECT cardinality, type, schema_name, table_name, constraint_name
FROM fk_constraint_names
UNION SELECT cardinality, type, schema_name, table_name, constraint_name
FROM exclude_constraint_names
ORDER BY type, cardinality DESC, schema_name, table_name, constraint_name;

Collections where the query belongs to

Collection nameCollection description
Find problems by overviewQueries that results point to different aspects of database that might have problems. A human reviewer has to decide based on the results as to whether there are problems or not .

Categories where the query belongs to

Category nameCategory description
CHECK constraintsQueries of this category provide information about CHECK constraints.
NamingQueries of this category provide information about the style of naming.
Relationships between tablesQueries of this category provide information about how database tables are connected to each other and whether such connections have been explicitly defined and whether it has been done correctly.
UniquenessQueries of this category provide information about uniqueness constraints (PRIMARY KEY, UNIQUE, EXCLUDE) as well as unique indexes.

The list of all the queries