Session Plan
Focus on practical modeling exercises and SQL practice
Ensure trainees understand how to translate business requirements into database structures
Emphasize database design principles and when to use different relationships
Build toward the assignment step by step
Table of Contents
The main idea is to start with a requirement
Build the Entity-Relationship Diagram (ERD) the trainees
Translate the ERD into SQL statements
See the limitations of the initial design
Introduce foreign keys (status)
Add many-to-many relationships (user-task)
Practice querying relationships
Let the trainees practice designing a database based on articles_example.json
Mentor teaching notes
Teaching formats
DEMO = Mentor shows on screen, trainees observe
EXERCISE = Trainees work on their own machines
Exercises solution
The demos and exercises in this session build on top of each other, and a working solution is required for the assignment and going into Week 2. If, for whatever reason, you need a final working solution for yourself or to help a trainee, you can run the following:
sqlite3 tasks.sqlite3 < session-materials/tasks-solution.sqlEntity-Relationship Modeling
What is an ERD and why do we use it?
Entities, attributes, relationships
Types of relationships (1:1, 1:M, M:M)
Primary keys and foreign keys
DEMO: ERD Task Management System
Mentor leads ERD creation with trainees input using Excalidraw/Draw.io
Ask trainees to suggest entities, attributes, and relationships
Scenario: Design a simple task management system with these requirements:
Users can have multiple tasks
Tasks have a title, description, and due date
Tasks can have one of three statuses: "Not started", "In progress", "Done"
Each task is assigned to exactly one user
Translating ERDs to Database Schema
DEMO: Moving from conceptual to physical model
How do you actually store this in a database?
Tables, columns, constraints
Data types in SQL
Normalization basics (focus up to 3rd normal form)
Example: move
statusto a separate table to avoid redundancy
EXERCISE 1: Database Creation (10/15 minutes)
Trainees should now create their own tasks.sqlite3 database under the databases/database directory in their assignment repo. Using the ERD from the previous exercise, trainees need to write suitable CREATE TABLE statements for storing users and tasks with statuses.
CREATE TABLE user (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
-- Your columns here
);
CREATE TABLE task (
id INTEGER PRIMARY KEY AUTOINCREMENT,
-- Your columns here
);Hints:
Use INTEGER PRIMARY KEY for IDs
Don't forget NOT NULL constraints
created DATETIME NOT NULL,
Use appropriate data types (TEXT, INTEGER, DATETIME)
DEMO: Database creation solution
Add some sample data
INSERT INTO user (name, email, phone) VALUES
('John Doe', '', '+4512345678'),
('Jane Smith', '[email protected]', '+4512345679');
INSERT INTO task (title, description, created, updated, due_date, status) VALUES
('Study SQL Queries', 'Practice writing SQL queries for data retrieval', datetime('now'), datetime('now'), '2025-08-02', 'Done'),
('Learn Database Design', 'Study ER modeling and normalization', datetime('now'), datetime('now'), '2025-08-10', 'Not started'),
('Write Unit Tests', 'Add test coverage for user authentication', datetime('now'), datetime('now'), '2025-08-05', 'Not started'),
('Deploy Application', 'Set up production environment', datetime('now'), datetime('now'), '2025-08-20', 'Not started');At this moment, the database works but... is not very useful. 😅 It still has some limitations.
The status is repeated in every task
The users are not linked to tasks.
![NOTE] Ask trainees for possible improvements.
Defining Relationships
DEMO: Understanding Foreign Keys and Relationships
Key concepts:
Foreign keys maintain data integrity
Many-to-many relationships require linking tables
JOINs connect related data across tables
The steps we will take:
1. Move status to a separate table to avoid redundancy
status to a separate table to avoid redundancyCREATE TABLE status (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL UNIQUE
);
-- Insert initial statuses
INSERT INTO status (name) VALUES ('Not started'), ('In progress'), ('Done');
-- Modify task table to use status_id
ALTER TABLE task ADD COLUMN status_id INTEGER REFERENCES status(id) DEFAULT 1;
-- Update existing tasks to use status_id
UPDATE task SET status_id = 1 WHERE status = 'Not started';
UPDATE task SET status_id = 2 WHERE status = 'In progress';
UPDATE task SET status_id = 3 WHERE status = 'Done';
-- Finally, remove old status column after migration
ALTER TABLE task DROP COLUMN status;[!IMPORTANT] What we have done:
Created a
statustable to store task statuses. The benefit? Avoids redundancy and allows easy updatesModified the
tasktable to referencestatus_idfrom thestatustable
Normally this is known as database migrations. We'll not cover this in detail, but it's a common practice in real-world applications.
2. Create a linking table for users and tasks to handle many-to-many relationships
-- Many-to-many relationship between users and tasks
CREATE TABLE user_task (
user_id INTEGER NOT NULL,
task_id INTEGER NOT NULL,
PRIMARY KEY (user_id, task_id),
FOREIGN KEY (user_id) REFERENCES user(id) ON DELETE CASCADE,
FOREIGN KEY (task_id) REFERENCES task(id) ON DELETE CASCADE
);
-- Link a user to a task
INSERT INTO user_task (user_id, task_id) VALUES (1, 1);[!IMPORTANT] > What we have done:
Created an intermediary table called
user_taskto connect users and tasks.This allows a user to have multiple tasks and a task to be assigned to multiple users.
This is usually called a linking table or junction table. It allows us to represent many-to-many relationships in a relational database. Wikipedia
Querying Relationships
Now that we have our tables set up, let's practice querying related data.
DEMO: Querying related data
Introduction to JOINs (INNER, LEFT)
Filtering with WHERE clauses
Using foreign keys effectively
Anatomy of a JOIN query
-- Example: Get all tasks with their status ids
SELECT title, status_id FROM task t;
-- Introducing JOIN to show the status name
SELECT title, status.name FROM task
JOIN status ON task.status_id = status.id;
-- Joining even further to get more information
SELECT task.title, status.name AS status_name, user.name AS user_name
FROM task
JOIN status ON task.status_id = status.id
JOIN user_task ON task.id = user_task.task_id
JOIN user ON user_task.user_id = user.id
WHERE user.phone LIKE '+45%'; -- Filter by phone number starting with +45![IMPORTANT] > What we have done: Used JOINs to connect related tables:
JOINconnects the tables
ONspecifies how they are related (foreign keys)
SELECTretrieves specific columns from both tables
ASallows renaming columns for clarity
Why does the query return only 1 row? The default JOIN is an INNER JOIN, which only returns rows where there is a match in both tables.
It's good to know there are several types of JOINs: 
EXERCISE 3: Practice more advanced queries (15 minutes)
Point the trainees to the sample data tasks-sample-data.sql in their assignment repo. They should run these commands to insert the data required for the following exercises.
Trainees practice writing these queries using the sample data:
Get all tasks assigned to a specific user name.
Find all users working on the task 'Deploy to production'
Find how many tasks each user is responsible for. Hint:
COUNT(), GROUP BYFind how many completed tasks each user has. Order them in descending order.
DEMO: Show the solution
EXERCISE 4: Design and implement a database for existing data
Design an ER model and implement the respective database for the data in this file.
Remember:
Don't worry if you can't do every step perfectly.
The important thing is to understand the main ideas.
Take your time and ask questions if you're confused.
Steps
Analyze the JSON structure
Identify entities and relationships
Create an ERD
Translate to CREATE TABLE statements
Insert sample data
Write queries to retrieve information
Last updated