A relational database is a structured system for storing data in tables where relationships between tables are defined using keys.
The concept comes from Edgar F. Codd at IBM in 1970.
Data is stored in relations (tables), not files.
Each table:
- Has rows (tuples)
- Has columns (attributes)
- Has a defined schema
Example:
| id | name | |
|---|---|---|
| 1 | Alice | alice@email.com |
This table represents a mathematical relation.
A DBMS (Database Management System) is software that:
- Stores data
- Retrieves data
- Enforces constraints
- Manages concurrency
- Ensures security
- Handles backups
Examples:
- PostgreSQL
- MySQL
- Oracle Database
- Microsoft SQL Server
You are learning the language (SQL), not the engine itself.
Logical container for structured data.
Single record.
Defines property and datatype.
Schema = Blueprint of table.
Example schema:
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100),
email VARCHAR(100),
created_at DATE
);A primary key:
- Uniquely identifies each row
- Cannot be NULL
- Must be unique
Example:
id INT PRIMARY KEYWhy important? Without it, you cannot reliably identify records.
A foreign key:
- Links one table to another
- Enforces referential integrity
Example:
CREATE TABLE orders (
id INT PRIMARY KEY,
user_id INT,
FOREIGN KEY (user_id) REFERENCES users(id)
);This ensures: You cannot create an order for a user that does not exist.
Normalization = process of reducing redundancy.
Goal:
- Avoid duplicate data
- Avoid update anomalies
- Improve consistency
Example of bad design:
| user_id | user_name | order_id |
|---|
If user_name changes, you update many rows.
Normalized design separates users and orders.
We will go deeper on Day 6.
ACID properties guarantee reliability:
Transaction happens fully or not at all.
Database remains valid after transaction.
Concurrent transactions don’t break each other.
Committed data survives crashes.
Example: Bank transfer must be atomic.
Choose ONE.
1️⃣ Download: https://www.postgresql.org/download/
2️⃣ Install:
- Keep default port (5432)
- Set password for user
postgres - Install pgAdmin
3️⃣ Open pgAdmin
4️⃣ Create database:
Right click → Create → Database → name it sql_bootcamp
5️⃣ Open Query Tool
1️⃣ Download: https://dev.mysql.com/downloads/installer/
2️⃣ Install:
- Choose Developer Default
- Set root password
- Install MySQL Workbench
3️⃣ Open MySQL Workbench 4️⃣ Create schema:
CREATE DATABASE sql_bootcamp;
USE sql_bootcamp;Basic form:
SELECT column_name FROM table_name;Example:
SELECT name FROM users;Returns one column.
SELECT * FROM users;Returns all columns.
⚠ Avoid in production when tables are large.
Filtering condition:
SELECT * FROM users
WHERE id = 1;Sort results:
SELECT * FROM users
ORDER BY name ASC;DESC for descending.
Restrict rows (Postgres/MySQL):
SELECT * FROM users
LIMIT 5;Remove duplicates:
SELECT DISTINCT name FROM users;Create this table:
CREATE TABLE users (
id INT PRIMARY KEY,
name VARCHAR(100),
age INT,
country VARCHAR(50),
signup_year INT
);Insert:
INSERT INTO users VALUES
(1, 'Alice', 25, 'USA', 2023),
(2, 'Bob', 30, 'UK', 2024),
(3, 'Carol', 22, 'USA', 2024),
(4, 'David', 35, 'Canada', 2022),
(5, 'Eve', 28, 'UK', 2023);Now practice 20+ queries.
Use:
- https://www.w3schools.com/sql/
- https://www.hackerrank.com/domains/sql
- https://leetcode.com/problemset/database/
Focus on:
- Filtering
- Sorting
- Simple retrieval
Write a query to retrieve all users from the UK.
Select only the names and ages of users older than 25.
Retrieve users who signed up after 2022 and sort them by age descending.
Count how many users are from the USA.
Explain why a primary key improves indexing performance internally in a B-tree structure.
If two concurrent transactions update the same row, which ACID property ensures they don’t corrupt data?
- https://pgexercises.com/ (PostgreSQL specific)
- https://sqlzoo.net/
- https://mode.com/sql-tutorial/
- https://leetcode.com/problemset/database/
You must:
✔ Understand what a relational model is ✔ Know what ACID means ✔ Know why primary keys matter ✔ Create tables ✔ Insert data ✔ Write filtered SELECT queries confidently
Design a simple students table with:
- id
- name
- age
- grade
- enrollment_year
Insert 10 records.
Write queries:
- Students older than 18
- Students in grade A
- Top 3 oldest students
- Distinct grades
- Students enrolled after 2022
Tomorrow (Day 2): We move into logical filtering mastery and SQL thinking patterns.