Welcome to the World of Relational Databases!

Have you ever wondered how Amazon remembers your past orders, or how your school keeps track of thousands of students, their grades, and their teachers without getting everything mixed up? The secret is a Relational Database.

In this chapter, we are going to learn how to organize data so it stays accurate, easy to find, and takes up as little space as possible. Don't worry if this seems a bit "data-heavy" at first—we'll break it down into small, simple steps!

1. What is a Relational Database?

Think of a Relational Database as a collection of digital filing cabinets (called Tables) that are connected to each other. Instead of putting every single piece of information into one giant, messy spreadsheet, we split it into smaller, organized tables.

Key Terms You Need to Know:

  • Relation: This is just the formal word for a Table.
  • Attribute: A column header. For example, StudentName or DateOfBirth.
  • Tuple: A fancy word for a Record (a single row in the table).
  • Domain: The set of allowed values for an attribute (e.g., the domain for "Grade" might be 1 to 10).

Analogy: Imagine a school. We have one table for Students and another for Classes. We "relate" them so we know which student is in which class without having to type the student's address five times!

Quick Review:

A relational database uses multiple tables linked together to reduce Redundancy (entering the same data twice).

2. Keys: The "ID Cards" of Databases

To link tables together, we need special columns called Keys.

Primary Key

A Primary Key is a unique identifier for every record in a table. No two rows can have the same Primary Key.
Example: Your Student ID number or a Car’s License Plate.

Foreign Key

A Foreign Key is a Primary Key from one table that appears in another table to link them together.

Composite Key

Sometimes, one single column isn't enough to be unique. A Composite Key is a Primary Key made up of two or more columns combined.

Memory Aid:
Primary = Personal (It's my own unique ID).
Foreign = Friend (It's an ID belonging to a friend in another table).

3. Entity-Relationship (ER) Modeling

Before we build a database, we draw a map called an Entity-Relationship Diagram (ERD). An Entity is just a "thing" we want to store data about (like a Student, a Teacher, or a Book).

Relationship Types:

  • One-to-One (1:1): One Husband has one Wife (usually!).
  • One-to-Many (1:M): One Mother can have many Children. This is the most common type.
  • Many-to-Many (M:M): Many Students study many Subjects. Note: Databases hate Many-to-Many; we usually break them down into smaller tables!

Did you know? In ER diagrams, we often use "Crow's Foot" notation. The side with the three little "toes" represents the "Many" side of the relationship!

4. Normalisation: Cleaning Up the Mess

Normalisation is the process of organizing data to reduce Redundancy (repetition) and improve Data Integrity (accuracy). We do this in three stages.

First Normal Form (1NF)

A table is in 1NF if:
1. There are no "repeating groups" of attributes.
2. All data cells are Atomic (this means you can't split the data any further).
Example: You shouldn't have "Soccer, Chess" in one cell. They should be in separate rows.

Second Normal Form (2NF)

A table is in 2NF if:
1. It is already in 1NF.
2. It has no Partial Dependencies. This means every non-key attribute must depend on the entire Primary Key (only relevant if you have a Composite Key).

Third Normal Form (3NF)

A table is in 3NF if:
1. It is already in 2NF.
2. It has no Transitive Dependencies.
The Golden Rule: Every attribute must depend on "The Key, the Whole Key, and Nothing But the Key!"

Common Mistake: Students often forget that 3NF requires the table to be in 2NF first. It’s like climbing a ladder—you can’t reach the third step without the first two!

5. Structured Query Language (SQL)

SQL is the language we use to talk to the database. It allows us to search, add, or delete data.

The Big Four Commands:

  1. SELECT: Which columns do you want to see?
  2. FROM: Which table are you looking at?
  3. WHERE: What is the search criteria? (Like a filter).
  4. ORDER BY: Do you want the results in alphabetical or numerical order?
Example SQL Query:
To find the names of all students who scored more than 80:

SELECT StudentName
FROM StudentTable
WHERE TestScore > 80
ORDER BY StudentName ASC

Quick Tip: Use the asterisk symbol * if you want to select ALL columns from a table.
\(SELECT * FROM Students\)

Summary Checklist

Can you explain these to a friend?
- The difference between a Tuple and an Attribute.
- Why a Primary Key must be unique.
- How a Foreign Key connects two tables.
- The three steps of Normalisation.
- How to write a basic SELECT statement.

Great job! Databases can be tricky because they require a very organized way of thinking, but once you master the "Keys" and "Normalisation," the rest is just simple logic. Keep practicing those SQL queries!