📖 Scenario: You are working as a database assistant for a small online bookstore. The bookstore wants to store information about books and their authors efficiently. You will create a simple table to hold book data and then improve the design to see how it affects performance.
🎯 Goal: Build a MySQL table for books with proper design choices to understand how table structure impacts query speed and storage.
📋 What You'll Learn
Create a table named books with columns id, title, author, and published_year
Add a configuration variable to limit the length of the title column
Write a query to select books published after a certain year
Add an index on the published_year column to improve query performance
💡 Why This Matters
🌍 Real World
Good table design helps websites and apps load data faster and use less storage, improving user experience.
💼 Career
Database designers and developers must create efficient tables and indexes to handle large data and keep applications responsive.
Progress0 / 4 steps
1
Create the initial books table
Create a MySQL table called books with these columns: id as an integer primary key, title as a VARCHAR(255), author as a VARCHAR(100), and published_year as an integer.
MySQL
Hint
Use CREATE TABLE statement with the specified columns and types.
2
Add a configuration for title length
Modify the books table creation to set the title column length to 100 characters instead of 255.
MySQL
Hint
Change the VARCHAR length for title to 100.
3
Write a query to select recent books
Write a SQL query to select all columns from books where published_year is greater than 2010.
MySQL
Hint
Use SELECT * FROM books WHERE published_year > 2010;
4
Add an index to improve query performance
Add an index on the published_year column in the books table to speed up queries filtering by year.
MySQL
Hint
Use CREATE INDEX statement on published_year.
Practice
(1/5)
1. Why does using proper data types in a MySQL table improve performance?
easy
A. It reduces the amount of storage space and speeds up data retrieval.
B. It makes the table look nicer in the database.
C. It allows more users to connect at the same time.
D. It automatically creates backups of the data.
Solution
Step 1: Understand data types and storage
Choosing the right data type means the database uses only the needed space for each value.
Step 2: Impact on speed
Smaller data sizes mean faster reading and writing, improving performance.
Final Answer:
It reduces the amount of storage space and speeds up data retrieval. -> Option A
Quick Check:
Proper data types = less space + faster queries [OK]
Hint: Smaller data types mean faster access and less storage [OK]
Common Mistakes:
Thinking data types only affect appearance
Believing data types control user connections
Confusing data types with backup features
2. Which of the following is the correct way to define a primary key in a MySQL table?
easy
A. CREATE TABLE users (id INT PRIMARY KEY);
B. CREATE TABLE users (id INT PRIMARY, KEY);
C. CREATE TABLE users (id INT, PRIMARY(id));
D. CREATE TABLE users (id INT, KEY PRIMARY);
Solution
Step 1: Review MySQL primary key syntax
The correct syntax places PRIMARY KEY immediately after the column type.
Step 2: Check each option
CREATE TABLE users (id INT PRIMARY KEY); uses 'id INT PRIMARY KEY' which is valid syntax.
Final Answer:
CREATE TABLE users (id INT PRIMARY KEY); -> Option A
Quick Check:
Primary key syntax = 'column type PRIMARY KEY' [OK]
Hint: Primary key goes right after column type [OK]
Common Mistakes:
Separating PRIMARY and KEY keywords incorrectly
Placing KEY before PRIMARY
Using comma incorrectly between keywords
3. Given the table products(id INT PRIMARY KEY, name VARCHAR(50), price DECIMAL(5,2)), what will be the result of this query? SELECT * FROM products WHERE price < 10.00;
medium
A. No rows will be returned because price is stored as text.
B. Syntax error because of the decimal comparison.
C. All products with price less than 10.00 will be listed.
D. Only products with price exactly 10.00 will be shown.
Solution
Step 1: Understand the table schema
The price column is DECIMAL(5,2), a numeric type suitable for decimal comparisons.
Step 2: Analyze the query condition
The query selects rows where price is less than 10.00, which is valid and will return matching rows.
Final Answer:
All products with price less than 10.00 will be listed. -> Option C