Test your understanding of SQL data types with these hands-on exercises.
Create tables for an online bookstore using appropriate data types.
Problem: Design an authors table with the following requirements:
- Author ID (unique identifier)
- Full name (up to 100 characters)
- Biography (large text, optional)
- Birth date (date only)
- Country (3-letter code like USA, GBR)
- Is active (true/false)
Your Task: Write the CREATE TABLE statement with appropriate data types.
Expected Considerations:
- What's the best ID type?
- Should name be VARCHAR or CHAR?
- How to handle optional biography?
- Date vs DateTime for birth date?
Problem: Design a books table with these requirements:
- Book ID (unique identifier)
- ISBN (13 characters, always present)
- Title (up to 255 characters)
- Description (large text, optional)
- Publication date (date only)
- Page count (whole number, could be large)
- Price (currency, must be precise)
- Discount percentage (0-100, with decimals)
- In stock (true/false)
- Stock quantity (whole number, never negative)
Your Task: Create the table with proper data types and constraints.
Problem: Design an orders table with these requirements:
- Order ID (unique identifier)
- Customer email (valid email format, up to 255 chars)
- Order timestamp (exact date and time)
- Subtotal (currency, precise)
- Tax amount (currency, precise)
- Shipping cost (currency, precise)
- Total amount (currency, precise)
- Order status (one of: 'pending', 'processing', 'shipped', 'delivered', 'cancelled')
- Tracking number (optional, up to 50 chars)
Your Task: Design the table with appropriate types and constraints.
Problem: For each scenario, choose the best numeric data type and explain why:
- Student grades (0-100, no decimals)
- Product prices ($0.01 to $99,999.99)
- Scientific measurements (very large/small numbers, approximations OK)
- Population counts (could exceed 2 billion)
- Percentage values (0.00% to 100.00%)
Your Task: List the best data type for each and justify your choice.
Problem: Choose the best string type for each:
- US state codes (always 2 characters: CA, NY, TX)
- Product names (vary from 5-200 characters)
- Blog post content (could be very long)
- Phone numbers (various formats, up to 20 chars)
- User passwords (hashed, always 60 characters)
Your Task: Select appropriate string types and explain your reasoning.
Problem: Add appropriate DEFAULT values to this table:
CREATE TABLE user_profiles (
user_id INT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(255) NOT NULL,
registration_date DATE,
last_login DATETIME,
is_active BOOLEAN,
preferred_language CHAR(2),
timezone VARCHAR(50),
notification_enabled BOOLEAN
);Your Task: Modify the table to include sensible default values.
Problem: For each field, decide if it should allow NULL and explain why:
- Customer first name
- Customer middle name
- Customer last name
- Customer phone number
- Customer birth date
- Order delivery instructions
- Product weight
- Employee salary
Your Task: Mark each as NULL or NOT NULL and justify your decisions.
Problem: Design a complete product catalog with these entities:
- Categories (electronics, books, clothing, etc.)
- Products (name, description, price, etc.)
- Product variants (size, color, etc.)
- Inventory tracking
Your Task: Create all necessary tables with proper data types.
Requirements:
- Support for hierarchical categories
- Variable pricing (sales, discounts)
- Track inventory levels
- Handle product variants (same product, different size/color)
- Store product images (URLs)
- Track when products were added/modified
Problem: Design a user system with these features:
- User accounts with profiles
- Role-based permissions
- Login history tracking
- Password reset functionality
Your Task: Design the database schema with appropriate data types.
Consider:
- User identification methods
- Password storage (security)
- Session management
- Audit trail requirements
- Performance for frequent logins
After completing these exercises, you should be comfortable with:
- Choosing between INT, BIGINT, and SMALLINT
- Using DECIMAL for financial data
- Deciding between VARCHAR and CHAR
- Handling large text with TEXT fields
- Working with DATE vs DATETIME vs TIMESTAMP
- Implementing NULL vs NOT NULL appropriately
- Setting useful DEFAULT values
- Considering storage efficiency
- Planning for data growth
- Understanding database-specific variations
- Beginner (Exercises 1.1-1.3): Basic table design with common data types
- Intermediate (Exercises 2.1-3.2): Data type selection and NULL handling
- Advanced (Exercises 4.1-4.2): Complex schema design with real-world requirements
- Think about the data first: What values will actually be stored?
- Consider edge cases: What's the largest/smallest possible value?
- Plan for growth: Will the data requirements change over time?
- Performance matters: Smaller data types are faster to query
- Be consistent: Use the same patterns throughout your schema
After mastering these exercises:
- Check your solutions against solutions.sql
- Try designing schemas for your own projects
- Move on to 3-Basic-Queries
- Practice with real datasets to validate your type choices