10. Transactions
Last updated
BEGIN TRANSACTION;
-- or
BEGIN WORK;
-- or
BEGIN;COMMIT WORK;
-- or
COMMIT TRANSACTION;
-- or
COMMIT;-- start a transaction
BEGIN;
-- deduct 1000 from account 1
UPDATE accounts
SET balance = balance - 1000
WHERE id = 1;
-- add 1000 to account 2
UPDATE accounts
SET balance = balance + 1000
WHERE id = 2;
-- select the data from accounts
SELECT id, name, balance
FROM accounts;
-- commit the transaction
COMMIT;ROLLBACK WORK;
-- or
ROLLBACK TRANSACTION;
-- or
ROLLBACK;-- begin the transaction
BEGIN;
-- deduct the amount from the account 1
UPDATE accounts
SET balance = balance - 1500
WHERE id = 1;
-- add the amount from the account 3 (instead of 2)
UPDATE accounts
SET balance = balance + 1500
WHERE id = 3;
-- roll back the transaction
ROLLBACK;