Query 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 about the query: | 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. |
Query type: | Sofware measure (Numeric values (software measures) about the database) |
Query license: | MIT License |
Data source: | INFORMATION_SCHEMA only |
SQL query: | Click on query to copy it
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); |
Collection name | Collection description |
---|---|
Find problems about integrity constraints | A 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 overview | Queries 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 database | Queries that return numeric values showing mostly the number of different types of database objects in the database |
Category name | Category description |
---|---|
Uniqueness | Queries of this category provide information about uniqueness constraints (PRIMARY KEY, UNIQUE, EXCLUDE) as well as unique indexes. |
Validity and completeness | Queries 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). |
Reference |
---|
https://www.red-gate.com/simple-talk/sql/database-administration/five-simple-database-design-errors-you-should-avoid/ |