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

DBMS Manual

The document is a laboratory manual for the Database Management Systems course (BCS403) at Jain Institute of Technology, detailing the vision and mission of the institution and department, program outcomes, educational objectives, and practical components. It outlines various experiments related to database creation, manipulation, and management using SQL and NoSQL technologies, along with assessment criteria for continuous internal evaluation and semester-end examinations. Suggested learning resources and activities are also included to enhance students' understanding and application of database concepts.

Uploaded by

Nayana Kulkarni
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)
0 views

DBMS Manual

The document is a laboratory manual for the Database Management Systems course (BCS403) at Jain Institute of Technology, detailing the vision and mission of the institution and department, program outcomes, educational objectives, and practical components. It outlines various experiments related to database creation, manipulation, and management using SQL and NoSQL technologies, along with assessment criteria for continuous internal evaluation and semester-end examinations. Suggested learning resources and activities are also included to enhance students' understanding and application of database concepts.

Uploaded by

Nayana Kulkarni
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/ 30

Arka Educational and Cultural Trust

Jain Institute of Technology, Davanagere


(A Unit of Jain Group of Institutions, Bengaluru )

Affiliated to VTU, Belagavi | Approved by AICTE, New Delhi | Recognized by UGC under (2f) |
Recognized by Govt. of Karnataka

Department of IS&E

LABORATORY MANUAL
DATABASE
MANAGEMENT SYSTEMS
BCS403

FOR FOURTH SEMESTER

2023-2024

1
PREPARED BY:
Mrs. Meghana G R, Asst. Prof., Department of IS&E
Vision and Mission of the Institution

Vision of the Institute:

Technical manpower development to build professionally excellent, globally


competitive, socially responsible engineers and entrepreneurs with human values.

Mission of the Institute:

To provide quality education through innovation in teaching to create


M1
technologically competent engineers.
To Achieve excellence in research and development to advance science
M2
and technology to the ever changing needs of society.
To create outstanding professionals by facilitating state of the art platform
M3
capable of working in multi-cultural environment.
To produce quality engineers with high ethical standards and
M4
professionalism.
Vision & Mission of the Program
Vision of the Department:

To develop professionally excellent, socially responsible Information Science Engineers and


Entrepreneurs through teaching, research and development and cognitive skills with human eminence

Mission of the Department:


To foster the development of accountable and proficient professionals in the field of
M1 information science and engineering by emphasizing experiential learning and a solid
foundation in the fundamentals.
To infuse research and entrepreneurship through the ongoing creation of innovative
M2
solutions via intellectual tools and research-driven education.
To promote social responsibility and ethical conduct, nurturing a culture of integrity,
M3
empathy, and sustainable principles by fostering a compassionate global community.

Program Specific Outcomes


PSO1 Effective Information Management: Ability to design and manage databases, information
repositories, and content management systems for ensuring data integrity, accessibility, and
usability.

PSO2 Software Development and Entrepreneurship for Successful Career: Analyze, design and
develop data-driven software systems and applications that harness the power of emerging
computing technologies for successful career.

Program Educational Objectives


Professional Excellence

PEO1 Graduates from the Information Science & Engineering program should demonstrate
professional excellence by applying their knowledge and skills to address real-world
challenges in the field.

Innovation and Entrepreneurship


PEO2 Alumni of the program should exhibit a strong inclination towards innovation and
entrepreneurship.

Social Responsibility and Ethical


PEO3 The program aims to produce graduates who are socially responsible and demonstrate
ethical leadership in their professional and personal lives.
IPCC (26.04.2022)

PRACTICAL COMPONENT OF IPCC (May cover all / major modules)


Sl.NO Experiments
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 theuser.
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.
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 Employeetable.
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.
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 employeetable
3. Find the Maximum age from employee table.
4. Find the Minimum age from employeetable.
5. Find salaries of employee in Ascending Order.
6. Find grouped salaries of employees.
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)
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.
Course outcomes (Course Skill Set):
At the end of the course, the student will be able to:
● Describe the basic elements of a relational database management system
● Design entity relationship for the given scenario.
● Apply various Structured Query Language (SQL) statements for database manipulation.
● Analyse various normalization forms for the given application.
● Develop database applications for the given real world problem.
● Understand the concepts related to NoSQL databases.
Assessment Details (both CIE and SEE)
The weightage of Continuous Internal Evaluation (CIE) is 50% and for Semester End Exam (SEE) is 50%. The minimum

@# 16032024 3
IPCC (26.04.2022)

passing mark for the CIE is 40% of the maximum marks (20 marks out of 50) and for the SEE minimum passing mark is
35% of the maximum marks (18 out of 50 marks). A student shall be deemed to have satisfied the academic requirements
and earned the credits allotted to each subject/ course if the student secures a minimum of 40% (40 marks out of 100) in
the sum total of the CIE (Continuous Internal Evaluation) and SEE (Semester End Examination) taken together.
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.
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 sub-
questions), 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 scoredby the student shall be proportionally scaled down to 50 Marks
The theory portion of the IPCC shall be for both CIE and SEE, whereas the practical portion will have a CIE
component only. Questions mentioned in the SEE paper may include questions from the practical component.
Suggested Learning Resources:
Text Books:
1. Fundamentals of Database Systems, Ramez Elmasri and Shamkant B. Navathe, 7th Edition, 2017, Pearson.
2. Database management systems, Ramakrishnan, and Gehrke, 3rd Edition, 2014, McGraw Hill
Activity Based Learning (Suggested Activities in Class)/ Practical Based learning
Mini Project:
• Project Based Learning

@# 16032024 4
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 'new_user'@'localhost' IDENTIFIED BY 'password123456';

GRANT ALL PRIVILEGES ON *.* TO 'new_user'@'localhost';

FLUSH PRIVILEGES;

Create the Employee table and insert three records using rollback:

CREATE TABLE Employee (

EMPNO INT,

ENAME VARCHAR(50),

JOB VARCHAR(50),

MANAGER_NO INT,

SAL DECIMAL(10, 2),

COMMISSION DECIMAL(10, 2)

);

desc Employee

Field Type Null Key Default


EMPNO int(11) Yes NULL

2
Field Type Null Key Default
ENAME varchar(50) Yes NULL

JOB varchar(50) Yes NULL

MANAGER_NO int(11) YES NULL

SAL decimal(10,2) NO NULL

COMMISSION decimal(10,2) YES NULL

insert three records

insert into Employee values (1, 'John Doe', 'Manager', NULL, 5000.00, 1000.00),

insert into Employee values (2, 'Jane Smith', 'Developer', 1, 4000.00, NULL),

insert into Employee values (3, 'Alice Johnson', 'Salesperson', 1, 3000.00, 500.00);

EMPNO ENAME JOB MANAGER_NO SAL COMMISSION


1 John Doe Manager NULL 5000.00 1000.00

2 Jane Smith Developer 1 4000.00 NULL

3 Alice Johnson Salesperson 1 3000.00 500.00

ROLLBACK;

2. Add primary key constraint and not null constraint to the Employee table
Add primary key constraint
ALTER TABLE Employee ADD PRIMARY KEY (EMPNO);

Field Type Null Key Default


EMPNO int(11) NO PRI NULL

ENAME varchar(50) YES NULL

JOB varchar(50) YES NULL

MANAGER_NO int(11) YES NULL

SAL decimal(10,2) YES NULL

COMMISSION decimal(10,2) YES NULL

Add not null constraint to the Employee table

3
ALTER TABLE Employee
MODIFY ENAME VARCHAR(50) NOT NULL,
MODIFY JOB VARCHAR(50) NOT NULL,
MODIFY SAL DECIMAL(10,2) NOT NULL;

Field Type Null Key Default


EMPNO int(11) NO PRI NULL

ENAME varchar(50) NO NULL

JOB varchar(50) NO NULL

MANAGER_NO int(11) YES NULL

SAL decimal(10,2) NO NULL

COMMISSION decimal(10,2) YES NULL

4. Attempt to insert null values into the Employee table:


INSERT INTO Employee values(1,NULL,'intern',2,NULL,NULL);

Output:

Column 'ENAME' cannot be null

4
2. Create a table called Employee that contain attributes EMPNO, ENAME,JOB, MGR,SAL &
execute the following.
CREATE TABLE Employee (
EMPNO INT,
ENAME VARCHAR(50),
JOB VARCHAR(50),
MGR INT,
SAL DECIMAL(10,2)
);
desc Employee

Field Type Null Key Default


Empno varchar(50) YES NULL

ENAME varchar(50) YES NULL

JOB varchar(50) YES NULL

MGR int(11) YES NULL

SAL decimal(10,2) YES NULL

1. Add a column commission with domain to the Employeetable.


ALTER TABLE Employee
ADD COMMISSION DECIMAL(10,2);

Field Type Null Key Default


Empno varchar(50) YES NULL

ENAME varchar(50) YES NULL

JOB varchar(50) YES NULL

MGR int(11) YES NULL

SAL decimal(10,2) YES NULL

COMMISSION decimal(10,2) YES NULL

2. Insert any five records into the table.


INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, SAL)
VALUES (101, 'John Doe', 'Manager', NULL, 5000.00),
(102, 'Jane Smith', 'Assistant', 101, 3000.00),
5
(103, 'Michael Johnson', 'Clerk', 102, 2000.00),
(104, 'Emily Brown', 'Manager', NULL, 5500.00),
(105, 'Alex Clark', 'Assistant', 104, 3200.00);

EMPNO ENAME JOB MGR SAL COMMISSION


101 John Doe Manager NULL 5000.00 NULL

102 Jane Smith Assistant 101 3000.00 NULL

103 Michael Johnson Clerk 102 2000.00 NULL

104 Emily Brown Manager NULL 5500.00 NULL

105 Alex Clark Assistant 104 3200.00 NULL

3. Update the column details of job


UPDATE Employee
SET JOB = 'Supervisor'
WHERE EMPNO = 105;

EMPNO ENAME JOB MGR SAL COMMISSION


101 John Doe Manager NULL 5000.00 NULL

102 Jane Smith Assistant 101 3000.00 NULL

103 Michael Johnson Clerk 102 2000.00 NULL

104 Emily Brown Manager NULL 5500.00 NULL

105 Alex Clark Supervisor 104 3200.00 NULL

3. Rename the column of Employ table using alter command.


ALTER TABLE Employee
CHANGE COLUMN Empno Empno2 VARCHAR(50);
Empno2 ENAME JOB MGR SAL COMMISSION
101 John Doe Manager NULL 5000.00 NULL

102 Jane Smith Assistant 101 3000.00 NULL

103 Michael Johnson Clerk 102 2000.00 NULL

104 Emily Brown Manager NULL 5500.00 NULL

105 Alex Clark Supervisor 104 3200.00 NULL

4. Delete the employee whose Empno is 105.

6
DELETE FROM Employee
WHERE EMPNO2 = 105;
Select * from Employee

Empno2 ENAME JOB MGR SAL COMMISSION


101 John Doe Manager NULL 5000.00 NULL

102 Jane Smith Assistant 101 3000.00 NULL

103 Michael Johnson Clerk 102 2000.00 NULL

104 Emily Brown Manager NULL 5500.00 NULL

7
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.

CREATE TABLE Employee (


E_id INT,
E_name VARCHAR(50),
Age INT,
Salary DECIMAL(10,2)
);
Field Type Null Key Default
E_id int(11) YES NULL

E_name varchar(50) YES NULL

Age int(11) YES NULL

Salary decimal(10,2) YES NULL

insert into Employee(E_id, E_name, Age, Salary) values (1, 'John',35,40000),


(2,'Alice',32,35700),
(3,'Bob',28,28000),
(4,'Charles',30,32000);

E_id E_name Age Salary


1 John 35 40000.00

2 Alice 32 35700.00

3 Bob 28 28000.00

4 Charles 30 32000.00

2. Count number of employee names from employeetable

SELECT COUNT(E_name) AS total_employees


FROM Employee;

total_employees
4

3. Find the Maximum age from employee table.

SELECT MAX(Age) AS max_age


FROM Employee;

8
max_age
35

4. Find the Minimum age from employeetable.

SELECT MIN(Age) AS min_age


FROM Employee;

min_age
28

5. Find salaries of employee in Ascending Order.

SELECT Salary
FROM Employee
ORDER BY Salary ASC;

Salary 1
28000.00

32000.00

35700.00

40000.00

6. Find grouped salaries of employees.


insert into Employee values (6, 'Peter',38,40000);

E_id E_name Age Salary


1 John 35 40000.00

2 Alice 32 35700.00

3 Bob 28 28000.00

4 Charles 30 32000.00

6 peter 38 40000.00

SELECT Salary, COUNT(*) AS num_employees


FROM Employee
GROUP BY Salary;

9
Salary num_employees
28000.00 1

32000.00 1

35700.00 1

40000.00 2

10
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)

CREATE TABLE CUSTOMERS (


ID INT PRIMARY KEY,
NAME VARCHAR(50),
AGE INT,
ADDRESS VARCHAR(100),
SALARY DECIMAL(10, 2)
);

Field Type Null Key Default


ID int(11) NO PRI NULL

NAME varchar(50) YES NULL

AGE int(11) YES NULL

ADDRESS varchar(100) YES NULL

SALARY decimal(10,2) YES NULL

Step 1: Create an Audit Table

First, create an audit table to store the salary differences and other relevant information.

CREATE TABLE customers_audit (


id INT AUTO_INCREMENT PRIMARY KEY,
operation VARCHAR(10),
customer_id INT,
old_salary DECIMAL(10,2),
new_salary DECIMAL(10,2),
salary_difference DECIMAL(10,2),
operation_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

11
Step 2: Create Triggers

Next, create the triggers for INSERT, UPDATE, and DELETE operations to log changes to the
customers_audit table.

DELIMITER //

CREATE TRIGGER trg_customers_insert


AFTER INSERT ON CUSTOMERS
FOR EACH ROW
BEGIN
INSERT INTO customers_audit (operation, customer_id, new_salary)
VALUES ('INSERT', NEW.ID, NEW.SALARY);
END//

DELIMITER ;

Trigger for UPDATE

DELIMITER //

CREATE TRIGGER trg_customers_update


AFTER UPDATE ON CUSTOMERS
FOR EACH ROW
BEGIN
DECLARE v_salary_diff DECIMAL(10,2);
SET v_salary_diff = NEW.SALARY - OLD.SALARY;
INSERT INTO customers_audit (operation, customer_id, old_salary, new_salary,
salary_difference)
VALUES ('UPDATE', NEW.ID, OLD.SALARY, NEW.SALARY, v_salary_diff);
END//

DELIMITER ;

12
Trigger for DELETE

DELIMITER //

CREATE TRIGGER trg_customers_delete


AFTER DELETE ON CUSTOMERS
FOR EACH ROW
BEGIN
INSERT INTO customers_audit (operation, customer_id, old_salary)
VALUES ('DELETE', OLD.ID, OLD.SALARY);
END//

DELIMITER ;

Inserting values to customers table

insert into CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) values

(1,’Peter’,35, ‘2nd cross vijaynagar’, 100000), (2,’Jhew’,42, ‘Dollars Colony’, 92000),

(3,’Pinto’,26, ‘RR Nagar’, 45000);

Output:

3 rows inserted

SELECT * FROM `customers`;

NAME ID AGE ADDRESS SALARY

Peter 1 35 2nd cross vijaynagar 100000.00

Jhew 2 42 Dollars Colony 92000.00

Pinto 3 26 RR Nagar 45000.00

Checking the trigger on insert operation, created by displaying the customers_audit


table

SELECT * FROM customers_audit

id operation customer_id old_salary new_salary salary_difference operation_time

13
1 INSERT 1 NULL 100000.00 NULL 2024-05-20
14:20:09
2 INSERT 2 NULL 92000.00 NULL 2024-05-20
14:20:09
3 INSERT 3 NULL 45000.00 NULL 2024-05-20
14:20:09

Updating the salary of an employee with id 2

update customers set salary = 150000 where id = 2;

SELECT * FROM customers;

ID NAME AGE ADDRESS SALARY

1 Peter 35 2nd cross vijaynagar 100000.00

2 Jhew 42 Dollars Colony 150000.00

3 Pinto 26 RR Nagar 45000.00

Checking the trigger on update operation, created by displaying the customers_audit


table

SELECT * FROM customers_audit;

id operation customer_i old_salar new_salar salary_differenc operation_tim


d y y e e
1 INSERT 1 NULL 100000.00 NULL 2024-05-20
14:20:09
2 INSERT 2 NULL 92000.00 NULL 2024-05-20
14:20:09
3 INSERT 3 NULL 45000.00 NULL 2024-05-20
14:20:09
4 UPDAT 2 92000.00 150000.00 58000.00 2024-05-20
E 14:57:56

14
Delete a row with id 1

delete from customers where id=1

Output:

1 row deleted. (Query took 0.1224 seconds.)

Checking the trigger on delete operation, created by displaying the customers_audit


table

SELECT * FROM `customers_audit`;

id operation customer_id old_salary new_salary salary_difference operation_time


1 INSERT 1 NULL 100000.00 NULL 2024-05-20
14:20:09
2 INSERT 2 NULL 92000.00 NULL 2024-05-20
14:20:09
3 INSERT 3 NULL 45000.00 NULL 2024-05-20
14:20:09
4 UPDATE 2 92000.00 150000.00 58000.00 2024-05-20
14:57:56
5 DELETE 1 100000.00 NULL NULL 2024-05-20
15:03:03

15
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)

1. Create the Employee table (if not already created):

CREATE TABLE Employee (

E_id INT PRIMARY KEY,

E_name VARCHAR(255),

Age INT,

Salary DECIMAL(10, 2)

);

desc employee;

E_id int(11) NO PRI NULL

E_name varchar(100) YES NULL

Age int(11) YES NULL

Salary decimal(10,2) YES NULL

2. Insert some sample data (if not already present):

INSERT INTO Employee (E_id, E_name, Age, Salary)

VALUES

(1, 'Alice', 30, 50000),

(2, 'Bob', 28, 48000),

(3, 'Charlie', 25, 45000);

16
3. Procedure to create routine:

DELIMITER //

CREATE PROCEDURE your_procedure_name()

BEGIN

-- Procedure logic here

SELECT * FROM your_table_name;

END //

DELIMITER ;

4. Edit in procedure code and execute the procedure

Create the stored procedure to work with the cursor:

BEGIN

-- Declare variables to hold employee data

DECLARE v_E_id INT;

DECLARE v_E_name VARCHAR(100);

DECLARE v_Age INT;

DECLARE v_Salary DECIMAL(10,2);

-- Variable to control cursor loop

DECLARE done INT DEFAULT 0;

-- Declare a cursor for the Employee table

17
DECLARE employee_cursor CURSOR FOR

SELECT E_id, E_name, Age, Salary FROM Employee;

-- Declare a handler to handle the end of the cursor

DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

-- Open the cursor

OPEN employee_cursor;

-- Fetch the data from the cursor

read_loop: LOOP

FETCH employee_cursor INTO v_E_id, v_E_name, v_Age, v_Salary;

IF done THEN

LEAVE read_loop;

END IF;

-- Do something with the fetched data

-- Here we will just select it to display

SELECT v_E_id, v_E_name, v_Age, v_Salary;

END LOOP read_loop;

-- Close the cursor

CLOSE employee_cursor;

END

Call the stored procedure:

CALL ExtractEmployeeData();

18
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.

Creation of Tables:
CREATE TABLE N_RollCall (
student_id INT,
rollcall_date DATE,
status VARCHAR(10),
PRIMARY KEY (student_id, rollcall_date)
);

CREATE TABLE O_RollCall (


student_id INT,
rollcall_date DATE,
status VARCHAR(10),
PRIMARY KEY (student_id, rollcall_date)
);

Insertion of values
-- Insert sample data into N_RollCall
INSERT INTO N_RollCall (student_id, rollcall_date, status) VALUES
(1, '2024-06-25', 'Present'),
(2, '2024-06-25', 'Absent'),
(3, '2024-06-25', 'Present');

-- Insert sample data into O_RollCall


INSERT INTO O_RollCall (student_id, rollcall_date, status) VALUES
(1, '2024-06-25', 'Present');

Creation of Procedure
DELIMITER $$

19
CREATE PROCEDURE merge_rollcall()
BEGIN
DECLARE done INT DEFAULT 0;
DECLARE v_student_id INT;
DECLARE v_rollcall_date DATE;
DECLARE v_status VARCHAR(10);

-- Declare a cursor to select data from N_RollCall


DECLARE cur_n_rollcall CURSOR FOR
SELECT student_id, rollcall_date, status FROM N_RollCall;

-- Declare a continue handler for cursor


DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;

-- Open the cursor


OPEN cur_n_rollcall;

-- Loop through each row in N_RollCall


read_loop: LOOP
FETCH cur_n_rollcall INTO v_student_id, v_rollcall_date, v_status;
IF done THEN
LEAVE read_loop;
END IF;

-- Check if the record already exists in O_RollCall


IF NOT EXISTS (
SELECT 1 FROM O_RollCall
WHERE student_id = v_student_id
AND rollcall_date = v_rollcall_date
) THEN
-- Insert the record into O_RollCall
INSERT INTO O_RollCall (student_id, rollcall_date, status)
VALUES (v_student_id, v_rollcall_date, v_status);
END IF;

20
END LOOP;

-- Close the cursor


CLOSE cur_n_rollcall;
END$$

DELIMITER ;

7. Install an Open Source NoSQL Data base MongoDB & perform basic
CRUD(Create, Read, Update & Delete) operations. Execute MongoDB basic
Queries using CRUD operations.

Performing Basic CRUD Operations:


1. Create Operation:
use basictask;

2. Insert Operation:
db.users.insertOne({
name: "Braham Kumar",
age: 25,
email: "[email protected]",
status: "inactive"
});
◉ Insert multiple documents:
db.users.insertMany([
{
name: "Braham Kumar",
age: 25,
email: "[email protected]",
status: "inactive"
},
{

21
name: "Shubham Kumar",
age: 35,
email: "[email protected]",
status: "active"
},
{
name: "Bikash Singh",
age: 28,
email: "[email protected]",
status: "active"
},
{
name: "Shoaib Akhtar",
age: 28,
email: "[email protected]",
status: "active"
}
]);
3. Read Query:
db.users.find();

OUTPUT:
[
{
_id: ObjectId('666c78f13c52fc36f3cdcdf6'),
name: 'Braham Kumar',
age: 25,
email: '[email protected]',
status: 'inactive'
},
{
_id: ObjectId('666c78f13c52fc36f3cdcdf7'),
name: 'Shubham Kumar',

22
age: 35,
email: '[email protected]',
status: 'active'
},
{
_id: ObjectId('666c78f13c52fc36f3cdcdf8'),
name: 'Bikash Singh',
age: 28,
email: '[email protected]',
status: 'active'
},
{
_id: ObjectId('666c78f13c52fc36f3cdcdf9'),
name: 'Shoaib Akhtar',
age: 28,
email: '[email protected]',
status: 'active'
}
]
◉ Find documents with specific criteria (e.g., find all users with age greater
than 25):

db.users.find({ age: { $gt: 25 } });

OUTPUT:
[
{
_id: ObjectId('666c78f13c52fc36f3cdcdf7'),
name: 'Shubham Kumar',
age: 35,
email: '[email protected]',
status: 'active'
},
{

23
_id: ObjectId('666c78f13c52fc36f3cdcdf8'),
name: 'Bikash Singh',
age: 28,
email: '[email protected]',
status: 'active'
},
{
_id: ObjectId('666c78f13c52fc36f3cdcdf9'),
name: 'Shoaib Akhtar',
age: 28,
email: '[email protected]',
status: 'active'
}
]

4. Update Query:
◉ Update a single document:

db.users.updateOne(
{ name: "Bikash Singh" },
{ $set: { age: 31 } }
);
db.users.find({ age: { $gt: 31 } });

OUTPUT:

[
{
_id: ObjectId('666c78f13c52fc36f3cdcdf7'),
name: 'Shubham Kumar',
age: 35,
email: '[email protected]',
status: 'active'

24
}
]

◉ Update multiple documents:

db.users.updateMany(
{ age: { $gt: 28 } },
{ $set: { status: "inactive" } }
);
db.users.find({ age: { $gt: 28 } });

OUTPUT:

[
{
_id: ObjectId('666c78f13c52fc36f3cdcdf7'),
name: 'Shubham Kumar',
age: 35,
email: '[email protected]',
status: 'inactive'
},
{
_id: ObjectId('666c78f13c52fc36f3cdcdf8'),
name: 'Bikash Singh',
age: 31,
email: '[email protected]',
status: 'inactive'
}
]

5. Delete Query:
◉ Delete a single document:

db.users.deleteOne({ name: "Bikash Singh" });


◉ Delete multiple documents:

25
db.mycollection.deleteMany({ age: { $gt: 28 } });

5. Basic Queries:
db.users.count();
◉ Sorting documents:

db.users.find().sort({ age: 1 }); // Ascending order


db.users.find().sort({ age: -1 }); // Descending order
◉ Limiting the number of documents returned:

db.users.find().limit(5);
◉ Aggregation queries (e.g., group by and aggregate functions):

db.users.aggregate([
{ $group: { _id: "$age", count: { $sum: 1 } } }
]);

26

You might also like