Why Use Transactions?

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 ...

Get Sams Teach Yourself MySQL now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.