CSE Roadmap

Core tier · about 3 hours · 5 parts

Part 3 of 5 · 1 min read

One-to-many becomes a foreign key

The foreign key goes on the "many" side, pointing at the "one" side.

Think of a classroom seating chart. Each seat holds exactly one student. One student uses exactly one seat per class period.

In plain words: a one-to-many relationship maps to a foreign key on the "many" side, referencing the "one" side's primary key. The "one" side needs no new column at all.

A worked example. Author-to-Book is one-to-many, so books gets an author_id foreign key. authors gets nothing new.

The key lines.

CREATE TABLE books (
    book_id   INTEGER PRIMARY KEY,
    title     TEXT NOT NULL,
    author_id INTEGER NOT NULL REFERENCES authors(author_id)
);

One-to-many mapping

A one-to-many mapping places a foreign key on the "many" side's table, referencing the "one" side's primary key.

Example: books.author_id referencing authors.author_id correctly places the key on the many side (books), not the one side (authors).