Should SQLite foreign-key columns have their own indexes?
Quick answer
Usually, yes: create an index on a child foreign-key column unless you have a specific reason not to. SQLite may need to find matching child rows when a parent row is deleted or its key changes; without a usable index, that search scans the child table. The index need not be UNIQUE, and creating it is separate from declaring the foreign-key constraint. [S1]
The question, the details, the next step.
A practical checklist
Identify each child table and its foreign-key column or columns.
Keep the foreign-key declaration; decide separately whether to add an index.
Consider a non-unique child-key index when parent rows may be deleted or their keys changed.
Check whether an existing index can serve the child-key lookups before adding another.
Verify foreign-key enforcement in the application’s connection setup; do not assume an index enables it.
A few more useful answers
Does SQLite require a child-key index for a foreign key to work?
No. SQLite says child-key indexes are not required, though they are almost always beneficial and are recommended in most real systems. [S1]
Should the child-key index be unique?
Not just because the column is a foreign key. SQLite says the index need not be unique; make it unique only if that is a separate requirement of your data model. [S1]
Does adding the index turn on foreign-key enforcement?
No. An index and a foreign-key declaration are separate choices, and SQLite’s documentation says enforcement must also be enabled on each connection, assuming support is present. [S1]
Sources
References used for this guide.
- [S1] SQLite Foreign Key Support: Required and Suggested Database Indexes sqlite.org · Checked
