JSS MAHAVIDYAPEETHA
JSS ACADEMY OF TECHNICAL EDUCATION, BENGALURU-60
DEPARTMENT OF INFORMATION SCIENCE AND
ENGINEERING
JSSATE Campus, Dr. Vishnuvardhana Main Road, Bengaluru – 560060
IV SEMESTER
DATABASE MANAGEMENT SYSTEM LABORATORY LAB
MANUAL
[BCS403]
Compiled By:
Mr. Chetan R Mrs. Sahana V
Assistant Professor, Assistant Professor,
Dept of Information Science & Dept of Information Science &
Engineering, JSSATEB Engineering, JSSATEB
Mrs. Gayathri Kamath
Assistant Professor,
Dept of Information Science & Engineering, JSSATEB
Signature of Faculty Signature of HOD
JSS MAHAVIDYAPEETHA
JSS ACADEMY OF TECHNICAL EDUCATION, BENGALURU-60
DEPARTMENT OF INFORMATION SCIENCE AND
ENGINEERING
VISION
To emerge as a centre for achieving academic excellence, by producing competent professionals
to meet the global challenges in the field of Information science and Technology.
MISSION
M1: To prepare the students as competent professionals to meet the advancements in the
industry and academia by imparting quality technical education.
M2: To enrich the technical ability of students to face the world with confidence, commitment,
and teamwork
.
M3: To inculcate and practice strong techno-ethical values to serve the society.
Program Educational Objectives (PEOs):
PEO1: To demonstrate analytical and technical problem-solving abilities.
PEO2: To be conversant in the developments of Information Science and Engineering, leading
towards the employability and higher studies.
PEO3: To engage in research and development leading to new innovations and products.
Program Specific Outcomes (PSOs):
PSO1: Apply the mathematical concepts for solving engineering problems by using appropriate
programming constructs
.
PSO2: Adaptability to software development methodologies.
PSO3: Demonstrate the knowledge towards the domain specific initiatives of Information
Science and Engineering.
Program Outcomes (POs):
Information Science and Engineering Graduates will be able to:
PO1 Apply the knowledge of mathematics, science, engineering fundamentals, and an
Engineering specialization to the solution of complex engineering problems.
PO2 Identify, formulate, review research literature, and analyze complex engineering
problems reaching substantiated conclusions using first principles of mathematics,
natural sciences, and engineering sciences.
PO3 Design solutions for complex engineering problems and design system components or
processes that meet the specified needs with appropriate consideration for the
public health and safety, and the cultural, societal, and environmental
considerations.
PO4 Use research-based knowledge and research methods including design of experiments,
analysis and interpretation of data, and synthesis of the information to provide valid
conclusions.
PO5 Create, select, and apply appropriate techniques, resources, and modern engineering and
IT tools including prediction and modeling to complex engineering activities with an
understanding of the limitations.
PO6 Apply reasoning informed by the contextual knowledge to assess societal, health, safety,
legal and cultural issues, and the consequent responsibilities relevant to the
professional engineering practice.
PO7 Understand the impact of the professional engineering solutions in societal and
environmental contexts, and demonstrate the knowledge of, and need for sustainable
development.
PO8 Apply ethical principles and commit to professional ethics and responsibilities and
norms of the engineering practice.
PO9 Function effectively as an individual, and as a member or leader in diverse teams, and
in multidisciplinary settings.
PO10 Communicate effectively on complex engineering activities with the engineering
community and with society at large, such as, being able to comprehend and write
effective reports and design documentation, make effective presentations, and give and
receive clear instructions.
PO11 Demonstrate knowledge and understanding of the engineering and management
principles and apply these to one’s own work, as a member and leader in a team, to
manage projects and in multidisciplinary environments.
PO12 Recognize the need for and have the preparation and ability to engage in independent
and life-long learning in the broadest context of technological change.
Lesson plan
Course Name: Database Management System Course Code: BCS403 Sem/Sec: 4
Contact Hrs: 2 Total Hrs.: 20
Hours/Week
SEE Marks: NA CIE Marks:25 Exam Duration: NA
Course Outcomes:
At the end of the course, students will be able to
CO# CO Statement BLL
Identify, analyze and define database objects, enforce integrity constraints on a
CO1 database using RDBMS concepts L3
Design Entity Relationship Diagrams and Algebraic expressions for given relational
CO2 model. L3
Apply the normalization procedure on relational database schema and Develop
CO3 applications to interact with databases using SQL. L3
CO4 Apply the concepts of Transaction processing, L3
CO5 Apply the concepts of concurrency control and CRUD operations in NOSQL. L3
Pre-requisites: Variables, Data Structures
Course overview: Provide a strong foundation in database concepts, technology, and practice.
Practice SQL programming through a variety of database problems. Demonstrate the use of
concurrency and transactions in database. Design and build database applications for real world
problems.
Pedagogical/Innovative methods planned: Demonstration of simple projects on database
connectivity, Group assignment on Database Design
Clas Reference
Topics to be covered
s No. material and CO
Create a table called Employee & execute the following. Employee
(EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION) 1.
Create a user and grant all permissions to the user. 2. Insert the any three
records in the employee table contains attributes EMPNO, ENAME JOB, T1 CO1, CO2,
1 MANAGER_NO, SAL, COMMISSION and use rollback. Check the CO3, CO4
result. 3. Add primary key constraint and not null constraint to the
employee table. 4. Insert null values to the employee table and verify the
result.
Create a table called Employee that contain attributes EMPNO, ENAME, T1 CO1, CO2,
JOB, MGR,SAL & execute the following. 1. Add a column commission CO3
with domain to the Employee table. 2. Insert any five records into the
2 table. 3. Update the column details of job 4. Rename the column of
Employ table using alter command. 5. Delete the employee whose Empno
is 105
Queries using aggregate functions (COUNT, AVG, MIN, MAX, SUM), T1 CO1, CO2,
3 Group by, Order by. Employee (E_id, E_name, Age, Salary) 1. Create CO3
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.
Create a row level trigger for the customers table that would fire for T1 CO1, CO2,
INSERT or UPDATE or DELETE operations performed on the CO3
4 CUSTOMERS table. This trigger will display the salary difference
between the old & new Salary. CUSTOMERS (ID, NAME, AGE,
ADDRESS, SALARY)
Create cursor for Employee table & extract the values from the table. T1 CO1, CO2,
5 Declare the variables, Open the cursor & extrct the values from the CO3
cursor. Close the cursor. Employee(E_id, E_name, Age, Salary)
Write a PL/SQL block of code using parameterized Cursor, that will merge T1 CO1, CO2,
the data available in the newly created table N_RollCall with the data CO3
6 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.
Install an Open Source NoSQL Data base MangoDB & perform basic T1 CO5
7 CRUD (Create, Read, Update & Delete) operations. Execute MangoDB
basic Queries using CRUD operations
Total No. of Hours required as per VTU:40
Total No. of classes planned: 40
Total No of labs Planned :08
TEXT BOOKS (TB):
● Fundamentals of Database Systems, Ramez Elmasri and Shamkant B. Navathe, 7th Edition,
2017, Pearson.
● Database management systems, Ramakrishnan, and Gehrke, 3rd Edition, 2014, McGraw
Hill
REFERENCE BOOKS (RB):
● Abraham Silberschatz, Henry F. Korth and S. Sudarshan’s Database System Concepts 6th
EditionTata Mcgraw Hill Education Private Limited
Scheme of Continuous Internal Evaluation (CIE):
CIE for the theory component of the IPCC (maximum marks 50)
● IPCC means practical portion integrated with the theory of the course.
● CIE marks for the theory component are 25 marks and that for the practical component is 25
marks.
● 25 marks for the theory component are split into 15 marks for two Internal Assessment Tests
(Two Tests, each of 15 Marks with 01-hour duration, are to be conducted) and 10 marks for
other assessment methods mentioned in 22OB4.2. The first test at the end of 40-50% coverage
of the syllabus and the second test after covering 85-90% of the syllabus.
● Scaled-down marks of the sum of two tests and other assessment methods will be CIE marks
for the theory component of IPCC (that is for 25 marks).
● The student has to secure 40% of 25 marks to qualify in the CIE of the theory component of
IPCC. CIE for the practical component of the IPCC
● 15 marks for the conduction of the experiment and preparation of laboratory record, and 10
marks for the test to be conducted after the completion of all the laboratory sessions.
● On completion of every experiment/program in the laboratory, the students shall be evaluated
including viva-voce and marks shall be awarded on the same day.
● The CIE marks awarded in the case of the Practical component shall be based on the
continuous evaluation of the laboratory report. Each experiment report can be evaluated for 10
marks. Marks of all experiments’ write-ups are added and scaled down to 15 marks.
● The laboratory test (duration 02/03 hours) after completion of all the experiments shall be
conducted for 50 marks and scaled down to 10 marks.
● Scaled-down marks of write-up evaluations and tests added will be CIE marks for the
laboratory component of IPCC for 25 marks.
● The student has to secure 40% of 25 marks to qualify in the CIE of the practical component
of the IPCC..
Scheme of Semester End Examination:
SEE for IPCC Theory SEE will be conducted by University as per the scheduled timetable, with
common question papers for the course (duration 03 hours)
1. The question paper will have ten questions. Each question is set for 20 marks.
2. There will be 2 questions from each module. Each of the two questions under a module (with a
maximum of 3 subquestions), should have a mix of topics under that module.
3. The students have to answer 5 full questions, selecting one full question from each module.
4. Marks scored by the student shall be proportionally scaled down to 50 Marks
Table of Contents
Sl. Content Page No
No.
Create a table called Employee & execute the following. Employee
(EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION) 1.
Create a user and grant all permissions to the user. 2. Insert the any three
records in the employee table contains attributes EMPNO, ENAME JOB,
1 8
MANAGER_NO, SAL, COMMISSION and use rollback. Check the
result. 3. Add primary key constraint and not null constraint to the
employee table. 4. Insert null values to the employee table and verify the
result.
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
2 11
table. 3. Update the column details of job 4. Rename the column of
Employ table using alter command. 5. Delete the employee whose
Empno is 105
Queries using aggregate functions (COUNT, AVG, MIN, MAX, SUM),
Group by, Order by. Employee (E_id, E_name, Age, Salary) 1. Create
Employee table containing all Records E_id, E_name, Age, Salary. 2.
3 Count number of employee names from employee table 3. Find the 14
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.
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
4 17
trigger will display the salary difference between the old & new Salary.
CUSTOMERS (ID, NAME, AGE, ADDRESS, SALARY)
Create cursor for Employee table & extract the values from the table.
5 Declare the variables, Open the cursor & extrct the values from the 19
cursor. Close the cursor. Employee(E_id, E_name, Age, Salary)
Write a PL/SQL block of code using parameterized Cursor, that will
6 merge the data available in the newly created table N_RollCall with the
21
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.
Install an Open Source NoSQL Data base MangoDB & perform basic CRUD
7 (Create, Read, Update & Delete) operations. Execute MangoDB basic Queries 23
using CRUD operations
DBMS Lab Manual BCS403
Program 1
Create a table called Employee & execute the following.
Employee (EMPNO,ENAME,JOB, MANAGER_NO, SAL, COMMISSION)
1. Create a user and grant all permissions to the user.
2. Insert the any three records in the employee table contains attributes EMPNO, ENAME
JOB, MANAGER_NO, SAL, COMMISSION and use rollback.
Check the result.
3. Add primary key constraint and not null constraint to the employee table.
4. Insert null values to the employee table and verify the result.
Solution:
1. Create a user and grant all permissions to the user.
CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'localhost';
FLUSH PRIVILEGES;
2. Login to user account using username and password from command prompt
3. Create database in the user account
CREATE DATABASE COMPANY;
USE COMPANY;
4. Create Table Employee
CREATE TABLE Employee (
EMPNO INT,
ENAME VARCHAR(50) ,
JOB VARCHAR(50),
MANAGER_NO INT,
SAL DECIMAL(10, 2),
COMMISSION DECIMAL(10, 2)
);
5. Insert the any three records in the employee table contains attributes EMPNO,
ENAME JOB, MANAGER_NO, SAL, COMMISSION and use rollback.
START TRANSACTION;
INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL,
COMMISSION) VALUES (1, 'John', 'Manager', 101, 50000, 1000);
INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL,
COMMISSION) VALUES (2, 'Alice', 'Developer', 101, 40000, 500);
INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL,
COMMISSION) VALUES (3, 'Bob', 'Analyst', 101, 35000, NULL);
ROLLBACK;
6. Add primary key constraint and not null constraint to the employee table.
ALTER TABLE Employee
ADD CONSTRAINT PK_EMPLOYEE PRIMARY KEY (EMPNO),
MODIFY ENAME VARCHAR(50) NOT NULL,
Dept. of ISE, JSSATEB 2023-24 8
DBMS Lab Manual BCS403
MODIFY JOB VARCHAR(50) NOT NULL,
MODIFY MANAGER_NO INT NOT NULL,
MODIFY SAL DECIMAL(10, 2) NOT NULL;
4. Insert null values to the employee table and verify the result.
INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL,
COMMISSION) VALUES (4, 'Carol', NULL, 101, 45000, 700);
Output:
mysql> CREATE USER 'myuser'@'localhost' IDENTIFIED BY 'mypassword';
Query OK, 0 rows affected (0.21 sec)
mysql> GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'localhost';
Query OK, 0 rows affected (0.00 sec)
mysql> FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.05 sec)
mysql> CREATE DATABASE COMPANY;
Query OK, 1 row affected (0.00 sec)
mysql> USE COMPANY
Database changed
Dept. of ISE, JSSATEB 2023-24 9
DBMS Lab Manual BCS403
mysql> ROLLBACK;
Query OK, 0 rows affected (0.03 sec)
mysql> SELECT * FROM EMPLOYEE;
Empty set (0.00 sec)
mysql> INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL,
COMMISSION) VALUES (4, 'Carol', NULL, 101, 45000, 700);
ERROR 1048 (23000): Column 'JOB' cannot be null
Dept. of ISE, JSSATEB 2023-24 10
DBMS Lab Manual BCS403
Program 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 Empno is 105.
Create table Employee:
CREATE TABLE Employee (
EMPNO INT PRIMARY KEY,
ENAME VARCHAR(50),
JOB VARCHAR(50),
MGR INT,
SAL DECIMAL(10, 2)
);
Output:
1. Add a column commission with domain to the Employee table.
ALTER TABLE Employee
ADD COLUMN commission DECIMAL(10, 2);
Dept. of ISE, JSSATEB 2023-24 11
DBMS Lab Manual BCS403
Output:
2. Insert any five records into the table.
INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, SAL, commission)
VALUES (101, 'John', 'Manager', 100, 50000, 1000),
(102, 'Alice', 'Developer', 101, 40000, 500),
(103, 'Bob', 'Analyst', 102, 35000, 200),
(104, 'Carol', 'Designer', 103, 45000, 800),
(105, 'David', 'Engineer', 101, 42000, 700);
3. Update the column details of job
UPDATE Employee
SET JOB = 'Lead Developer' WHERE ENAME = 'Alice';
Dept. of ISE, JSSATEB 2023-24 12
DBMS Lab Manual BCS403
4. Rename the column of Employ table using alter command.
ALTER TABLE Employee
CHANGE COLUMN MGR manager_no INT;
5. Delete the employee whose Empno is 105.
DELETE FROM Employee WHERE EMPNO = 105;
Dept. of ISE, JSSATEB 2023-24 13
DBMS Lab Manual BCS403
Program 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.
1. Create Employee table containing all Records E_id, E_name, Age, Salary.
CREATE TABLE Employee (
E_id INT PRIMARY KEY,
E_name VARCHAR(50),
Age INT,
Salary DECIMAL(10, 2)
);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES
(1, 'John', 30, 50000),
(2, 'Alice', 28, 45000),
(3, 'Bob', 35, 60000),
(4, 'Carol', 32, 55000),
(5, 'David', 25, 40000);
Dept. of ISE, JSSATEB 2023-24 14
DBMS Lab Manual BCS403
2. Count number of employee names from employee table
SELECT COUNT(E_name) AS num_employees FROM Employee;
3. Find the Maximum age from employee table.
SELECT MAX(Age) AS max_age FROM Employee;
4. Find the Minimum age from employee table.
SELECT MIN(Age) AS min_age FROM Employee;
Dept. of ISE, JSSATEB 2023-24 15
DBMS Lab Manual BCS403
5. Find salaries of employee in Ascending Order.
SELECT Salary FROM Employee ORDER BY Salary ASC;
6. Find grouped salaries of employees.
SELECT Salary, COUNT(*) AS num_employees_with_salary FROM Employee
GROUP BY Salary;
Dept. of ISE, JSSATEB 2023-24 16
DBMS Lab Manual BCS403
Program 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)
1. Create Table Customers
CREATE TABLE CUSTOMERS (
ID INT PRIMARY KEY,
NAME VARCHAR(255),
AGE INT,
ADDRESS VARCHAR(255),
SALARY DECIMAL(10, 2)
);
2. Create Trigger
DELIMITER //
CREATE TRIGGER salary_difference_trigger_after_insert
AFTER INSERT ON CUSTOMERS
FOR EACH ROW
BEGIN
DECLARE new_salary DECIMAL(10, 2);
-- Check if this is an INSERT operation
IF (NEW.ID IS NOT NULL) THEN
SET new_salary = NEW.SALARY;
SELECT CONCAT('New record inserted. ID: ', NEW.ID, ', Name: ',
NEW.NAME, ', Age: ', NEW.AGE, ', Address: ', NEW.ADDRESS, ', Salary: ',
new_salary) INTO @output;
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = @output;
END IF;
END //
CREATE TRIGGER salary_difference_trigger_after_update
Dept. of ISE, JSSATEB 2023-24 17
DBMS Lab Manual BCS403
AFTER UPDATE ON CUSTOMERS
FOR EACH ROW
BEGIN
DECLARE old_salary DECIMAL(10, 2);
DECLARE new_salary DECIMAL(10, 2);
DECLARE salary_diff DECIMAL(10, 2);
-- Check if this is an UPDATE operation
IF (NEW.ID IS NOT NULL AND OLD.ID IS NOT NULL) THEN
SET old_salary = OLD.SALARY;
SET new_salary = NEW.SALARY;
SET salary_diff = new_salary - old_salary;
SELECT CONCAT('Salary updated. ID: ', NEW.ID, ', Old Salary: ',
old_salary, ', New Salary: ', new_salary, ', Salary Difference: ', salary_diff) INTO
@output;
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = @output;
END IF;
END //
CREATE TRIGGER salary_difference_trigger_after_delete
AFTER DELETE ON CUSTOMERS
FOR EACH ROW
BEGIN
DECLARE old_salary DECIMAL(10, 2);
-- Check if this is a DELETE operation
IF (OLD.ID IS NOT NULL) THEN
SET old_salary = OLD.SALARY;
SELECT CONCAT('Record deleted. ID: ', OLD.ID, ', Name: ', OLD.NAME, ',
Age: ', OLD.AGE, ', Address: ', OLD.ADDRESS, ', Salary: ', old_salary) INTO
@output;
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = @output;
END IF;
END //
DELIMITER ;
Dept. of ISE, JSSATEB 2023-24 18
DBMS Lab Manual BCS403
Program 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)
DELIMITER //
CREATE PROCEDURE fetch_employee_data()
BEGIN
-- Declare variables to store values from the cursor
DECLARE emp_id INT;
DECLARE emp_name VARCHAR(255);
DECLARE emp_age INT;
DECLARE emp_salary DECIMAL(10, 2);
-- Declare a variable to indicate whether there are more rows to fetch
DECLARE no_more_rows BOOLEAN DEFAULT FALSE;
-- Declare a cursor for the Employee table
DECLARE emp_cursor CURSOR FOR
SELECT E_id, E_name, Age, Salary
FROM Employee;
-- Declare a NOT FOUND handler to exit loop when there are no more rows
DECLARE CONTINUE HANDLER FOR NOT FOUND
SET no_more_rows = TRUE;
-- Open the cursor
OPEN emp_cursor;
-- Loop to fetch values from the cursor
employee_loop: LOOP
-- Fetch values from the cursor into variables
FETCH emp_cursor INTO emp_id, emp_name, emp_age, emp_salary;
-- Check if there are no more rows to fetch
IF no_more_rows THEN
LEAVE employee_loop;
END IF;
-- Process the fetched values (you can perform any operations here)
-- For example, you can print the values
SELECT CONCAT('Employee ID: ', emp_id, ', Name: ', emp_name, ', Age: ', emp_age,
', Salary: ', emp_salary) AS output;
END LOOP;
-- Close the cursor
CLOSE emp_cursor;
END //
Dept. of ISE, JSSATEB 2023-24 19
DBMS Lab Manual BCS403
DELIMITER ;
CALL fetch_employee_data();
Output:
mysql> CALL fetch_employee_data();
+-------------------------------------------------------+
| output |
+-------------------------------------------------------+
| Employee ID: 1, Name: John, Age: 30, Salary: 50000.00 |
+-------------------------------------------------------+
1 row in set (0.01 sec)
+--------------------------------------------------------+
| output |
+--------------------------------------------------------+
| Employee ID: 2, Name: Alice, Age: 28, Salary: 45000.00 |
+--------------------------------------------------------+
1 row in set (0.04 sec)
+------------------------------------------------------+
| output |
+------------------------------------------------------+
| Employee ID: 3, Name: Bob, Age: 35, Salary: 60000.00 |
+------------------------------------------------------+
1 row in set (0.06 sec)
+--------------------------------------------------------+
| output |
+--------------------------------------------------------+
| Employee ID: 4, Name: Carol, Age: 32, Salary: 55000.00 |
+--------------------------------------------------------+
1 row in set (0.10 sec)
+--------------------------------------------------------+
| output |
+--------------------------------------------------------+
| Employee ID: 5, Name: David, Age: 25, Salary: 40000.00 |
+--------------------------------------------------------+
1 row in set (0.12 sec)
Query OK, 0 rows affected, 1 warning (0.13 sec)
Dept. of ISE, JSSATEB 2023-24 20
DBMS Lab Manual BCS403
Program 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.
Solution:
CREATE TABLE N_RollCall (
id INT,
name VARCHAR(255),
roll_no INT,
present INT
);
INSERT INTO N_RollCall (id, name, roll_no, present) VALUES
(1, 'John', 101, 1),
(2, 'Alice', 102, 1),
(3, 'Bob', 103, 0),
(4, 'Emily', 104, 1),
(5, 'David', 105, 0);
CREATE TABLE O_RollCall (
id INT,
name VARCHAR(255),
roll_no INT,
present INT
);
PL/SQL Code
DELIMITER $$
CREATE PROCEDURE merge_data_from_n_to_o()
BEGIN
DECLARE done BOOLEAN DEFAULT FALSE;
DECLARE n_id INT;
DECLARE n_name VARCHAR(255);
DECLARE n_roll_no INT;
DECLARE n_present INT;
DECLARE v_id INT;
DECLARE n_cursor CURSOR FOR
SELECT id, name, roll_no, present FROM N_RollCall;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
OPEN n_cursor;
fetch_loop: LOOP
Dept. of ISE, JSSATEB 2023-24 21
DBMS Lab Manual BCS403
FETCH n_cursor INTO n_id, n_name, n_roll_no, n_present;
IF done THEN
LEAVE fetch_loop;
END IF;
SET v_id = NULL;
SELECT id INTO v_id
FROM O_RollCall
WHERE id = n_id;
-- Diagnostic output
SELECT n_id, n_name, n_roll_no, n_present, v_id;
IF v_id IS NULL THEN
INSERT INTO O_RollCall (id, name, roll_no, present)
VALUES (n_id, n_name, n_roll_no, n_present);
END IF;
END LOOP;
CLOSE n_cursor;
END$$
DELIMITER ;
OUTPUT:
mysql> CALL merge_data_from_n_to_o();
Query OK, 1 row affected (0.08 sec)
mysql> select * from O_RollCall;
+------+------+---------+---------+
| id | name | roll_no | present |
+------+------+---------+---------+
| 1 | John | 101 | 1|
+------+------+---------+---------+
1 row in set (0.00 sec)
mysql> CALL merge_data_from_n_to_o();
Query OK, 1 row affected (0.08 sec)
Dept. of ISE, JSSATEB 2023-24 22
DBMS Lab Manual BCS403
Program 7
Install an Open Source NoSQL Data base MangoDB & perform basic CRUD (Create, Read,
Update & Delete) operations. Execute MangoDB basic Queries using CRUD operations.
Install mongoDB and mongosh
Data Types:
int
double
string
boolean
date
null
courses : [“Biology”,”Chemistry”,”Calculus”]
address:{Street:”123, 3rd main”, city:”Bengaluru”,zip:560074}
Basic Commands:
show dbs //show databases
use Employee //Create a database
db.createCollection(“students”) //create collections
show collections //displays the collections
db.createCollection(“teachers”)
db.createCollection(“teachers”,{capped:true,size:1000000,max:100},{autoIndexId:true})
db.createCollection(“courses”)
show collections
Insert Records to collections
db.students.insertOne({usn:”1JS21IS001”,name:”ABC”,age:30,gpa:3.2})
db.students.insertMany([{usn:”1JS21IS002”,name:”AAC”,age:30,gpa:4.2},{
usn:”1JS21IS003”,name:”BAC”,age:30,gpa:2.2},
{usn:”1JS21IS004”,name:”BCA”,age:30,gpa:2.4}])
Dept. of ISE, JSSATEB 2023-24 23
DBMS Lab Manual BCS403
Sorting:
db.students.find().sort()
db.students.find().sort({name:1}) Alphabetical Order
db.students.find().sort({name:-1}) Reverse Order
db.students.find().limit(1)
db.students.find().sort({gpa:-1}).limit(1)
find():
db.students.find({name:”spongebob”})
db.students.find({name:”spongebob”},{name:true})
update():
db.students.updateOne({name:”spongebob”},{$set:{fullTime:true}})
db.students.updateOne({name:”spongebob”},{$unset:{fullTime:” ”}})
db.students.updateMany({ },{$set:{fullTime:false}})
db.students.updateMany({fullTime:{$exists:false}},{$set:{fullTime:true}})
delete():
db.students.deleteOne({name:”spongebob”})
db.students.deleteMany({fullTime:false})
db.students.deleteMany({registerDate:{$exists:false}})
Comparison Operators:
db.students.find({name:{$ne:”spongebob”}})
db.students.find({age:{$lt:20}})
db.students.find({gpa:{$gte:3,$lte:4}})
db.students.find({name:{$nin:[“pattrick”,”sandy”]}})
Logical Operators:
db.students.find({$and:[{fullTime:true},{age:{$lte:22}}]})
db.students.find({age:{$not:{$gte:30}}})
Drop Database:
db.courses.drop()
Dept. of ISE, JSSATEB 2023-24 24