Core Relational Database Concepts
Before writing a single line of SQL, you must understand the core vocabulary of relational databases. This chapter covers the foundational building blocks — Tables, Rows, Columns, Keys, and Relationships — the same concepts that underpin every MySQL database you’ll ever build.
Tables: The Basic Unit of Storage
A table is the fundamental storage structure in a relational database. Think of it like a spreadsheet with a strictly defined format: every row holds one record, and every column holds one specific type of data. Each table represents a single real-world entity — a customer, a product, an order.
| customer_id PK | first_name NN | last_name NN | email UK | city |
|---|---|---|---|---|
| 1 | Priya | Sharma | priya@example.com | Mumbai |
| 2 | Arjun | Patel | arjun@example.com | Delhi |
| 3 | Sneha | Reddy | sneha@example.com | Hyderabad |
| 4 | Rahul | Gupta | rahul@example.com | Bengaluru |
PK = Primary Key · UK = Unique Key · NN = NOT NULL
customers, orders, productscustomers = one customer’s complete data.email VARCHAR(255)Good design rule: Each table should represent one and only one entity type. Mixing customers and orders in a single table leads to data redundancy and update anomalies — this is why we normalise databases.
Keys: The Backbone of Data Integrity
Keys are special column(s) that serve specific roles in a table — they identify records uniquely, enforce constraints, and link tables together. Understanding keys is essential before working with any real-world database.
Natural Key vs Surrogate Key: A Natural Key uses real-world data as the PK (e.g., email or passport number). A Surrogate Key is an artificial auto-incremented integer (e.g., id INT AUTO_INCREMENT). In practice, surrogate keys are almost always preferred because natural keys can change, be NULL, or be too long to index efficiently.
Relationships: Linking Tables Together
The true power of relational databases comes from relationships — the ability to connect data across multiple tables. This avoids storing the same data twice (redundancy) and keeps data consistent. There are three types of relationships:
1. One-to-Many (1:N) — The Most Common
One record in Table A can relate to many records in Table B, but each record in Table B relates to only one record in Table A. This is by far the most common relationship type. The Foreign Key is placed on the “many” side.
Example: One customer can place many orders. Each order belongs to exactly one customer.
2. One-to-One (1:1)
One record in Table A corresponds to exactly one record in Table B, and vice versa. Used to split a table for performance or security reasons (e.g., keep sensitive data like passwords in a separate table).
Example: Each user has exactly one profile. Each profile belongs to exactly one user.
3. Many-to-Many (M:N)
Many records in Table A can relate to many records in Table B. This cannot be represented directly with two tables — a junction table (also called a bridge or linking table) is required to connect them. The junction table holds the Foreign Keys from both sides.
Example: A student can enrol in many courses. Each course can have many students enrolled.
The Golden Rule: You can always identify the correct relationship by asking two questions:
“Can one A have multiple B’s?” and “Can one B have multiple A’s?”
If both answers are YES → M:N (need a junction table). If only one is YES → 1:N. If both are NO → 1:1.
Key Takeaways
- A table stores data for one entity type in rows (records) and columns (fields).
- A Primary Key uniquely identifies each row — every table needs one.
AUTO_INCREMENTintegers are the preferred approach in MySQL. - A Foreign Key references a Primary Key in another table, enforcing referential integrity between related data.
- A Unique Key ensures no duplicate values in a column while allowing NULLs; a Composite Key uses multiple columns together as a unique identifier.
- The three relationship types are 1:1 (one-to-one), 1:N (one-to-many — most common), and M:N (many-to-many — requires a junction table).
- In a 1:N relationship, the Foreign Key always lives on the “many” side of the relationship.
What’s Next?
In Chapter 1.4 — Introduction to SQL, we’ll learn what SQL actually is, explore its five sublanguages (DDL, DML, DQL, DCL, TCL), and write our first real SQL statements — the commands that will drive everything we build in this course.