Understanding Relational Databases
# CHAPTER 3
Understanding Relational Databases
1. Introduction
If you start building a house without a blueprint, the roof will eventually collapse. The same applies to databases. PostgreSQL is a Relational Database Management System (RDBMS). To use it effectively, you cannot just dump data wherever you want. You must adhere to the strict, architectural rules of relational theory. In this chapter, we will learn how to design unbreakable, logical data structures using Tables, Relationships, and Normalization.2. Learning Objectives
By the end of this chapter, you will be able to:- Define the components of a Table (Rows and Columns).
- Understand how Primary Keys uniquely identify data.
- Understand how Foreign Keys link tables together.
- Explain the three core types of database relationships.
- Understand the concept of Database Normalization.
3. Tables, Rows, and Columns
A Relational Database stores data in Tables. You can think of a table as a single tab in an Excel spreadsheet.-
Table: Represents a single entity type (e.g.,
users,products,orders).
-
Columns (Fields): Define the *attributes* of the entity. The
userstable might have columns forfirst_name,email, andpassword.
- Rows (Records): Represent individual, specific entries. Row 1 is John's data. Row 2 is Sarah's data.
4. The Magic of Keys
How do we tell John from Sarah if they both have the same last name?- Primary Key (PK): A specific column designated as the unique identifier for every row. Usually, this is an ID number (1, 2, 3). No two rows can have the same Primary Key.
- Foreign Key (FK): A column in one table that contains the Primary Key of another table. This is the "Relational" part of a Relational Database. It physically links two tables together.
5. Types of Relationships
Databases model the real world. In the real world, things interact.- 1. One-to-One (1:1): One User has exactly One Passport. (Rare in databases, usually merged into one table).
-
2.
One-to-Many (1:N): One User can place Many Orders. (The most common relationship. The
orderstable will hold auser_idForeign Key).
- 3. Many-to-Many (N:M): Many Students can enroll in Many Classes, and a single Class holds Many Students. (Requires a 3rd "Pivot" table in the middle to track the enrollments).
6. Database Normalization (The Golden Rule)
Normalization is the process of organizing data to eliminate redundancy (data duplication) and ensure logical dependencies.The Problem (Unnormalized Data):
Imagine an orders table that stores the Order Date, the Customer Name, and the Customer's Address.
If John places 50 orders, his Name and Address are saved 50 times on the hard drive. If John moves to a new house, you have to update 50 separate rows. If you miss one, your data is corrupted.
The Solution (Normalized Data):
We split the data into two tables: users and orders.
John's name and address are saved exactly once in the users table (ID: 1).
The orders table only saves the Order Date and a user_id of 1.
If John moves, we update his address in the users table once, and all 50 orders instantly point to the correct, updated address!
7. The Three Normal Forms (Simplified)
To be considered "Normalized," a database usually follows three rules:-
1st Normal Form (1NF): Every cell must hold a single, indivisible value. (Don't put "Milk, Eggs, Bread" in a single
items_boughtcell. Put them in separate rows).
- 2nd Normal Form (2NF): All columns must depend on the Primary Key. (Don't put the Product Price in the Users table).
-
3rd Normal Form (3NF): No column should depend on another non-key column. (If you have
Item PriceandQuantity, do NOT create aTotal Costcolumn. Let the database calculate the total mathematically using SQL when needed).
8. Mini Project: E-Commerce Blueprint
Let's draw a mental blueprint for a normalized E-Commerce store:-
userstable (id, name, email)
-
productstable (id, name, price)
-
orderstable (id, user_id, order_date) -> *Links an order to a user.*
-
order_itemstable (order_id, product_id, quantity) -> *The Pivot table connecting Many Orders to Many Products!*
9. Common Mistakes
- Over-Normalization: Taking normalization too far is a mistake. If you split a User's First Name and Last Name into two different tables just to be "pure," it will require complex, CPU-heavy queries to stitch them back together just to display "John Smith." Balance theory with performance.
- Using Names as Primary Keys: Never use a Natural Key (like an Email or SSN) as a Primary Key. If the user changes their email, the Primary Key changes, breaking all Foreign Key relationships across the entire database. Always use meaningless ID numbers.
10. Best Practices
-
Naming Conventions: Name your tables as lowercase plurals (
users,orders). Name your Foreign Keys as the singular parent table name followed by_id(user_id,order_id). This makes your database infinitely easier for other developers to read.
11. Exercises
- 1. If a Company has Many Departments, and a Department has Many Employees, what relationships exist between these entities?
- 2. Explain how a Foreign Key prevents "orphan" data (e.g., an order belonging to a user that doesn't exist).
12. SQL Challenges
While we haven't learned the syntax yet, understand the logic: To view John's orders, PostgreSQL will run aJOIN command that matches the id in the users table to the user_id in the orders table.
13. MCQ Quiz with Answers
What is the primary purpose of Database Normalization?
In a One-to-Many relationship (e.g., One User has Many Orders), where does the Foreign Key go?
14. Interview Questions
- Q: Explain the concept of Database Normalization. Give a specific example of an anomaly that can occur if a database is NOT normalized.
- Q: Why is a "Pivot" (or Junction) table required to mathematically model a Many-to-Many relationship in a relational database?