Presentation is loading. Please wait.

Presentation is loading. Please wait.

MySQL Transaction.

Similar presentations


Presentation on theme: "MySQL Transaction."— Presentation transcript:

1 MySQL Transaction

2 Transaction A transaction is a sequential group of database manipulation operations, which is performed as if it were one single work unit. In other words, a transaction will never be complete unless each individual operation within the group is successful. If any operation within the transaction fails, the entire transaction will fail. you will club many SQL queries into a group and you will execute all of them together as a part of a transaction.

3 Transaction Statement
The transactions begin with the statement BEGIN WORK and end with either a COMMIT or a ROLLBACK statement. The SQL commands between the beginning and ending statements form the bulk of the transaction.

4 Example Break your MySQL statements into logical portions and determine when data should be committed or rolled back. Start transaction using START TRANSACTION statement Get latest sales order number from the ORDERS table, and use the next sales order number as the new sales order number. Insert a new sales order into the ORDERS table for a given customer. Insert new sales order items into the ORDERDETAILS table. Commit changes using COMMIT statement. Get data from both the table ORDERS and ORDERDETAILS tables to confirm changes.

5 -- start a new transaction start transaction; -- get latest order number := max(orderNUmber) from orders; -- set new order number + 1; -- insert a new order for customer 145 insert into orders(orderNumber, orderDate, requiredDate, shippedDate, status, customerNumber) now(), date_add(now(), INTERVAL 5 DAY), date_add(now(), INTERVAL 2 DAY), 'In Process', 145); -- insert 2 order line items insert into orderdetails(orderNumber, productCode, quantityOrdered, priceEach, orderLineNumber) 30, '136', 1), 50, '55.09', 2); -- commit changes commit; -- get the new inserted order select * from orders a inner join orderdetails b on a.ordernumber = b.ordernumber where a.ordernumber

6 AUTOCOMMIT To force MySQL not to commit changes automatically, you use the following statement: SET autocommit = 0;

7 Statements that cannot be rolled back
Some statements cannot be rolled back. In general, these include data definition language (DDL) statements, such as those that create or drop databases, those that create, drop, or alter tables or stored routines. You should design your transactions not to include such statements. If you issue a statement early in a transaction that cannot be rolled back, and then another statement later fails, the full effect of the transaction cannot be rolled back in such cases by issuing a ROLLBACK statement.


Download ppt "MySQL Transaction."

Similar presentations


Ads by Google