Skip to main content
Data Quality Dimensions

Referential Integrity: The Data Quality Dimension That Holds Databases Together

Referential integrity violations — orphaned records, broken foreign keys — silently corrupt relationships in your data. Here's what integrity means and how to detect violations.

Key Takeaways
  • Integrity means every foreign key references a valid primary key — no orphaned records
  • Orphaned records happen when parent records are deleted without removing dependent child records
  • A LEFT JOIN with WHERE parent IS NULL identifies all orphaned records for any relationship
  • Enable foreign key constraints in your database — they prevent integrity violations at write time
  • Run integrity checks after every bulk import or database migration before trusting the data

What Referential Integrity Means

Referential integrity means that relationships between data in a database are consistent: every foreign key value in a child table must correspond to an existing primary key in a parent table.

In plain English: every order must belong to a customer that exists. Every invoice line must reference a product that exists. Every activity log must reference a user that exists.

When this relationship breaks, you have orphaned records — data that references something that no longer exists.

How Integrity Violations Happen

Cascading deletes not configured: A customer is deleted from the customer table. Their orders remain in the orders table, now referencing a customer_id that no longer exists. The orders are orphaned.

Import errors: A data import adds orders without first importing the customers they reference. Foreign key constraints would catch this — but many databases have constraints disabled for performance during imports.

Sohovi validates your dataset before it enters the warehouse — catching format errors, nulls, and duplicates at the source.

Manual database edits: A developer deletes a record directly from the database to fix a bug, without deleting the dependent records in child tables.

Migration errors: During a platform migration, customers and orders are migrated separately. Some customer records fail to migrate due to data errors. The orders that referenced them are now orphaned.

Detecting Integrity Violations

A SQL query finds orphaned records:

SELECT o.order_id
FROM orders o
LEFT JOIN customers c ON o.customer_id = c.customer_id
WHERE c.customer_id IS NULL

This returns all orders where the customer_id doesn't match any customer record. Each row is a referential integrity violation.

Sohovi finds gaps, duplicates, and format errors in your CRM data — so your team is working from records they can trust.

Run similar queries for every foreign key relationship in your schema.

Measuring Integrity

Integrity rate = (Records with valid references / Total records) × 100

A score of 100% means no orphaned records. A score below 100% means some records reference things that don't exist.

Preventing Integrity Violations

  • Enable foreign key constraints in your database (they prevent integrity violations at write time)
  • Use cascading deletes (or restricts) appropriately for each relationship
  • Test data migrations for integrity before cutting over to the new system
  • Run integrity checks after any bulk import or database operation

Frequently Asked Questions

Should I always enable foreign key constraints?

Yes for production systems where data integrity matters. During bulk imports, constraints are sometimes temporarily disabled for performance — but always re-enable them immediately after the import, and run an integrity check before doing so.

What do I do with orphaned records once I find them?

Options: delete them, link them to a placeholder 'unknown' parent record, or investigate whether the parent records can be restored. The right choice depends on whether the orphaned records have business value. Document your decision.

Do spreadsheets have referential integrity?

No. Spreadsheets don't enforce foreign key relationships. This is a fundamental limitation — relationships in spreadsheets are maintained entirely by human discipline, which is why they break so easily. Moving relational data from spreadsheets to a real database is often about integrity as much as scale.

Selva Santosh

Data quality, for people who ship

Selva writes practical guides on data quality, profiling, and governance to help teams ship better data.

Start for free

Stop guessing. Start knowing your data quality.

Sohovi profiles your datasets in minutes — surfacing completeness gaps, type mismatches, and duplicate patterns before they reach production.

No credit card required · Free forever plan