Lab 2 (DBMS)
Lab 2 (DBMS)
Theory:
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:
Use eemc_db;
3) Create a table named employee_info with following columns and data type
Start transaction;
//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
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
To see records
rollback;
To see records
Start transaction
Commit;
Start transaction;
savepoint update_hari;
9) Delete the information of employee whose department is civil
savepoint delete_civil;
//We can see the information is deleted but it is not reflected in database
Rollback to update_hari;
Commit;
12) Create two users named Anish and Rita with following privilege to performing operations on
database
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
14) Try to perform the above operations that is not given privilege to user anish
quit;
15) Now, try to perform the above operations that is given privilege to user sita
16) Try to perform the above operations that is not given privilege to user sita
quit;
Quit;
sita@123;
use eemc_db;