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