Query goal: | Find base table columns that belong to a primary key, unique, or foreign key constraint and that name refers to the possibility that these are used to hold surrogate key values. Find the columns where the data type of the column is not an integer type or uuid. |
Query type: | Problem detection (Each row in the result could represent a flaw in the design) |
Query reliability: | Medium (Medium number of false-positive results) |
Query license: | MIT License |
Fixing suggestion: | Choose data types carefully. Be as precise as possible. In PostgreSQL one should use SMALLINT, INTEGER, or BIGINT type in case of storing surrogate key values (ID-s). Change the data type of the column. It could also be that the column name is misleading and should instead contain a word kood or code. In this case changing the data type might not be needed and instead one should rename the column. |
Data source: | INFORMATION_SCHEMA+system catalog |
SQL query: | Click on query to copy it
WITH key AS (SELECT (SELECT nspname FROM pg_namespace WHERE oid=c.relnamespace) AS key_schema, c.relname AS key_table, o.conkey AS key_col, c.oid AS key_table_oid FROM pg_constraint o INNER JOIN pg_class c ON c.oid = o.conrelid WHERE o.contype IN ('f','u','p')), key_unnest AS (SELECT key_schema, key_table, key_table_oid, key_col, key_col_num, ordin FROM key, unnest(key.key_col) with ordinality AS k(key_col_num, ordin)), key_with_names AS (SELECT key_schema, key_table, a_key.attname AS key_col FROM key_unnest k INNER JOIN pg_attribute a_key ON k.key_col_num = a_key.attnum AND k.key_table_oid = a_key.attrelid AND a_key.attisdropped = FALSE) SELECT table_schema, table_name, column_name, data_type FROM INFORMATION_SCHEMA.columns AS c WHERE 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 (table_schema, table_name) IN (SELECT table_schema, table_name FROM INFORMATION_SCHEMA.tables WHERE table_type='BASE TABLE') AND EXISTS (SELECT 1 FROM key_with_names AS kwn WHERE c.table_schema=kwn.key_schema AND c.table_name=kwn.key_table AND c.column_name=kwn.key_col) AND column_name~*'(_id$|^id_|^id$)' AND column_name!~*'code' AND data_type NOT IN ('integer', 'smallint','bigint','uuid') ORDER BY table_schema, table_name, ordinal_position; |
Collection name | Collection 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 . |
Category name | Category description |
---|---|
Data types | Queries of this category provide information about the data types and their usage. |
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). |
Reference |
---|
http://whileicompile.com/2012/03/my-take-on-identifier-semantics-id-vs-no-vs-code-vs-key/ |