Database design is deciding which tables you need, what columns they have and how they connect. A good design keeps data correct, avoids duplication and makes queries simple and fast.
What You Will Learn
- Entities, attributes and relationships
- Primary keys, foreign keys and constraints
- One-to-one, one-to-many and many-to-many
- Normalization: 1NF, 2NF and 3NF
- When to denormalize
Prerequisites
Step 1: Find the Entities
For an institute: Students, Courses, Teachers and Enrollments. Each entity becomes a table; each property (name, fee) becomes a column.
Step 2: Relationships
| Type | Example | How to model |
|---|---|---|
| One-to-one | Student ↔ ID card | Foreign key with UNIQUE, or same table |
| One-to-many | Teacher → many courses | Foreign key teacher_id in courses |
| Many-to-many | Students ↔ courses | A junction table enrollments(student_id, course_id) |
Step 3: Write the Schema
CREATE TABLE teachers (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(150) UNIQUE
);
CREATE TABLE courses (
id INTEGER PRIMARY KEY,
title VARCHAR(100) NOT NULL,
fee INTEGER CHECK (fee >= 0),
teacher_id INTEGER REFERENCES teachers(id)
);
CREATE TABLE students (
id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
phone VARCHAR(15)
);
CREATE TABLE enrollments (
student_id INTEGER REFERENCES students(id),
course_id INTEGER REFERENCES courses(id),
enrolled_on DATE,
PRIMARY KEY (student_id, course_id)
);
| Constraint | Guarantees |
|---|---|
| PRIMARY KEY | Unique, not null identifier |
| FOREIGN KEY / REFERENCES | The linked row exists |
| NOT NULL | Value must be given |
| UNIQUE | No duplicates (e.g. email) |
| CHECK | Value obeys a rule (fee ≥ 0) |
Step 4: Normalize
Normalization removes repeated data so that each fact is stored in one place.
| Form | Rule | Bad example → fix |
|---|---|---|
| 1NF | Each cell holds one value; no repeating groups | “Python, SQL” in one cell → one row per course |
| 2NF | 1NF + every column depends on the whole key | Course title stored in enrollments → move to courses |
| 3NF | 2NF + no column depends on another non-key column | Teacher email stored in courses → keep it in teachers |
When to Denormalize
Analytics databases and data warehouses often duplicate some data on purpose (star schema: a fact table plus dimension tables) because reading is far more common than writing and fewer joins make reports faster.
Design Checklist
- Every table has a primary key.
- Names are clear and consistent (snake_case, singular or plural – pick one).
- Use the right data types (DATE for dates, DECIMAL for money).
- Add indexes on columns used in joins and filters.
- Write down the relationships in an ER diagram.
Series
SQL series: SQL Basics · MySQL · PostgreSQL · MongoDB · Database Design · SQL for Data Analytics · Advanced SQL Queries · Python MySQL series
Interview Questions
Q: What is normalization?
Organising tables to reduce redundancy and update problems, typically up to 3NF.
Q: How do you model many-to-many?
With a junction table holding both foreign keys.
Q: Why DECIMAL for money?
Floating-point types can cause rounding errors.
Practice Exercises
- Design tables for a library: books, members, loans.
- Find the normalization problem in a table (order_id, customer_name, customer_city, product, price).
Summary
Good design starts from entities and relationships, uses keys and constraints to protect data, and normalizes to 3NF to avoid duplication – denormalizing only for read-heavy analytics.
Next: MySQL · PostgreSQL
Machine Learning Tutorials, Courses and Certifications