Lesson 1 · Unit 04
Modeling data
One fact in one place, and a schema that says no
What you practise
- Normal forms by example (repeated groups, duplicated facts), update anomalies, when to denormalize, choosing keys and types,
NOT NULL,UNIQUE, foreign keys
Try a question from this lesson
Pick one
Everything lives in one table. Ana changes her email. How many rows must be updated?
CREATE TABLE orders(
id, customer, email, item);
INSERT INTO orders VALUES
(1, 'Ana', 'ana@x.io', 'pen'),
(2, 'Bo', 'bo@x.io', 'ink'),
(3, 'Ana', 'ana@x.io', 'pad'),
(4, 'Ana', 'ana@x.io', 'cap');Correct answer
3
Why
Her email is stored once per order. Update two of the three and the table now says Ana has two different emails, with no way to tell which is right.
The ideas
Concept cards are short on purpose. In the app they appear between questions, after you have met the idea.
Concept
One fact, one place
When one fact is stored in many rows, every change must find all the copies. Miss one and the data contradicts itself: an update anomaly.
Normalizing means splitting tables until each fact lives in exactly one row. Other tables point at that row by its key.
CREATE TABLE customers(
id INTEGER PRIMARY KEY, name, email);
CREATE TABLE orders(
id INTEGER PRIMARY KEY,
customer_id, item);Concept
Let the schema say no
A schema is more than column names. Types and constraints are rules the database checks on every write, so bad data is refused at the door instead of found in a report months later.
Every rule you declare is a bug that no application, script or tired colleague can introduce.
CREATE TABLE users(
id INTEGER PRIMARY KEY,
email TEXT NOT NULL UNIQUE,
age INT CHECK (age >= 0));10 more exercises in the app
- 4× multiple choice
- 1× fill the blanks
- 2× type the answer
- 1× true or false
- 1× match the pairs
- 1× order the lines
Bitpath is an iOS app in development. Join the waitlist and you get one email when it is ready to try.