Hands-on task
PostgreSQL: transferring money in a transaction
In the shop database there is a table accounts(id, owner, balance) with a CHECK (balance >= 0) constraint: alice = 100, bob = 0. Transfer 30 from alice to bob in a single transaction (BEGIN … COMMIT, two UPDATEs). Result: alice = 70, bob = 30.
Write the transaction as the script /root/transfer.sql in the Editor tab and run it in the shop database — the whole file goes through in one psql session.
- postgresql
- transactions
- begin/commit
- update
- check constraint