0
0
MySQLquery~5 mins

ON DUPLICATE KEY UPDATE in MySQL - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does ON DUPLICATE KEY UPDATE do in MySQL?
It updates an existing row if a new insert would cause a duplicate key error, instead of causing an error.
Click to reveal answer
beginner
Which SQL statement uses ON DUPLICATE KEY UPDATE?
The INSERT statement can use ON DUPLICATE KEY UPDATE to handle duplicate keys gracefully.
Click to reveal answer
intermediate
How do you update a column named count by adding 1 when a duplicate key occurs?
Use ON DUPLICATE KEY UPDATE count = count + 1 in your INSERT statement.
Click to reveal answer
intermediate
Can ON DUPLICATE KEY UPDATE be used to update multiple columns?
Yes, you can update multiple columns by separating assignments with commas, like ON DUPLICATE KEY UPDATE col1 = val1, col2 = val2.
Click to reveal answer
beginner
What happens if no duplicate key is found when using ON DUPLICATE KEY UPDATE?
The row is inserted normally without any update.
Click to reveal answer
What does ON DUPLICATE KEY UPDATE do in a MySQL INSERT statement?
ADeletes the existing row if a duplicate key is found
BThrows an error and stops the query
CIgnores the insert if a duplicate key is found
DUpdates the existing row if a duplicate key is found
Which keyword is used with INSERT to handle duplicate keys?
AON ERROR IGNORE
BON DUPLICATE KEY UPDATE
CIF EXISTS UPDATE
DON CONFLICT DO NOTHING
How do you increase a numeric column visits by 1 on duplicate key?
AON DUPLICATE KEY UPDATE visits = visits + 1
BON DUPLICATE KEY UPDATE visits = 1
CON DUPLICATE KEY UPDATE visits += 1
DON DUPLICATE KEY UPDATE visits++
Can ON DUPLICATE KEY UPDATE update multiple columns?
AYes, by separating assignments with commas
BNo, only one column can be updated
CYes, but only if columns are numeric
DNo, it only inserts new rows
What happens if no duplicate key exists when using ON DUPLICATE KEY UPDATE?
AThe row is updated anyway
BThe query fails
CThe row is inserted normally
DThe query is ignored
Explain how ON DUPLICATE KEY UPDATE works in MySQL and give a simple example.
Think about inserting a row that might already exist.
You got /3 concepts.
    Describe a real-life scenario where ON DUPLICATE KEY UPDATE is useful.
    Consider tracking website visits or inventory counts.
    You got /3 concepts.