Goal This query identifies base table columns whose names suggest they store national identification numbers (personal codes), specifically targeting those that serve as a primary or unique key. Using a national ID as a standalone key enforces a highly restrictive business rule: it assumes that all individuals in the system come from a single country, as identical ID numbers can exist in different countries. The query flags these instances to prompt a review, ensuring that this single-country limitation is intentional and actually aligns with the domain rules.
Notes The query considers both column names in English and Estonian.
Type General (Overview of some aspect of the database.)
License MIT License
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
WITH personal_code AS (SELECT c.table_schema, c.table_name, c.column_name,  c.data_type, c.character_maximum_length
FROM information_schema.columns AS c
WHERE (c.table_schema, c.table_name) IN (SELECT table_schema, table_name
FROM INFORMATION_SCHEMA.tables WHERE table_type='BASE TABLE') AND 
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 c.column_name~*'^(isik[u_ ]*kood|person[_ ]*code|personal[_ ]*code|(national|nat)[_ ]*identification[_ ]*(number|nr)|(national|nat)[_ ]*identity[_ ]*(number|nr)|(national|nat)[_ ]*insurance[_ ]*(number|nr))$'
AND (c.data_type ILIKE '%char%' OR c.data_type='text')),
keys as (select 
o.conname, 
(select nspname from pg_namespace where oid=c.relnamespace) as key_schema,
c.relname as key_table, 
c.oid as key_table_oid,
unnest(o.conkey) as key_col,
case when o.contype='p' then 'PRIMARY KEY' ELSE 'UNIQUE' END as contype
from pg_constraint o inner join pg_class c on c.oid = o.conrelid
where o.contype in ('u', 'p') and cardinality(o.conkey)=1),
keys_unnest as (select conname, key_schema, key_table, key_table_oid, key_col, contype
from keys),
keys_with_names as (select conname, key_schema, key_table, contype, a_key.attname  as key_col
from keys_unnest k inner join pg_attribute a_key on k.key_col = a_key.attnum and k.key_table_oid = a_key.attrelid and a_key.attisdropped = false)
select conname, key_schema, key_table, contype, key_col, data_type, character_maximum_length
from personal_code AS p INNER JOIN keys_with_names AS k ON p.table_schema=k.key_schema AND p.table_name=k.key_table AND p.column_name=k.key_col
order by key_schema, key_table, contype, key_col;

Collections

This query belongs to the following collections:

NameDescription
Find problems automaticallyQueries, 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:

NameDescription
Data typesQueries of this category provide information about the data types and their usage.
Result quality depends on namesQueries 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.
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).