Find table columns with CHAR or VARCHAR type that have a default value that length is longer from the character maximum length of the column. Choose a suitable data type, field size, and default value. If the value is longer, then it is impossible to register it in a field, i.e., it makes registration of data impossible (except if the excessive characters are spaces).
Type
Problem detection (Each row in the result could represent a flaw in the design)
Increase the field size or shorthen the default value.
Data Source
INFORMATION_SCHEMA only
SQL Query
WITH defaults AS (SELECT c.table_schema, t.table_type, c.table_name, c.column_name, CASE WHEN column_default IS NOT NULL THEN 'COLUMN' ELSE 'DOMAIN' END AS object_type, c.data_type, c.character_maximum_length, coalesce(c.column_default, domain_default) AS def_value
FROM information_schema.columns AS c LEFT JOIN information_schema.domains d ON c.domain_schema=d.domain_schema AND c.domain_name=d.domain_name
INNER JOIN INFORMATION_SCHEMA.tables AS t USING (table_schema, table_name)
WHERE 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.data_type IN ('character', 'character varying' )
AND (coalesce(c.column_default, domain_default) LIKE '%::bpchar' OR coalesce(c.column_default, domain_default) LIKE '%::character varying')
AND (column_default IS NOT NULL OR domain_default IS NOT NULL)
ORDER BY c.table_schema, c.table_name, c.ordinal_position)
SELECT table_schema, table_type, table_name, column_name, character_maximum_length, data_type, def_value, object_type
FROM defaults
WHERE char_length(trim(substring(def_value,'[''][^'']*['']'),'''')) >character_maximum_length
ORDER BY table_type, table_schema, table_name;
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
Data types
Queries of this category provide information about the data types and their usage.
Default value
Queries of this catergory provide information about the use of default values.
Fatal problems
Queries of this category provide information about problems that render a part of a database unusable.
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).