Goal Find the extent in which repeating rows are permitted in the database. Find the number and percentage (from the total number of base tables) of base tables that do not have the PRIMARY KEY constraint and also do not have any UNIQUE constraints.
Notes The condition "WHERE EXISTS (SELECT * FROM base_tables)" ensures that there is now division by zero, i.e., if there are no base tables in the database the query does not return any rows.
Type Sofware measure (Numeric values (software measures) about the database)
License MIT License
Data Source INFORMATION_SCHEMA only
SQL Query
WITH base_tables AS (SELECT A.table_schema, A.table_name 
FROM INFORMATION_SCHEMA.tables A
INNER JOIN INFORMATION_SCHEMA.schemata B
ON A.table_schema=B.schema_name
WHERE A.table_type='BASE TABLE'
AND (A.table_schema = 'public'
OR B.schema_owner<>'postgres')),
number_of_base_tables AS (SELECT Count(*) AS number_of_base_tables 
FROM base_tables),
number_of_base_tables_without_keys AS (SELECT Count(*) AS number_of_base_tables_without_keys
FROM INFORMATION_SCHEMA.tables A
INNER JOIN INFORMATION_SCHEMA.schemata B
ON A.table_schema=B.schema_name
WHERE (A.table_schema, A.table_name) NOT IN 
(SELECT table_schema, table_name FROM INFORMATION_SCHEMA.table_constraints
WHERE constraint_type IN ('PRIMARY KEY', 'UNIQUE'))
AND A.table_type='BASE TABLE'
AND (A.table_schema = 'public'
OR B.schema_owner<>'postgres'))
SELECT number_of_base_tables_without_keys, 
(number_of_base_tables-number_of_base_tables_without_keys) AS number_of_base_tables_with_keys,
number_of_base_tables, 
Round(number_of_base_tables_without_keys::decimal*100/number_of_base_tables::decimal,2) AS percentage_of_base_tables_without_keys
FROM number_of_base_tables, number_of_base_tables_without_keys
WHERE EXISTS (SELECT * FROM base_tables);

Collections

This query belongs to the following collections:

NameDescription
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

This query is classified under the following categories:

NameDescription
UniquenessQueries of this category provide information about uniqueness constraints (PRIMARY KEY, UNIQUE, EXCLUDE) as well as unique indexes.
Validity and completenessQueries of this category provide information about whether database design represents the world (domain) correctly (validity) and whether database design captures all the information about the world (domain) that is correct and relevant (completeness).