MySQL Transactions

2,801 views

Published on

MySQL Transactions
- Definition
- Sample scenario
- Properties (ACID)
- Syntax
- Example

Published in: Technology, Economy & Finance
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,801
On SlideShare
0
From Embeds
0
Number of Embeds
37
Actions
Shares
0
Downloads
38
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

MySQL Transactions

  1. 1. MySQL Transactions Reggie Niccolo Santos UP ITDC
  2. 2. Outline  What are transactions?  Sample scenario  Properties (ACID)  Syntax  Example
  3. 3. What are transactions?  Sequential group of DML statements, which is performed as if it were one single work unit  Will never complete unless each individual operation within the group is successful. If any operation within the transaction fails, the entire transaction will fail
  4. 4. What are transactions?  Begins with the first executable SQL statement  Ends when it is committed or rolled back, either explicitly with a COMMIT or ROLLBACK statement or implicitly when a DDL statement is issued
  5. 5. Sample Scenario Suppose a bank customer transfers money from his savings account (SB a/c) to his current account (CA a/c), the statement will be divided into four blocks:  Debit SB a/c  Credit CA a/c  Record in Transaction Journal  End Transaction
  6. 6. Sample Scenario The SQL statement to debit SB a/c is as follows: UPDATE sb_accounts SET balance = balance - 1000 WHERE account_no = 932656;
  7. 7. Sample Scenario The SQL statement to credit CA a/c is as follows: UPDATE ca_accounts SET balance = balance + 1000 WHERE account_no = 933456;
  8. 8. Sample Scenario The SQL statement for recording in the transaction journal is as follows: INSERT INTO journal VALUES (100896, 'Transaction on Benjamin Hampshair a/c', '26-AUG-08' 932656, 933456, 1000);
  9. 9. Sample Scenario The SQL statement for End Transaction is as follows: COMMIT WORK;
  10. 10. Properties (ACID)  Atomicity − Ensures that all operations within the work unit are completed successfully; otherwise, the transaction is aborted at the point of failure and previous operations are rolled back to their former state
  11. 11. Properties (ACID)  Consistency − Ensures that the database properly changes states upon a successfully committed transaction
  12. 12. Properties (ACID)  Isolation − Enables transactions to operate independently of and transparent to each other
  13. 13. Properties (ACID)  Durability − Ensures that the result or effect of a committed transaction persists in case of a system failure
  14. 14. Syntax START TRANSACTION [transaction_characteristic [, transaction_characteristic] ...] transaction_characteristic: WITH CONSISTENT SNAPSHOT | READ WRITE | READ ONLY BEGIN [WORK] COMMIT [WORK] [AND [NO] CHAIN] [[NO] RELEASE] ROLLBACK [WORK] [AND [NO] CHAIN] [[NO] RELEASE] SET autocommit = {0 | 1}
  15. 15. Example START TRANSACTION; SELECT @A:=SUM(salary) FROM employee_salary WHERE type=1; UPDATE salary_report SET summary=@A WHERE type=1; COMMIT;
  16. 16. References  Http://www.tutorialspoint.com/mysql/mysql-transactions.htm  Http://www.w3resource.com/mysql/mysql-transaction.php

×