SQLite Syntax for Creating Table with Foreign Key SQLite Syntax for Creating Table with Foreign Key sqlite sqlite

SQLite Syntax for Creating Table with Foreign Key


This answer might not be related to yours but i thought it should be helpful for others who are working with android database.
IN SQLite Foreign key constraints are disabled by default (for backwards compatibility). You have to enable it explicitly using

PRAGMA foreign_keys = 1

after you establishing your connection with the database.Here's the link to the official docs that explains it in more depth.http://sqlite.org/foreignkeys.html Please navigate to enabling foreign key support in the above link.


See the syntax diagrams.

The first syntax is a table constraint, while the second syntax is a column constraint.In these examples, they behave the same.

You would need a table constraint for a key over multiple columns (where you do not have a single column you could attach it to).