Lesson 13 of 22 · SQLite

Foreign Keys

Link tables with SQL FOREIGN KEY constraints and enforce referential integrity.

Foreign keys create relationships between tables, enforcing referential integrity.

Syntax

CREATE TABLE child_table (
    id INTEGER PRIMARY KEY,
    parent_id INTEGER,
    FOREIGN KEY (parent_id) REFERENCES parent_table(id)
);

Your challenge

Create a table sightings with id (integer primary key), person_id (integer, NOT NULL, a foreign key to person.id) and place (text). Record that person 32 was seen at 'Get Fit Now Gym'.

Solve it in your browser

Free, no sign-up. The SQLite database runs in your browser; your answer is checked instantly, with hints if you need them.