1. Home
  2. Data Science
  3. SQL for Data Analysis
  4. Two transactions read the same row and then write it back…

Added 30 Nov -0001

Two transactions read the same row and then write it back. Which measure prevents the lost update?

  1. SELECT with FOR UPDATE, which holds a row lock until the transaction ends Correct answer
  2. Wrapping both reads in a statement with DISTINCT applied to it first
  3. Setting the session to READ UNCOMMITTED so that writers see each other work
  4. Adding an ORDER BY clause so the rows are processed in turn by each

Correct answer

A. SELECT with FOR UPDATE, which holds a row lock until the transaction ends

22 views

MCQSEXAM

Explanation

SELECT ... FOR UPDATE takes a row-level lock, so a concurrent transaction blocks at its read until the first one commits, which serialises the read-modify-write cycle. An atomic UPDATE t SET n = n + 1 avoids the pattern altogether by doing the arithmetic inside one statement. Raising the isolation level to SERIALIZABLE also works, at the cost of more aborts and retries.

Reference

Asked in: NTS, PPSC IT, Interviews

Preparing for an exam?

Doubts & discussion

Not sure why the answer is A? Ask here and get an explanation.

Ask a doubt about this MCQ

Shown after review. No links please.

More SQL for Data Analysis MCQs

Practise all SQL for Data Analysis MCQs

Similar MCQs