Skip to content

Learn online at your pace

Databases & SQL • November 29, 2026 • 8 min read

Database Design for Beginners: Tables, Keys and Normalisation

The Qtec Mastery

Author

Database Design for Beginners: Tables, Keys and Normalisation

Most messy databases start life as a spreadsheet that grew too big: names typed three different ways, fees copied into every row and several payments squeezed into one cell. This guide is for beginners who want to design tables properly from the start. You will learn primary keys, foreign keys and relationships, then use database normalisation (often searched as "database normalization") to turn a messy sheet into clean tables, step by step.

Normalisation is the process of organising tables so that each fact is stored in one place only. In practice that means one value per cell (1NF), every column depending on the whole key (2NF) and no column depending on another non-key column (3NF). Tables are then linked with primary and foreign keys.

Databases from Zero, part 4 of 6. Previous: SQL JOINs Explained Simply: INNER, LEFT and RIGHT JOIN With Examples · Next: Database Indexes Explained: Why Your Queries Are Slow and How to Fix Them

What is a primary key?

A primary key is the column (or set of columns) that identifies each row uniquely. It can never be empty and never repeat. In our college database, students.student_id, courses.course_id and payments.payment_id are all primary keys:

student_id INT AUTO_INCREMENT PRIMARY KEY

Good primary keys are stable and meaningless. A number generated by AUTO_INCREMENT is ideal. Avoid using names (two students can both be called Kevin Mutua) or phone numbers (people change lines). If a value must be unique but is not the key, such as a phone number or an M-Pesa code, give it a UNIQUE constraint instead, as our schema does.

What is a foreign key?

A foreign key is a column that points to the primary key of another table. In payments, the student_id column says which student made the payment:

FOREIGN KEY (student_id) REFERENCES students(student_id)

MySQL now enforces the link. You cannot record a payment for a student who does not exist, and you cannot delete a student who still has payments. Our students.course_id column points at courses but has no constraint yet. Let's add one:

ALTER TABLE students
  ADD CONSTRAINT fk_students_course
  FOREIGN KEY (course_id) REFERENCES courses(course_id);

-- This now fails with error 1452, because course 99 does not exist:
INSERT INTO students (full_name, phone, county, course_id, enrolled_on)
VALUES ('Test Student', '0700000001', 'Nyeri', 99, '2026-03-05');

A NULL foreign key is still allowed, which is how Faith Chebet can be registered without a course. You can also choose what happens when the parent row is deleted with ON DELETE RESTRICT (block it, which is also how MySQL behaves if you say nothing), ON DELETE CASCADE (delete the child rows too) or ON DELETE SET NULL. Never cascade deletes onto money records like payments: you want an error, not silently vanishing receipts.

What is a one-to-many relationship?

One row in table A relates to many rows in table B, but each row in B relates to only one row in A. Our database has two:

  • One course, many students. Python Programming has Brian and Kevin, but each student has one course_id.
  • One student, many payments. Brian paid twice, but each payment belongs to one student.

The rule is simple: the foreign key goes on the "many" side. Payments hold student_id; students do not hold a list of payment IDs.

How do I handle a many-to-many relationship?

Suppose the college now lets a student take more than one course. A student can have many courses and a course has many students. You cannot store that with one course_id column, and you must not store "1,2" in it either. Instead, create a junction table (also called a link or bridge table) with one row per student-course pair:

CREATE TABLE enrolments (
  student_id INT NOT NULL,
  course_id INT NOT NULL,
  enrolled_on DATE NOT NULL,
  PRIMARY KEY (student_id, course_id),
  FOREIGN KEY (student_id) REFERENCES students(student_id),
  FOREIGN KEY (course_id) REFERENCES courses(course_id)
);

INSERT INTO enrolments (student_id, course_id, enrolled_on) VALUES
  (1, 1, '2026-01-10'),
  (2, 2, '2026-01-12'),
  (2, 1, '2026-03-02'),
  (3, 3, '2026-02-03');

SELECT s.full_name, c.course_name, e.enrolled_on
FROM enrolments e
INNER JOIN students s ON s.student_id = e.student_id
INNER JOIN courses c ON c.course_id = e.course_id
WHERE e.student_id = 2
ORDER BY e.enrolled_on;
full_namecourse_nameenrolled_on
Brian OtienoPython Programming2026-01-12
Brian OtienoWeb Development2026-03-02

The composite primary key (student_id, course_id) stops the same student being enrolled twice on the same course. Notice that enrolled_on lives here, because it describes the pair, not the student or the course alone. In a full redesign you would then drop students.course_id. The rest of this series keeps the simpler one-course-per-student schema.

What does a messy spreadsheet look like?

Here is the kind of sheet many colleges and small businesses start with:

StudentPhoneCountyCoursesFeesPayments
Wanjiku Kamau0712345678NairobiWeb Development10000SKA4H7XQ2P 10000
Brian Otieno0723456789KisumuPython Programming, Web Development20000, 10000SKB2M9RT5L 10000; SKC7P3WN8D 5000
Amina Hassan0734567890MombasaCyber Security15000SKD5Q1ZV6H 15000

It works until it does not. You cannot easily total Brian's payments. If the Web Development fee changes, you must find and edit every copy. You cannot add a new course until someone enrols. And if Amina leaves and you delete her row, you lose the only record that Cyber Security costs KES 15,000. These are called update, insert and delete anomalies, and normalisation removes them.

How do 1NF, 2NF and 3NF work?

First normal form (1NF): one value per cell

Every cell holds a single value and there are no repeating groups. Split Brian's courses into separate rows, and move payments into their own list, one payment per row:

phonefull_namecountycourse_namefee_kesenrolled_on
0712345678Wanjiku KamauNairobiWeb Development100002026-01-10
0723456789Brian OtienoKisumuPython Programming200002026-01-12
0723456789Brian OtienoKisumuWeb Development100002026-03-02
0734567890Amina HassanMombasaCyber Security150002026-02-03

The key of this table is the pair (phone, course_name). It is now queryable, but Brian's name and county are still repeated.

Second normal form (2NF): depend on the whole key

Every non-key column must depend on the whole key, not just part of it. Here full_name and county depend only on the phone, and fee_kes depends only on the course. Only enrolled_on needs both. So we split into three tables, and give each a proper ID: students (student_id, full_name, phone, county), courses (course_id, course_name, fee_kes) and enrolments (student_id, course_id, enrolled_on). That is exactly the design from the previous section.

Third normal form (3NF): no facts about other facts

No non-key column may depend on another non-key column. Imagine a payments table like this:

payments(payment_id, student_id, student_name, mpesa_code, amount_kes, paid_on)

student_name depends on student_id, not on the payment. If Wanjiku corrects the spelling of her name, every old payment still shows the old spelling. The fix is to remove student_name and get it with a JOIN when you need it, as shown in part 3. Our shared payments table is already in 3NF.

A handy summary: every column should describe "the key, the whole key and nothing but the key".

What are the most common database design mistakes?

  • Lists in one column. "Python, Web" in a single cell cannot be joined, counted or checked. Use a junction table.
  • Storing what you can calculate. A balance_kes column goes out of date the moment a new payment arrives. Calculate balances from fees and payments instead.
  • Phone numbers as numbers. An INT column drops the leading zero of 0712345678. Store phones as VARCHAR.
  • Money as FLOAT. Floating-point numbers can introduce tiny rounding errors. Use DECIMAL(10,2) for amounts.
  • Names as keys. Names repeat and change. Use an ID.
  • Skipping foreign keys. Without them, payments for deleted students quietly pile up.
  • Over-normalising. Not every column needs its own table. Normalise until each fact lives in one place, then stop.

Good design also means planning how data will be searched. That is where indexes come in, in part 5.

Design databases with confidence

DBMS 1 teaches table design, keys, relationships and normalisation alongside SQL, over 10 weeks online with a certificate on completion.

See the course →

Also useful: Backend Development (8 months, KES 60,000)

Frequently asked questions

Do I always need to normalise to 3NF?

For most business systems, 3NF is a sensible target. Reporting databases sometimes deliberately repeat data to make reads simpler, but that is a choice you make once you understand the rules.

What is the difference between a primary key and a unique key?

A table has one primary key, which identifies rows and cannot be NULL. It can have several unique keys, such as phone and M-Pesa code, which stop duplicates in other columns.

Is normalisation the same as normalization?

Yes. Normalisation is the British spelling and normalization the American one. They mean the same thing.

Should every table have an AUTO_INCREMENT ID?

Most do, and it is a safe default. Junction tables like enrolments often use the pair of foreign keys as their primary key instead.

Comments (0)

Comments appear after a quick review. Your email is never shown.

You Might Also Like

What Is a Database? A Beginner's Guide With Real Kenyan Examples

What Is a Database? A Beginner's Guide With Real Kenyan Examples

Every app you use, from your school portal to M-Pesa, keeps its information some...

Read More →
SQL for Beginners: 10 Queries You Will Use Every Day

SQL for Beginners: 10 Queries You Will Use Every Day

Most of the SQL people write at work comes down to about ten patterns. This guid...

Read More →
SQL JOINs Explained Simply: INNER, LEFT and RIGHT JOIN With Examples

SQL JOINs Explained Simply: INNER, LEFT and RIGHT JOIN With Examples

Real databases split information across several tables, so sooner or later you n...

Read More →