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

  1. The main idea is to start with a requirement

  2. Build the Entity-Relationship Diagram (ERD) the trainees

  3. Translate the ERD into SQL statements

    • See the limitations of the initial design

  4. Introduce foreign keys (status)

    • Add many-to-many relationships (user-task)

  5. Practice querying relationships

  6. 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 status to 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

Mentor shows the complete solution

Add some sample data

At this moment, the database works but... is not very useful. 😅 It still has some limitations.

  1. The status is repeated in every task

  2. 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

[!IMPORTANT] What we have done:

  • Created a status table to store task statuses. The benefit? Avoids redundancy and allows easy updates

  • Modified the task table to reference status_id from the status table

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_task to 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.

  • 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:

  • JOIN connects the tables

  • ON specifies how they are related (foreign keys)

  • SELECT retrieves specific columns from both tables

  • AS allows 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: SQL 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:

  1. Get all tasks assigned to a specific user name.

  2. Find all users working on the task 'Deploy to production'

  3. Find how many tasks each user is responsible for. Hint: COUNT(), GROUP BY

  4. Find how many completed tasks each user has. Order them in descending order.

DEMO: Show the solution

Queries 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

  1. Analyze the JSON structure

  2. Identify entities and relationships

  3. Create an ERD

  4. Translate to CREATE TABLE statements

  5. Insert sample data

  6. Write queries to retrieve information

Last updated