SQL & Databases·Database skills from the start

Database design basics

Learn the building blocks of a database: tables, keys, and how records link together.

Beginner 12 min read Lesson 3 of 480 Challenge included
What you will be able to do
  • Identify tables, columns, rows, and keys in a schema
  • Use primary and foreign keys to link related records
  • Tell a one-to-many relationship from a many-to-many relationship
  • Explain why normalisation reduces duplicated data

In plain English

A database stores information in tables, which work like spreadsheets: each table is a grid of columns (the fields, such as a name) and rows (one record, such as a single person). A primary key is a column that uniquely identifies each row, while a foreign key is a column in another table that points back to a primary key so records can be linked. A one-to-many relationship means one row can be linked to many rows in another table (one author, many books), while a many-to-many relationship needs a joining table in the middle (one book can have many tags, and one tag can appear on many books). Normalisation means organising tables so each fact is stored in exactly one place, which reduces duplicated data and keeps updates consistent.

Step by step

  1. 1List the real-world things you need to track, such as authors, books, and tags, and give each one its own table.
  2. 2Add a primary key column to each table so every row has a unique id.
  3. 3Add foreign key columns to link tables together, like author_id on books.
  4. 4For a many-to-many link, create a joining table with foreign keys to both sides.
  5. 5Split repeated information into separate tables so each fact lives in one place.
schema.sqlSQL
CREATE TABLE authors (
  id SERIAL PRIMARY KEY,
  name TEXT NOT NULL
);

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

CREATE TABLE tags (
  id SERIAL PRIMARY KEY,
  label TEXT NOT NULL UNIQUE
);

CREATE TABLE book_tags (
  book_id INTEGER NOT NULL REFERENCES books(id),
  tag_id INTEGER NOT NULL REFERENCES tags(id),
  PRIMARY KEY (book_id, tag_id)
);

Common mistakes

  • Storing the same author name on every one of their books, so a rename must be updated in many rows.
  • Forgetting to add a primary key, which makes rows impossible to identify uniquely.
  • Skipping a joining table for a many-to-many relationship and cramming several values into one column.
  • Creating one huge table with every piece of information instead of a few focused tables.
Your turn

Design a borrowing table

Easy

Design a library_loans table that records which borrower took out which book and when. The books and borrowers tables already exist and each has a primary key column named id. Each loan should also have a unique id of its own.

SQL · starter
CREATE TABLE library_loans (

);
Check your understanding before moving on.
Knowledge checkQuestion 1

What is the purpose of a foreign key?

Select an answer to continue
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 3 of 480 · 0% of course