Query goal: | Find domain check constraint names that are used more than once (within the same schema or in different schemas). Different things should have different names. However, here different constraints have the same name. Also make sure that this is not a sign of duplication of domains. |
Query type: | Problem detection (Each row in the result could represent a flaw in the design) |
Query reliability: | High (Few or no false-positive results) |
Query license: | MIT License |
Fixing suggestion: | Give different constraints different names. |
Data source: | INFORMATION_SCHEMA only |
SQL query: | Click on query to copy it
SELECT constraint_name, count(*) AS number_of_occurrences, string_agg(domain_schema || '.' || domain_name, ';<br>' ORDER BY domain_schema, domain_name) AS domains FROM INFORMATION_SCHEMA.domain_constraints WHERE 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 constraint_name HAVING Count(*)>1 ORDER BY Count(*) DESC, constraint_name; |
SQL query | Description |
---|---|
WITH domain_constraints AS (SELECT constraint_name, domain_schema, domain_name FROM INFORMATION_SCHEMA.domain_constraints WHERE 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)), duplicate_names AS (SELECT constraint_name FROM domain_constraints GROUP BY constraint_name HAVING Count(*)>1) SELECT format('ALTER DOMAIN %1$I.%2$I RENAME CONSTRAINT %3$I TO %3$s%2$s;', domain_schema, domain_name, constraint_name) AS statements FROM domain_constraints WHERE constraint_name IN (SELECT constraint_name FROM duplicate_names) ORDER BY domain_schema, domain_name; | Rename the constraint by adding domain name to the end of the constraint name. |
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 |
---|---|
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. |
Naming | Queries of this category provide information about the style of naming. |