This SQLite post explains how to use Foreign Keys in SQLite with syntax and examples.
What is a Foreign Key in SQLite?
A foreign key is a way to enforce referential integrity within your SQLite database. A foreign key means that values in one table must also appear in another table.
The referenced table is called the parent table while the table with the foreign key is called the child table. The foreign key in the child table will generally reference a primary key in the parent table.
A foreign key can only be defined in a CREATE TABLE statement.
TIP: You can not add a foreign key to a table using ALTER TABLE because SQLite does not support ADD CONSTRAINT in the ALTER TABLE statement. However, we will show you a workaround later in this tutorial that will allow you to add a foreign key to an existing table.
How to Create a Foreign Key using the CREATE TABLE Statement
Syntax
The syntax to create a foreign key using a CREATE TABLE statement in SQLite is:
Example
Let's look at an example of how to create a foreign key using the CREATE TABLE statement in SQLite.
For example:
In this example, we've created a primary key on the departments table that consists of only one field - the department_id field. Then we've created a foreign key called fk_departments on the employees table that references the departments table based on the department_id field.
How to Add a Foreign Key to an Existing Table
You can not use the ALTER TABLE statement to add a foreign key in SQLite. Instead you will need to rename the table, create a new table with the foreign key, and then copy the data into the new table.
Syntax
The syntax to add a foreign key to an existing table in SQLite is:
Example
First, let's start by creating our 2 tables (departments and employees):
Next, let's add some data to these tables:
Now, let's add a foreign key to the employees table:
This example will rename our existing employees table to _employees_old. Then it will create the new employees table with a foreign key called fk_departments that references the departments table based on the department_id field. Then it will insert all of the data from the _employees_old table into the employees table.
This workaround allows you to add a foreign key to the employees table without losing the data in the table.