BCS403 -DBMS Lab Manual_updated
BCS403 -DBMS Lab Manual_updated
Lab Manual
Database Management System Laboratory
Semester: IV
Teaching Hours/Week - 2
CIE Marks 50
SEE Marks 50
Credits 04
Pedagogy: For the above experiments the following pedagogy can be considered. Problem-based
learning, Active learning, MOOC, Chalk & Talk
PART A – List of problems for which students should develop programs and execute in the
Laboratory.
Course outcomes (Course Skill Set):
At the end of the course, the student will be able to:
CO 1. Describe the basic elements of a relational database management system
CO 2. Design entity relationship for the given scenario.
CO 3. Apply various Structured Query Language (SQL) statements for database manipulation.
CO 4. Analyse various normalization forms for the given application.
CO 5. Develop database applications for the given real world problem
CO 6. Understand the concepts related to NoSQL databases.
List of Problems/Experiments
Experiments
List of problems for which students should develop the program and execute it in the laboratory
2 Create a table called Employee that contain attributes EMPNO, ENAME, JOB,
MGR,SAL & execute the following.
1. Add a column commission with domain to the Employee table.
2. Insert any five records into the table.
3. Update the column details of job
4. Rename the column of Employ table using alter command.
5. Delete the employee whose Emp no is 105.
4 Create a row level trigger for the customers table that would fire for INSERT or
UPDATE
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)
6 Write a PL/SQL block of code using parameterized Cursor, that will merge the
data Available in the newly created table N_RollCall with the data available in the table
O_RollCall. If the data in the first table already exist in the second table then that
data should be skipped.
7 Install an Open Source NoSQL Data base MangoDB & perform basic
CRUD(Create, Read, Update & Delete) operations.
Execute MangoDB basic Queries using CRUD operations.
Creation of Table:
Working of Rollback;
alter table employee modify column empno int not null;/alter table employee modify
empno int not null;
Experiment 2: Create a table called Employee that contain attributes EMPNO, ENAME, JOB,
MGR,SAL & alter table employee modify column empno int not null;execute the following.
1. Add a column commission with domain to the Employee table.
2. Insert any five records into the table.
3. Update the column details of job
4. Rename the column of the Employee table using alter command.
5. Delete the employee whose Emp no is 105.
Code:
Creation of Table: create table Emp(empno int, name varchar(30),job varchar(30),mgr
int, sal int);
2. insert into Emp values (101, 'John Doe', 'Manager', 100, 5000, 1000.28),(102,
'Jane Smith', 'Developer', 101, 4000, 500.10),(103, 'Alice Johnson', 'Analyst', 102,
3500, 120.12),(104, 'Bob Brown', 'Assistant', 103, 3000, 200.05),(105, 'Emily
Davis', 'Clerk', 102, 2500, 300.07);
Experiment 3: Queries using aggregate functions (COUNT, AVG, MIN, MAX,SUM),Group by,
Orderby.
Employee(E_id, E_name, Age, Salary)
1. Create Employee table containing all Records E_id, E_name, Age, Salary.
2. Count number of employee names from employee table
3. Find the Maximum age from employee table.
4. Find the Minimum age from employee table.
5. Find salaries of employee in Ascending Order.
6. Find grouped salaries of employees.
Code:
1. CREATE TABLE Employee1( E_id INT,E_name VARCHAR(50),Age INT, Salary int);
INSERT INTO Employee1 VALUES (1, 'John Doe', 30, 50000),(2, 'Jane Smith', 25,
45000),(3, 'Alice Johnson', 35, 60000),(4, 'Bob Brown', 28, 48000),(5, 'Emily Davis', 32,
55000);
Experiment 4: 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 & new Salary.
CUSTOMERS(ID , NAME , AGE , ADDRESS, SALARY)
Code:
Creation of Table:
CREATE TABLE customers (ID INT,NAME VARCHAR(50),AGE INT,ADDRESS
VARCHAR(100),SALARY INT);
DELIMITER ;
Experiment 5: Create cursor for Employee table & extract the values from the table. Declare the
variables. Open the cursor & extract the values from the cursor. Close the cursor.
CALL fetch_employee_data();
Experiment 6: Write a PL/SQL block of code using parameterized Cursor, that will merge the
data Available in the newly created table N_RollCall with the data available in the table
O_RollCall. If the data in the first table already exist in the second table then that
data should be skipped.
Code:
CREATE TABLE O_RollCall (id INT,name VARCHAR(100));
INSERT INTO O_RollCall (id, name) VALUES(1, 'John'),(2, 'Alice'),(3, 'Bob');
CREATE TABLE N_RollCall (id INT,name VARCHAR(100));
PL/SQL Code:
DELIMITER //
OPEN o_cursor;
read_loop: LOOP
FETCH o_cursor INTO o_id, o_name;
IF done THEN
LEAVE read_loop;
END IF;
CLOSE o_cursor;
END //
DELIMITER ;
CALL MergeRollCallData();
Experiment 7:.Install an Open Source NoSQL Data base MangoDB & perform basic
CRUD(Create, Read, Update & Delete) operations.
Execute MangoDB basic Queries using CRUD operations.
Code:
How to Connect Mongo database:
Mongosh
Basic Operations:
Creating Database:
use databasename;
Show dbs;
db.createCollection(“cmrit”);
Create Operation:
Read Operation:
db.Students.find();
Update Operation:
db.Students.updateOne({name:'John'},{$set: {age:40}});
db.Students.updateMany(
{ name: "John" },
{ $set: { age: 35 } }
);
The above command will update the "age" field to 35 for all documents in the
"users" collection where the "name" field is "John"
db.Students.updateMany(
Deleting Operation:
db.Students.deleteOne({ name: "Alice" });
To delete Collection:
db.cmrit.drop();
To delete database:
Program 1
About the Program/Problem Statement
Keywords/Key Concepts
Group by clause, Having clause, Aggregate Functions (Count, Avg, Max), Nested Query (Single
and Multiple row Subquery concepts ), Union
Questions
1. Count the customers with grades above Bangalore’s average.
2. Find the name and numbers of all salesman who had more than one
customer.
3. List all the salesman and indicate those who have and don’t have
customers in their cities (UseUNION operation.)
Program 2
About the Program/Problem Statement
In this program we are creating order database with 3 entities as “Salesman”, “Customer”,
“Orders”. The objective of this program is to design a view that finds salesman who has the
Keywords/Key Concepts
Questions
1. Create a view that finds the salesman who has the customer with the
highest order of a day.
---------------------------------------------------------------------------------------------------------------------
ON DELETE CASCADE: SQL Server deletes the rows in the child table that is corresponding to the
row deleted from the parent table.
ON DELETE SET NULL: SQL Server sets the rows in the child table to NULL if the corresponding
rows in the parent table are deleted.
--for Customer table foreign key-> Salesman_id, need to mention on delete set null.
--for Orders table foreign key-> Salesman_id, and Customer_id need to mention on delete
cascade.
In the 5th query question, it is asked to delete the Salesman_id 1000 from Salesman table and also
to ensure that all the orders of Salesman_id 1000 is also deleted. So, we have to mention on delete
cascade for Orders table for both the foreign keys.
In the 5th query question it is not asked to delete anything from the customer table. So, just to
ensure that referential integrity constraint (foreign key reference) is preserved in customer table,
on delete set null is used.
Table Creation
• Create table SALESMAN (Salesman_id int primary key, Name varchar (40), City varchar
(40), Commission varchar (40));
• Create table CUSTOMER (Customer_id int primary key, Cust_Name varchar (40), City
varchar (40), Grade int, Salesman_id int, Salesman_id references
SALESMAN(Salesman_id) on delete set null);
• Create table ORDERS (Ord_No int primary key, Purchase_Amt float, Ord_Date date,
Customer_id int, Salesman_id int, Customer_id references CUSTOMER(Customer_id) on
delete cascade, Salesman_id references SALESMAN(Salesman_id) on delete cascade);
Always while inserting the values in the tables, refer the query questions and check whether any
hint for values to be inserted is given or not.
1. The 1st query question is “Count the customers with grades above Bangalore’s average”.
So, to calculate the average grade, city value should be inserted as “Bangalore” for more
than one row in the customer table.
2. The 2nd query question is “Find the name and numbers of all salesman who had more than
one customer”. So, same Salesman_id should be inserted for more than one row in
Program 1 Solution
2. Find the name and numbers of all salesman who had more than one
customer.
Method 1
SELECT
SALESMAN_ID,
NAMEFROM
SALESMAN
WHERE SALESMAN_ID IN (SELECT SALESMAN_ID
FROM
CUSTOMER
C GROUP
BY
SALESMAN
_ID
HAVING
COUNT(*)>1
);
Method 2
3. List all the salesman and indicate those who have and don’t have
customers in their cities (UseUNION operation.)
Program 2 Solution
1. Create a view that finds the salesman who has the customer with the
highest order of a day.
SELECT * FROM
SALESMAN;
SELECT * FROM
ORDERS;
SELECT * FROM
CUSTOMER;