Query goal: | Find base tables that implement a highly generic database design (EAV design - Entiry-Attribute-Value design), according to which attribute values are recorded in a generic table that contains attribute-value pairs. |
Notes about the query: | In case of the string_agg function, the line break (br) tag is used as a part of the separator for the better readability in case the query result is displayed in a web browser. Because the query relies on the names of base tables the query result can have false positive and false negative results. The query considers both table names in English and Estonian. |
Query type: | Problem detection (Each row in the result could represent a flaw in the design) |
Query reliability: | Low (Many false-positive results) |
Query license: | MIT License |
Data source: | INFORMATION_SCHEMA only |
SQL query: | Click on query to copy it
WITH columns AS (SELECT c.table_schema, c.table_name, regexp_replace(c.column_name, '[[:digit:]]','') AS repeating_column, c.data_type || CASE WHEN c.character_maximum_length IS NOT NULL THEN '(' || c.character_maximum_length::text || ')' WHEN c.numeric_precision ||'.'|| c.numeric_scale IS NOT NULL THEN '(' || c.numeric_precision ||'.'|| c.numeric_scale || ')' ELSE '' END AS type, c.column_name, c.ordinal_position FROM INFORMATION_SCHEMA.columns AS c INNER JOIN INFORMATION_SCHEMA.tables AS t USING (table_schema, table_name) WHERE t.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 table_name~* '(attribute|property|properties|atribuu|omadus)' ), col_values AS (SELECT table_schema, table_name FROM columns WHERE column_name~*'(value|vaartus|väärtus)') SELECT table_schema, table_name, string_agg(column_name || ' ' || type, ';<br>' ORDER BY ordinal_position) AS columns FROM columns WHERE (table_schema, table_name) IN (SELECT table_schema, table_name FROM col_values) GROUP BY table_schema, table_name ORDER BY table_schema, table_name; |
Category name | Category description |
---|---|
Database design antipatterns | Queries of this category provide information about possible occurrences of SQL database design antipatterns. |
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. |
Structure of base tables | Queries of this category provide information about the structuring of base tables at the database conceptual level |
Reference |
---|
This is one of the antipatterns from the Bill Karwin's book of SQL antipatterns. See Chapter 6: Entity-Attribute-Value. |
Smell "Metadata as data": Sharma, T., Fragkoulis, M., Rizou, S., Bruntink, M. and Spinellis, D.: Smelly relations: measuring and understanding database schema quality. In: Proceedings of the 40th International Conference on Software Engineering: Software Engineering in Practice, pp. 55-64. ACM, (2018). |
Eessaar, E. and Soobik, M.: On universal database design. Baltic DB & IS, pp. 349-360, (2008). |
Mistake (3): https://www.red-gate.com/simple-talk/sql/database-administration/five-simple--database-design-errors-you-should-avoid/ |