INSERT Basics
Result:
| id | name | city |
|---|---|---|
| 7 | Gita Nair | Delhi |
| 8 | Hari Om | NULL |
| 9 | Isha Roy | NULL |
The second statement inserted two rows at once. A multi-row insert is faster than many single inserts and is all-or-nothing in InnoDB.
AUTO_INCREMENT
Let MySQL create ids. A column marked AUTO_INCREMENT (it must be a key) gets the next number automatically.
Result:
| id | body |
|---|---|
| 1 | first |
| 2 | second |
| 3 | third |
Ask for the id that was just created with LAST_INSERT_ID(). It is per connection, so other users cannot disturb it:
Result:
| last_id |
|---|
| 2 |
After a multi-row insert, LAST_INSERT_ID() returns the id of the first row of that insert:
Result:
| first_id_of_batch | rows_inserted |
|---|---|
| 1 | 3 |
AUTO_INCREMENT ids to be continuous, and never use them to count rows.
Result:
| id | body |
|---|---|
| 1 | a |
| 3 | b |
Notice that b got id 3, not 2: the ignored duplicate still consumed id 2.
When a Duplicate Appears: Four Choices
| Statement | On duplicate key |
|---|---|
INSERT | Error 1062, nothing inserted |
INSERT IGNORE | Skips the bad row silently (with a warning) |
REPLACE INTO | Deletes the old row, inserts the new one |
INSERT ... ON DUPLICATE KEY UPDATE | Updates the existing row |
ERROR 1062: Duplicate entry '1' for key 'products.PRIMARY'
INSERT IGNORE
Result:
| id | name | price |
|---|---|---|
| 1 | Laptop | 899.00 |
| 9 | Monitor | 189.00 |
The duplicate row 1 was skipped, and the new row 9 was inserted. IGNORE also turns some other errors into warnings (for example truncated text), so use it carefully.
REPLACE INTO
REPLACE deletes the conflicting row and inserts the new one. That means columns you do not mention are reset to defaults, and foreign keys may block the delete:
Result:
| name | val | updated |
|---|---|---|
| theme | dark | 0 |
updated went back to 0, because the old row was deleted. A parent row with child rows cannot be replaced:
ERROR 1451: Cannot delete or update a parent row: a foreign key constraint fails ('shop'.'orders', CONSTRAINT 'orders_ibfk_1' FOREIGN KEY ('customer_id') REFERENCES 'customers' ('id'))
ON DUPLICATE KEY UPDATE: The Upsert
This updates only what you say and leaves everything else alone. It is the usual tool for "insert or update":
Result:
| id | name | price | stock |
|---|---|---|---|
| 1 | Laptop | 949.00 | 27 |
The old stock was 15, and stock + 12 made it 27. In MySQL 8.0.19 and later, a row alias is the newer style (VALUES(price) is deprecated):
Result:
| id | name | price | stock |
|---|---|---|---|
| 1 | Laptop | 949.00 | 27 |
A Counter with Upsert
Result:
| page | views |
|---|---|
| /about | 1 |
| /home | 2 |
It is safe even when many users hit the page at once, because the database does each increment atomically.
INSERT ... SELECT
Result:
| city | customers |
|---|---|
| London | 2 |
| Mumbai | 2 |
| Singapore | 1 |
| Tel Aviv | 1 |
How Many Rows Changed?
MySQL clients show "Query OK, 2 rows affected". Programs read the same number (ROW_COUNT()). With ON DUPLICATE KEY UPDATE, an existing row that was updated counts as 2, an inserted row counts as 1, and an unchanged row counts as 0:
Result:
| updated_row_counts_as |
|---|
| 2 |
Common Beginner Mistakes
| Mistake | What happens | Fix |
|---|---|---|
Using REPLACE as an update | Deletes and re-inserts, loses columns, fails with child rows | Use ON DUPLICATE KEY UPDATE |
Using INSERT IGNORE to hide errors | Bad data silently skipped | Handle the error properly |
| Assuming ids have no gaps | Wrong counts | Use COUNT(*) |
| Hand-picking ids | Clashes between users | Use AUTO_INCREMENT |
LAST_INSERT_ID() after another insert on the same connection | Gets the wrong id | Read it right after the insert |
Practice
- Create a
visitstable withAUTO_INCREMENTand insert 3 rows, then read the last id. - Write an upsert that adds stock to a product, or creates it.
- Insert 3 rows with
INSERT IGNOREwhere one is a duplicate. - Copy all Electronics products into a new table with
INSERT ... SELECT.
Conclusion
INSERT adds rows, AUTO_INCREMENT creates ids (with gaps), INSERT IGNORE skips duplicates, REPLACE deletes and re-inserts, and ON DUPLICATE KEY UPDATE is the clean upsert.

