Validating database integrity

From jeremylongshore/claude-code-plugins-plus-skills · View interactive version

Validating database integrity is an AI agent skill from the GitHub repository jeremylongshore/claude-code-plugins-plus-skills. Process use when you need to ensure database integrity through comprehensive data validation. This skill validates data types, ranges, formats, referential integrity, and business rules. Trigger with phrases like "validate database data", "implement data validation rules", "enforce data integrity co

How to use this skill

Skill file

Data Validation Engine

Overview

Implement and enforce data integrity rules at the database level using CHECK constraints, triggers, foreign keys, and custom validation functions across PostgreSQL and MySQL.

Prerequisites

Instructions

  1. Audit existing data quality by running validation queries before adding constraints. Check for NULL values in columns that should be required: SELECT column_name, COUNT(*) FILTER (WHERE column_name IS NULL) AS null_count, COUNT(*) AS total FROM table_name GROUP BY column_name.
  1. Detect orphaned records (broken referential integrity): SELECT c.id FROM child_table c LEFT JOIN parent_table p ON c.parent_id = p.id WHERE p.id IS NULL. Document all orphaned records for cleanup or archival before adding foreign key constraints.
  1. Validate data format compliance:
  1. Check numeric range violations: SELECT * FROM products WHERE price < 0 OR price > 999999.99 and SELECT * FROM users WHERE age < 0 OR age > 150. Map each column to its valid range based on business rules.
  1. Identify duplicate records that violate intended uniqueness: SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1. Determine which duplicate to keep (most recent, most complete) and plan deduplication.
  1. Generate CHECK constraints for validated rules:
  1. Create foreign key constraints with appropriate cascade behavior:
  1. Implement complex business rule validation using database triggers when CHECK constraints are insufficient:
  1. Apply constraints in a safe two-phase approach:
  1. Generate a data quality report summarizing: total records per table, violation counts by constraint type, cleanup actions taken, constraints applied, and remaining data quality issues requiring manual review.

Output

Error Handling

| Error | Cause | Solution |

|-------|-------|---------|

| check constraint violated by existing row | Existing data fails the new constraint | Run the validation query first to find violations; clean up data; use NOT VALID option to add constraint without checking existing data, then validate separately |

| cannot add foreign key: referenced row not found | Orphaned child records reference non-existent parent | Clean up orphaned records first with DELETE or UPDATE to valid parent; or insert missing parent records |

| column cannot be made NOT NULL: contains NULL values | Existing rows have NULL in the target column | Backfill NULLs with UPDATE table SET column = default_value WHERE column IS NULL before adding NOT NULL |

| Trigger function causes performance regression | Complex validation logic executes on every INSERT/UPDATE | Optimize trigger function; use WHEN clause to limit trigger firing; consider CHECK constraints instead of triggers for simple rules |

| Circular foreign key prevents constraint creation | Tables reference each other, preventing creation order | Use ALTER TABLE ADD CONSTRAINT after both tables exist; or use DEFERRABLE INITIALLY DEFERRED constraints |

Examples

Auditing a legacy database with 50,000 invalid email addresses: Validation query reveals 50,000 of 2M user records have invalid email formats (missing @, double dots, spaces). A cleanup script normalizes common issues (trim whitespace, lowercase) and flags 3,000 unfixable records for manual review. After cleanup, a CHECK constraint with regex validation is applied.

Enforcing referential integrity on a database without foreign keys: An application relied on application-level FK enforcement, resulting in 12,000 orphaned order_items, 800 orphaned payments, and 200 orphaned reviews. Cleanup scripts archive orphaned records to backup tables, then foreign key constraints with ON DELETE CASCADE are added. A nightly validation job monitors for new orphans.

Implementing business rules for a financial application: Constraints enforce: account balance cannot be negative (CHECK (balance >= 0)), transfer amount must be positive (CHECK (amount > 0)), transaction date cannot be in the future (CHECK (transaction_date <= CURRENT_DATE)), and a trigger prevents transfers between accounts owned by different customers unless explicitly authorized.

Resources

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.