SQLite Forum

Help with foreign key
You're welcome, of course.

Without gainsaying what Doug and Keith have said, (which I second, for what it's worth), a few more points may help it to *work*, for a definition of "work" that you may have meant and might want to adopt. (BTW, that is a word infamous for **not** conveying useful information. Better is to show what happened, and where the disfunction is not obvious from that, to show or explain what you expected or consider to *work*.)

Per that syntax diagram I linked earlier, you can specify the optional [foreign-key-clause](https://sqlite.org/syntax/foreign-key-clause.html) nested among the expansions. And when you study what it does, you will see that, without some of the optional parts below the top row of the railroad chart, your declared foreign key (with Doug's rewrite) does little more than signify intent. To get some enforcement with SQLite's help, you need <code>pragma foreign_keys=on</code> (as Keith mentioned) **and**, unless you plan to carefully honor the constraint during modifications via ordering of operations and extra code, you need to tell the DBMS what to do when, in the course of table content creation and modification, you cause the specified relationship to be violated. Absent those clauses, the enforcement you will get from SQLite is rejection, by returned error codes, of the changes that produce a violation.

(Edited to cure overstatement of bare FK clause effect.)