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

DBMS lab manual final 2022 scheme

The document provides a comprehensive guide on various database management tasks, including creating and manipulating tables in SQL, using triggers, cursors, and stored procedures. It covers CRUD operations in MongoDB and demonstrates how to perform basic queries and data management tasks. Additionally, it includes examples of using aggregate functions and handling data merging between tables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

DBMS lab manual final 2022 scheme

The document provides a comprehensive guide on various database management tasks, including creating and manipulating tables in SQL, using triggers, cursors, and stored procedures. It covers CRUD operations in MongoDB and demonstrates how to perform basic queries and data management tasks. Additionally, it includes examples of using aggregate functions and handling data merging between tables.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Database Management System (BCS403)

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.

CREATE DATABASE COMPANY;

USE COMPANY

CREATE TABLE COMPANY.Employee (


EMPNO INT,
ENAME VARCHAR(255),
JOB VARCHAR(255),
MANAGER_NO INT,
SAL DECIMAL(10, 2),
COMMISSION DECIMAL(10, 2)
);
SHOW TABLES
DESC COMPANY.Employee;

1. Create a user and grant all permissions to the user.


CREATE USER IF NOT EXISTS 'dbuser'@'localhost' IDENTIFIED BY 'T0p5E(RET)';

GRANT ALL PRIVILEGES ON COMPANY.Employee TO 'dbuser'@'localhost';

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.
START TRANSACTION
INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
VALUES (1, 'Kavana Shetty', 'Manager', NULL, 5000.00, 1000.00);
COMMIT;

1
Database Management System (BCS403)

INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)


VALUES (2, 'Ram Charan', 'Developer', 1, 4000.00, NULL);
INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
VALUES (3, 'Honey Singh', 'Salesperson', 2, 3000.00, 500.00);

SELECT * FROM Employee;


DELETE FROM Employee where ENAME = 'Kavana Shetty';
SELECT * FROM Employee;
ROLLBACK;
SELECT * FROM Employee;

3. Add primary key constraint and not null constraint to the employee table.
ALTER TABLE Employee
ADD CONSTRAINT pk_employee PRIMARY KEY (EMPNO);
DESC Employee;

4. Insert null values to the employee table and verify the result.
ALTER TABLE Employee
MODIFY ENAME VARCHAR(255) NOT NULL,
MODIFY JOB VARCHAR(255) NOT NULL,
MODIFY SAL DECIMAL(10, 2) NOT NULL;

INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)


VALUES (4, 'Ranjan', 'Manager', NULL, 5000.00, 1000.00);

INSERT INTO Employee (ENAME, JOB, MANAGER_NO, SAL, COMMISSION)


VALUES (NULL, 'Tester', NULL, 3500.00, NULL)

2
Database Management System (BCS403)

2. Create a table called Employee that contain attributes EMPNO, ENAME, JOB, MGR, SAL
& execute the following.
 Add a column commission with domain to the Employee table.
 Insert any five records into the table.
 Update the column details of job
 Rename the column of Employ table using alter command.
 Delete the employee whose Empno is 105.

create database lab2;


use lab2;
create table employee (empno int,ename varchar(30), job varchar(10),mgr int,sal int);
desc employee;

1. Add a column commission with domain to the Employee table


alter table employee add(commission int);
desc employee;

2. Insert any five records into the table.


INSERT INTO employee (empno,ename,job,mgr,sal,commission) VALUES (101, 'Kavana Shetty',
'Manager', 1, 5000.00, 1000.00);
INSERT INTO employee (empno,ename,job, mgr,sal,commission) VALUES (102, 'Ram Charan',
'Developer', 2, 4000.00, 200);
INSERT INTO employee (empno,ename,job, mgr,sal,commission) VALUES (103,
'Singh','Saleman',3, 3000.00, 500.00);
INSERT INTO employee (empno,ename,job, mgr,sal,commission) VALUES (104, 'Laxman',
'Trainer', 4, 4000.00, 200);
INSERT INTO employee (empno,ename,job, mgr,sal,commission) VALUES (105, 'Mahender',
'Analyst',5, 3000.00, 500.00);

Select * from employee;

3. Update the column details of job


update employee set job='trainee' where empno=103;
Select * from employee;

3
Database Management System (BCS403)

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


alter table employee rename column mgr to manager_no;
desc employee;

5. Delete the employee whose Empno is 105


delete from employee where empno=105;
Select * from employee;

4
Database Management System (BCS403)

3. Queries using aggregate functions(COUNT,AVG,MIN,MAX,SUM),Group by, Orderby.


Employee(E_id, E_name, Age, Salary)
 Create Employee table containing all Records E_id, E_name, Age, Salary.
 Count number of employee names from employeetable
 Find the Maximum age from employee table.
 Find the Minimum age from employeetable.
 Find salaries of employee in Ascending Order.
 Find grouped salaries of employees.

 Create Employee table containing all Records E_id, E_name, Age, Salary.
Create database lab3;
use lab3;
create table emp(E_id int,E_name varchar(10),Age int,Salary int);
desc emp;
INSERT INTO emp(E_id,E_name,Age,Salary) VALUES (101,'Kavana',30, 5000.00);
INSERT INTO emp(E_id,E_name,Age,Salary) VALUES (102,'Patil',35, 7000.00);
INSERT INTO emp(E_id,E_name,Age,Salary) VALUES (103,'Ranjitha',22,25000.00);
INSERT INTO emp(E_id,E_name,Age,Salary) VALUES (104,'Ram',36,42000.00);
INSERT INTO emp(E_id,E_name,Age,Salary) VALUES (105,'Naveen',30,5000.00);
Select * from emp;

 Count number of employee names from employeetable


select count(E_name) as Name_of_emp from emp;

 Find the Maximum age from employee table.


select max(age) as Max_of_emp from emp;
 Find the Minimum age from employeetable.
select min(age) as Min_of_emp from emp;

 Find salaries of employee in Ascending Order.


select Salary from emp order by salary;

 Find grouped salaries of employees.


SELECT Salary, COUNT(*) AS EmployeeCount FROM Employee GROUP BY Salary;

5
Database Management System (BCS403)

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 database lab4example;


CREATE TABLE CUSTOMERS1 (ID INT PRIMARY KEY AUTO_INCREMENT,NAME
VARCHAR(255),AGE INT,ADDRESS VARCHAR(255), SALARY DECIMAL(10, 2));

-- INSERT TRIGGER
DELIMITER //

CREATE TRIGGER after_insert_salary_difference2


AFTER INSERT ON CUSTOMERS1
FOR EACH ROW
BEGIN
SET @my_sal_diff = CONCAT('salary inserted is ', NEW.SALARY);
END;//

DELIMITER ;

-- UPDATE TRIGGER
DELIMITER //

CREATE TRIGGER after_update_salary_difference2


AFTER UPDATE ON CUSTOMERS1
FOR EACH ROW
BEGIN
DECLARE old_salary DECIMAL(10, 2);
DECLARE new_salary DECIMAL(10, 2);

SET old_salary = OLD.SALARY;


SET new_salary = NEW.SALARY;
SET @my_sal_diff = CONCAT('salary difference after update is ', NEW.SALARY -
OLD.SALARY);

6
Database Management System (BCS403)

END;//
DELIMITER ;

-- DELETE TRIGGER
DELIMITER //

CREATE TRIGGER after_delete_salary_difference2


AFTER DELETE ON CUSTOMERS1
FOR EACH ROW
BEGIN
SET @my_sal_diff = CONCAT('salary deleted is ', OLD.SALARY);
END;//

DELIMITER ;

-- test INSERT TRIGGER


INSERT INTO CUSTOMERS1 (NAME, AGE, ADDRESS, SALARY) VALUES ('Shankara',
35, '123 Main St', 50000.00);

SELECT @my_sal_diff AS SAL_DIFF;

-- test UPDATE TRIGGER


UPDATE CUSTOMERS SET SALARY = 55000.00 WHERE ID = 1;

SELECT @my_sal_diff AS SAL_DIFF;


-- test DELETE TRIGGER
DELETE FROM CUSTOMERS WHERE ID = 1;

7
Database Management System (BCS403)

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.
CUSTOMERS(ID,NAME,AGE,ADDRESS,SALARY)

CREATE DATABASE lab05;

USE lab05;

CREATE TABLE Employee (


E_id INT,
E_name VARCHAR(255),
Age INT,
Salary DECIMAL(10, 2)
);

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


VALUES
(1, 'Samarth', 30, 50000.00),
(2, 'Ramesh Kumar', 25, 45000.00),
(3, 'Seema Banu', 35, 62000.00),
(4, 'Dennis Anil', 28, 52000.00),
(5, 'Rehman Khan', 32, 58000.00);

DELIMITER //

CREATE PROCEDURE fetch_employee_data()


BEGIN
-- Declare variables to store cursor values
DECLARE emp_id INT;
DECLARE emp_name VARCHAR(255);
DECLARE emp_age INT;
DECLARE emp_salary DECIMAL(10, 2);

-- Declare a cursor for the Employee table


DECLARE emp_cursor CURSOR FOR

8
Database Management System (BCS403)

SELECT E_id, E_name, Age, Salary


FROM Employee;

-- Declare a continue handler for the cursor


DECLARE CONTINUE HANDLER FOR NOT FOUND
SET @finished = 1;

-- Open the cursor


OPEN emp_cursor;

-- Initialize a variable to control cursor loop


SET @finished = 0;

-- Loop through the cursor results


cursor_loop: LOOP
-- Fetch the next row from the cursor into variables
FETCH emp_cursor INTO emp_id, emp_name, emp_age, emp_salary;

-- Check if no more rows to fetch


IF @finished = 1 THEN
LEAVE cursor_loop;
END IF;

-- Output or process each row (for demonstration, print the values)


SELECT CONCAT('Employee ID: ', emp_id, ', Name: ', emp_name, ', Age: ', emp_age, ',
Salary: ', emp_salary) AS Employee_Info;
END LOOP;

-- Close the cursor


CLOSE emp_cursor;
END//

DELIMITER ;

CALL fetch_employee_data();

9
Database Management System (BCS403)

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.
CREATE DATABASE ROLLCALL;

USE ROLLCALL;

-- Create N_RollCall table


CREATE TABLE N_RollCall (
student_id INT PRIMARY KEY,
student_name VARCHAR(255),
birth_date DATE
);

-- Create O_RollCall table with common data


CREATE TABLE O_RollCall (
student_id INT PRIMARY KEY,
student_name VARCHAR(255),
birth_date DATE
);

-- Insert common data into O_RollCall


INSERT INTO O_RollCall (student_id, student_name, birth_date) VALUES (1, 'Shivanna', '1995-08-
15'), (3, 'Cheluva', '1990-12-10');

-- Insert sample records into N_RollCall


INSERT INTO N_RollCall (student_id, student_name, birth_date) VALUES
(1, 'Shivanna', '1995-08-15'), -- Common record with O_RollCall
(2, 'Bhadramma', '1998-03-22'),
(3, 'Cheluva', '1990-12-10'), -- Common record with O_RollCall
(4, 'Devendra', '2000-05-18'),
(5, 'Eshwar', '1997-09-03');

-- Define the Stored Procedure

10
Database Management System (BCS403)

DELIMITER //

CREATE PROCEDURE merge_rollcall_data()


BEGIN
DECLARE done INT DEFAULT FALSE;
DECLARE n_id INT;
DECLARE n_name VARCHAR(255);
DECLARE n_birth_date DATE;

-- Declare cursor for N_RollCall table


DECLARE n_cursor CURSOR FOR
SELECT student_id, student_name, birth_date
FROM N_RollCall;

-- Declare handler for cursor


DECLARE CONTINUE HANDLER FOR NOT FOUND
SET done = TRUE;

-- Open the cursor


OPEN n_cursor;

-- Start looping through cursor results


cursor_loop: LOOP
-- Fetch data from cursor into variables
FETCH n_cursor INTO n_id, n_name, n_birth_date;

-- Check if no more rows to fetch


IF done THEN
LEAVE cursor_loop;
END IF;

-- Check if the data already exists in O_RollCall


IF NOT EXISTS (
SELECT 1
FROM O_RollCall

11
Database Management System (BCS403)

WHERE student_id = n_id


) THEN
-- Insert the record into O_RollCall
INSERT INTO O_RollCall (student_id, student_name, birth_date)
VALUES (n_id, n_name, n_birth_date);
END IF;
END LOOP;

-- Close the cursor


CLOSE n_cursor;
END//

DELIMITER ;

-- Execute the Stored Procedure


CALL merge_rollcall_data();

-- Select all records from O_RollCall


SELECT * FROM O_RollCall;

12
Database Management System (BCS403)

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

Perform basic CRUD(Create, Read, Update & Delete) operations.


1. Start MongoDB.
Launch the MongoDB daemon using the following command:
sudo systemctl start mongod

2. Start the MongoDB Shell


Launch the MongoDB shell to perform basic CRUD operations.
mongosh

3. Switch to a Database (Optional):

test> use bookDB


switched to db bookDB
bookDB>

4. Create the ProgrammingBooks Collection


bookDB> db.createCollection("ProgrammingBooks")

5. INSERT operations
Insert 5 Documents into the ProgrammingBooks Collection :

bookDB> db.ProgrammingBooks.insertMany([
{
title: "Clean Code: A Handbook of Agile Software Craftsmanship",
author: "Robert C. Martin",
category: "Software Development",
year: 2008
},
{
title: "JavaScript: The Good Parts",
author: "Douglas Crockford",
category: "JavaScript",

13
Database Management System (BCS403)

year: 2008
},
{
title: "Design Patterns: Elements of Reusable Object-Oriented Software",
author: "Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides",
category: "Software Design",
year: 1994
},
{
title: "Introduction to Algorithms",
author: "Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein",
category: "Algorithms",
year: 1990
},
{
title: "Python Crash Course: A Hands-On, Project-Based Introduction to Programming",
author: "Eric Matthes",
category: "Python",
year: 2015
}
])

Insert a Single Document into ProgrammingBooks:


bookDB> db.ProgrammingBooks.insertOne({
title: "The Pragmatic Programmer: Your Journey to Mastery",
author: "David Thomas, Andrew Hunt",
category: "Software Development",
year: 1999
})

6. Read (Query) Operations


Find All Documents
bookDB> db.ProgrammingBooks.find().pretty()
[
{

14
Database Management System (BCS403)

_id: ObjectId('663eaaebae582498972202df'),
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'Robert C. Martin',
category: 'Software Development',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e0'),
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'JavaScript',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e1'),
title: 'Design Patterns: Elements of Reusable Object-Oriented Software',
author: 'Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides',
category: 'Software Design',
year: 1994
},
{
_id: ObjectId('663eaaebae582498972202e2'),
title: 'Introduction to Algorithms',
author: 'Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein',
category: 'Algorithms',
year: 1990
},
{
_id: ObjectId('663eaaebae582498972202e3'),
title: 'Python Crash Course: A Hands-On, Project-Based Introduction to Programming',
author: 'Eric Matthes',
category: 'Python',
year: 2015
},
{

15
Database Management System (BCS403)

_id: ObjectId('663eab05ae582498972202e4'),
title: 'The Pragmatic Programmer: Your Journey to Mastery',
author: 'David Thomas, Andrew Hunt',
category: 'Software Development',
year: 1999
}
]

Find Documents Matching a Condition


bookDB> db.ProgrammingBooks.find({ year: { $gt: 2000 } }).pretty()
[
{
_id: ObjectId('663eaaebae582498972202df'),
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'Robert C. Martin',
category: 'Software Development',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e0'),
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'JavaScript',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e3'),
title: 'Python Crash Course: A Hands-On, Project-Based Introduction to Programming',
author: 'Eric Matthes',
category: 'Python',
year: 2015
}
]

16
Database Management System (BCS403)

7. Update Operations
Update a Single Document
bookDB>db.ProgrammingBooks.updateOne(
{ title: "Clean Code: A Handbook of Agile Software Craftsmanship" },
{ $set: { author: "Robert C. Martin (Uncle Bob)" } }
)

//verify by displaying books published in year 2008


bookDB> db.ProgrammingBooks.find({ year: { $eq: 2008 } }).pretty()
[
{
_id: ObjectId('663eaaebae582498972202df'),
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'Robert C. Martin (Uncle Bob)',
category: 'Software Development',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e0'),
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'JavaScript',
year: 2008
}
]

Update Multiple Documents


bookDB> db.ProgrammingBooks.updateMany(
{ year: { $lt: 2010 } },
{ $set: { category: "Classic Programming Books" } }
)

//verify the update operation by displaying books published before year 2010
bookDB> db.ProgrammingBooks.find({ year: { $lt: 2010 } }).pretty()
[

17
Database Management System (BCS403)

{
_id: ObjectId('663eaaebae582498972202df'),
title: 'Clean Code: A Handbook of Agile Software Craftsmanship',
author: 'Robert C. Martin (Uncle Bob)',
category: 'Classic Programming Books',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e0'),
title: 'JavaScript: The Good Parts',
author: 'Douglas Crockford',
category: 'Classic Programming Books',
year: 2008
},
{
_id: ObjectId('663eaaebae582498972202e1'),
title: 'Design Patterns: Elements of Reusable Object-Oriented Software',
author: 'Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides',
category: 'Classic Programming Books',
year: 1994
},
{
_id: ObjectId('663eaaebae582498972202e2'),
title: 'Introduction to Algorithms',
author: 'Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein',
category: 'Classic Programming Books',
year: 1990
},
{
_id: ObjectId('663eab05ae582498972202e4'),
title: 'The Pragmatic Programmer: Your Journey to Mastery',
author: 'David Thomas, Andrew Hunt',
category: 'Classic Programming Books',
year: 1999
}

18
Database Management System (BCS403)

8. Delete Operations
Delete a Single Document
bookDB> db.ProgrammingBooks.deleteOne({ title: "JavaScript: The Good Parts" })
{ acknowledged: true, deletedCount: 1 }

Delete Multiple Documents


bookDB> db.ProgrammingBooks.deleteMany({ year: { $lt: 1995 } })
{ acknowledged: true, deletedCount: 2 }

Delete All Documents in the Collection:


--- delete all documents in a collection
bookDB> db.ProgrammingBooks.deleteMany({})
{ acknowledged: true, deletedCount: 3 }

--- verify by displaying the collection


bookDB> db.ProgrammingBooks.find().pretty()

Delete the Collection Using drop():


bookDB> show collections
ProgrammingBooks

bookDB> db.ProgrammingBooks.drop()
true

bookDB> show collections

bookDB>

19

You might also like