📖 Scenario: You are managing a small bookstore database. You need to add several new books to the books table at once to keep the inventory updated.
🎯 Goal: Learn how to insert multiple rows into a MySQL table using a single INSERT INTO statement.
📋 What You'll Learn
Create a table named books with columns id, title, and author
Insert multiple rows into the books table in one query
Use exact values for the books as specified
💡 Why This Matters
🌍 Real World
In real life, inserting multiple rows at once is faster and more efficient than inserting one row at a time, especially when adding new data to a database.
💼 Career
Database administrators and developers often need to bulk insert data into tables quickly and correctly, making this skill essential for managing real-world databases.
Progress0 / 4 steps
1
Create the books table
Write a SQL statement to create a table called books with three columns: id as an integer primary key, title as a variable character string of length 100, and author as a variable character string of length 100.
MySQL
Hint
Use CREATE TABLE with column definitions and specify PRIMARY KEY for id.
2
Prepare the list of books to insert
Define the exact three books to insert: (1, 'The Great Gatsby', 'F. Scott Fitzgerald'), (2, '1984', 'George Orwell'), and (3, 'To Kill a Mockingbird', 'Harper Lee'). You will use these values in the next step.
MySQL
Hint
Just add comments listing the exact book tuples for clarity before inserting.
3
Insert multiple rows into the books table
Write a single INSERT INTO books (id, title, author) statement that inserts all three books: (1, 'The Great Gatsby', 'F. Scott Fitzgerald'), (2, '1984', 'George Orwell'), and (3, 'To Kill a Mockingbird', 'Harper Lee').
MySQL
Hint
Use one INSERT INTO statement with multiple sets of values separated by commas.
4
Verify the inserted rows
Write a SELECT * FROM books; statement to retrieve all rows from the books table and confirm the insertion.
MySQL
Hint
Use SELECT * FROM books; to see all rows in the table.
Practice
(1/5)
1. What does the INSERT INTO statement with multiple rows do in MySQL?
easy
A. Updates multiple rows at once
B. Adds several records to a table in one command
C. Deletes multiple rows from a table
D. Creates a new table with multiple rows
Solution
Step 1: Understand the purpose of INSERT INTO
The INSERT INTO statement is used to add new data into a table.
Step 2: Recognize multiple rows insertion
When multiple rows are inserted, several sets of values are added in one command, saving time.
Final Answer:
Adds several records to a table in one command -> Option B
Quick Check:
Multiple rows insertion = Adds many records [OK]
Hint: Multiple rows use commas between parentheses [OK]
Common Mistakes:
Confusing INSERT with DELETE or UPDATE
Thinking it creates a new table
Believing it modifies existing rows
2. Which of the following is the correct syntax to insert multiple rows into a table named students with columns id and name?
easy
A. INSERT INTO students (id, name) VALUES (1, 'Alice'), (2, 'Bob');
B. INSERT INTO students VALUES (1, 'Alice'); (2, 'Bob');
C. INSERT INTO students (id, name) VALUES (1, 'Alice') AND (2, 'Bob');
D. INSERT INTO students (id, name) SET (1, 'Alice'), (2, 'Bob');
Solution
Step 1: Check the correct use of VALUES and parentheses
The correct syntax uses VALUES followed by multiple sets of values in parentheses separated by commas.
Step 2: Identify the correct option
INSERT INTO students (id, name) VALUES (1, 'Alice'), (2, 'Bob'); correctly uses VALUES (1, 'Alice'), (2, 'Bob') with commas separating rows.
Final Answer:
INSERT INTO students (id, name) VALUES (1, 'Alice'), (2, 'Bob'); -> Option A