MySQL INSERT Statement - Complete Beginner Tutorial
Quick Answer
The MySQL INSERT statement is used to add new rows of data into a table. It supports inserting single or multiple rows, specifying columns, and inserting data from other tables. Mastering INSERT is essential for managing data in MySQL databases.
Learning Objectives
- Understand the syntax and usage of the MySQL INSERT statement.
- Learn how to insert single and multiple rows into a table.
- Explore inserting data by specifying columns and using SELECT statements.
Introduction
In MySQL, the INSERT statement is a fundamental Data Manipulation Language (DML) command used to add new data to tables.
Whether you need to add a single record or multiple rows, the INSERT statement provides flexible syntax to accomplish this efficiently.
Data is the new oil — and INSERT is how you fill the tank.
Basic Syntax of INSERT
The simplest form of the INSERT statement adds a single row to a table by specifying values for all columns in order.
The general syntax is: INSERT INTO table_name VALUES (value1, value2, ...);
- The table must exist before inserting data.
- Values must match the column order and data types.
- If the table has an auto-increment column, you can omit its value.
Inserting Data by Specifying Columns
To avoid errors and improve readability, specify the columns you want to insert data into.
This also allows you to omit columns that have default values or allow NULL.
- Syntax: INSERT INTO table_name (col1, col2) VALUES (val1, val2);
- The order of columns and values must match.
- You can insert partial data if other columns have defaults.
Inserting Multiple Rows
MySQL supports inserting multiple rows in a single INSERT statement to improve performance.
This is done by listing multiple sets of values separated by commas.
- Syntax: INSERT INTO table_name (col1, col2) VALUES (val1, val2), (val3, val4), ...;
- All rows must have the same number of values matching the columns specified.
Inserting Data from Another Table
You can insert data into a table by selecting rows from another table using INSERT ... SELECT syntax.
This is useful for copying or transforming data between tables.
- Syntax: INSERT INTO target_table (col1, col2) SELECT colA, colB FROM source_table WHERE condition;
- The SELECT query must return the same number of columns as specified in the INSERT.
Practical Example
This inserts one new employee record with specified first name, last name, and age.
This adds two new employee records in a single statement.
This copies employees older than 60 into an archive table.
Examples
INSERT INTO employees (first_name, last_name, age) VALUES ('John', 'Doe', 30);This inserts one new employee record with specified first name, last name, and age.
INSERT INTO employees (first_name, last_name, age) VALUES ('Jane', 'Smith', 25), ('Mike', 'Brown', 40);This adds two new employee records in a single statement.
INSERT INTO archive_employees (first_name, last_name, age) SELECT first_name, last_name, age FROM employees WHERE age > 60;This copies employees older than 60 into an archive table.
Best Practices
- Always specify columns in your INSERT statements to avoid errors.
- Use transactions when inserting multiple rows to maintain data integrity.
- Validate data types and constraints before inserting to prevent failures.
- Use INSERT ... SELECT for efficient bulk data copying.
Common Mistakes
- Omitting columns and providing values in the wrong order.
- Inserting NULL into columns that do not allow it.
- Not matching the number of columns and values.
- Ignoring constraints like unique keys or foreign keys causing errors.
Hands-on Exercise
Insert Single and Multiple Rows
Create a table named 'products' and insert one product, then insert three more products in a single statement.
Expected output: The 'products' table contains four rows with correct data.
Hint: Use CREATE TABLE with appropriate columns, then use INSERT with and without multiple rows.
Insert Data Using SELECT
Create a backup table 'products_backup' and insert all products from 'products' into it using INSERT ... SELECT.
Expected output: 'products_backup' table has the same data as 'products'.
Hint: Use CREATE TABLE LIKE to create the backup table, then INSERT ... SELECT syntax.
Interview Questions
What is the purpose of the MySQL INSERT statement?
InterviewThe INSERT statement adds new rows of data into a MySQL table.
How do you insert multiple rows in a single MySQL statement?
InterviewBy listing multiple sets of values separated by commas in the VALUES clause.
Can you insert data into specific columns only?
InterviewYes, by specifying the column names in the INSERT statement before the VALUES clause.
MCQ Quiz
1. What is the best first step when learning INSERT Statement?
A. Understand the purpose and basic idea
B. Skip directly to advanced implementation
C. Ignore examples and practice
D. Memorize terms without context
Correct answer: A
Starting with the purpose and basic idea makes later examples and practice easier to understand.
2. Which activity helps reinforce INSERT Statement?
A. Reading once without practice
B. Building or writing a small practical example
C. Avoiding review questions
D. Skipping the summary
Correct answer: B
A small practical example helps connect the topic to real usage.
3. Which statement is most accurate about this topic?
A. The MySQL INSERT statement is used to add new rows of data into a table.
B. INSERT Statement never needs examples
C. INSERT Statement is unrelated to practical work
D. INSERT Statement should be learned without checking results
Correct answer: A
The correct option is based on the available topic explanation.
Key Takeaways
- INSERT adds new records to MySQL tables.
- You can insert one or many rows in a single statement.
- Specifying columns in INSERT helps avoid errors and improves clarity.
- INSERT can also add data from SELECT queries for bulk operations.
- The MySQL INSERT statement is used to add new rows of data into a table.
Summary
The MySQL INSERT statement is essential for adding new data to tables.
You can insert single or multiple rows, specify columns, and insert data from other tables.
Following best practices ensures your data inserts are efficient and error-free.
Frequently Asked Questions
Can I insert data without specifying all columns?
Yes, if the omitted columns have default values or allow NULL, you can specify only the columns you want to insert.
What happens if I insert a row with a duplicate primary key?
MySQL will return an error unless you use INSERT IGNORE or ON DUPLICATE KEY UPDATE clauses.
Is it better to insert multiple rows at once or one by one?
Inserting multiple rows in a single statement is more efficient and faster than inserting one row at a time.





