This SQLite post explains how to drop a foreign key in SQLite with syntax and examples.
Once a foreign key has been created, you may find that you wish to drop the foreign key from the table. Let's explore how to do this.
You can not use the ALTER TABLE statement to drop a foreign key in SQLite. Instead you will need to rename the table, create a new table without the foreign key, and then copy the data into the new table.
The syntax to drop a foreign key in SQLite is:
If you had created a foreign key in SQLite as follows:
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.
If we then wanted to drop the foreign key called fk_departments, we could execute the following command:
This example will rename our existing employees table to _employees_old. Then it will create the new employees table without a foreign key. Then it will insert all of the data from the _employees_old table into the employees table.
This workaround allows you to drop a foreign key from the employees table without losing the data in the table.