Query goal: | Find not valid foreign key constraints. These constraints have been created so that the existing data has not been checked against the constraint. It could be deliberate in case of legacy systems that have data quality problems. However, ideally all the data in the table conforms to the constraint. |
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: | Validate the constraint after fixing the data problems. |
Data source: | system catalog only |
SQL query: | Click on query to copy it
SELECT (select nspname from pg_namespace where oid=c.relnamespace) AS table_schema, c.relname AS table_name, o.conname AS constraint_name FROM pg_constraint o INNER JOIN pg_class c ON c.oid = o.conrelid WHERE o.contype = 'f' AND o.convalidated=false ORDER BY table_schema, c.relname, o.conname; |
SQL query | Description |
---|---|
WITH fk AS (SELECT (select nspname from pg_namespace where oid=c.relnamespace) AS table_schema, c.relname AS table_name, o.conname AS constraint_name FROM pg_constraint o INNER JOIN pg_class c ON c.oid = o.conrelid WHERE o.contype = 'f' AND o.convalidated=false) SELECT format('ALTER TABLE %1$I.%2$I VALIDATE CONSTRAINT %3$I;', table_schema, table_name, constraint_name) AS statements FROM fk ORDER BY table_schema, table_name, constraint_name; | Validate the constraint. |
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 |
---|---|
Relationships between tables | Queries of this category provide information about how database tables are connected to each other and whether such connections have been explicitly defined and whether it has been done correctly. |