The list of all the queries

The number of table constraints with the cardinality bigger than one

Query goal: Find the number of table constraints with the cardinality bigger than one. Find the number, based on the cardinality, constraint type, and cardinality+constraint type.
Query type: Sofware measure (Numeric values (software measures) about 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 
o.oid,
'PRIMARY KEY' AS type,
cardinality(o.conkey) as cardinality
from pg_constraint o INNER JOIN pg_class AS c ON o.conrelid=c.oid
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 
o.oid,
'UNIQUE' AS type,
cardinality(o.conkey) as cardinality
from pg_constraint o INNER JOIN pg_class AS c ON o.conrelid=c.oid
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 
o.oid,
'CHECK' AS type,
cardinality(o.conkey) as cardinality
from pg_constraint o INNER JOIN pg_class AS c ON o.conrelid=c.oid
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 
o.oid,
'FOREIGN KEY' AS type,
cardinality(o.conkey) as cardinality
from pg_constraint o INNER JOIN pg_class AS c ON o.conrelid=c.oid
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 
o.oid,
'EXCLUDE' AS type,
cardinality(o.conkey) as cardinality
from pg_constraint o INNER JOIN pg_class AS c ON o.conrelid=c.oid
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),

constr AS (SELECT oid, cardinality, type
FROM pkey_constraint_names
UNION SELECT oid, cardinality, type
FROM ukey_constraint_names
UNION SELECT oid, cardinality, type
FROM chk_constraint_names
UNION SELECT oid, cardinality, type
FROM fk_constraint_names
UNION SELECT oid, cardinality, type
FROM exclude_constraint_names)

SELECT cardinality, type AS constraint_type, Count(*) AS number_of_constraints
FROM constr
GROUP BY CUBE (cardinality, type);

Collections where the query belongs to

Collection nameCollection description
Find problems about integrity constraintsA selection of queries that return information about the state of integrity constraints in the datadabase. Contains all the types of queries - problem detection, software measure, and general overview
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 .
Find quick numeric overview of the databaseQueries that return numeric values showing mostly the number of different types of database objects in the database

Categories where the query belongs to

Category nameCategory description
CHECK constraintsQueries of this category provide information about CHECK constraints.
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