0% found this document useful (0 votes)
24 views

Lab 2 (DBMS)

Uploaded by

Sandesh Chhettri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views

Lab 2 (DBMS)

Uploaded by

Sandesh Chhettri
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Lab 2

DCL AND TCL statements in SQL


Objective:

 To be familiar with DCL and TCL statements in SQL

Theory:

DCL (Data Control Languages)


 DCL includes commands such as GRANT and REVOKE which mainly deals with the rights,
permissions and other controls of the database system.
 It used to give and withdraw specific privileges (as defined by query) to the user in a
multi-user database.
 By setting up the permission, user can prevent unauthorized access to the database.
DCL commands are:

 GRANT
 REVOKE
GRANT:
 This is a SQL command which is used to provide privileges/permissions to modify and
retrieve database objects like tables, views, indexes etc.
 It can be used to grant SELECT, INSERT, UPDATE, DELETE etc. privileges to a user.
Syntax:
GRANT <privilege list> on <relation or view> to <user>;
Example: GRANT INSERT, SELECT,UPDATE on student_info to ram;

REVOKE:
 It revokes the given access to the user.
syntax:
REVOKE<privilege list> on <relation or view> from <user>;
Example: REVOKE UPDATE on student_info from ram;
TCL (Transaction Control Language)
 Transaction Control Language (TCL) is a set of special commands that deal with the
transactions within the database.
 Basically, they are used to manage transactions within the database.
 TCL commands are also used for maintaining the consistency of the database.
 These commands are generally used along with the DML commands such as INSERT,
UPDATE and DELETE.
 The changes made by DML commands are either committed or rolled back by TCL
commands.
 There is another TCL command that can place a save point in the transactions which
makes it possible to rollback all the transaction till the last save point.
COMMIT:
Commit command make the changes made to the database permanent.
Syntax:
COMMIT;
Here's the syntax demonstrating the use of the COMMIT command with a transaction in
MySQL:
START TRANSACTION;
{a set of SQL statements};
COMMIT;
The parameters used in the syntax are:
 START TRANSACTION: It is used for marking the beginning of changes or operations in a
transaction.
 {a set of SQL statements}: It is used for mentioning the task that is supposed to be
completed.
 COMMIT: It is used to save transactional changes made by SQL statements.
Example:
BEGIN TRANSACTION;
DELETE FROM student_info
WHERE sid = 11;
COMMIT ;
ROLLBACK:
 Rollback command is used to undo the changes that have been made to the database
temporarily.
 The important point to note here is that the changes saved using COMMIT command
cannot be undone using ROLLBACK command.
Example:
UPDATE student_info SET location=’Dharan’ WHERE name=’ram’;
ROLLBACK;
SAVEPOINT:
It’s used to roll back a transaction to a specific point rather than the complete transaction.
Syntax:
SAVEPOINT SavepointName;
 Among all transactions, this command is exclusively used to create SAVEPOINT.
 ROLLBACK is a command that is used to undo a set of transactions.
The syntax for rollback to savepoint command:
ROLLBACK TO SavepointName;
Example:
UPDATE student_info
SET program = ‘BBA’
WHERE sid = 5;
Savepoint A;

UPDATE student_info
SET name = ‘ram’
WHERE location = ‘pokhara;
SAVEPOINT B;

Now if we want to roll back the certain DML commands, we can do so by using Rollback like
this:
This will rollback the transaction till save point A:
Rollback to A;
Solution:

 Open MySQL and Apache xampp control panel


 Open the command prompt
 Change directory to xampp\mysql\bin
 Now, xampp\mysql\bin>mysql –u root –p –h localhost and it will ask for password
 Press Enter
 Now, you can perform below operations

1) Create a database named eemc _db

create database eemc_db;

//database named eemc_db will be created

2) select database named eemc_db

Use eemc_db;

3) Create a table named employee_info with following columns and data type

Eid Name address department


Int varchar(30) varchar(30) varchar(30)

create table employee_info(eid int,name varchar(30),address varchar(30),department varchar(30));

3) Now insert minimum 5 records into table named employee_info

Start transaction;

insert into employee_info values(1,'anish', 'kathmandu','civil');

insert into employee_info values(2,'Roshan', 'pokhara','computer');

insert into employee_info values(3,'rojina','kathmandu','computer');

insert into employee_info values (4,'ramesh','bhaktapur','it');

insert into employee_info values(5,'hari','pokhara','it');

//you can see that no any changes is reflected in database while opening localhost
phpmyadmin
//But changes is made locally, you can see this by using following query

select * from employee_info;

Now commit the transaction

commit ;

Now, you can see changes is reflected in database while opening localhost phpmyadmin
4) Now update the department to civil whose location is kathmandu

start transaction

update employee_info set department='civil' where address='kathmandu';

To see records

select * from employee_info;

Note: Update is not reflected in database

5) Now revert the operation of step(4)

rollback;

//Rollback operation will cancelled the above operation

To see records

select * from employee_info;

//We can see the previous record that is not committed

6) Now again update the department to civil whose location is kathmandu

Start transaction

update employee_info set department='civil' where address='kathmandu';

7) Commit the transaction

Commit;

//we can see that the above updation is reflected in database

8)Update the address of employee to kathmandu whose name is ‘hari’

Start transaction;

update employee_info set address='kathmandu' where name='hari';

savepoint update_hari;
9) Delete the information of employee whose department is civil

delete from employee_info where department='civil';

savepoint delete_civil;

select *from employee_info;

//We can see the information is deleted but it is not reflected in database

10) Rollback the transaction to step(8)

Rollback to update_hari;

11) Commit the transaction

Commit;

12) Create two users named Anish and Rita with following privilege to performing operations on
database

Anish: SELECT, UPDATE, INSERT

Sita: SELECT,INSERT, DELETE

create user anish identified by 'anish@123';

create user sita identified by 'sita@123';

grant select ,update, insert on employee_info to anish;

grant select,insert,delete on employee_info to sita;

quit;

mysql -u anish –p

anish@123

use eemc_db;
13) Now, try to perform the above operations that is given privilege to user anish

insert into employee_info values(6,'pradip','palpa','computer');

14) Try to perform the above operations that is not given privilege to user anish

delete from employee_info where address='kathmandu';

quit;

15) Now, try to perform the above operations that is given privilege to user sita

delete from employee_info where address='kathmandu';

16) Try to perform the above operations that is not given privilege to user sita

update employee_info set deparment='civil' where address='palpa';

this operation cannot be done

17) Revoke delete operation that is given to sita.

quit;

mysql -u root –p;

revoke delete on employee_info from sita;

18)Now try to perform delete operation by sita

Quit;

Mysql –u sita -p;

sita@123;

use eemc_db;

We can see that operation is not allowed.

You might also like