SlideShare une entreprise Scribd logo
1  sur  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

Contenu connexe

En vedette (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
 

Similaire à Introduction to mysql part 5

Subqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactionsSubqueries views stored procedures_triggers_transactions
Subqueries views stored procedures_triggers_transactions
maxpane
 

Similaire à 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
 

Plus de baabtra.com - No. 1 supplier of quality freshers

Plus de 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
 

Dernier

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Dernier (20)

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
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
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
 
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
Apidays Singapore 2024 - Scalable LLM APIs for AI and Generative AI Applicati...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
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
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 

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