Lab 19: MySQL Specific Features

Time: 30 minutes | Level: Foundations | DB: MySQL 8

Overview

Explore MySQL-specific features: AUTO_INCREMENT, ENUM/SET types, FULLTEXT search, JSON columns, GENERATED columns, ON DUPLICATE KEY UPDATE, and SHOW PROCESSLIST.


Step 1: Setup

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

Step 2: AUTO_INCREMENT

docker exec mysql-lab mysql -uroot -prootpass << 'EOF'
CREATE DATABASE mysqllab;
USE mysqllab;

-- AUTO_INCREMENT: automatically assigns sequential integers
CREATE TABLE items (
    item_id    INT NOT NULL AUTO_INCREMENT,
    name       VARCHAR(100) NOT NULL,
    PRIMARY KEY (item_id)
) AUTO_INCREMENT = 1000;  -- start from 1000

INSERT INTO items (name) VALUES ('Alpha'), ('Beta'), ('Gamma');
SELECT * FROM items;

-- Check last inserted ID
SELECT LAST_INSERT_ID();

-- Check current auto_increment value
SELECT AUTO_INCREMENT
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = 'mysqllab' AND TABLE_NAME = 'items';

-- Gap behavior: deleted rows create gaps
DELETE FROM items WHERE item_id = 1001;
INSERT INTO items (name) VALUES ('Delta');  -- gets 1003, not 1002!
SELECT * FROM items;

-- Reset AUTO_INCREMENT
ALTER TABLE items AUTO_INCREMENT = 10;
INSERT INTO items (name) VALUES ('Epsilon');
SELECT * FROM items ORDER BY item_id;
EOF

📸 Verified Output:

💡 AUTO_INCREMENT values are never reused, even after deletes. This is intentional — IDs should be stable references. Use UUID for distributed systems where sequence predictability is a security concern.


Step 3: ENUM and SET Types

📸 Verified Output (priority = 'high'):

⚠️ ENUM/SET trade-offs: Fast storage and validation, but changing valid values requires ALTER TABLE — expensive on large tables. Consider a lookup table + FK for flexibility.


Step 4: FULLTEXT Index and MATCH AGAINST

📸 Verified Output (natural language search):


Step 5: JSON Column and JSON_EXTRACT

📸 Verified Output:


Step 6: GENERATED Columns

📸 Verified Output:


Step 7: ON DUPLICATE KEY UPDATE

📸 Verified Output:


Step 8: Capstone — SHOW PROCESSLIST and Session Info

📸 Verified Output (SHOW PROCESSLIST):

Cleanup:


Summary

Feature
Syntax
Notes

Auto-increment

INT AUTO_INCREMENT

Gaps are normal, never reused

Start value

AUTO_INCREMENT = 1000 in CREATE

Or ALTER TABLE

Last ID

LAST_INSERT_ID()

Session-scoped

ENUM column

ENUM('a','b','c')

Stored as integer internally

SET column

SET('a','b','c')

Bitmask storage, multi-value

FULLTEXT index

FULLTEXT KEY ft (col1, col2)

InnoDB supported since 5.6

Fulltext search

MATCH(cols) AGAINST('term')

Returns relevance score

JSON column

JSON type

Validated JSON storage

JSON extract

JSON_EXTRACT(col, '$.key')

or col -> '$.key'

Generated (virtual)

AS (expr) VIRTUAL

Computed on-the-fly

Generated (stored)

AS (expr) STORED

Stored on disk, indexable

Upsert

INSERT ... ON DUPLICATE KEY UPDATE

Idiomatic MySQL upsert

Process list

SHOW PROCESSLIST

Active connections/queries

Kill query

KILL QUERY process_id

Stop runaway query

Next: Lab 20 — Capstone: E-Commerce Database

Last updated