Database Normalization · Lesson 35 of 112

Database First Normal Form

What you will learn

  • The rule of first normal form (1NF)
  • Why cells with lists break searching and counting
  • Why repeating columns (phone1, phone2...) are also wrong
  • How to fix both with a child table

1. The rule

A table is in first normal form when:

  1. every cell holds one value (it is atomic: not a list, not several facts glued together);
  2. there are no repeating groups of columns (phone1, phone2, phone3);
  3. every row can be identified (it has a key).

2. Lists in a cell

This lesson runs on the sample database you chose in lesson 4.1 (use the Example data picker at the top). It adds small tables of its own to your browser's copy; run the examples from top to bottom, and press Reset data to start again.

Here a person's phones are stored as text with commas:

name phones
Rina 01711, 01712
Sam 01822
Lee 01933, 01934, 01935
CREATE TABLE people (name TEXT, phones TEXT);
INSERT INTO people VALUES ('Rina', '01711, 01712'), ('Sam', '01822'), ('Lee', '01933, 01934, 01935');
SELECT * FROM people;
name  phones
----  -------------------
Rina  01711, 01712
Sam   01822
Lee   01933, 01934, 01935
(3 rows)
CREATE TABLE people (name TEXT, phones TEXT);
INSERT INTO people VALUES ('Rina', '01711, 01712'), ('Sam', '01822'), ('Lee', '01933, 01934, 01935');
SELECT * FROM people;
name  phones
----  -------------------
Rina  01711, 01712
Sam   01822
Lee   01933, 01934, 01935
(3 rows)
CREATE TABLE people (name TEXT, phones TEXT);
INSERT INTO people VALUES ('Rina', '01711, 01712'), ('Sam', '01822'), ('Lee', '01933, 01934, 01935');
SELECT * FROM people;
name  phones
----  -------------------
Rina  01711, 01712
Sam   01822
Lee   01933, 01934, 01935
(3 rows)

"Who has 01712?" Plain equality finds nobody useful, because the cell is 01711, 01712, not 01712:

SELECT name FROM people WHERE phones = '01712';
SELECT name FROM people WHERE phones LIKE '%01712%';
name
----
(0 rows)

name
----
Rina
(1 row)
SELECT name FROM people WHERE phones = '01712';
SELECT name FROM people WHERE phones LIKE '%01712%';
name
----
(0 rows)

name
----
Rina
(1 row)
SELECT name FROM people WHERE phones = '01712';
SELECT name FROM people WHERE phones LIKE '%01712%';
name
----
(0 rows)

name
----
Rina
(1 row)

LIKE '%...%' works here, but it cannot use an index (chapter 7) and it matches parts of other values. Add a person whose number is 0171, then search for 0171:

INSERT INTO people VALUES ('Amy', '0171');
SELECT name FROM people WHERE phones LIKE '%0171%' ORDER BY name;
name
----
Amy
Rina
(2 rows)
INSERT INTO people VALUES ('Amy', '0171');
SELECT name FROM people WHERE phones LIKE '%0171%' ORDER BY name;
name
----
Amy
Rina
(2 rows)
INSERT INTO people VALUES ('Amy', '0171');
SELECT name FROM people WHERE phones LIKE '%0171%' ORDER BY name;
name
----
Amy
Rina
(2 rows)

Rina is returned even though none of her numbers is 0171. Other jobs are just as awkward: counting phones per person, removing one number, or keeping numbers unique all mean cutting up text.

3. Repeating columns

The "fix" many people try is more columns:

name phone1 phone2 phone3
Rina 01711 01712 NULL
CREATE TABLE phone_columns (name TEXT, phone1 TEXT, phone2 TEXT, phone3 TEXT);
INSERT INTO phone_columns VALUES ('Rina', '01711', '01712', NULL);
SELECT name FROM phone_columns WHERE '01712' IN (phone1, phone2, phone3);
name
----
Rina
(1 row)
CREATE TABLE phone_columns (name TEXT, phone1 TEXT, phone2 TEXT, phone3 TEXT);
INSERT INTO phone_columns VALUES ('Rina', '01711', '01712', NULL);
SELECT name FROM phone_columns WHERE '01712' IN (phone1, phone2, phone3);
name
----
Rina
(1 row)
CREATE TABLE phone_columns (name TEXT, phone1 TEXT, phone2 TEXT, phone3 TEXT);
INSERT INTO phone_columns VALUES ('Rina', '01711', '01712', NULL);
SELECT name FROM phone_columns WHERE '01712' IN (phone1, phone2, phone3);
name
----
Rina
(1 row)

Every search must name every column. Person number four with four phones means changing the table and every query. This also breaks 1NF, because the three columns are one repeating group.

4. The fix: a child table

Give the many values their own table, one row per value, linked to the person (a one-to-many relationship, lesson 3.2):

person_id name
1 Rina
2 Sam
3 Lee
4 Amy
person_id phone
1 01711
1 01712
2 01822
3 01933
3 01934
3 01935
4 0171
CREATE TABLE persons (person_id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE phones (
    person_id INTEGER REFERENCES persons (person_id),
    phone     TEXT,
    PRIMARY KEY (person_id, phone)
);
INSERT INTO persons VALUES (1, 'Rina'), (2, 'Sam'), (3, 'Lee'), (4, 'Amy');
INSERT INTO phones VALUES (1, '01711'), (1, '01712'), (2, '01822'), (3, '01933'), (3, '01934'), (3, '01935'), (4, '0171');
(7 rows changed)
SELECT p.name
FROM persons p JOIN phones f ON f.person_id = p.person_id
WHERE f.phone = '01712';
name
----
Rina
(1 row)
SELECT p.name, COUNT(*) AS phones
FROM persons p JOIN phones f ON f.person_id = p.person_id
GROUP BY p.name
ORDER BY p.name;
name  phones
----  ------
Amy        1
Lee        3
Rina       2
Sam        1
(4 rows)
CREATE TABLE persons (person_id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE phones (
    person_id INTEGER REFERENCES persons (person_id),
    phone     TEXT,
    PRIMARY KEY (person_id, phone)
);
INSERT INTO persons VALUES (1, 'Rina'), (2, 'Sam'), (3, 'Lee'), (4, 'Amy');
INSERT INTO phones VALUES (1, '01711'), (1, '01712'), (2, '01822'), (3, '01933'), (3, '01934'), (3, '01935'), (4, '0171');
(7 rows changed)
SELECT p.name
FROM persons p JOIN phones f ON f.person_id = p.person_id
WHERE f.phone = '01712';
name
----
Rina
(1 row)
SELECT p.name, COUNT(*) AS phones
FROM persons p JOIN phones f ON f.person_id = p.person_id
GROUP BY p.name
ORDER BY p.name;
name  phones
----  ------
Amy        1
Lee        3
Rina       2
Sam        1
(4 rows)
CREATE TABLE persons (person_id INTEGER PRIMARY KEY, name TEXT);
CREATE TABLE phones (
    person_id INTEGER REFERENCES persons (person_id),
    phone     TEXT,
    PRIMARY KEY (person_id, phone)
);
INSERT INTO persons VALUES (1, 'Rina'), (2, 'Sam'), (3, 'Lee'), (4, 'Amy');
INSERT INTO phones VALUES (1, '01711'), (1, '01712'), (2, '01822'), (3, '01933'), (3, '01934'), (3, '01935'), (4, '0171');
(7 rows changed)
SELECT p.name
FROM persons p JOIN phones f ON f.person_id = p.person_id
WHERE f.phone = '01712';
name
----
Rina
(1 row)
SELECT p.name, COUNT(*) AS phones
FROM persons p JOIN phones f ON f.person_id = p.person_id
GROUP BY p.name
ORDER BY p.name;
name  phones
----  ------
Amy        1
Lee        3
Rina       2
Sam        1
(4 rows)

An exact search and a count per person are now one simple query each. The primary key (person_id, phone) also stops the same number being stored twice for one person.

The sample databases follow this rule. Each of their phone or email cells holds one value, and a thing that can have many (orders of a customer, accounts of a customer, enrollments of a student) lives in its own table:

SELECT customer_id, phone FROM customers WHERE phone IS NOT NULL ORDER BY customer_id LIMIT 3;
SELECT customer_id, COUNT(*) AS orders FROM orders GROUP BY customer_id ORDER BY customer_id LIMIT 3;
customer_id  phone
-----------  ------------
          1  +35 49854883
          3  +58 13864688
          4  +48 26805141
(3 rows)

customer_id  orders
-----------  ------
          1       9
          2       6
          3       6
(3 rows)
SELECT customer_id, full_name FROM customers ORDER BY customer_id LIMIT 3;
SELECT customer_id, COUNT(*) AS accounts FROM accounts GROUP BY customer_id ORDER BY customer_id LIMIT 3;
customer_id  full_name
-----------  -----------
          1  Chen Park
          2  Rafi Uddin
          3  Grace Kamau
(3 rows)

customer_id  accounts
-----------  --------
          1         1
          3         2
          5         2
(3 rows)
SELECT student_id, email FROM students WHERE email IS NOT NULL ORDER BY student_id LIMIT 3;
SELECT student_id, COUNT(*) AS enrollments FROM enrollments GROUP BY student_id ORDER BY student_id LIMIT 3;
student_id  email
----------  ------------------------------
         1  vera.khan38@student.example
         2  elena.haque48@student.example
         3  chloe.garcia13@student.example
(3 rows)

student_id  enrollments
----------  -----------
         1            3
         2            5
         3            4
(3 rows)

Worked example: is it 1NF?

student subjects
Rina Math, Art
Sam Math

Not 1NF: subjects holds a list. Fix: student_subjects (student_id, subject) with one row per pair.

What about a full address in one address column ("House 4, Road 2, Dhaka")? It is one value in the sense that it is a single text, but if you often search by city you should split it into house, road, city. Atomic means "the smallest piece you need to use on its own", which depends on what you do with the data. (Some databases offer array or JSON column types; they can be a deliberate choice for data you only store and show, but values you search, count or link should stay in their own rows, see lesson 11.4.) Data is made up.

Common mistakes

  • Storing lists in a cell ("a,b,c", or JSON for things you need to search or count).
  • Numbering columns (item1, item2).
  • Splitting too far. You do not need to store each letter of a name in its own column; atomic is relative to your use.
  • Forgetting the key. Rows that cannot be told apart are not 1NF.

Key takeaways

  • 1NF: one value per cell, no repeating column groups, a key for each row.
  • Lists in a cell break exact search, counting and uniqueness.
  • Repeating columns hard-code a limit and force every query to name them all.
  • Fix both with a child table holding one row per value.

Practice

1. Why does WHERE phones = '01712' find nobody?

Show answer

The cell contains 01711, 01712, which is not equal to 01712. The value is buried in a list.

2. Why did searching '%0171%' return Rina?

Show answer

LIKE matches any text containing the pattern; 01711 and 01712 both contain 0171, so Rina matched although none of her numbers equals it.

3. What is wrong with columns skill1, skill2, skill3?

Show answer

It is a repeating group: a fourth skill needs a new column and every query must name all three.

4. Design the tables for a person who can have many email addresses.

Show answer

people (person_id, name) and emails (person_id, email) with primary key (person_id, email).

5. What does the key (person_id, phone) prevent?

Show answer

Storing the same phone twice for the same person.

6. Is a full_name column a 1NF violation?

Show answer

Not necessarily. It is one value; split it into first and last name only if you need to use the parts separately.