QA Interview Hub · Knowledge base · Quizzes · Українська

SQL › Database basics

Relationships between tables and normalization

Tables link through keys: one-to-many is a foreign key in the child table, one-to-one an FK with UNIQUE. Many-to-many goes through a join table. Normalization stores every fact once: 1NF has no lists in a cell, 2NF no dependence on part of a key, 3NF no dependence of non-key columns on each other. This removes update, insert and delete anomalies.

SQL · Basics

A test created an order and the database holds two spellings of one customer. Why did the schema allow it?

The orders table holds the customer name and email in every row. Someone fixed the email in one order, while two others kept the old one: now the system has "two Ivans". This is not a test error but a consequence of a schema where the same fact is written several times. This lesson is about how tables are linked and how normalization prevents such drift.

How it works and why. A relational database keeps each entity in its own table and expresses relationships with keys. There are three kinds:

RelationshipExampleHow it is stored
One-to-manyuser → ordersFK in the child table
One-to-oneuser → profileFK + UNIQUE
Many-to-manystudents ↔ coursesa join table

Many-to-many cannot be written directly: it is split into two one-to-many relationships through a join table with one foreign key per side.

Many-to-many through a join table

studentsid (PK)enrollments2 foreign keyscoursesid (PK)11NN
Figure 1. A student has many rows in enrollments, so does a course; each row points to one student and one course.
CREATE TABLE enrollments (
  student_id int NOT NULL REFERENCES students (id),
  course_id  int NOT NULL REFERENCES courses (id),
  PRIMARY KEY (student_id, course_id)
);

The composite primary key also forbids enrolling a student in the same course twice.

Normalization is the set of rules for splitting data across tables so that each fact is stored once. The first three normal forms:

  • 1NF: one value per cell, no lists ("10, 11, 12") and no repeating column groups (phone1, phone2).
  • 2NF: every non-key column depends on the whole composite key, not on part of it.
  • 3NF: non-key columns depend only on the key, not on each other (city, country and country code in one row).

Example: before and after. First a denormalized table, then the split one:

-- before: the customer repeats in every order
CREATE TABLE orders (id int PRIMARY KEY, item text, customer_name text, customer_email text);

-- after: a fact about the customer in one place
CREATE TABLE customers (id int PRIMARY KEY, name text, email text UNIQUE);
CREATE TABLE orders (
  id int PRIMARY KEY,
  item text,
  customer_id int NOT NULL REFERENCES customers (id)
);

One table or two

Avoid

orders

id, item, customer_name, customer_email. The customer repeats in every order.

Better

orders + customers

orders: id, item, customer_id. customers: id, name, email. A fact about the customer lives in one place.

Figure 2. In the split schema an email is changed in one row, and orders cannot drift apart.

Now changing an email is one UPDATE in customers. Before normalization the same UPDATE had to hit every order of the customer, and one missed row produced an update anomaly. There are also insert anomalies (a customer cannot be added without an order) and delete anomalies (the customer vanishes with the last order).

Limits and alternatives. Normalization is not free: a report now needs a JOIN. For analytics and data warehouses tables are denormalized on purpose, while OLTP systems keep 3NF. Historical snapshots are an exception too: the price of an item at the moment of the order must stay in orders even if the catalog price changed. That is not duplication but a different fact.

Common mistakes.

  • Storing a comma-separated list of ids instead of a join table: the database cannot check that those ids exist, and the query 'who is enrolled in course 12' turns into string parsing.
  • Building one-to-one without UNIQUE: it is really one-to-many.
  • Forgetting the composite key in a join table, so a student enrols in a course twice.
  • Copying the same name and email into every test data row without noticing the schema allows it.

Check yourself

Why is one foreign key not enough for students ↔ courses?

Answer

One foreign key gives one-to-many: either a student has one course or a course has one student. Many-to-many needs a join table with foreign keys to both sides.

Does storing the price in an order row break normalization when the catalog has it too?

Answer

No: it is the price at the time of purchase, while the catalog holds the current one. They are different facts, and the historical value must stay in the order.

In an interview. Junior: Tables are linked by keys: one-to-many is a foreign key in the child table, many-to-many is a join table. Normalization stores each fact once so data does not drift apart. Middle/Senior: I know 1NF, 2NF and 3NF and the update, insert and delete anomalies they remove. I allow denormalization for reports deliberately, and I do not treat historical values (the price at purchase) as duplication. As QA I check relationship integrity and look for orphan or duplicated records.

More articles in this chapter