Goal | Find names of foreign key constraints that are used within the same schema more than once. Find names of check constraints that are used within the same schema more than once. Find names of constraint triggers that are used within the same schema more than once. Different things should have different names. But here different constraints have the same name. Also make sure that this is not a sign of 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. The query considers only constraints that are connected directly with a base table or a foreign table, i.e., it does not consider domain CHECK constraints. It is entirely normal that a domain has a check constraint and the domain is used in case of multiple columns. The query considers only locally-defined constraints, i.e., constraints that are not inherited. The query does not consider constraint triggers becasuse these could be used to implement foreign key constraints and the same name could be used in case of different triggers that implement the same constraint. |
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 License |
Fixing Suggestion | Give to foreign key and check constraints as well as to constraint triggers unique names within a schema. Use table name in the constraint name. Follow a naming convention. |
Data Source | system catalog only |
SQL Query |
|
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 . |
This query is classified under the following categories:
Name | Description |
---|---|
Comfortability of database evolution | Queries of this category provide information about the means that influence database evolution. |
Naming | Queries of this category provide information about the style of naming. |
Uniqueness | Queries of this category provide information about uniqueness constraints (PRIMARY KEY, UNIQUE, EXCLUDE) as well as unique indexes. |
Further reading and related materials: