smartyfy.mrindigi.com

Databases & SQL Master Course — 074. First Normal Form

Lesson 074 First Normal Form

Prerequisites and objectives

You can define row grain and choose keys. You will replace a repeated-column design with a consistent child-row structure, preserve meaningful order, and explain what one value per attribute means in this practical relational model. The example converts a two-stop route form into a route with any number of recorded stops.

Create the practice fixture

Use a fresh disposable SQLite database with foreign keys enabled. The wide table has stop1 and stop2 columns. The target separates route facts from stop occurrences. The source is preserved throughout the exercises, and every transformation or insertion ends with rollback so the target starts empty again.

PRAGMA foreign_keys=ON;
CREATE TABLE wide_routes (
 route_id INTEGER PRIMARY KEY,
 route_name TEXT NOT NULL,
 stop1 TEXT NOT NULL,
 stop2 TEXT
);
INSERT INTO wide_routes VALUES (1,'Morning','Dock A','Depot B'),(2,'Afternoon','Depot C',NULL);
CREATE TABLE routes (route_id INTEGER PRIMARY KEY, route_name TEXT NOT NULL);
CREATE TABLE route_stops (
 route_id INTEGER NOT NULL REFERENCES routes(route_id),
 stop_no INTEGER NOT NULL CHECK(stop_no>0),
 location TEXT NOT NULL,
 PRIMARY KEY(route_id,stop_no)
);

Find the repeating group

The stop1 and stop2 columns store repeated instances of the same kind of fact. A third stop would require another column, and every search would need another branch. This is a sign that the form’s layout has been mistaken for the logical data model. The number of stops should be represented by rows, not by repeatedly changing the schema.

The target route row contains facts about the route itself. Each route_stops row represents one numbered stop occurrence within a route. Its key combines route identity and position. This permits two routes to each have a first stop and permits a route to visit the same location more than once.

BEGIN;
INSERT INTO routes SELECT route_id,route_name FROM wide_routes;
INSERT INTO route_stops(route_id,stop_no,location)
SELECT route_id,1,stop1 FROM wide_routes
UNION ALL
SELECT route_id,2,stop2 FROM wide_routes WHERE stop2 IS NOT NULL;
SELECT route_id,stop_no,location FROM route_stops ORDER BY route_id,stop_no;
ROLLBACK;

The transformation creates three stop occurrences from two routes. The absent second stop on route two produces no child row; it is not represented as a mysterious stop with a NULL location. Position is explicit, so a later query can request the route order using ORDER BY.

Morning route 1 initially has stop1 Dock A and stop2 Depot B. A route row stores its name; child occurrence rows (1, 1) Dock A and (1, 2) Depot B store ordered stops. Adding (1, 3) Dock A revisits a location without adding a column. The key includes position rather than location.
Separate route facts from repeated stop occurrences. Explicit position preserves order and allows meaningful repeated locations. Open full-size diagram

Interpret atomic values in context

First normal form is commonly introduced through single values from the declared attribute domains and the removal of repeating groups. In this lesson, location is one location label per stop. The text Dock A is not split into separate columns merely because it contains a space. Atomicity depends on what the model treats as a value, not on whether a string can be broken into characters.

A comma-separated collection of independently queried stops would be a poor fit for this model because each stop has its own position and relationship meaning. Some database systems support structured or array values for deliberate purposes, but storing a list in text is not a substitute for deciding the grain and constraints needed here.

Preserve order and multiplicity deliberately

SQL tables have no inherent row order. The stop_no attribute records the route’s business sequence, while ORDER BY controls the displayed sequence. Do not rely on insertion order or generated row identifiers to mean the first stop unless that is an explicitly governed domain rule.

Using the key route_id plus location would prohibit returning to Dock A later in the same route. That would be the wrong identity for stop occurrences. The key route_id plus stop_no allows repeated locations at distinct positions while preventing two stops from claiming the same position.

Recognize the limits of this step

Removing repeated groups does not automatically solve every redundancy or dependency problem. A table can use one value per field while still repeating a supplier’s name across many unrelated rows. Later normal forms examine how attributes depend on keys and one another. Do not label a schema fully normalized solely because it has no comma-separated lists.

Likewise, the positive-position CHECK does not require contiguous positions or forbid gaps. It permits positions one and three without two. If gap-free sequencing matters, that is an additional workflow requirement. State it separately instead of assuming the composite primary key enforces it.

Four worked exercises

Exercise 1

Convert the fixture and count stops per route, including routes with no matching children if any existed.

Worked solution
BEGIN;
INSERT INTO routes SELECT route_id,route_name FROM wide_routes;
INSERT INTO route_stops(route_id,stop_no,location)
SELECT route_id,1,stop1 FROM wide_routes
UNION ALL
SELECT route_id,2,stop2 FROM wide_routes WHERE stop2 IS NOT NULL;
SELECT r.route_id,COUNT(s.stop_no) FROM routes AS r LEFT JOIN route_stops AS s ON s.route_id=r.route_id GROUP BY r.route_id ORDER BY r.route_id;
ROLLBACK;

Morning has two stops and Afternoon has one. Counting the non-NULL child key makes the pattern suitable for a route with no stops, which would produce zero rather than counting the outer-join placeholder row.

Exercise 2

Add a third stop that revisits Dock A without changing the table schema.

Worked solution
BEGIN;
INSERT INTO routes SELECT route_id,route_name FROM wide_routes;
INSERT INTO route_stops(route_id,stop_no,location)
SELECT route_id,1,stop1 FROM wide_routes
UNION ALL
SELECT route_id,2,stop2 FROM wide_routes WHERE stop2 IS NOT NULL;
INSERT INTO route_stops VALUES (1,3,'Dock A');
SELECT stop_no,location FROM route_stops WHERE route_id=1 ORDER BY stop_no;
ROLLBACK;

The repeated location is a distinct occurrence at position three. No stop3 column is needed. This is the main extensibility benefit of modeling the repeated fact as rows with an explicit identity.

Exercise 3

Search for routes that visit Depot B using one consistent child column.

Worked solution
BEGIN;
INSERT INTO routes SELECT route_id,route_name FROM wide_routes;
INSERT INTO route_stops(route_id,stop_no,location)
SELECT route_id,1,stop1 FROM wide_routes
UNION ALL
SELECT route_id,2,stop2 FROM wide_routes WHERE stop2 IS NOT NULL;
SELECT r.route_id,r.route_name FROM routes AS r WHERE EXISTS (SELECT 1 FROM route_stops AS s WHERE s.route_id=r.route_id AND s.location='Depot B') ORDER BY r.route_id;
ROLLBACK;

The query checks one location column regardless of how many stops exist. EXISTS returns each qualifying route once, even if it visits the same location repeatedly. No new OR branch is needed for additional positions.

Exercise 4

Try assigning a second location to route one’s existing first position.

Worked solution
BEGIN;
INSERT INTO routes SELECT route_id,route_name FROM wide_routes;
INSERT INTO route_stops(route_id,stop_no,location)
SELECT route_id,1,stop1 FROM wide_routes
UNION ALL
SELECT route_id,2,stop2 FROM wide_routes WHERE stop2 IS NOT NULL;
INSERT INTO route_stops VALUES (1,1,'Depot D');
SELECT location FROM route_stops WHERE route_id=1 AND stop_no=1;
ROLLBACK;

The composite primary key rejects the conflicting position. If the intended action is to change the first stop, that should be an explicit targeted update with its own review, not a second row that ambiguously claims the same occurrence identity.

Knowledge check

Question 1

What is the repeating group in the source?

  1. The route_id primary key
  2. The stop1 and stop2 columns
  3. The route_name text value
  4. The two rows themselves
Answer and explanation

B. The columns repeat the same kind of stop fact by position.

Question 2

What identifies a target stop occurrence?

  1. Location alone
  2. Route name alone
  3. Route identifier plus stop number
  4. Route identifier alone
Answer and explanation

C. Position is scoped to a route and permits repeated locations.

Question 3

Why is Dock A one value here?

  1. Any string containing spaces must be split into separate attributes
  2. First normal form prohibits all text splitting
  3. A text type proves every possible location representation is atomic
  4. The domain treats it as one location label
Answer and explanation

D. Atomicity follows the modeled domain rather than arbitrary character splitting.

Question 4

How is the route’s sequence represented?

  1. The physical storage order
  2. The order of previous INSERT statements alone
  3. An explicit stop number and an ordered query
  4. Alphabetical location sorting by default
Answer and explanation

C. A business sequence must be stored explicitly and requested in output.

Question 5

What does this normalization step fail to guarantee?

  1. All higher-normal-form dependency requirements
  2. A consistent child-row structure
  3. One location value per stop row
  4. Uniqueness of the declared stop key
Answer and explanation

A. Eliminating repeated groups is only an initial design step.

Pitfalls and summary

Do not mirror a fixed form layout when the domain contains a variable number of occurrences. Replace repeated columns with child rows, choose an occurrence key, and preserve order explicitly when it matters. First normal form provides a useful starting structure, while dependency analysis and additional business rules still need attention.

SQLite behavior and references

Examples were checked with SQLite 3.53.1 on 10 October 2026. Use a SQLite SQL editor; Python is not a prerequisite. Verify dialect-specific functions, comparison rules, and numeric behavior before moving the examples to another engine.

Primary references