This query audits the database for inconsistent column naming conventions, specifically focusing on non-generic, domain-specific attributes (excluding primary keys, foreign keys, and generic fields like 'comment'). It detects a hybrid naming strategy: some columns are prefixed with the table name (e.g., employee_salary) and others are not (e.g., hiring_date). This inconsistency suggests a lack of a standardized data dictionary, making query writing unpredictable.
Notes
The query considers both column names in English and Estonian. The query lists both column names that begin with the table name and column names that do not.
Type
Problem detection (Each row in the result could represent a flaw in the design)
WITH primary_key_columns AS (select target_schema as table_schema, target_table as table_name, a.attname as column_name
from (select
(select nspname from pg_namespace where oid=c.relnamespace) as target_schema,
c.relname as target_table,
c.oid as target_table_oid,
unnest(o.conkey) AS target_col
from pg_constraint o inner join pg_class c on c.oid = o.conrelid
where o.contype in ('p')) t
inner join pg_attribute a on t.target_col = a.attnum and t.target_table_oid = a.attrelid and a.attisdropped = false),
fk_columns AS (
select target_schema as table_schema, target_table as table_name, a.attname as column_name
from (select
(select nspname from pg_namespace where oid=c.relnamespace) as target_schema,
c.relname as target_table,
c.oid as target_table_oid,
unnest(o.conkey) AS target_col
from pg_constraint o inner join pg_class c on c.oid = o.conrelid
where o.contype = 'f') t
inner join pg_attribute a on t.target_col = a.attnum and t.target_table_oid = a.attrelid and a.attisdropped = false
),
columns AS (SELECT table_schema, table_name, column_name
FROM INFORMATION_SCHEMA.columns AS c
WHERE (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) AND
NOT EXISTS (SELECT 1 FROM primary_key_columns AS pk WHERE pk.table_schema=c.table_schema AND pk.table_name=c.table_name AND pk.column_name=c.column_name) AND
NOT EXISTS (SELECT 1 FROM fk_columns AS fk WHERE fk.table_schema=c.table_schema AND fk.table_name=c.table_name AND fk.column_name=c.column_name)
AND column_name!~*'(nimetus|kommentaar|kirjeldus|staatus|seisund|olek|tyyp|tüüp|tekst|comment|description|explanation|state|status|type|text)'),
stats AS (
SELECT Count(*) FILTER (WHERE column_name ~* ('^' || table_name || '.{0,1}_')) AS nr_of_columns_that_start_table_name ,
string_agg(table_schema || '.' || table_name || '.' || column_name, '; ' ORDER BY table_name, column_name) FILTER (WHERE column_name ~* (table_name || '.{0,1}_')) AS columns_that_start_table_name,
Count(*) FILTER (WHERE column_name !~* ('^' || table_name || '.{0,1}_')) AS nr_of_columns_that_do_not_start_table_name,
string_agg(table_schema || '.' || table_name || '.' || column_name, '; ' ORDER BY table_name, column_name) FILTER (WHERE column_name !~* (table_name || '.{0,1}_')) AS columns_that_start_table_name
FROM columns)
SELECT *
FROM stats
WHERE EXISTS (SELECT * FROM columns WHERE column_name ~* ('^' || table_name || '.{0,1}_'))
AND EXISTS (SELECT * FROM columns WHERE column_name !~* ('^' || table_name || '.{0,1}_'));
Collections
This query belongs to the following collections:
Name
Description
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 .
Categories
This query is classified under the following categories:
Name
Description
Inconsistencies
Queries of this catergory provide information about inconsistencies of solving the same problem in different places.
Naming
Queries of this category provide information about the style of naming.