.mode column
.header ON
.nullvalue NULL

-- Same scenario, but with transactions

-- Assume that Alice has exactly $100 in her account:
UPDATE acc 
  SET balance = 100 
  WHERE usr = 'Alice';

-- Run this in two separate terminals: 1 and 2
-- Alternate the command between the two terminals, as indicated

-- TERMINAL 1: Alice withdraws $100
BEGIN TRANSACTION;
-- application cheks if balance is >= 100
SELECT balance 
FROM acc 
WHERE usr = 'Alice';
-- is >= 100? yes
-- in the meantime...

-- TERMINAL 2: Alice starts a second session where she also withdraws $100
BEGIN TRANSACTION;
-- set transaction isolation level serializable;
-- application checks if balance is >= 100
SELECT balance 
FROM acc
WHERE usr='Alice';
-- is >= 100? yes
-- updates balance
UPDATE acc 
  SET balance = balance-100 
  WHERE usr='Alice';
COMMIT;
-- No money for Alice at this machine!
ROLLBACK;
.quit


-- ... finally the first application continues
-- updates balance
UPDATE acc 
  SET balance = balance - 100 
  WHERE usr = 'Alice';
COMMIT;
-- *********BANK PAYS $100 TO ALICE*******

-- How much money is now in Alice's account?
