Lab 01: Intro to Relational Databases

Time: 30 minutes | Level: Foundations | DB: MySQL 8 / PostgreSQL 15

Overview

In this lab you will understand the relational model, compare RDBMS to flat files, spin up MySQL 8 and PostgreSQL 15 with Docker, and run your first SQL commands.


Step 1: The Relational Model

A relational database organizes data into tables (relations). Each table has:

Concept
Description
Example

Table

A named collection of related data

employees

Row (tuple)

One record in the table

A single employee

Column (attribute)

A named field with a data type

first_name VARCHAR(50)

Primary Key

Uniquely identifies each row

employee_id INT

Foreign Key

Links to another table's PK

department_id → departments.id

RDBMS vs Flat Files

Feature
Flat File (CSV)
RDBMS

Data integrity

None

Constraints enforced

Relationships

Manual (error-prone)

Foreign keys

Concurrent access

Dangerous

ACID transactions

Querying

Line-by-line parsing

SQL — set-based operations

Scalability

Poor

Indexing, partitioning

💡 Flat files like CSVs are fine for small, single-use datasets. Once you need integrity, joins, or concurrent writes — you need an RDBMS.


Step 2: Start MySQL 8 with Docker

📸 Verified Output:


Step 3: Connect to MySQL and Explore

Inside the MySQL shell:

📸 Verified Output:

💡 The built-in databases (mysql, information_schema, etc.) are system databases. Never drop them.


Step 4: Start PostgreSQL 15 with Docker

Open a new terminal (keep MySQL running):

📸 Verified Output:


Step 5: Connect to PostgreSQL and Explore

Inside the psql shell:

📸 Verified Output:

💡 \l is a psql meta-command (not SQL). It lists databases. Use \q to quit psql, \h for SQL help, \? for meta-command help.


Step 6: Key SQL Meta-Commands Comparison

Action
MySQL
PostgreSQL

List databases

SHOW DATABASES;

\l

Use a database

USE dbname;

\c dbname

List tables

SHOW TABLES;

\dt

Describe a table

DESCRIBE tablename;

\d tablename

Quit

quit or exit

\q

Show version

SELECT VERSION();

SELECT VERSION();


Step 7: Run a Cross-DB Comparison Query

MySQL:

📸 Verified Output (MySQL):

PostgreSQL:

📸 Verified Output (PostgreSQL):


Step 8: Capstone — Design a Conceptual Schema

Based on what you've learned, design a conceptual schema for a Library Management System.

Identify the tables, columns, and relationships:

Now sketch why this is better than a flat CSV:

  • No repeated author data for multi-author books

  • loans can track due dates with foreign key integrity

  • Querying "all books by Author X" is a single JOIN — not string parsing

Cleanup:


Summary

Topic
Key Takeaway

Relational model

Tables, rows, columns, keys — set-based thinking

RDBMS vs flat files

Integrity, relationships, concurrency, SQL

MySQL 8 Docker

docker run -e MYSQL_ROOT_PASSWORD=rootpass mysql:8.0

PostgreSQL 15 Docker

docker run -e POSTGRES_PASSWORD=rootpass postgres:15

MySQL connect

mysql -uroot -prootpassSHOW DATABASES;

PostgreSQL connect

psql -U postgres\l

Version check

SELECT VERSION(); (both DBs)

Next: Lab 02 — Creating Databases and Tables

Last updated