Published on

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring Partner Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd
  2. 2. TRANSACTIONS IN MYSQL Jaseena A P Muhammed A P me 9539443588
  3. 3. WHAT IS TRANSACTION?  Many applications require a lot of users to access the data simultaneously (e.g. airline booking systems)  Uncontrolled simultaneous access can result in inconsistency, so some controlling mechanism is required  A transaction is a logical unit of work which takes the DB from one consistent state to another, i.e. obeying constraints  It will probably be made up of smaller operations which temporarily cause inconsistency
  4. 4. DATABASE TRANSACTION Database transactions are logical units of work which must ALL be performed to maintain data integrity E.g. Move money from one account to another UPDATE Account SET balance = balance – 100 WHERE accountNo = 123; UPDATE Account SET balance = balance + 100 WHERE accountNo = 124;
  5. 5. DATABASE TRANSACTION  A transaction is the execution of a program that accesses the DB and starts with a BEGIN operation, followed by a sequence of READ and WRITE operations, ending with a COMMIT operation.  An update, for example adding 10 to a value, will actually begin first read the value, calculate the new value, and then write the new value commit
  6. 6. DATABASE TRANSACTION  Transactions are used for three purposes in DBMS:  To determine when integrity constraint checks should occur (only at the end of transactions)  To control concurrent access. Gives a single user the illusion of being the sole user of the database  To manage recovery from system crashes
  7. 7. ACID PROPERTIES OF TRANSACTIONS Atomicity  ALL operations in a transaction must be completed. If not, the transaction is aborted. The entire transaction is treated as a single, indivisible unit of work which must be performed completely or not at all.  Consistency  A successful transaction takes the database from one state that is consistent with the rules to another state that is also consistent with the rules.  If an operation is executed that violates the database’s integrity constraints, the entire transaction will be rolled back.
  8. 8. ACID PROPERTIES OF TRANSACTIONS Isolation  Data used within a transaction cannot be used by another transaction until the first transaction is completed. (or it must appear that this happened!). The partial effects of incomplete transactions should not be visible to other transactions. Durability  Once the transaction changes have been made, they will survive failure. The recovery system must ensure this.
  9. 9. TRANSACTIONS IN MYSQL To successfully transfer money, use the START TRANSACTION and COMMIT commands: START TRANSACTION UPDATE Account SET balance = balance – 100 WHERE accountNo = 123; UPDATE Account SET balance = balance + 100 WHERE accountNo = 124; COMMIT; The database is not updated until the COMMIT command is executed
  10. 10. TRANSACTIONS IN MYSQL  MySQL runs by default with auto-commit enabled. Each MySQL statement is treated as a single transaction, with an implicit COMMIT at the end. In this case, UPDATE Account SET balance = balance + 100; is the same as START TRANSACTION UPDATE Account SET balance = balance + 100; COMMIT;
  11. 11. ROLL BACK The DBMS maintains a transaction log. If the computer crashes in the middle of a transaction, the DBMS will rollback the database to the last completed transaction
  12. 12. ROLL BACK Also, you can use the MySQL ROLLBACK command This is most useful for testing updates – your database is restored to the state immediately before the last transaction. Check it worked, then rollback START TRANSACTION UPDATE Account SET bal = bal – 100 WHERE accountNo = 123; UPDATE Account SET bal = bal + 100 WHERE accountNo = 124; SELECT balance FROM Account WHERE accountNo = 123; SELECT balance FROM Account WHERE accountNo = 124; ROLLBACK;
  13. 13. Pk_student_id Fk_teacher_id Student_course Pk_teacher_id Teacher_name ANOTHER EXAMPLE
  14. 14. ANOTHER EXAMPLE Create procedure sample() Begin Set a int defaul 0; Set b int default 0; START TRANSACTION; Insert into tbl_teachers (teacher_name) values(’john’); Set a = last_insert_id(); Insert into Tbl_student (Fk_teacher_id,student_course) values(1,’php’); Set b = last_insert_id(); If a>0 && b>0 THEN COMMIT; ELSE ROLLBACK; End
  15. 15. THANK YOU
  16. 16. If this presentation helped you, please visit our page and like it. Thanks in advance. | |
  17. 17. Contact Us Emarald Mall (Big Bazar Building) Mavoor Road, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 NC Complex, Near Bus Stand Mukkam, Kozhikode, Kerala, India. Ph: + 91 – 495 40 25 550 Start up Village Eranakulam, Kerala, India. Email: