Whatever backend language you pick, PHP, Node.js or Python, you will end up talking to a database. And for a lot of web projects that database is MySQL. Frameworks hide some of the SQL, but the ideas underneath, tables, keys and joins, are things you have to understand yourself.
Let us build a tiny database for a library: students, books, and which student borrowed which book.
Tables and data types
A table is like a spreadsheet with strict rules. Each column has a name and a type, and every row must follow them.
CREATE DATABASE library CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE library;
CREATE TABLE students (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(150) NOT NULL UNIQUE,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
); Some quick notes on the choices here:
- utf8mb4 is the character set that properly supports all of Unicode, including Hindi text and emoji. The older utf8 in MySQL is incomplete. Always use utf8mb4 for new databases.
- VARCHAR(100) stores text up to 100 characters. Use TEXT for long content like blog posts.
- INT UNSIGNED stores whole numbers that cannot be negative, which suits IDs.
- NOT NULL means the column must have a value.
- For money, use DECIMAL(10,2), never FLOAT. Floating point numbers cannot store values like 0.1 exactly, and rounding errors in prices are not fun to debug.
Primary keys
A primary key uniquely identifies each row. No two students can share the same id, and it can never be empty. AUTO_INCREMENT makes MySQL assign the next number automatically, so you do not have to.
Why not use email as the primary key, since it is unique anyway? Because people change their email. Primary keys should never change, and other tables will point to them. A plain number with no meaning is the safest choice.
The UNIQUE on email still matters though. It makes the database itself refuse duplicate emails, even if your application code has a bug.
Foreign keys: connecting tables
Now the books and the loans:
CREATE TABLE books (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
title VARCHAR(200) NOT NULL,
author VARCHAR(100) NOT NULL
);
CREATE TABLE loans (
id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
student_id INT UNSIGNED NOT NULL,
book_id INT UNSIGNED NOT NULL,
borrowed_on DATE NOT NULL,
returned_on DATE NULL,
FOREIGN KEY (student_id) REFERENCES students(id),
FOREIGN KEY (book_id) REFERENCES books(id)
); The loans table does not copy the student’s name or the book’s title. It just stores their IDs. This is the core idea of a relational database: store each fact once, and link to it.
The FOREIGN KEY lines tell MySQL that student_id must match a real row in students. Try to insert a loan for student 999 who does not exist, and MySQL will reject it. It will also stop you deleting a student who still has loans, unless you add ON DELETE CASCADE or handle it yourself. That protection is free, so use it. Foreign keys need the InnoDB engine, which is the default in current MySQL.
Some data to play with
INSERT INTO students (name, email) VALUES
('Riya', 'riya@example.com'),
('Aman', 'aman@example.com'),
('Sneha', 'sneha@example.com');
INSERT INTO books (title, author) VALUES
('Clean Code', 'Robert C. Martin'),
('Eloquent JavaScript', 'Marijn Haverbeke');
INSERT INTO loans (student_id, book_id, borrowed_on) VALUES
(1, 1, '2025-01-10'),
(1, 2, '2025-01-12'),
(2, 2, '2025-01-15'); INNER JOIN: rows that match on both sides
Now, who borrowed what? The names are in one table, the titles in another, and the links in a third. A join brings them together:
SELECT s.name, b.title, l.borrowed_on
FROM loans l
INNER JOIN students s ON s.id = l.student_id
INNER JOIN books b ON b.id = l.book_id
ORDER BY l.borrowed_on; The ON part says how rows connect. The short names l, s and b are aliases, they just save typing. An INNER JOIN only returns rows where a match exists on both sides. Sneha has not borrowed anything, so she does not appear.
LEFT JOIN: keep everything on the left
What if you want every student, including the ones with no loans? Use a LEFT JOIN. It keeps all rows from the first table and fills in NULL where there is no match.
SELECT s.name, COUNT(l.id) AS books_borrowed
FROM students s
LEFT JOIN loans l ON l.student_id = s.id
GROUP BY s.id, s.name; Now Sneha shows up with 0. We count l.id rather than using COUNT(*), because COUNT(*) would count her one row with NULLs as 1. Small details like this are exactly where beginners get wrong answers without noticing.
Indexes: making lookups fast
When a table is small, every query is fast. When it grows, searching a column without an index means MySQL reads every single row. Primary keys and UNIQUE columns are indexed automatically, and InnoDB also creates indexes for foreign keys. For other columns you search or sort on often, add one:
CREATE INDEX idx_books_author ON books (author); Do not index everything, though. Each index makes inserts and updates a little slower and uses disk space. Index the columns that appear in your WHERE, JOIN and ORDER BY clauses, and put EXPLAIN in front of a slow query to see whether MySQL is using an index at all.
Practice is the only way
Reading about joins is not enough. Create these tables yourself, add more rows, and try to answer questions: which books are currently borrowed, which student has never returned a book, which author is most popular. Write the query, check the result by hand, and fix it when it is wrong. That loop is how SQL actually gets into your head.