I have a column called balance in a table.
the values represent real money. I need to tansfer safely certain amount from one row to another.
so the value is subtracted from row 1 and added to row 2. if row 1 balance is lower than the value the whole transfer operation should fail and both row ramin unchanged.
Since i’m using innodb I’m able to use a transaction to wrap the selects and updates
but it would be nice to have an efficient atomic operation to prevent locks for long time rollbacks if something fails.
Is there an efficient way updating those two rows?
I developed my own solution:
Table name is Balance Columns are id and balance.
Precondition for this to work source and dest ids exist
parameter syntax is “:< paramter >” for php PDO::prepare
please check if any doupts