0
0
MySQLquery~10 mins

ON DUPLICATE KEY UPDATE in MySQL - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - ON DUPLICATE KEY UPDATE
Attempt INSERT row
Check for duplicate key
Insert row
Finish
When inserting a row, MySQL checks if a duplicate key exists. If no duplicate, it inserts. If yes, it updates the existing row instead.
Execution Sample
MySQL
INSERT INTO users (id, name, age)
VALUES (1, 'Alice', 30)
ON DUPLICATE KEY UPDATE age = age + 1;
Try to insert a user with id=1; if id=1 exists, increase age by 1 instead.
Execution Table
StepActionDuplicate Key CheckResultTable State
1Insert (id=1, name='Alice', age=30)No duplicateRow inserted[{id:1, name:'Alice', age:30}]
2Insert (id=1, name='Alice', age=30)Duplicate on id=1Update age = age + 1[{id:1, name:'Alice', age:31}]
3Insert (id=2, name='Bob', age=25)No duplicateRow inserted[{id:1, name:'Alice', age:31}, {id:2, name:'Bob', age:25}]
4Insert (id=2, name='Bob', age=25)Duplicate on id=2Update age = age + 1[{id:1, name:'Alice', age:31}, {id:2, name:'Bob', age:26}]
5Insert (id=3, name='Carol', age=22)No duplicateRow inserted[{id:1, name:'Alice', age:31}, {id:2, name:'Bob', age:26}, {id:3, name:'Carol', age:22}]
6Insert (id=3, name='Carol', age=22)Duplicate on id=3Update age = age + 1[{id:1, name:'Alice', age:31}, {id:2, name:'Bob', age:26}, {id:3, name:'Carol', age:23}]
7Insert (id=4, name='Dave', age=40)No duplicateRow inserted[{id:1, name:'Alice', age:31}, {id:2, name:'Bob', age:26}, {id:3, name:'Carol', age:23}, {id:4, name:'Dave', age:40}]
8Insert (id=4, name='Dave', age=40)Duplicate on id=4Update age = age + 1[{id:1, name:'Alice', age:31}, {id:2, name:'Bob', age:26}, {id:3, name:'Carol', age:23}, {id:4, name:'Dave', age:41}]
9Insert (id=5, name='Eve', age=28)No duplicateRow inserted[{id:1, name:'Alice', age:31}, {id:2, name:'Bob', age:26}, {id:3, name:'Carol', age:23}, {id:4, name:'Dave', age:41}, {id:5, name:'Eve', age:28}]
10Insert (id=5, name='Eve', age=28)Duplicate on id=5Update age = age + 1[{id:1, name:'Alice', age:31}, {id:2, name:'Bob', age:26}, {id:3, name:'Carol', age:23}, {id:4, name:'Dave', age:41}, {id:5, name:'Eve', age:29}]
11Attempt Insert (id=6, name='Frank', age=35)No duplicateRow inserted[{id:1, name:'Alice', age:31}, {id:2, name:'Bob', age:26}, {id:3, name:'Carol', age:23}, {id:4, name:'Dave', age:41}, {id:5, name:'Eve', age:29}, {id:6, name:'Frank', age:35}]
12Attempt Insert (id=6, name='Frank', age=35)Duplicate on id=6Update age = age + 1[{id:1, name:'Alice', age:31}, {id:2, name:'Bob', age:26}, {id:3, name:'Carol', age:23}, {id:4, name:'Dave', age:41}, {id:5, name:'Eve', age:29}, {id:6, name:'Frank', age:36}]
13StopN/AAll inserts processedFinal table state as above
💡 All insert attempts processed; no more rows to insert.
Variable Tracker
VariableStartAfter 1After 2After 3After 4After 5After 6After 7After 8After 9After 10After 11After 12Final
Table rows count01122334455666
Alice ageN/A30313131313131313131313131
Bob ageN/AN/AN/A2526262626262626262626
Carol ageN/AN/AN/AN/AN/A222323232323232323
Dave ageN/AN/AN/AN/AN/AN/AN/A40414141414141
Eve ageN/AN/AN/AN/AN/AN/AN/AN/AN/A2829292929
Frank ageN/AN/AN/AN/AN/AN/AN/AN/AN/AN/AN/A353636
Key Moments - 3 Insights
Why does the age increase by 1 instead of replacing the old age?
Because the ON DUPLICATE KEY UPDATE clause uses 'age = age + 1', it adds 1 to the existing age instead of overwriting it. See execution_table rows 2, 4, 6, etc.
What happens if there is no duplicate key found?
The row is simply inserted as new without any update. This is shown in execution_table rows 1, 3, 5, etc.
Does ON DUPLICATE KEY UPDATE run if the duplicate key is on a different column?
No, it only triggers if the duplicate key conflicts with a unique or primary key constraint. Here, the duplicate is on 'id'.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What is Bob's age after the update?
A25
B26
C24
D27
💡 Hint
Check the 'Table State' column at step 4 in execution_table.
At which step does Carol get inserted into the table?
AStep 6
BStep 3
CStep 5
DStep 2
💡 Hint
Look for the first 'No duplicate' insert action for Carol in execution_table.
If the ON DUPLICATE KEY UPDATE clause was removed, what would happen at step 2?
AError due to duplicate key
BAge increased by 1
CRow inserted normally
DRow updated with new values
💡 Hint
Without ON DUPLICATE KEY UPDATE, duplicate key insert causes error.
Concept Snapshot
INSERT INTO table (columns) VALUES (values)
ON DUPLICATE KEY UPDATE column = value;

- Tries to insert a row.
- If duplicate key exists, runs UPDATE instead.
- Useful to avoid errors and update existing rows.
- Can use expressions like column = column + 1.
Full Transcript
The ON DUPLICATE KEY UPDATE clause in MySQL lets you try inserting a row. If the row's key already exists, instead of erroring, it updates the existing row. For example, inserting a user with id=1 will add a new row if id=1 is new. If id=1 exists, it updates the age by adding 1. This way, you can keep data fresh without manual checks. The execution table shows each insert attempt, whether it inserts or updates, and the table state after. Variables like age change only on updates. This helps beginners see how MySQL handles duplicates smoothly.