Find non-primary key and non-unique base table foreign key columns that name refers to the possibility that these are used to register references to a state classifier. The column must belong to a foreign key and does not have a default value.
Notes
The query takes into account a possibility that the default value can be declared to the column directly or through a domain. The query considers both column names in English and Estonian.
Type
Problem detection (Each row in the result could represent a flaw in the design)
Declare to the column a default value that corresponds to the first state that the entity gets after its data is registered in the system.
Data Source
INFORMATION_SCHEMA+system catalog
SQL Query
with fk AS (select
(select nspname from pg_namespace where oid=c.relnamespace) as fk_schema,
c.relname as fk_table,
c.oid as fk_table_oid,
o.conkey AS fk_col
from pg_constraint o inner join pg_class c on c.oid = o.conrelid
where o.contype='f' and c.relkind = 'r'),
keys_unnest as (select fk_schema, fk_table, fk_table_oid, fk_col, fk_col_num
from fk, unnest(fk.fk_col) with ordinality as f(fk_col_num, ordin)),
keys_with_names as (select fk_schema, fk_table, a_target.attname fk_column
from keys_unnest k inner join pg_attribute a_target on k.fk_col_num = a_target.attnum and k.fk_table_oid = a_target.attrelid and a_target.attisdropped = false)
SELECT c.table_schema, c.table_name , c.column_name
FROM information_schema.columns c INNER JOIN information_schema.tables T USING (table_schema, table_name)
INNER JOIN information_schema.schemata S ON c.table_schema=S.schema_name
LEFT JOIN information_schema.domains d USING (domain_schema, domain_name)
WHERE coalesce(c.column_default, d.domain_default) IS NULL
AND T.table_type='BASE TABLE'
AND c.column_name~*'(olek|staatus|seisund|state(?!ment)|status)'
AND (c.table_schema, c.table_name , c.column_name) NOT IN
(SELECT table_constraints.table_schema, table_constraints.table_name, constraint_column_usage.column_name
FROM information_schema.table_constraints INNER JOIN information_schema.constraint_column_usage
USING (table_schema, table_name, constraint_name)
WHERE table_constraints.constraint_type IN ('PRIMARY KEY', 'UNIQUE'))
AND (c.table_schema, c.table_name , c.column_name) IN (
SELECT fk_schema, fk_table, fk_column
FROM keys_with_names)
AND (c.table_schema = 'public'
OR S.schema_owner<>'postgres')
ORDER BY c.table_schema, c.table_name, c.column_name;
Collections
This query belongs to the following collections:
Name
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 .
Categories
This query is classified under the following categories:
Name
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.
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.
Validity and completeness
Queries 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).