Skip to main content
SidsProjectImpact

Back to all posts

How to Add A Foreign Key In SQLite?

Published on
4 min read
How to Add A Foreign Key In SQLite? image

Best Database Management Tools to Buy in January 2026

1 Office Suite 2025 Special Edition for Windows 11-10-8-7-Vista-XP | PC Software and 1.000 New Fonts | Alternative to Microsoft Office | Compatible with Word, Excel and PowerPoint

Office Suite 2025 Special Edition for Windows 11-10-8-7-Vista-XP | PC Software and 1.000 New Fonts | Alternative to Microsoft Office | Compatible with Word, Excel and PowerPoint

  • COMPREHENSIVE SUITE: ALL-IN-ONE TOOL FOR DOCUMENTS, SPREADSHEETS, AND PRESENTATIONS.

  • CREATIVE FREEDOM: ACCESS 1,000 FONTS AND 20,000 CLIPART IMAGES!

  • SEAMLESS COMPATIBILITY: WORKS FLAWLESSLY WITH MICROSOFT OFFICE FORMATS.

BUY & SAVE
$14.00
Office Suite 2025 Special Edition for Windows 11-10-8-7-Vista-XP | PC Software and 1.000 New Fonts | Alternative to Microsoft Office | Compatible with Word, Excel and PowerPoint
2 LibreOffice Suite 2025 Home and Student for - PC Software Professional Plus - compatible with Word, Excel and PowerPoint for Windows 11 10 8 7 Vista XP 32 64-Bit PC

LibreOffice Suite 2025 Home and Student for - PC Software Professional Plus - compatible with Word, Excel and PowerPoint for Windows 11 10 8 7 Vista XP 32 64-Bit PC

  • ALL-IN-ONE SUITE: WORD, EXCEL, PRESENTATIONS & MORE, 20,000 CLIPART!
  • SEAMLESS COMPATIBILITY: WORKS WITH ALL OFFICE VERSIONS & WINDOWS OS.
  • RELIABLE SUPPORT: GET QUICK HELP WITH E-MAIL TECHNICAL SUPPORT INCLUDED!
BUY & SAVE
$12.99
LibreOffice Suite 2025 Home and Student for - PC Software Professional Plus - compatible with Word, Excel and PowerPoint for Windows 11 10 8 7 Vista XP 32 64-Bit PC
3 Office Suite 2026 on CD DVD Disc | Compatible with Microsoft Office 2024 2021 365 2019 2016 2013 2010 2007 Word Excel PowerPoint | Powered by Apache OpenOffice for Windows 11 10 8 7 Vista XP PC & Mac

Office Suite 2026 on CD DVD Disc | Compatible with Microsoft Office 2024 2021 365 2019 2016 2013 2010 2007 Word Excel PowerPoint | Powered by Apache OpenOffice for Windows 11 10 8 7 Vista XP PC & Mac

  • AFFORDABLE ALTERNATIVE TO MICROSOFT OFFICE, NO SUBSCRIPTIONS NEEDED!

  • FULL COMPATIBILITY WITH WORD, EXCEL, AND POWERPOINT FORMATS.

  • LIFETIME UPDATES AND UNLIMITED USERS FOR HASSLE-FREE INSTALLATION.

BUY & SAVE
$19.99
Office Suite 2026 on CD DVD Disc | Compatible with Microsoft Office 2024 2021 365 2019 2016 2013 2010 2007 Word Excel PowerPoint | Powered by Apache OpenOffice for Windows 11 10 8 7 Vista XP PC & Mac
4 EZ Home and Office Address Book Software

EZ Home and Office Address Book Software

  • EASILY ORGANIZE CONTACTS WITH CUSTOMIZABLE CATEGORIES & DATABASES.

  • PRINT VIBRANT LABELS WITH CLIP ART ON POPULAR AVERY FORMATS!

  • ENJOY PERSONAL SUPPORT DIRECTLY FROM THE SOFTWARE'S CREATOR!

BUY & SAVE
$29.95
EZ Home and Office Address Book Software
5 Membership Manage Professional; 100,000 Member Database Tracking and Management Software; Multiuser License (Online Access Code Card) Win, Mac, Smartphone

Membership Manage Professional; 100,000 Member Database Tracking and Management Software; Multiuser License (Online Access Code Card) Win, Mac, Smartphone

  • LIFETIME ACCESS WITH NO MONTHLY FEES-SAVE MONEY LONG-TERM!
  • EFFORTLESSLY MANAGE MEMBER DETAILS AND TRACK PAYMENTS IN ONE PLACE.
  • STREAMLINE ATTENDANCE, INVOICING, AND EVENT REGISTRATION WITH EASE!
BUY & SAVE
$40.00
Membership Manage Professional; 100,000 Member Database Tracking and Management Software; Multiuser License (Online Access Code Card) Win, Mac, Smartphone
6 Family Tree Heritage Gold 16 - Genealogy Software - Includes Free Searches to FamilySearch, the World's Largest Genealogy Database - CD/PC

Family Tree Heritage Gold 16 - Genealogy Software - Includes Free Searches to FamilySearch, the World's Largest Genealogy Database - CD/PC

  • ACCESS 14B+ HISTORICAL RECORDS FOR FREE & UNCOVER YOUR ANCESTRY!

  • USER-FRIENDLY INTERFACE: BUILD AND SHARE YOUR FAMILY TREE EASILY!

  • INNOVATIVE TREETIPS PROVIDE ANCESTRY HINTS RIGHT AT YOUR FINGERTIPS!

BUY & SAVE
$49.99
Family Tree Heritage Gold 16 - Genealogy Software - Includes Free Searches to FamilySearch, the World's Largest Genealogy Database - CD/PC
7 Database Design for Mere Mortals: 25th Anniversary Edition

Database Design for Mere Mortals: 25th Anniversary Edition

BUY & SAVE
$18.88 $54.99
Save 66%
Database Design for Mere Mortals: 25th Anniversary Edition
8 Database Development For Dummies

Database Development For Dummies

  • QUALITY ASSURED: ALL BOOKS CHECKED FOR READABILITY AND MINOR WEAR.
  • ECO-FRIENDLY: SAVE TREES BY CHOOSING SUSTAINABLE PRE-OWNED BOOKS.
  • GREAT VALUE: AFFORDABLE PRICING FOR HIGH-QUALITY USED LITERATURE.
BUY & SAVE
$21.42 $41.99
Save 49%
Database Development For Dummies
+
ONE MORE?

To add a foreign key in SQLite, you need to follow these steps:

  1. Ensure you are using SQLite version 3.6.19 or higher, as foreign key support was added in this version.
  2. Create the main table (referred to as the parent table) using the CREATE TABLE statement. Define the primary key for this table using the PRIMARY KEY constraint.
  3. Create the child table that will have the foreign key reference (referred to as the child table) using the CREATE TABLE statement. Define the foreign key column using the FOREIGN KEY constraint.
  4. Specify the relationship between the parent and child table by setting the foreign key constraint. To do this, use the ALTER TABLE statement with the ADD CONSTRAINT keyword. Specify the foreign key column and the REFERENCES clause to indicate the parent table and the primary key column it references. Example: ALTER TABLE child_table ADD CONSTRAINT fk_name FOREIGN KEY (foreign_key_column) REFERENCES parent_table (primary_key_column); Note that the column names used in the foreign key constraint should match the respective column names in the tables.
  5. Enable foreign key constraints for your database connection by executing the PRAGMA foreign_keys = ON; command. This ensures that the foreign key constraints are enforced.

Once you have followed these steps, the foreign key relationship will be established between the two tables, and SQLite will enforce referential integrity by automatically checking and enforcing the constraints.

What is the significance of the ON DELETE CASCADE clause in a foreign key constraint in SQLite?

The ON DELETE CASCADE clause in a foreign key constraint in SQLite is used to specify the action that should be taken when a referenced row in the parent table is deleted.

When the ON DELETE CASCADE clause is included in a foreign key constraint, it means that if a row in the parent table is deleted, then all the rows in the child table that are referencing it will also be deleted automatically. This cascading effect ensures data integrity and consistency between the two tables.

The significance of the ON DELETE CASCADE clause is that it simplifies the process of deleting related rows in the child table when a referenced row in the parent table is removed. It saves developers from manually deleting each dependent row in the child table, reducing complexity and potential errors in managing data relationships.

However, it is important to use the ON DELETE CASCADE clause with caution as it can lead to unintended data loss if not implemented properly.

Are foreign keys supported in all versions of SQLite?

Yes, foreign keys are supported in all modern versions of SQLite, starting from version 3.6.19. However, it is important to note that foreign key support is not enabled by default. It needs to be specifically enabled for each database connection by executing the "PRAGMA foreign_keys = ON" command, or by configuring the database to enable foreign key support at compile time.

How can you check if a foreign key constraint exists in SQLite?

To check if a foreign key constraint exists in SQLite, you can use the following query:

PRAGMA foreign_key_list(table_name);

Replace table_name with the actual name of the table you want to check. This query will return a list of foreign key constraints for the specified table. If there are no foreign key constraints, an empty result will be returned.

Alternatively, you can use the sqlite_master table to check for the existence of foreign key constraints. Here's an example query:

SELECT name FROM sqlite_master WHERE type = 'table' AND sql LIKE '%FOREIGN KEY%';

This query will return the names of the tables that have foreign key constraints defined. If there are no foreign key constraints, an empty result will be returned.