SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Recall
• What is the difference between UDF and
SP?
• What are the advantages of Views?
• What is indexing?
Introduction to MySQL
Transactions and DCL statements
Day 1,Week 7
Transactions
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
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.
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
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
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
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
Questions?
“A good question deserve a good
grade…”
Self Check !!
• 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 !!
• 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 !!
• 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 !!
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?
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
End of Day

Weitere ähnliche Inhalte

Andere mochten auch (10)

Different dimensions of android development baabtra.com
Different dimensions of android development baabtra.comDifferent dimensions of android development baabtra.com
Different dimensions of android development baabtra.com
 
Code optimization
Code optimization Code optimization
Code optimization
 
Transaction
TransactionTransaction
Transaction
 
Functions with heap and stack
Functions with heap and stackFunctions with heap and stack
Functions with heap and stack
 
Mvc
MvcMvc
Mvc
 
scope of variables
scope of variablesscope of variables
scope of variables
 
Error handling in ASP.NET
Error handling in ASP.NETError handling in ASP.NET
Error handling in ASP.NET
 
C# loops
C# loopsC# loops
C# loops
 
Intoduction to php strings
Intoduction to php  stringsIntoduction to php  strings
Intoduction to php strings
 
Dom
DomDom
Dom
 

Ähnlich wie Introduction to mysql part 5

1_Transaction.pdf
1_Transaction.pdf1_Transaction.pdf
1_Transaction.pdfNhtHong96
 
What is Advance DBMS, introduction to ADBMS
What is Advance DBMS, introduction to ADBMSWhat is Advance DBMS, introduction to ADBMS
What is Advance DBMS, introduction to ADBMSSaqibAlam14
 
Changing your huge table's data types in production
Changing your huge table's data types in productionChanging your huge table's data types in production
Changing your huge table's data types in productionJimmy Angelakos
 
TRANSACTION MANAGEMENT AND TIME STAMP PROTOCOLS AND BACKUP RECOVERY
TRANSACTION MANAGEMENT AND TIME STAMP PROTOCOLS AND BACKUP RECOVERYTRANSACTION MANAGEMENT AND TIME STAMP PROTOCOLS AND BACKUP RECOVERY
TRANSACTION MANAGEMENT AND TIME STAMP PROTOCOLS AND BACKUP RECOVERYRohit Kumar
 
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)Dave Stokes
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsmaxpane
 
MySQL Timeout Variables Explained
MySQL Timeout Variables Explained MySQL Timeout Variables Explained
MySQL Timeout Variables Explained Mydbops
 
database1.pptx
database1.pptxdatabase1.pptx
database1.pptxOmarKamil1
 

Ähnlich wie Introduction to mysql part 5 (20)

Chapter 5 transactions and dcl statements
Chapter 5  transactions and dcl statementsChapter 5  transactions and dcl statements
Chapter 5 transactions and dcl statements
 
Transaction
TransactionTransaction
Transaction
 
Transaction in MYSQL
Transaction in MYSQLTransaction in MYSQL
Transaction in MYSQL
 
1_Transaction.pdf
1_Transaction.pdf1_Transaction.pdf
1_Transaction.pdf
 
Transactions
TransactionsTransactions
Transactions
 
Sql transacation
Sql transacationSql transacation
Sql transacation
 
What is Advance DBMS, introduction to ADBMS
What is Advance DBMS, introduction to ADBMSWhat is Advance DBMS, introduction to ADBMS
What is Advance DBMS, introduction to ADBMS
 
Changing your huge table's data types in production
Changing your huge table's data types in productionChanging your huge table's data types in production
Changing your huge table's data types in production
 
Les09
Les09Les09
Les09
 
TRANSACTION MANAGEMENT AND TIME STAMP PROTOCOLS AND BACKUP RECOVERY
TRANSACTION MANAGEMENT AND TIME STAMP PROTOCOLS AND BACKUP RECOVERYTRANSACTION MANAGEMENT AND TIME STAMP PROTOCOLS AND BACKUP RECOVERY
TRANSACTION MANAGEMENT AND TIME STAMP PROTOCOLS AND BACKUP RECOVERY
 
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
MySQL 8 -- A new beginning : Sunshine PHP/PHP UK (updated)
 
Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactions
 
Transation.....thanveeer
Transation.....thanveeerTransation.....thanveeer
Transation.....thanveeer
 
Less09 Data
Less09 DataLess09 Data
Less09 Data
 
Chapter 3 stored procedures
Chapter 3 stored proceduresChapter 3 stored procedures
Chapter 3 stored procedures
 
Autonomous transaction
Autonomous transactionAutonomous transaction
Autonomous transaction
 
MySQL Timeout Variables Explained
MySQL Timeout Variables Explained MySQL Timeout Variables Explained
MySQL Timeout Variables Explained
 
Oracle SQL Basics
Oracle SQL BasicsOracle SQL Basics
Oracle SQL Basics
 
database1.pptx
database1.pptxdatabase1.pptx
database1.pptx
 
Les21
Les21Les21
Les21
 

Mehr von baabtra.com - No. 1 supplier of quality freshers

Mehr von baabtra.com - No. 1 supplier of quality freshers (20)

Agile methodology and scrum development
Agile methodology and scrum developmentAgile methodology and scrum development
Agile methodology and scrum development
 
Best coding practices
Best coding practicesBest coding practices
Best coding practices
 
Core java - baabtra
Core java - baabtraCore java - baabtra
Core java - baabtra
 
Acquiring new skills what you should know
Acquiring new skills   what you should knowAcquiring new skills   what you should know
Acquiring new skills what you should know
 
Baabtra.com programming at school
Baabtra.com programming at schoolBaabtra.com programming at school
Baabtra.com programming at school
 
99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love 99LMS for Enterprises - LMS that you will love
99LMS for Enterprises - LMS that you will love
 
Php sessions & cookies
Php sessions & cookiesPhp sessions & cookies
Php sessions & cookies
 
Php database connectivity
Php database connectivityPhp database connectivity
Php database connectivity
 
Chapter 6 database normalisation
Chapter 6  database normalisationChapter 6  database normalisation
Chapter 6 database normalisation
 
Chapter 4 functions, views, indexing
Chapter 4  functions, views, indexingChapter 4  functions, views, indexing
Chapter 4 functions, views, indexing
 
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
Chapter 2  grouping,scalar and aggergate functions,joins   inner join,outer joinChapter 2  grouping,scalar and aggergate functions,joins   inner join,outer join
Chapter 2 grouping,scalar and aggergate functions,joins inner join,outer join
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Chapter 1 introduction to sql server
Chapter 1 introduction to sql serverChapter 1 introduction to sql server
Chapter 1 introduction to sql server
 
Microsoft holo lens
Microsoft holo lensMicrosoft holo lens
Microsoft holo lens
 
Blue brain
Blue brainBlue brain
Blue brain
 
5g
5g5g
5g
 
Aptitude skills baabtra
Aptitude skills baabtraAptitude skills baabtra
Aptitude skills baabtra
 
Gd baabtra
Gd baabtraGd baabtra
Gd baabtra
 
Baabtra soft skills
Baabtra soft skillsBaabtra soft skills
Baabtra soft skills
 
Cell phone jammer
Cell phone jammerCell phone jammer
Cell phone jammer
 

Kürzlich hochgeladen

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...Neo4j
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 

Kürzlich hochgeladen (20)

08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 

Introduction to mysql part 5

  • 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
  • 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…”
  • 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