Creating Databases, Tables, Storage Engines (InnoDB) & MySQL Data Types
Overview & Learning Objectives
Before storing data, analysts must architect database tables with strict column types. Unlike dynamic programming languages where variables can hold any value, MySQL requires declaring data types upfront: `INT` or `BIGINT` for identifiers, `VARCHAR` for variable text, `DECIMAL(10,2)` for monetary values (never FLOAT due to rounding errors), and `DATETIME` for temporal tracking.
- Create new MySQL databases using `CREATE DATABASE ... CHARACTER SET utf8mb4`.
- Create structured tables declaring appropriate column types, constraints, and default values.
- Understand why `DECIMAL` must be used for currency instead of `FLOAT` or `DOUBLE`.
Industry Standard
Standard: Enterprise database architectures at Amazon and Stripe strictly mandate `DECIMAL(12,2)` for financial transactions to avoid IEEE 754 floating-point rounding errors.
Real-World Example: ScoopCraft Ice Cream Menu Table Schema
ScoopCraft creates its core product inventory table to store tub flavors, wholesale costs, retail prices, and inventory levels.
| Column Name | MySQL Data Type | Constraint | Purpose |
|---|---|---|---|
| item_id | INT | PRIMARY KEY AUTO_INCREMENT | Unique product identifier |
| flavor_name | VARCHAR(100) | NOT NULL UNIQUE | Customer-facing flavor title |
| retail_price | DECIMAL(6,2) | NOT NULL | Exact consumer pricing in ₹ |
| stock_units | INT | DEFAULT 0 | Current available tub count |
| created_at | DATETIME | DEFAULT CURRENT_TIMESTAMP | Timestamp of item creation |
Syntax & Core SQL Implementation
CREATE DATABASE IF NOT EXISTS scoopcraft_db
CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
USE scoopcraft_db;
CREATE TABLE menu_items (
item_id INT AUTO_INCREMENT PRIMARY KEY,
flavor_name VARCHAR(100) NOT NULL UNIQUE,
category VARCHAR(50) NOT NULL,
retail_price DECIMAL(6,2) NOT NULL,
stock_units INT DEFAULT 0,
is_active BOOLEAN DEFAULT TRUE,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;
Parameters & Arguments Breakdown
AUTO_INCREMENT: MySQL keyword that automatically assigns consecutive integers to new rows.VARCHAR(n): Variable-length character string storing up to n characters without wasting disk space.DECIMAL(p, s): Exact fixed-point numeric type where p is total precision digits and s is scale (decimals).ENGINE=InnoDB: Specifies MySQL's transactional, foreign-key supporting storage engine.
Sample Walkthrough (Q&A)
Question: Why should you never store product prices using the FLOAT or DOUBLE data type in MySQL?
SQL Pattern: DECIMAL(10, 2) vs FLOAT / DOUBLE
Result: FLOAT introduces IEEE 754 binary rounding approximations (e.g. 19.99 becomes 19.9899997)
Explanation: DECIMAL stores numbers as exact binary-coded decimal representations, guaranteeing financial calculation precision.
Common Mistakes & Limitations
- Using `FLOAT` for financial currencies, which causes balancing errors in accounting reconciliations.
- Setting `VARCHAR` length arbitrarily small (e.g. `VARCHAR(10)` for names), causing string truncation errors when longer inputs arrive.
Keyboard Shortcuts (MySQL Workbench)
| Action | Windows | Mac |
|---|---|---|
| Describe Table Structure | DESCRIBE table_name; | DESCRIBE table_name; |
| Show Create Table DDL | SHOW CREATE TABLE table_name; | SHOW CREATE TABLE table_name; |
Pro Tips & Modern Best Practices
- Always specify `CHARACTER SET utf8mb4` so your MySQL tables can natively store emojis, special symbols, and multi-language scripts.
- Use `BIGINT` instead of `INT` for primary key IDs on high-volume transaction tables to avoid hitting the 2.1-billion ID cap.