foreign_keys
Enforce FOREIGN KEY constraints.
- Par défaut
0 (off)- Range
- 0, 1
- Restart needed
- Non
- Default checked on
- SQLite 3.5x
Ce que cela fait
Off by default for backwards compatibility: REFERENCES clauses are parsed and stored but not checked. It cannot be changed inside a transaction; the PRAGMA is silently ignored there.
Valeur par défaut
0 (off). Off (a stock SQLite 3.5x build) unless the build sets SQLITE_DEFAULT_FOREIGN_KEYS=1.
Check the current value
PRAGMA foreign_keys; SELECT * FROM pragma_foreign_keys; -- table-valued form, usable in queries
Change it
PRAGMA foreign_keys = ON; -- this connection only
No restart. It applies at once to the connection that runs it and is lost when that connection closes, so set it every time a connection opens.
When to change it
In every application that declares foreign keys. Run PRAGMA foreign_key_check after enabling it on an existing database to find violations.
What can go wrong
Existing orphan rows are not detected until you run foreign_key_check; ON DELETE CASCADE actions start firing.
Questions about foreign_keys
What is the default value of foreign_keys in SQLite?
0 (off). Off (a stock SQLite 3.5x build) unless the build sets SQLITE_DEFAULT_FOREIGN_KEYS=1.
Does changing foreign_keys require a restart?
No. No restart. It applies at once to the connection that runs it and is lost when that connection closes, so set it every time a connection opens.
When should I change foreign_keys?
In every application that declares foreign keys. Run PRAGMA foreign_key_check after enabling it on an existing database to find violations.
Back to all SQLite settings, or check the SQLite manual for every option.
Related on SQLly
Check foreign_keys on your own server
SQLly runs the query above against SQLite and the other engines in this reference, from one desktop app on macOS, Windows and Linux.