Query goal: | Find base tables where all the columns have a default value. |
Notes about the query: | The query also considers a possibility that the default value is associated with a column through a domain. |
Query type: | Problem detection (Each row in the result could represent a flaw in the design) |
Query reliability: | Low (Many false-positive results) |
Query license: | MIT License |
Fixing suggestion: | Perhaps in some cases the default value is not needed and should be dropped. |
Data source: | INFORMATION_SCHEMA only |
SQL query: | Click on query to copy it
WITH cols AS (SELECT c.table_schema, c.table_name, c.column_name, coalesce(c.column_default, domain_default) AS default_value, c.ordinal_position FROM information_schema.columns AS c LEFT JOIN information_schema.domains d USING (domain_schema, domain_name) INNER JOIN INFORMATION_SCHEMA.tables AS t USING (table_schema, table_name) WHERE c.table_schema NOT IN (SELECT schema_name FROM INFORMATION_SCHEMA.schemata WHERE schema_name<>'public' AND schema_owner='postgres' AND schema_name IS NOT NULL) AND t.table_type='BASE TABLE') SELECT table_schema, table_name, Count(*) AS nr_of_columns, string_agg(column_name || '.' || default_value, ';<br>' ORDER BY ordinal_position) AS defaults FROM cols GROUP BY table_schema, table_name HAVING Count(*)=Count(*) FILTER (WHERE default_value IS NOT NULL) ORDER BY table_schema, table_name; |
Collection name | Collection description |
---|---|
Find problems about base tables | A selection of queries that return information about the data types, field sizes, default values as well as general structure of base tables. Contains all the types of queries - problem detection, software measure, and general overview |
Find problems automatically | Queries, that results point to problems in the database. Each query in the collection produces an initial assessment. However, a human reviewer has the final say as to whether there is a problem or not . |
Category name | Category description |
---|---|
Comfortability of data management | Queries of this category provide information about the means that have been used to make the use or management of database more comfortable and thus, more efficient. |
Default value | Queries of this catergory provide information about the use of default values. |