Video in progress
What is normalization, and what does the first normal form (1NF) require?
Normalization means splitting data across tables so that nothing is duplicated and no contradictions can appear. 1NF: every cell holds one atomic value, with no lists or repeating columns (phone1, phone2).
📖 Answer
A table that violates 1NF:
| id | name | phones |
|---|---|---|
| 1 | Ali | 901112233, 935556677 |
Now the question "whose number is 935556677?" turns into LIKE '%935556677%', which is slow and error-prone. To delete a single phone you have to parse the string.
Bringing it to 1NF:
CREATE TABLE persons (id bigint PRIMARY KEY, name text NOT NULL);
CREATE TABLE person_phones (
person_id bigint REFERENCES persons(id),
phone text NOT NULL,
PRIMARY KEY (person_id, phone)
);
Columns phone1, phone2, phone3 also violate 1NF: when a fourth phone shows up, you have to change the table structure.
One note: PostgreSQL has the text[] and jsonb types, and sometimes you deliberately step away from 1NF with them (tags, for example). That isn't bad if the values aren't searched individually, or if they are searched with a GIN index. What matters is that it's a deliberate choice, not an accident.
Sign up to mark studied questions and save your progress.