Databases · Middle · Question 18 of 75

What is a 'lost update', and how can you prevent it?

-- Two requests at the same time:
SELECT balance FROM accounts WHERE id = 1;   -- both read 100
UPDATE accounts SET balance = 150 WHERE id = 1;  -- +50
UPDATE accounts SET balance = 130 WHERE id = 1;  -- +30

Explanation

Read-modify-write in application code is the classic source. Let the database do the arithmetic, or lock or version the row.

Further reading

Problem with a question

Databases · Middle · question 18

Test yourself

Take the Databases · Middle quiz: questions and options are shuffled, and you get a review of your mistakes at the end.

Take this quiz