DBMS Lab Manual
DBMS Lab Manual
A MANUAL FOR
II SEMESTER
PREPARED
BY
Mr. Ghouse Pasha
Assistant Professor
Dept. of Computer Science & Engineering
[IoT, Cyber security including Block chain
Technology]R.R.C.E, Bangalore
PREFACE
This manual is intended for the VTU fourth semester Computer Science & Engineering,
Database Management System Lab. I have provided description of the installation of
various components required to carry out the lab. Also, it contains stepwise
information on the usability of the tool Oracle database 11g being used for the
execution of programs. The programs are well tested and simplified providing useful
information as to how to execute them. A set of viva questions has also been included
for the benefit of the students.
WEEK-1
QUERIES USING DDL AND DML
SOLUTION:
SYNTAX:
create user ‘username’@localhost identified by ‘password’;
EXAMPLE:
create user ‘yamuna’@localhost identified by ‘rrce’;
flush privileges;
Query OK, 0 rows affected (0.07 sec)
--Drop the user
drop user 'yamuna'@localhost;
Query OK, 0 rows affected (0.01 sec)
+------------+-------------+------+-----+---------+-------+
+------------+-------------+------+-----+---------+-------+
+------------+-------------+------+-----+---------+-------+
b) Insert the any three records in the employee table and use rollback. Check the
result.
mysql> rollback;
Query OK, 0 rows affected (0.07 sec)
d) Insert null values to the employee table and verify the result.
mysql> insert into employee
values(5,'Dinesh','assistant professor',11,60000,NULL);
+-------+-------------+---------------------+------------+-------+----
--------+
+-------+-------------+---------------------+------------+-------+----
--------+
Dept of CSE [IC], RRCE Page 4
Database Management System BCS403
+-------+-------------+---------------------+------------+-------+----
--------+
WEEK-2
CREATION OF TABLES
Name Type
Empno Int
Ename Varchar2(10)
Job Varchar2(10)
Mgr Int
Sal Int
a. AddacolumncommissionwithdomaintotheEmployeetable.
b. Insert any five records into the table.
c. Update the column details of job
d. Rename the column of Employ table using alter command.
e. Delete the employee whose Empno is 105.
SOLUTION:
Table created.
+-------+-------------+------+-----+---------+-------+
+-------+-------------+------+-----+---------+-------+
+-------+-------------+------+-----+---------+-------+
+------------+-------------+------+-----+---------+-------+
+------------+-------------+------+-----+---------+-------+
+------------+-------------+------+-----+---------+-------+
1 row updated.
Table altered.
SQL>desc employee1;
+------------+-------------+------+-----+---------+-------+
+------------+-------------+------+-----+---------+-------+
+------------+-------------+------+-----+---------+-------+
WEEK-3
OUTPUT: MIN(AGE)
30
OUTPUT: AVG(AGE)
36.250
View created.
--Display views
SQL>select * from A;
+------+
| age |
+------+
| 34 |
| 34 |
| 34 |
| 34 |
| 35 |
| 35 |
| 30 |
+------+
7 rows in set (0.00 sec)
+-------------+-------+
| ename | sal |
+-------------+-------+
| smitha | NULL |
| smitha | NULL |
| sinchana | NULL |
| asha | 40000 |
| yamuna | 50000 |
| yamunashree | 50000 |
| yamunashree | 50000 |
| shree | 70000 |
+-------------+-------+
8 rows in set (0.00 sec)
+-------+
| sal |
+-------+
| 50000 |
| 70000 |
| 40000 |
| NULL |
+-------+
4 rows in set (0.00 sec)
WEEK-4 :TRIGGERS
1. Create a row level trigger for the customers table that would fire for
INSERT or UPDATE or DELETE operations performed on the
CUSTOMERS table. This trigger will display the salary difference between
the old values and new values:
CUSTOMERS table:
old_salary int,
new_salary int,
salary_difference int );
mysql> DELIMITER //
begin
end;
//
-> //
-> //
+------------+------------+-------------------+
+------------+------------+-------------------+
+------------+------------+-------------------+
+------+-------------+------+------------+--------+
+------+-------------+------+------------+--------+
+------+-------------+------+------------+--------+
Triggercreated.
Here following two points are important and should be noted carefully:
OLD and NEW references are not available for table level triggers, rather you can use
them for record level triggers.
If you want to query the table in the same trigger, then you should use the AFTER
keyword, because triggers can query the table or change it again only after the initial
changes are applied and the table is back in a consistent state.
Above trigger has been written in such a way that it will fire before any DELETE or
INSERT or UPDATE operation on the table, but you can write your trigger on a single or
multiple operations, for example BEFORE DELETE, which will fire whenever a record
will be deleted using DELETE operation on the table.
Let us perform some DML operations on the CUSTOMERS table. Here is one INSERT
statement, which will create a new record in the table:
When a record is created in CUSTOMERS table, above create trigger display_salary_changes will be
fired and it will display the following result:
Oldsalary:15000
Newsalary:25000
Salarydifference:10000
WEEK- 5: CURSORS
DEFINITION OF A CURSOR
5 . Create cursor for Employee table & extract the values from the table. Declare the variables
,Open the cursor & extrct the values from the cursor. Close the cursor.
Employee(E_id, E_name, Age, Salary)
CURSOR EXAMPLE:
Database changed
+-------+-------------+---------------------+------------+--------+------------+
+-------+-------------+---------------------+------------+--------+------------+
+-------+-------------+---------------------+------------+--------+------------+
mysql> delimiter $$
begin
open stud_cursor;
get_list:loop
select s_name;
if is_done=1 then
leave get_list;
end if;
close stud_cursor;
end $$
-> $$
+--------+
| s_name |
+--------+
| sowmya |
+--------+
+-------------+
| s_name |
+-------------+
| yamunashree |
+-------------+
+--------+
| s_name |
+--------+
| shree |
+--------+
+--------+
| s_name |
+--------+
| Dinesh |
+--------+
+--------+
| s_name |
+--------+
| shree |
+--------+
+--------+
| s_name |
+--------+
| sowmya |
+--------+
+--------+
| s_name |
+--------+
| shree |
+--------+
6 . Write a PL/SQL code using parameterized Cursor, that will merge the data available
in the newly created table N_RollCall with the data available in table O_RollCall. If the
data in the first table already exist in the second table then that data should be skipped.
BEGIN
FOR n_rec IN n_rollcall_cursor LOOP
-- Check if the record exists in O_RollCall
SELECT id
INTO v_id
FROM O_RollCall
WHERE id = n_rec.id;
COMMIT;
END merge_roll_call_data;
(or)
merge into old c1 using new c2
on (c1.id=c2.id)
when matched then update set
c1.name=c2.name
when not matched then
insert values(c2.id,c2.name);
7. Install an Open Source NoSQL Data base MangoDB & perform basic CRUD(Create,
Read,Update & Delete) operations. Execute MangoDB basic Queries using CRUD operations.
MongoDB is a NoSQL database, and it doesn't require "installation" in the traditional sense as it is often
run as a service. However, you can set it up on your local machine or use a cloud-based service. Below are
the basic steps to get started with MongoDB and perform CRUD operations:
1. Install MongoDB: You can download and install MongoDB from the official website
(https://www.mongodb.com/try/download/community). Follow the installation instructions provided for
your operating system.
2. Start MongoDB Server: After installation, start the MongoDB server. If you've installed it locally, you
might run it as a service or manually start it using commands like mongod on the terminal.
3. Access MongoDB Shell: MongoDB provides a shell interface (mongo) that allows you to interact with the
database. Open a new terminal or command prompt window and run the mongo command to start the
MongoDB shell.
4. Perform CRUD Operations:
Create (Insert): To insert documents into a collection, you use the insertOne() or
insertMany() methods.
Example: db.collectionName.insertOne({ field1: value1, field2: value2, ... });
Read (Query): To query documents from a collection, you use the find() method.
/* q Example: db.collectionName.find({ /* query criteria */ });