Reference a parent table from a child table.
Section: Create tables and constraints
Add a foreign key
sql
sql
CREATE TABLE posts (
id INTEGER PRIMARY KEY,
user_id INTEGER NOT NULL,
title TEXT NOT NULL,
body TEXT NOT NULL,
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE
);Explanation
Remember to enable foreign key enforcement per connection with `PRAGMA foreign_keys = ON;`.
Learn the surrounding workflow
Compare similar commands or jump into common fixes when this command is part of a bigger troubleshooting path.
Related commands
Same sheet · prioritizing Create tables and constraints
Create a basic table
Define a rowid-backed table with primary key and timestamps.
Create a generated column
Materialize or compute a derived value from another column.