I am taking a Database Design & Development course, and I just learned about SQL transactions. The textbook (Learning SQL 3rd Edition by Alan Beaulieu) is really good at explaining how things work, but isn't very good at giving examples of the actual code that we would need to use.
In the textbook, pretty much the only place where it talks about the error handling in a transaction, is an example of a transaction statement, but it is only in pseudocode:
START TRANSACTION;
/* withdraw money from first account, making sure balance is sufficient */
UPDATE account SET avail_balance = avail_balance - 500
WHERE account_id = 9988
AND avail_balance > 500;
IF <exactly one row was updated by the previous statement> THEN
/* deposit money into second account */
UPDATE account SET avail_balance = avail_balance + 500
WHERE account_id = 9989;
IF <exactly one row was updated by the previous statement> THEN
/* everything worked, make the changes permanent */
COMMIT;
ELSE
/* something went wrong, undo all changes in this transaction */
ROLLBACK;
END IF;
ELSE
/* insufficient funds, or error encountered during update */
ROLLBACK;
END IF;
In MySQL workbench (the software we use to test our queries), I tried to use a CASE
statement instead of the IF
(since MySQL uses CASE
instead of IF
), but it would give me an error that says:
"CASE" is not valid at this position, expecting EOF, ALTER...
I tried looking up how to do it, but everything I found was either not for MySQL (Oracle Database or Microsoft SQL Server), or it was just as (not) helpful as the textbook was...
Any help would be appreciated!