Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

How do SQLite foreign keys connect child rows to parent rows?

Quick answer

A foreign key is a REFERENCES constraint on a child table’s column or columns. It points to named column(s) in a parent table; those parent columns must be a primary key or have a qualifying UNIQUE constraint or index. A child reference is valid when its value matches the parent key under SQLite’s comparison rules. If any child-key column is NULL, a matching parent row is not required.

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I have projects and tasks. How does a foreign key connect a task to its project?

Geez

Put the REFERENCES constraint on the task table: tasks is the child, and projects is the parent. The child column stores the value that refers to the parent’s key. The parent key must use named columns, not the rowid, and must be a primary key or have a qualifying UNIQUE constraint or unique index. [S1]

For example, assuming each project has an integer project_id and tasks belong to one project:

CREATE TABLE projects (
  project_id INTEGER PRIMARY KEY,
  name TEXT NOT NULL
);

CREATE TABLE tasks (
  task_id INTEGER PRIMARY KEY,
  project_id INTEGER REFERENCES projects(project_id),
  title TEXT NOT NULL
);

Here, tasks.project_id is the child key and projects.project_id is the parent key. The constraint describes the relationship; it does not mean the task row contains the project’s other fields.

Follow-up question

What makes a task’s project_id a valid reference?

Geez

For a non-NULL child key, its value must match the corresponding parent-key value under SQLite’s comparison rules. [S1] Suppose the projects table contains a row with project_id 12. A task with project_id 12 has a matching parent reference. If there is no parent row whose key matches a task’s value, that value has no corresponding parent and does not meet the reference condition.

For this example, the parent key is the single column project_id, so the comparison is one value to one value. With a parent key made of multiple columns, each child-key value must match its corresponding parent-key value. This guide explains the schema relationship and what counts as a match; it does not cover retrieving related rows.

Follow-up question

Can I leave project_id empty, and what would be an invalid example?

Geez

With the schema above, project_id is nullable because it was not declared NOT NULL. SQLite does not require a matching parent row when any child-key column is NULL. [S1] If every task must refer to a project, declare the child column NOT NULL as well:

project_id INTEGER NOT NULL REFERENCES projects(project_id)

Assuming the only project keys are 12 and 15, a task whose project_id is 99 has no matching parent key, so its reference is invalid. A task whose project_id is NULL has no parent match, but the NULL rule means a parent row is not required. [S1]

Follow-up question

How should I check my schema before relying on this relationship?

Geez

Check that the REFERENCES clause is on the child table, that it names the intended parent table and columns, and that those parent columns qualify as a key. Then decide whether an unmatched child should be allowed to have NULL. In the example, projects.project_id is the parent primary key; tasks.project_id refers to it; and NOT NULL is appropriate only if every task is required to have a project.

A compact review example:

-- Parent key: projects.project_id
-- Child reference: tasks.project_id
-- Matching value: 12, if a parent with key 12 exists
-- Unmatched value: 99, if no parent with key 99 exists
-- Optional reference: NULL, when the child key is nullable

These comments describe the intended relationship. The schema example shows the parent key, child reference, and the difference between matching, unmatched, and nullable child-key values; it does not show how to retrieve related records.

A practical checklist

  • Put the REFERENCES constraint on the child table’s intended foreign-key column or columns.

  • Name the parent table and its named key columns; do not use rowid as the parent key.

  • Confirm the parent columns form a primary key or have a qualifying UNIQUE constraint or unique index.

  • Decide whether NULL is an acceptable child-key value; use NOT NULL if every child must name a parent.

  • Use sample values to distinguish a matching parent key from a value with no corresponding parent.

A few more useful answers

Can a foreign key refer to a parent column that is not the primary key?

Yes, if the referenced parent columns collectively have a qualifying UNIQUE constraint or unique index. The parent key must be named columns, not rowid. [S1]

Does a NULL child key need a matching parent row?

No. If any column in the child key is NULL, SQLite does not require a corresponding parent row. [S1]

Does this relationship tell me how to display a task with its project?

No. A foreign key describes the schema relationship and reference condition; this guide does not provide a retrieval-query pattern.

Sources

References used for this guide.

  1. [S1] SQLite foreign keys: child keys, parent keys, and valid references
    sqlite.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work