| Goal | The same domain should not have multiple CHECK constraints with exactly the same Boolean expression. Do remember that the same task can be solved in SQL usually in multiple different ways. Thus, the exact copies are not the only possible duplication. |
|---|---|
| Notes | 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. |
| Type | Problem detection (Each row in the result could represent a flaw in the design) |
| Reliability | High (Few or no false-positive results) |
| License | MIT (opens in new tab) |
| Fixing Suggestion | Drop the redundant constraints. |
| Data Source | INFORMATION_SCHEMA only |
| SQL Query |
|
SQL statements that help generate fixes for the identified problem.
Fix Action #1
WITH duplicate_checks AS (SELECT dc.domain_schema, dc.domain_name, cc.check_clause,
array_agg(DISTINCT cc.constraint_name) AS constraints_array
FROM INFORMATION_SCHEMA.domain_constraints AS dc
INNER JOIN INFORMATION_SCHEMA.domains AS d USING (domain_schema, domain_name)
INNER JOIN INFORMATION_SCHEMA.check_constraints AS cc USING (constraint_schema, constraint_name)
WHERE cc.check_clause NOT LIKE '%IS NOT NULL' AND
dc.domain_schema NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
GROUP BY dc.domain_schema, dc.domain_name, cc.check_clause
HAVING Count(DISTINCT cc.constraint_name)>1)
SELECT format ('ALTER DOMAIN %1$I.%2$I DROP CONSTRAINT %3$I;', domain_schema, domain_name, unnest(constraints_array)) AS statements
FROM duplicate_checks
ORDER BY domain_schema, domain_name;Drop the constraint. One of the constraints must stay in place.
| SQL Query to Generate Fix | Description |
|---|---|
| Drop the constraint. One of the constraints must stay in place. |
Collections
This query belongs to the following collections:
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 .
| 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:
CHECK constraints
Queries of this category provide information about CHECK constraints.
Domains
Queries of this category provide information about reusable specifications of column properties.
Duplication of implementation elements
Queries of this catergory provide information about the duplication of the database objects.
| Name | Description |
|---|---|
| CHECK constraints | Queries of this category provide information about CHECK constraints. |
| Domains | Queries of this category provide information about reusable specifications of column properties. |
| Duplication of implementation elements | Queries of this catergory provide information about the duplication of the database objects. |
Further reading and related materials:
The corresponding code smell in case of cleaning code is "G5: Duplication". (Robert C. Martin, Clean Code)
| Reference |
|---|
| https://refactoring.guru/smells/alternative-classes-with-different-interfaces |
| https://refactoring.guru/smells/duplicate-code |
| The corresponding code smell in case of cleaning code is "G5: Duplication". (Robert C. Martin, Clean Code) |