Given the table users with columns id (primary key) and score, what will be the content of the table after executing the following query?
INSERT INTO users (id, score) VALUES (1, 10) ON DUPLICATE KEY UPDATE score = score + 5;
Assume the table initially contains: {id: 1, score: 20}.
Think about what happens when the primary key already exists.
The ON DUPLICATE KEY UPDATE clause updates the existing row by adding 5 to the current score (20 + 5 = 25).
What happens when you run this query on a table where the primary key does not exist yet?
INSERT INTO products (product_id, quantity) VALUES (5, 100) ON DUPLICATE KEY UPDATE quantity = quantity + 10;
Consider what happens if the key is not found.
If the key does not exist, the row is inserted normally with the given values.
Which of the following queries is syntactically correct in MySQL?
INSERT INTO inventory (item_id, stock) VALUES (3, 50) ON DUPLICATE KEY UPDATE stock = stock - 1;
Check the exact keywords and their order.
The correct syntax uses ON DUPLICATE KEY UPDATE followed by the column assignments.
You want to insert multiple rows into a table and update existing rows if keys duplicate. Which query is the most efficient?
Think about reducing the number of queries sent to the database.
A single multi-row INSERT with ON DUPLICATE KEY UPDATE reduces overhead and is more efficient.
Consider this table orders with a unique key on order_number. You run:
INSERT INTO orders (order_number, status) VALUES ('A123', 'pending') ON DUPLICATE KEY UPDATE status = 'pending';But the status does not update when you expect it to. What is the most likely cause?
Think about what happens if the update sets the same value as before.
If the existing row's status is already 'pending', the update does not change the data, so it may seem like nothing happened.