The list of all the queries

Base table has a national identification number as a key

Query goal: Find base table columns that name refers to the possibility that these are used to register national identification numbers (personal codes). Find the columns that constitute a key of a table. This in turn enforces a (restrictive) business rule that all persons of interest come from one country. Make sure that the enforced constraint is valid, i.e., there is indeed such rule in the domain.
Notes about the query: The query considers both column names in English and Estonian.
Query type: General (Overview of some aspect of the database.)
Query license: MIT License
Data source: INFORMATION_SCHEMA+system catalog
SQL query: Click on query to copy it

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 where the query belongs to

Collection nameCollection description
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 where the query belongs to

Category nameCategory description
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).

Reference materials for further reading

Reference
https://en.wikipedia.org/wiki/National_identification_number
https://www.ria.ee/riigiarhitektuur/blog/2014/12/11/on-estonian-id-code/index.html
https://www.workinestonia.com/coming-to-estonia/personal-id-code/

The list of all the queries