|
Sams Teach Yourself MySQL in 10 Minutes Authors: Newman C. Published year: 2006 Pages: 87-90/165 |
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 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. |
Transactions in MySQLTransactions in MySQL are available only on tables that use the InnoDB or BDB storage engines. See Lesson 14, "Creating and Modifying Tables," for more information on how to select a specific table handler. To begin a transaction, issue the BEGIN TRANSACTION command. Any database-manipulation statements issued after that command form part of the same transaction. Until you issue a statement that ends a transaction, the effects of any commands that you enter are visible only within the current MySQL session. For instance, if you delete rows from a table after issuing the START TRANSACTION command, other database users can still query those rows, even though in your session they appear to be already deleted. To commit all the changes made in a transaction, issue the COMMIT command. Doing so causes each INSERT, UPDATE , and DELETE statement that forms part of the transaction to be executed immediately and as one unit. |
Autocommit ModeBy default, the system variable AUTOCOMMIT is set to 1, which instructs MySQL to process each database-manipulation statement issued immediately as a single transaction. This is the behavior you have already seen, with an INSERT, UPDATE , or DELETE committed to the database as soon as you issue the command. Consider this simple UPDATE statement:
UPDATE PRODUCTS SET price = 5.99 WHERE code = 'MINI'; When autocommit mode is turned on, the previous statement is executed as if you had entered the following:
BEGIN TRANSACTION; UPDATE PRODUCTS SET price = 5.99 WHERE code = 'MINI'; COMMIT; By setting the value of AUTOCOMMIT to 0, you disable this feature so that you have to use COMMIT to store any changes madeor use ROLLBACK to discard changessince the beginning of the transaction. To disable AUTOCOMMIT , use the SET command as shown:
mysql>
SET AUTOCOMMIT=0;
Query OK, 0 rows affected (0.00 sec)
|
Rolling Back a TransactionYou can also end a transaction without committing the changes with the ROLLBACK command. The result is that the database state is rolled back to how it was before you issued the START TRANSACTION command. The following example shows how you can recover from a disastrously wrong DELETE statementremember to always include the WHERE clauseif it occurs within a transaction:
mysql>
START TRANSACTION;
Query OK, 0 rows affected (0.00 sec)
mysql>
DELETE FROM products;
Query OK, 3 rows affected (0.03 sec)
mysql>
ROLLBACK;
Query OK, 0 rows affected (0.01 sec)
If you then query the products table, you will see that it still contains records.
|
|
Sams Teach Yourself MySQL in 10 Minutes Authors: Newman C. Published year: 2006 Pages: 87-90/165 |