I'm reading A Critique of ANSI SQL Isolation Levels and get confused by the two definitions:
Dirty write
w1[x]...w2[x]...((c1 or a1) and (c2 or a2) in any order)
Lost update
r1[x]...w2[x]...w1[x]...c1
Doesn't the history definition of lost update include dirty write? (The w2[x]...w1[x] part). If yes then if we prevent dirty write we prevent lost update, right? I know this is clearly wrong but I don't understand why. Can anyone give some hints on what I misunderstood here? Many thanks!
I found the article explaining this example:
There is no Dirty Write since T2 commits before T1 writes x.
But the definition of lost update does not require the 2nd write to happen after the first transaction commits, right?

Dirty write is that a transaction updates or deletes (overwrites) the uncommitted data which other transactions insert, update or delete. *Basically, dirty write doesn't occur with all isolation levels in many databases.
Lost update is that two transactions read the same row to update it but the first committed update is overwritten by the second committed update. *Basically, lost update doesn't occur in
SERIALIZABLEisolation level in many databases and lost update is prevented usingSELECT FOR UPDATEin MySQL and PostgreSQL.The difference between dirty write and lost update is that uncommitted data is overwritten or committed data is overwritten.
For example, there is
producttable withid,nameandstockas shown below. *The stocks of the products decrease when customers buy the products.producttable:These steps below shows dirty write:
BEGIN;BEGIN;SELECT stock FROM product WHERE id = 2;20
20which is updated later to13because a customer buys 7 oranges.SELECT stock FROM product WHERE id = 2;20
20which is updated later to16because a customer buys 4 oranges.UPDATE product SET stock = '13' WHERE id = 2;20to13.UPDATE product SET stock = '16' WHERE id = 2;13to16before T1 commits.*Dirty write occurs.
COMMIT;COMMIT;These steps below shows lost update:
BEGIN;BEGIN;SELECT stock FROM product WHERE id = 2;20
20which is updated later to13because a customer buys 7 oranges.SELECT stock FROM product WHERE id = 2;20
20which is updated later to16because a customer buys 4 oranges.UPDATE product SET stock = '13' WHERE id = 2;20to13.COMMIT;UPDATE product SET stock = '16' WHERE id = 2;13to16after T1 commits.COMMIT;*Lost update occurs.