Find and Repair an Orphaned SQLite Reference in DB Browser
A SQLite database can pass quick_check while containing a declared foreign-key violation. This DB Browser for SQLite tutorial finds one fictional invoice pointing to a missing customer, repairs only the independently verified reference and checks the saved file through a new connection.
Work on a copy and keep the evidence
Use the fictional practice database, or a backed-up working copy of your own file. The example has customers 1 and 2, and three invoices with units 10, 20 and 30. Invoice 2 deliberately references customer 9, which does not exist. A separate fictional intake receipt identifies customer 2 as the correct reference.
The receipt matters. An orphaned reference proves that a relationship is invalid; it does not establish who the intended customer was. Stop if you cannot obtain trustworthy correction evidence. Do not assign an invoice to whichever customer happens to be available.
Run the two checks separately
Open the database in DB Browser and select Execute SQL. Run each statement and inspect its own result:
PRAGMA quick_check;
PRAGMA foreign_key_check;In the practice file, the first statement returns ok. The second returns a row with invoices, 2, customers and 0. Those values identify the child table, offending rowid, parent table and foreign-key identifier. Here the integer primary key matches rowid; do not assume that mapping for every schema.
SQLite’s quick-check documentation describes its structural checks, while foreign_key_check tests declared references. A clean structural result does not replace a relationship check.
Inspect the unmatched row without hiding it
SELECT i.id, i.customer_id, c.name, i.units
FROM invoices AS i
LEFT JOIN customers AS c ON c.id = i.customer_id
ORDER BY i.id;The left join keeps invoice 2 visible, with customer ID 9 and a NULL customer name. An inner join would omit that invoice. Compare the row with the separate receipt and verify the intended customer record before preparing an update.
Apply a narrowly guarded correction
The recorded repair targets both the invoice ID and its old, invalid reference:
UPDATE invoices
SET customer_id = 2
WHERE id = 2 AND customer_id = 9;
SELECT changes() AS rows_repaired;
SELECT COUNT(*) AS violations
FROM pragma_foreign_key_check;Run the update, then immediately read changes() before performing another write. Expect one affected row. Expect zero foreign-key violations afterward. If the update affects zero rows, inspect the current data; do not broaden the WHERE clause to force a match. More than one affected row would contradict this example’s primary-key target and needs investigation.
Use DB Browser’s Write Changes control to persist the correction. The interface may already have an open transaction, so do not blindly add a second BEGIN around these statements. If you find an unexpected result, use the application’s transaction controls deliberately and retain your original copy.
Verify the saved database through a new connection
Close and reopen the file, then inspect the saved state. The recorded independent checker confirmed all three original invoices remained, total units stayed 60, invoice 2 referenced customer 2 and no declared foreign-key violations remained.
SELECT COUNT(*) AS invoices, SUM(units) AS units FROM invoices;
SELECT customer_id FROM invoices WHERE id = 2;
PRAGMA foreign_key_check;Expected results are 3 invoices, 60 units, customer 2, and no rows from the final PRAGMA. An empty foreign-key result validates declared constraints; it cannot detect an undeclared relationship or a semantically wrong but existing customer.
Prevent later orphaned writes
Enable and verify PRAGMA foreign_keys = ON; for each application connection, before starting its transaction. This is connection-specific and does not repair older data. See SQLite’s foreign-key guidance for enforcement details. Keep the backup, correction source, targeted update and independent readback together. For another data-integrity check, our SQLite integer-cents guide verifies exact stored money totals. More workflows are in the tutorial library.
James Kennedy is a writer and product researcher at Drives Hero with a background in IT administration and consulting. He has hands-on experience with storage, networking, and system performance, and regularly improves and optimizes his home networking setup.

