Skip to lesson content
navan✳THE BUILD LABPYTHON 3
0/101 complete
BUILD LAB / PYTHON/ Python meets MySQL
EXPERIMENT 88 / 101

A schema change with an exit plan.

Explore a temporary table lifecycle without confusing DDL with row transactions.

ADVANCED LAB
BY NAVAN SAMI◷ 10 MIN READPYTHON 3

01 The idea

DDL changes database structure. CREATE defines a table, ALTER changes it, and DROP removes it. A real application should track persistent schema changes as reviewed migrations with deployment and recovery plans. This example uses only a temporary table so its scope is limited to the fixture connection. The workbench creates the practice database through its server setup rather than granting a lesson script broad account-administration privileges.

02 The experiment

Read it once. Predict what will happen. Then copy it into a .py file and run it in your local Python setup.

mysql_table_lifecycle.py
from lab_support import mysql_lab

with mysql_lab() as (connection, cursor):
    cursor.execute("CREATE TEMPORARY TABLE lab_archive (id INT PRIMARY KEY, title VARCHAR(80) NOT NULL)")
    cursor.execute("ALTER TABLE lab_archive ADD COLUMN reviewed BOOLEAN NOT NULL DEFAULT FALSE")
    cursor.execute("INSERT INTO lab_archive (id, title) VALUES (%s, %s)", (1, "Orbit"))
    cursor.execute("SELECT title, reviewed FROM lab_archive WHERE id = %s", (1,))
    row = cursor.fetchone()
    print(row["title"], bool(row["reviewed"]))
    cursor.execute("DROP TEMPORARY TABLE lab_archive")
    print("Temporary table retired")
↳ EXPECTED OUTPUT
Orbit False
Temporary table retired

03 Under the hood

  1. 1

    The ALTER gives existing or newly inserted rows a defined default. A persistent migration must also account for table size, locking, application compatibility, and backfill costs.

  2. 2

    DROP TEMPORARY TABLE makes the intended temporary target explicit. Removing a table is different from deleting selected rows and removes its structure as well.

  3. 3

    To create a persistent practice database outside Compose, an administrator can create a dedicated database and scoped user using MySQL’s administration tools. Keep those privileges out of ordinary application reporting code.

04 Your turn, builder

A SMALL MISSION

Create and drop a temporary scratch table, leaving the fixture task table untouched.

Reveal one possible solution +
one_possible_solution.py
from lab_support import mysql_lab

with mysql_lab() as (connection, cursor):
    cursor.execute("CREATE TEMPORARY TABLE lab_scratch (id INT PRIMARY KEY)")
    cursor.execute("DROP TEMPORARY TABLE lab_scratch")
    cursor.execute("SELECT COUNT(*) AS total FROM lab_tasks")
    print(cursor.fetchone()["total"])

05 Check your signal

QUICK SIGNAL CHECK

What does DROP TABLE remove?

THAT’S ANOTHER IDEA IN YOUR TOOLKIT.

Experiment. Logged.

When you can explain the idea and make the example your own, mark it complete. Your log stays in this browser.

Keep exploring

Go deeper with the official Python guide ↗.

Built with curiosity. By Navan Sami. ↗Python 3 · Think. Try. Build.