Database Labs

Interactive exercises bridging database theory and execution.

Database Labs

Understanding database concepts requires bridging the gap between abstract theory and practical execution. This document provides a cheat sheet of core database concepts alongside practical, interactive scenarios designed to reinforce your engineering mental models.

Work in Progress

Lab exercises are currently conceptual. A fully interactive sandbox environment will be available soon.

Cheat Sheet: Core Database Concepts

Use this reference guide to quickly recall the fundamental mechanics of database engineering:

  • Entity & Relationship: Defines what data exists (Tables) and how it connects (Primary/Foreign Keys).
  • Constraints: Database-level rules that enforce data validity and prevent corruption (e.g., NOT NULL, UNIQUE).
  • Transactions: Atomic operations that guarantee all steps succeed together, or all fail together (Rollback).
  • Indexes: Data structures that drastically improve data retrieval speeds at the cost of slower writes.
  • N+1 Query Problem: A performance anti-pattern where a system executes one query to retrieve a list, followed by N separate queries to retrieve related data. Solved via Joins or batching.

Practical Examples: Interactive Scenarios

The following workshop-style tasks guide you through practical database engineering using a hypothetical Project Tracker application.

Lab 1: Data Modeling and Relationships

Scenario: You need to track which users own which projects.

Practical Exercise:

  1. Identify the entities: User and Project.
  2. Define the schema layout.
  3. Establish the relationship: Create a user_id Foreign Key in the Project table that references the id Primary Key in the User table.

Outcome: A reliable mapping where a single user can own multiple distinct projects without data duplication.

Lab 2: Schema Normalization

Scenario: You discover a table that stores both user details and project details in the same row, leading to redundant data whenever a user creates multiple projects.

Practical Exercise:

  1. Identify the duplicated data fields (e.g., user email, user name).
  2. Split the monolithic table into two separate tables: Users and Projects.
  3. Reconnect the data using Foreign Keys.

Outcome: A normalized schema that prevents data anomalies during updates and reduces storage overhead.

Lab 3: Mitigating Performance Bottlenecks

Scenario: Your dashboard is loading slowly because it executes a separate query to fetch the author details for every single project in a list of 100 projects (The N+1 Problem).

Practical Exercise:

  1. Analyze the current iterative query loop.
  2. Refactor the data retrieval process to use a JOIN statement or batch the queries (e.g., using a data loader).
  3. Execute a single query to retrieve all 100 projects alongside their associated author details simultaneously.

Outcome: Drastically reduced database load and faster application response times.

Lab 4: Securing Agent Workflows

Scenario: An AI assistant is tasked with managing outdated projects but proposes a destructive query: DELETE FROM Projects WHERE last_updated < '2023-01-01'.

Practical Exercise:

  1. Implement a Human-in-the-loop (HITL) interception layer.
  2. Review the proposed query without executing it.
  3. Reject the raw SQL deletion and instead route the agent to use a predefined API endpoint: POST /api/projects/archive.

Outcome: Complete protection against autonomous, destructive database modifications while retaining AI-driven automation capabilities.


Next Step: Return to the Database Systems Overview to review the core concepts.

AI Knowledge Assistant

สวัสดีครับ! ผมคือ AI Assistant ประจำเว็บไซต์

คุณสามารถสอบถามข้อมูลด้าน Computer Science, Business, หรือ Finance ได้เลยครับ