Está en la página 1de 4

--------------------------------------------------------------------------------

--------
Transactions Control Language(Tcl)
--------------------------------------------------------------------------------
--------
Define:
Transaction is nothing but a unit of work. We can control these transact
ions using the
following statements.

1.COMMIT Statement
2.ROLLBACK Statement
3.SAVE TRAN [SACTIONS]
1.COMMIT Statement:
This statement makes a transaction permanent. It is not possible to roll
back the committed
The COMMIT statement deletes the data from emp permanently. It is not po
ssible to ROLLBACK
the delete operation.
Example:
create table emp(sno int,ename varchar(20),address varchar(20),sal int)
insert into emp values(101,'Kannan','Erode',4000)
insert into emp values(102,'Velu','bhabani',6000)
insert into emp values(103,'Bala','Gobi',8000)
insert into emp values(104,'Kavi','Chennai',7800)
insert into emp values(105,'Sri','Trichi',4500)
select * from emp
delete * from emp
select * from emp
commit
select * from emp
or
begin tran
insert into emp values(106,'Siva','Erode',7000)
GO
update emp set sal=sal+500 where eno=101
GO
delete from emp where eno=105
Select the entire transaction and press F5 for one time
commit tran
The above COMMIT TRAN makes all three transactions permanent. We
cannot ROLLBACK
the transactions.
2.ROLLBACK Statement:
This statement is used to cancel a particular performed transaction. To
perform this statement
in Sql Server we have to follow any one of the below 2 approaches.
Approach 1: SET IMPLICIT_TRANSACTIONS ON:
This approach is only to cancel a single recently performed oper
ation.
Example:
set implicit_transactions ON
delete from emp
delete from emp
select from emp
rollback
select * from emp
select * from emp

Approach 2: Explicit Transactions: To approach is to cancel recently per


formed multiple
operations.
Syntax:
BEGIN TRAN
----------
GO
---------
GO
--------
ROLLBACK TRAN
GO is query separator
Example:
begin tran
insert into emp values(106,'Siva','Erode',7000)
GO
update emp set sal=sal+500 where eno=101
GO
delete from emp where eno=105

Select the entire transaction and press F5 for one time


rollback tran
The ROLLBACK TRAN statement cancels INSERT on Dept, UPDATE on EM
P and DELETE on
student tables.
3.SAVE TRANS Statement:
This statement is used to COMMIT/ROLLBACK a particular performed transac
tion from the set
of transactions. It is associated with alphabets in order to save the tr
ansaction.
Example:
begin tran
save tran a
insert into emp values(107,'Hari','Bhavani',6700)
save tran b
update dept set sal=sal+500 where sno=101
save tran c
delete from stock where sno=105
select * from emp
rollback tran c
(The delete operation will be cancelled)
commit trans b
(The update operation performed permanently we cannot rollback)
--------------------------------------------------------------------------------
--------
Create a User
--------------------------------------------------------------------------------
--------
--------------------------------------------------------------------------------
--------
Data Control Language
--------------------------------------------------------------------------------
---------
It is the 3rd sub language available in T-SQL.It is used to control the
data between
different user accounts. It includes the following statements.
1. GRANT Statement
Grant Statement: This statement is used to grant the permissions (INSERT,SELECT,
UPDATE,DELETE) on a specific table to different user accounts.
Syntax:
GRANT {ALL/SPECIFIC PERMISSIONS} ON TABLENAME TO USER ACCOUNT (S)[WITH G
RANT OPTION]
WITH GRANT OPTION: When any user got the permission on a specific table
from other user
with this option, then that user can grant the permission on that same t
able to another
user account. At that time sub user acts as owner.
Example:
GRANT ALL ON EMP TO BABBU WITH GRANT OPTION
From the above statement BABBU user account got all permission on EMP ta
ble from SA user
account.Mean time BABBU can give the permission on EMP to another user a
ccount because he
got the permission WITH GRANT OPTION.
Example: GRANT INSERT,SELECT ON EMP TO JOHNSON
Now JOHNSON can perform select and insert operations on EMP table. But J
OHNSON cannot
perform update and delete operations on EMP table because he does not ha
ve the corresponding
permissions.
2. REVOKE Statement
REVOKE Statement: This statement is used to revoke the permission (INSER
T,SELECT,UPDATE,
DELETE) on a specific table from different user accounts.
Syntax:
REVOKE {ALL/SPECIFIC PERMISSIONS} ON TABLENAME FROM USER ACCOUNT (S) [CA
SCADE]
CASCADE: Using this option we can destroy the communication link between
user account more over from the main user it self we can revoke the permission
from all sub users.
Example:
REVOKE ALL ON EMP FROM JOHNSON CASCADE
The above statement revokes the permissions on EMP table from BABBU and
JOHNSON. Now BABBU
and JOHNSON users cannot access EMP Table.

También podría gustarte