SQLite Forum

Help with foreign key
Login
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 might be considered to *work*.)

Per that syntax diagram I linked for you, you can specify the optional [foreign-key-clause](https://sqlite.org/syntax/foreign-key-clause.html) buried among the expansions. And if you study what it does, you will learn 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** 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. That is what those additional optional clauses of the foreign key clause are for, to make the clause something more than decorative.