Database Design

Back

Loading concept...

๐Ÿ—๏ธ Database Design: Building Your Digital Filing Cabinet

Imagine youโ€™re organizing the worldโ€™s best library. Every book needs a home, every shelf needs a label, and everything must connect perfectly. Thatโ€™s database design!


๐ŸŽฏ What is a Database?

Think of a database like a super-organized filing cabinet for a hospital, school, or store.

Simple Example:

  • Your mom keeps recipes in a recipe box
  • Each card has the recipe name, ingredients, and steps
  • She can find any recipe quickly by looking at the right section
  • A database is the same thing, but on a computer!

Real Life:

  • Your school keeping track of all students = Database
  • Netflix remembering what shows you watched = Database
  • A store tracking all products and prices = Database

๐Ÿ“Š The Relational Model: Tables Are Your Friends

The relational model organizes data into tables (like spreadsheets).

Think of it like a classroom seating chart:

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Seat_ID โ”‚ Student  โ”‚ Grade   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 1       โ”‚ Emma     โ”‚ A       โ”‚
โ”‚ 2       โ”‚ Noah     โ”‚ B       โ”‚
โ”‚ 3       โ”‚ Olivia   โ”‚ A       โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Key Parts:

  • Table = The whole chart (called a โ€œrelationโ€)
  • Row = One studentโ€™s info (called a โ€œtupleโ€)
  • Column = One type of info like โ€œNameโ€ (called an โ€œattributeโ€)

๐Ÿ”‘ Keys: The Name Tags of Your Data

Keys are like name tags that help us find exactly what we need.

Primary Key ๐Ÿท๏ธ

The one unique identifier for each row.

Example: Your student ID number

  • No two students have the same ID
  • It never changes
  • Itโ€™s never empty
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ StudentID โ”‚ Name     โ”‚ Age     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ S001      โ”‚ Emma     โ”‚ 10      โ”‚ โ† S001 is unique!
โ”‚ S002      โ”‚ Noah     โ”‚ 11      โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

Foreign Key ๐Ÿ”—

A connector that links one table to another.

Example: Like saying โ€œEmma (S001) is in Class 3Aโ€

STUDENTS TABLE              CLASSES TABLE
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ StudentID โ”‚ Name   โ”‚      โ”‚ ClassID  โ”‚ Teacher   โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค      โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ S001      โ”‚ Emma   โ”‚โ”€โ”€โ”   โ”‚ 3A       โ”‚ Ms. Smith โ”‚
โ”‚ S002      โ”‚ Noah   โ”‚  โ””โ”€โ”€โ–บโ”‚ 3A       โ”‚ Ms. Smith โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜      โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿšง Constraints: The Rules of the Game

Constraints are rules that keep your data clean and correct.

Types of Constraints:

Constraint What It Does Example
NOT NULL Must have a value Every student needs a name
UNIQUE No duplicates Each email must be different
PRIMARY KEY Unique + Not null Student ID
FOREIGN KEY Must exist in other table Class ID must be real
CHECK Must pass a test Age must be > 0

Real Life Example:

  • You canโ€™t sign up for a game without a username (NOT NULL)
  • Two players canโ€™t have the same username (UNIQUE)
  • You canโ€™t join a team that doesnโ€™t exist (FOREIGN KEY)

๐Ÿ”’ Referential Integrity: No Broken Links!

Referential integrity means: โ€œIf you point to something, it must exist!โ€

Imagine a treasure map:

  • โŒ Bad: Map says โ€œGo to the Red Treeโ€ but thereโ€™s no red tree
  • โœ… Good: Map says โ€œGo to the Red Treeโ€ and itโ€™s really there!

Database Example:

ORDERS TABLE
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ OrderID โ”‚ CustomerID โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ 001     โ”‚ C100       โ”‚ โ† C100 MUST exist in
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   CUSTOMERS table!

CUSTOMERS TABLE
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ CustomerID โ”‚ Name     โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ C100       โ”‚ Emma     โ”‚ โœ… It exists!
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

What happens if you break it?

  • Canโ€™t add an order for a customer that doesnโ€™t exist
  • Canโ€™t delete a customer who has orders (unless you handle it)

๐ŸŽจ Entity-Relationship Model: Drawing the Blueprint

Before building a house, you draw a blueprint. Before building a database, you create an ER Model!

The Three Building Blocks:

1. Entities ๐Ÿ“ฆ = Things we store data about

  • Student, Teacher, Class, Book

2. Attributes ๐Ÿท๏ธ = Details about entities

  • Student has: Name, Age, ID

3. Relationships ๐Ÿ”— = How entities connect

  • Student enrolls in Class
  • Teacher teaches Subject

๐Ÿ“ ER Diagrams: Pictures Worth 1000 Words

ER Diagrams turn our blueprint into a visual map.

graph TD A["๐Ÿ“ฆ STUDENT"] -->|enrolls in| B["๐Ÿ“ฆ CLASS"] B -->|taught by| C["๐Ÿ“ฆ TEACHER"] A -.->|has| D((name)) A -.->|has| E((age)) A -.->|has| F((ID))

Symbols to Know:

Symbol Meaning
๐Ÿ“ฆ Rectangle Entity (thing)
โฌญ Oval Attribute (detail)
โ—‡ Diamond Relationship
โ€” Line Connection

Relationship Types:

One-to-One (1:1)

  • One person has one passport

One-to-Many (1:N)

  • One teacher has many students

Many-to-Many (M:N)

  • Many students take many classes

๐Ÿงน Normalization: Marie Kondo for Databases

Normalization is cleaning up your database to remove mess and repetition.

Why Normalize?

Before (Messy):

โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Student โ”‚ Class   โ”‚ Teacher          โ”‚
โ”œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ผโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ค
โ”‚ Emma    โ”‚ Math    โ”‚ Ms. Smith        โ”‚
โ”‚ Emma    โ”‚ Science โ”‚ Mr. Jones        โ”‚
โ”‚ Noah    โ”‚ Math    โ”‚ Ms. Smith        โ”‚
โ”‚ Noah    โ”‚ Math    โ”‚ Ms. Smith        โ”‚ โ† Duplicate!
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

After (Clean):

STUDENTS    ENROLLMENTS    CLASSES
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”  โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Emma   โ”‚  โ”‚S   โ”‚ C   โ”‚  โ”‚ Math  โ”‚Ms.Smith โ”‚
โ”‚ Noah   โ”‚  โ”‚Emmaโ”‚Math โ”‚  โ”‚Scienceโ”‚Mr.Jones โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜  โ”‚Emmaโ”‚Sci  โ”‚  โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
            โ”‚Noahโ”‚Math โ”‚
            โ””โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿ“š Normal Forms: The Levels of Clean

Think of normal forms like cleaning levels in a video game!

๐Ÿฅ‰ First Normal Form (1NF)

Rule: No lists in a single cell!

โŒ Bad:

โ”‚ Student โ”‚ Hobbies              โ”‚
โ”‚ Emma    โ”‚ Reading, Gaming, Art โ”‚  โ† List in one cell!

โœ… Good:

โ”‚ Student โ”‚ Hobby   โ”‚
โ”‚ Emma    โ”‚ Reading โ”‚
โ”‚ Emma    โ”‚ Gaming  โ”‚
โ”‚ Emma    โ”‚ Art     โ”‚

๐Ÿฅˆ Second Normal Form (2NF)

Rule: Everything depends on the WHOLE key!

First, pass 1NF, thenโ€ฆ

โŒ Bad: Storing teacher name with student-class combo

โ”‚ StudentID โ”‚ ClassID โ”‚ TeacherName โ”‚

TeacherName depends only on ClassID, not StudentID!

โœ… Good: Move teacher to Classes table

๐Ÿฅ‡ Third Normal Form (3NF)

Rule: No โ€œmiddlemanโ€ dependencies!

First, pass 2NF, thenโ€ฆ

โŒ Bad:

โ”‚ StudentID โ”‚ ZipCode โ”‚ City     โ”‚

City depends on ZipCode, not StudentID directly!

โœ… Good:

STUDENTS           ZIP_CODES
โ”‚ StudentID โ”‚ Zip โ”‚  โ”‚ Zip   โ”‚ City    โ”‚
โ”‚ S001      โ”‚ 123 โ”‚  โ”‚ 123   โ”‚ Boston  โ”‚

๐ŸŽฎ Quick Summary: Your Database Design Checklist

graph TD A["๐ŸŽฏ Start"] --> B["๐Ÿ“ฆ Identify Entities"] B --> C["๐Ÿท๏ธ List Attributes"] C --> D["๐Ÿ”— Draw Relationships"] D --> E["๐Ÿ“ Create ER Diagram"] E --> F["๐Ÿ”‘ Choose Keys"] F --> G["๐Ÿงน Normalize Tables"] G --> H["๐Ÿšง Add Constraints"] H --> I["โœ… Done!"]

๐Ÿ’ก Remember This!

Concept Think Of It As
Database Digital filing cabinet
Table One drawer with organized cards
Primary Key Unique name tag
Foreign Key String connecting two cards
Constraint Rules written on the drawer
ER Diagram Blueprint before building
Normalization Cleaning and organizing

๐ŸŒŸ You Did It!

You now understand how to design databases like a pro! Remember:

  1. Plan first with ER diagrams
  2. Keep it clean with normalization
  3. Stay connected with keys
  4. Follow rules with constraints

Your data will always have a happy, organized home! ๐Ÿ 

Loading story...

Story - Premium Content

Please sign in to view this story and start learning.

Upgrade to Premium to unlock full access to all stories.

Stay Tuned!

Story is coming soon.

Story Preview

Story - Premium Content

Please sign in to view this concept and start learning.

Upgrade to Premium to unlock full access to all content.