MySQL INSERT: Complete Guide
Add data to your MySQL tables the right way.
Basic INSERT Syntax
The INSERT statement adds new rows to a table. Here's the most common form:
For example, inserting a user:
Inserting Multiple Rows
Don't run INSERT in a loop—batch your inserts for dramatically better performance:
This is much faster than three separate INSERT statements because it reduces network round trips and allows MySQL to optimize the operation.
Performance tip: For bulk loading thousands of rows, batch them in groups of 1,000-5,000. Too many values in a single statement can hit MySQL's
max_allowed_packetlimit.
Column Order and Omission
You can specify columns in any order:
Omit columns with default values or that allow NULL:
INSERT Without Column List
You can omit the column list entirely, but don't:
This breaks if you add, remove, or reorder columns. Always specify columns explicitly.
INSERT...SET Syntax
MySQL offers an alternative syntax that some find more readable:
This only works for single-row inserts.
INSERT...SELECT
Copy data from one table to another:
Create a backup table:
Handling Duplicates
ON DUPLICATE KEY UPDATE (Upsert)
When a unique key conflict occurs, update the existing row instead:
Use VALUES() to reference the value that would have been inserted:
INSERT IGNORE
Skip rows that would cause duplicate key errors:
Warning: INSERT IGNORE also ignores other errors like data truncation. Use with caution.
REPLACE INTO
Delete the existing row and insert the new one:
Warning: REPLACE deletes then inserts, which triggers DELETE and INSERT triggers, resets AUTO_INCREMENT values, and breaks foreign key relationships. Prefer ON DUPLICATE KEY UPDATE.
Getting the Inserted ID
After inserting, get the auto-generated ID:
In application code, your MySQL driver will have a method for this (e.g., insertId in Node.js, lastrowid in Python).
Inserting Date and Time
Use MySQL functions for current time:
Inserting NULL
Explicitly insert NULL:
Or omit the column if it allows NULL:
Inserting JSON
MySQL 5.7+ supports native JSON:
Common Errors
Column Count Mismatch
Fix: Ensure you have one value for each column listed.
Data Too Long
Fix: Check your VARCHAR limits or truncate the data.
Duplicate Entry
Fix: Use ON DUPLICATE KEY UPDATE, INSERT IGNORE, or check before inserting.
Quick Reference
| Pattern | Use Case |
|---------|----------|
| INSERT INTO t (cols) VALUES (vals) | Basic insert |
| INSERT INTO t (cols) VALUES (...), (...), (...) | Bulk insert |
| INSERT INTO t SET col = val | Alternative single-row syntax |
| INSERT INTO t SELECT ... | Copy from another table |
| INSERT ... ON DUPLICATE KEY UPDATE | Upsert (insert or update) |
| INSERT IGNORE | Skip duplicates silently |
| REPLACE INTO | Delete and reinsert (use sparingly) |
Summary
- Always specify column names explicitly
- Batch multiple rows into single INSERT statements for performance
- Use ON DUPLICATE KEY UPDATE for upserts instead of REPLACE
- Use LAST_INSERT_ID() to get auto-generated IDs
- Be careful with INSERT IGNORE—it hides more than just duplicate errors