Transactions are one of the most important concepts to understand when work-
ing with a relational database. Few decisions you make will have a greater impact
on stability, performance, and data integrity. Knowing how to identify and demar-
cate the transactions in the system you are building is imperative. In this chapter,
we'll discuss what transactions are and how to work with them.
7.1 What is a transaction?
In the simplest terms, a transaction is a unit of work, usually involving a number of
steps that must succeed or fail as a group. Should any step in the transaction fail,
all steps are rolled back so that the data is left in a consistent state. The easiest way
to explain a transaction is with an example.
A simple banking example
A common example of why transactions are important is a bank funds transfer.
Consider two bank accounts owned by Alice and Bob, as shown in table 7.1.
Now consider a transfer of $1,000.00 from Alice to Bob (table 7.2).
The withdrawal from Alice's account and the deposit into Bob's account must be
completed within a single transaction. Otherwise, if the deposit failed, Alice's
account balance would be $4,000.00, but Bob's would still only be $10,000.00
(table 7.3). The $1,000.00 would be lost in limbo.
Search WWH ::