Uploaded on

 

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
240
On Slideshare
0
From Embeds
0
Number of Embeds
8

Actions

Shares
Downloads
28
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. Recall • What is the difference between UDF and SP? • What are the advantages of Views? • What is indexing?
  • 2. Introduction to MySQL Transactions and DCL statements Day 1,Week 7
  • 3. Transactions
  • 4. Relevance of Transaction tbl_login pk_user_id username password 1 John Admin123 2 alex Alex123 tbl_user pk_user_id fk_user_id user_name user_dob 1 2 Alex 1995-12-12 2 1 John 1995-09-21 • In the above tables, Whenever a user register, entry must go into both tbl_user and tbl_login. • There should not be a situation with entry in only one table . ie user without login details or login details without any user information • This means whenever we enter data into these tables, we should make sure entry is done for both table or to neither tables
  • 5. Transactions • 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.
  • 6. Create procedure sample() Begin Set a int defaul 0; Set b int default 0; START TRANSACTION; Insert into tbl_login values(„john‟,‟john123‟); Set a = last_insert_id(); Insert into tbl_user values(a,‟john mathew‟,1980-12- 13); Set b = last_insert_id(); If a>0 && b>0 THEN COMMIT; ELSE ROLLBACK; Will start a new transaction. So any sql operations will get effected until we give commit or rollback
  • 7. Create procedure sample() Begin Set a int defaul 0; Set b int default 0; START TRANSACTION; Insert into tbl_login values(„john‟,‟john123‟); Set a = last_insert_id(); Insert into tbl_user values(a,‟john mathew‟,1980-12- 13); Set b = last_insert_id(); If a>0 && b>0 THEN COMMIT; ELSE ROLLBACK; Will make the changes permanently
  • 8. Create procedure sample() Begin Set a int defaul 0; Set b int default 0; START TRANSACTION; Insert into tbl_login values(„john‟,‟john123‟); Set a = last_insert_id(); Insert into tbl_user values(a,‟john mathew‟,1980-12- 13); Set b = last_insert_id(); If a>0 && b>0 THEN COMMIT; ELSE ROLLBACK; Will discard the changes and will return to previous state
  • 9. Create procedure sample() Begin SET a int defaul 0; SET b int default 0; SET done default 0; DECLARE CONTINUE HANDLER FOR SQLWARNING,SQLEXCEPTION SET done=1; START TRANSACTION; Insert into tbl_login values(„john‟,‟john123‟); Insert into tbl_user values(a,‟john mathew‟,1980-12- 13); If done==0 THEN COMMIT; ELSE ROLLBACK; Transactions with declare handler
  • 10. Questions? “A good question deserve a good grade…”
  • 11. Self Check !!
  • 12. • Why should someone use Transaction? –To ensure data redundancy –To reduce network traffic between application server and database server –To ensure data integrity Self Check !!
  • 13. • Why should someone use Transaction? –To ensure data redundancy –To reduce network traffic between application server and database server –To ensure data integrity Self Check !!
  • 14. • Why should someone use Transaction? –To ensure data redundancy –To reduce network traffic between application server and database server –To ensure data integrity Self Check !!
  • 15. Create Procedure insertData() Begin DECLARE done default 0; DECLARE CONTINUE HANDLER FOR SQLWARNING,SQLEXCEPTION SET done=1; START TRANSACTION; Insert into tbl_login(pk_int_id,vchr_uname,vchr_pword) values(1,‟john123‟,‟321‟); Insert into tbl_user (vchr_name,dat_dob) values(‟john mathew‟,‟1980-12-13‟); If done==0 THEN COMMIT; ELSE ROLLBACK; Will it be committed or rolled back? And reason?
  • 16. Create Procedure insertData() Begin DECLARE done default 0; DECLARE CONTINUE HANDLER FOR SQLWARNING,SQLEXCEPTION SET done=1; START TRANSACTION; Insert into tbl_login(pk_int_id,vchr_uname,vchr_pword) values(1,‟john123‟,‟321‟); Insert into tbl_user (vchr_name,dat_dob) values(‟john mathew‟,‟1980-12-13‟); If done==0 THEN COMMIT; ELSE ROLLBACK; Will it be committed or rolled back? And reason? Things will work fine (commited)when you call the SP for the first time. But for the second time onwards it will keep roll back as the table already has the same primary key and hence SQLWARNING will be triggered 1 2
  • 17. End of Day