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:
Entity-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.
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
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 redundancy[!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
[!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
![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