Find base tables with more than one name columns. Perhaps the normalization level of the table is low.
Notes
In case of the string_agg function, the line break (br) tag is used as a part of the separator for the better readability in case the query result is displayed in a web browser. The query considers both column names in English and Estonian.
Type
General (Overview of some aspect of the database.)
WITH name_cols AS (SELECT table_schema, table_name, column_name,
CASE WHEN character_maximum_length IS NOT NULL
THEN data_type || '(' || character_maximum_length || ')'
ELSE data_type END AS data_type, CASE WHEN is_nullable='NO' THEN 'NOT NULL' ELSE 'NULL' END AS not_null,
ordinal_position
FROM INFORMATION_SCHEMA.columns
WHERE column_name ~* '(name|nimi)'
AND (table_schema, table_name) IN (SELECT table_schema, table_name
FROM INFORMATION_SCHEMA.tables WHERE table_type='BASE TABLE')
AND 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))
SELECT table_schema, table_name, Count(*) AS nr_of_columns, string_agg(column_name || ' ' || data_type || ' ' || not_null,'; ' ORDER BY ordinal_position) AS columns
FROM name_cols
GROUP BY table_schema, table_name
HAVING Count(*)>1
ORDER BY Count(*) DESC, table_schema, table_name;
Collections
This query belongs to the following collections:
Name
Description
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 .
Categories
This query is classified under the following categories:
Name
Description
Result quality depends on names
Queries of this category use names (for instance, column names) to try to guess the meaning of a database object. Thus, the goodness of names determines the number of false positive and false negative results.
Structure of base tables
Queries of this category provide information about the structuring of base tables at the database conceptual level