Introduction to SQL Server
Transactions and DCL statements
Transactions
Relevance of Transactiontbl_login
pk_user_id username password1 John Admin123
2 alex Alex123
tbl_userpk_user_id fk_user_id user_name user_dob
1 2 Alex 1995-12-122 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 TRANSACTIONInsert into tbl_login values(‘john’,’john123’);Insert into tbl_user values(’john mathew’,1980-12-13);
COMMIT TRANSACTIONend try
begin catch
ROLLBACK TRANSACTIONend 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 TRYBEGIN 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 TRANSACTIONEND TRYBEGIN catchROLLBACK END catchEnd
Will it be committed or rolled back? And reason? when it will be happened
Create Procedure insertData()Begin
begin tryBEGIN 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’);
COMMITEND TRANSACTIONend tryBEGIN CATCHROLLBACK END catch
EndIFEnd
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
Top Related