A transaction is a group of Database Manipulation Language statements that occur as a unit. Either the entire transaction takes place and is committed to the database, or none of the attempted changes is saved.
Transactions enable you to ensure data integrity when performing multiple database operations. Consider a product database that includes the number of each item in stock. When a customer places an order, a record is inserted into the orders table for each product ordered, and the corresponding stock levels are decreased.
Both of these changes to the database need to take place in a single transaction. Otherwise, two possible pitfalls could be encountered.
If the items are added to a customer's order before the stock levels are decreased, there will be a period of timealbeit usually only a fraction of a secondduring which another database user could query the stock levels of a product and see the wrong value.
Although both of these pitfalls are extremely uncommon, the severity of the data corruption if one occursparticularly where financial data is involvedmeans that transactions are essential to make sure both actions happen as a unit.