Integrity Check
Integrity checks help detect low-level structural issues early.
Run them as part of maintenance and before critical migrations.
Core Concepts
flowchart TD
A[Run integrity pragma] --> B{Result}
B -->|ok| C[Structure looks healthy]
B -->|errors| D[Investigate and restore/repair]
| Check | Scope |
|---|---|
PRAGMA integrity_check; | Thorough structural validation |
PRAGMA quick_check; | Faster, less exhaustive check |
Code Examples
-- Thorough integrity verification.
PRAGMA integrity_check;
| integrity_check |
|---|
| ok |
-- Faster check for routine health monitoring.
PRAGMA quick_check;
| quick_check |
|---|
| ok |
-- Optional schema-level sanity listing.
SELECT name, type
FROM sqlite_schema
WHERE type IN ('table', 'index', 'view')
ORDER BY type, name;
| name | type |
|---|---|
| Schema objects listed |
SQLite-Specific Nuances
SQLite Nuance
Integrity checks validate on-disk structure, not business correctness.
You still need application-level validation for domain rules.
Common Pitfalls / Best Practices
Pitfall
Ignoring non-ok integrity output and continuing writes as if nothing happened.
Best Practice
Automate regular quick_check and schedule deeper integrity_check on maintenance windows.
Quick Challenge
Run a health check and return a single ok result for a healthy database.
View Solution
PRAGMA integrity_check;
| integrity_check |
|---|
| ok |