Normalization
Database design is the process of creating a logical structure for storing data in a database. It involves defining the relationships between tables, identifying the primary and foreign keys, and ensuring that the data is normalized to minimize redundancy and improve data integrity.
Understanding Database Design Principles
- Good database design avoids data redundancy and ensures data integrity.
- Example: Instead of storing the city name in every row for each chai type, store city details in a separate
Citiestable and link it with theChaitable using foreign keys.
What is Normalization?
Normalization organizes a database into tables and columns to:
- Eliminate data redundancy.
- Ensure data dependencies are logical.
First Normal Form (1NF)
- 1NF ensures that the data in a table is organized into rows and columns, with each column holding atomic (indivisible) values.
- Example: A
Chaitable should have columns likename,ingredients,price, and each cell should contain a single value. You shouldn’t have multiple cities listed in one cell.
Second Normal Form (2NF)
- 2NF ensures that each table depends on the primary key. There should be no partial dependencies (where a non-key column depends on part of a composite key).
- Example: If a table has a composite key (
chai_id,city_id), ensure all non-key columns (likeprice) depend on the entire key, not just one part.
Third Normal Form (3NF)
- 3NF ensures that non-primary-key columns do not depend on other non-primary-key columns.
- Example: A
Chaitable shouldn’t have bothcity_nameandcity_population. Instead,city_nameandcity_populationshould belong in a separateCitytable, and you can link it to theChaitable with acity_id.
Entity Relationship Diagrams (ERDs)
ERDs are used to visually represent relationships between database entities.
- Example: You might have two tables:
ChaiandCity. An ERD would show a one-to-many relationship between cities and chai varieties. - One-to-Many Relationship: One city can have many chai varieties, but each chai variety is sold in one city.
Relationship in SQL
There are different types of relationships in SQL:
- One-to-One Relationship: One entity can have only one instance of another entity.
- One-to-Many Relationship: One entity can have multiple instances of another entity.
- Many-to-Many Relationship: One entity can have multiple instances of another entity, and vice versa.
LMS practice Exercise
We will create a fun practice database design for Learning Management System (LMS) using SQL. The LMS will have the following entities:
- Users
- Courses
- Videos
- Enrollments
users [icon:user] { _id string pk name string email string unique password string userType enum "student", "instructor", "admin" isPaid boolean enrolledCourses ObjectId[] courses createdAt Date updatedAt Date}
courses [icon:book] { _id string pk title string description string price number instructorId ObjectId users category string tags string[] durationInHours number videos ObjectId[] videos createdAt Date updatedAt Date}
videos [icon:video] { _id string pk courseId ObjectId courses title string description string url string durationInMinutes number createdAt Date updatedAt Date}
enrollments [icon:user] { _id string pk userId ObjectId users courseId ObjectId courses enrollmentDate Date progress number completedAt Date createdAt Date updatedAt Date}
users._id < enrollments.userIdenrollments.userId > users._idcourses._id < enrollments.courseIdenrollments.courseId > courses._idcourses._id < videos.courseIdvideos.courseId > courses._idusers._id < courses.instructorIdcourses.instructorId > users._idSummary
In this chapter, we have learned about database design and normalization. We have also learned about the basic data types and how to use them. We have also learned about the different types of relationships and how to use them.
Start your journey with ChaiCode
All of our courses are available on chaicode.com. Feel free to check them out.