Introduction to
SQL Server
Transactions and DCL statements
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
declare @a int;
declare @b int;
START TRANSACTION
Insert into tbl_login values(‘john’,’john123’);
Set @a = scope_identity();
Insert into tbl_user values(a,’john mathew’,1980-12-13);
Set @b = scope_identity();
if(@a>0&&@b>0)
begin
COMMIT TRANSACTION end
ELSE begin
ROLLBACK TRANSACTION end
Will start a new
transaction. So any
sql operations will
get effected until we
give commit or
rollback
Create procedure sample()
Begin
Declare @a int;Declare @b int
START TRANSACTION
Insert into tbl_login values(‘john’,’john123’);
Set @a = scope_identity();
Insert into tbl_user values(a,’john mathew’,1980-12-13);
Set @b = scope_identity();
If @a>0 && @b>0 THEN
COMMIT TRANSACTION
ELSE begin
ROLLBACK end
End
Will make the
changes
permanently
Create procedure sample()
Begin
begin try
BEGIN TRANSACTION
Insert into tbl_login values(‘john’,’john123’);
Insert into tbl_user values(’john mathew’,1980-12-13);
COMMIT TRANSACTION
end try
begin catch
ROLLBACK TRANSACTION
end catch
End
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;
BEGIN TRY
BEGIN 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’);
COMMIT TRANSACTION
END TRY
BEGIN catch
ROLLBACK
END catch
End
Will it be committed or rolled back? And
reason? when it will be happened
Create Procedure insertData()
Begin
begin try
BEGIN 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’);
COMMIT
END TRANSACTION
end try
BEGIN CATCH
ROLLBACK END catch
EndIF
End
Will it be committed or rolled back? And
reason? when it will be happened
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 Day1

Chapter 5 transactions and dcl statements

  • 1.
  • 2.
  • 3.
    Relevance of Transaction tbl_login pk_user_idusername 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
  • 4.
    Transactions • A transactionis 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.
  • 5.
    Create procedure sample() Begin declare@a int; declare @b int; START TRANSACTION Insert into tbl_login values(‘john’,’john123’); Set @a = scope_identity(); Insert into tbl_user values(a,’john mathew’,1980-12-13); Set @b = scope_identity(); if(@a>0&&@b>0) begin COMMIT TRANSACTION end ELSE begin ROLLBACK TRANSACTION end Will start a new transaction. So any sql operations will get effected until we give commit or rollback
  • 6.
    Create procedure sample() Begin Declare@a int;Declare @b int START TRANSACTION Insert into tbl_login values(‘john’,’john123’); Set @a = scope_identity(); Insert into tbl_user values(a,’john mathew’,1980-12-13); Set @b = scope_identity(); If @a>0 && @b>0 THEN COMMIT TRANSACTION ELSE begin ROLLBACK end End Will make the changes permanently
  • 7.
    Create procedure sample() Begin begintry BEGIN TRANSACTION Insert into tbl_login values(‘john’,’john123’); Insert into tbl_user values(’john mathew’,1980-12-13); COMMIT TRANSACTION end try begin catch ROLLBACK TRANSACTION end catch End Transactions with declare handler
  • 8.
    Questions? “A good questiondeserve a good grade…”
  • 9.
  • 10.
    • Why shouldsomeone use Transaction? – To ensure data redundancy – To reduce network traffic between application server and database server – To ensure data integrity Self Check !!
  • 11.
    • Why shouldsomeone use Transaction? – To ensure data redundancy – To reduce network traffic between application server and database server – To ensure data integrity Self Check !!
  • 12.
    • Why shouldsomeone use Transaction? – To ensure data redundancy – To reduce network traffic between application server and database server – To ensure data integrity Self Check !!
  • 13.
    Create Procedure insertData() Begin DECLAREdone default 0; BEGIN TRY BEGIN 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’); COMMIT TRANSACTION END TRY BEGIN catch ROLLBACK END catch End Will it be committed or rolled back? And reason? when it will be happened
  • 14.
    Create Procedure insertData() Begin begintry BEGIN 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’); COMMIT END TRANSACTION end try BEGIN CATCH ROLLBACK END catch EndIF End Will it be committed or rolled back? And reason? when it will be happened 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
  • 15.