Lab 02: Creating Databases and Tables

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

Overview

Learn to create databases and tables with proper data types, constraints, and defaults. Inspect schema with DESCRIBE and \d. Clean up with DROP.


Step 1: Start the Databases

# MySQL
docker run -d --name mysql-lab -e MYSQL_ROOT_PASSWORD=rootpass mysql:8.0
for i in $(seq 1 30); do docker exec mysql-lab mysql -uroot -prootpass -e "SELECT 1" 2>/dev/null && break || sleep 2; done

# PostgreSQL
docker run -d --name pg-lab -e POSTGRES_PASSWORD=rootpass postgres:15
sleep 10

Step 2: CREATE DATABASE

MySQL:

CREATE DATABASE IF NOT EXISTS school;
USE school;
SELECT DATABASE();

📸 Verified Output (MySQL):

PostgreSQL:

📸 Verified Output (PostgreSQL):

💡 In MySQL, USE dbname switches context for the session. In PostgreSQL, connect with \c dbname or -d dbname flag.


Step 3: Data Types Reference

Category
MySQL
PostgreSQL
Notes

Integer

INT, BIGINT, TINYINT

INTEGER, BIGINT, SMALLINT

PG also has SERIAL (auto-increment)

Auto-increment

INT AUTO_INCREMENT

SERIAL / BIGSERIAL

PG SERIAL = INTEGER + sequence

String

VARCHAR(n), CHAR(n)

VARCHAR(n), CHAR(n)

Max n=65535 MySQL, 10485760 PG

Long text

TEXT, MEDIUMTEXT, LONGTEXT

TEXT

PG TEXT is unlimited

Decimal

DECIMAL(p,s)

NUMERIC(p,s)

Use for money — exact precision

Float

FLOAT, DOUBLE

REAL, DOUBLE PRECISION

Approximate — avoid for money

Boolean

TINYINT(1) or BOOL

BOOLEAN

MySQL stores 0/1; PG: TRUE/FALSE

Date

DATE

DATE

YYYY-MM-DD

Time

TIME

TIME

HH:MM:SS

Datetime

DATETIME, TIMESTAMP

TIMESTAMP

TIMESTAMP in PG is timezone-aware with WITH TIME ZONE


Step 4: CREATE TABLE with Constraints

MySQL:

📸 Verified Output (MySQL):

PostgreSQL:

💡 NOT NULL prevents storing NULL (missing) values. DEFAULT provides a fallback when no value is specified on INSERT.


Step 5: NULL vs NOT NULL vs DEFAULT

Column
INSERT with no value
INSERT NULL

NOT NULL

Error

Error

nullable

Stores NULL

Stores NULL

DEFAULT 'x'

Stores 'x'

Stores NULL

NOT NULL DEFAULT ''

Stores ''

Error


Step 6: DESCRIBE / \d — Inspect Table Structure

MySQL:

📸 Verified Output (MySQL):

PostgreSQL:

📸 Verified Output (PostgreSQL):


Step 7: CREATE TABLE with Multiple Tables and Relationships

📸 Verified Output (MySQL):


Step 8: Capstone — DROP and Recreate

Practice the full lifecycle:

📸 Verified Output:

💡 Always DROP TABLE IF EXISTS to avoid errors if the table doesn't exist. Drop child tables (with foreign keys) before parent tables.

Cleanup:


Summary

Command
MySQL
PostgreSQL

Create database

CREATE DATABASE name;

CREATE DATABASE name;

Use database

USE name;

\c name or -d name

Create table

CREATE TABLE t (...)

CREATE TABLE t (...)

Auto-increment PK

INT AUTO_INCREMENT

SERIAL

Inspect table

DESCRIBE t;

\d t

List tables

SHOW TABLES;

\dt

Drop table

DROP TABLE IF EXISTS t;

DROP TABLE IF EXISTS t;

Drop database

DROP DATABASE name;

DROP DATABASE name;

Next: Lab 03 — Inserting and Querying Data

Last updated