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?✗ Incorrect
It updates the existing row instead of throwing an error when a duplicate key is found.
Which keyword is used with
INSERT to handle duplicate keys?✗ Incorrect
ON DUPLICATE KEY UPDATE is the MySQL syntax for handling duplicate keys during insert.How do you increase a numeric column
visits by 1 on duplicate key?✗ Incorrect
You must explicitly set
visits = visits + 1 in the update clause.Can
ON DUPLICATE KEY UPDATE update multiple columns?✗ Incorrect
Multiple columns can be updated by separating each assignment with commas.
What happens if no duplicate key exists when using
ON DUPLICATE KEY UPDATE?✗ Incorrect
If no duplicate key exists, the row is inserted as usual.
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.