0% found this document useful (0 votes)
32 views30 pages

LabManual - Gopala Krishna

The document is a laboratory manual for the Database Management System course (BCS403) at the Cambridge Institute of Technology. It includes detailed experiments and SQL commands for creating and manipulating employee and customer tables, using aggregate functions, triggers, cursors, and NoSQL database operations. Each experiment outlines specific tasks and SQL commands to be executed by students to enhance their understanding of database management concepts.

Uploaded by

suhasy756
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)
32 views30 pages

LabManual - Gopala Krishna

The document is a laboratory manual for the Database Management System course (BCS403) at the Cambridge Institute of Technology. It includes detailed experiments and SQL commands for creating and manipulating employee and customer tables, using aggregate functions, triggers, cursors, and NoSQL database operations. Each experiment outlines specific tasks and SQL commands to be executed by students to enhance their understanding of database management concepts.

Uploaded by

suhasy756
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

Cambridge Institute of Technology Lab Manual Database Management System

North Campus [BCS403]

Off International Airport Road, Kundana, Bengaluru - 562110

DATABASE MANAGEMENT SYSTEM


LABORATORY MANUAL

SUBJECT CODE: BCS403


SEMESTER: IV

Prepared by:
Prof. Gopala Krishna P
Assistant Professor
Department of Cyber Security
CIT-NC

[ DEPARTMENT OF CYBER] 1
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

Experiments
1. Create a table called Employee & execute the following.
Employee(EMPNO,ENAME,JOB, MANAGER_NO, SAL,
COMMISSION)
a) Create a user and grant all permissions to the user.
b) Insert any three records in the employee table containing
attributes EMPNO,ENAME JOB, MANAGER_NO, SAL,
COMMISSION and use rollback. Check the result.
c) Add primary key constraint and not null constraint to the
employee table.
d) 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.
a) Add a column commission with domain to the
Employeetable.
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.
3. Queries using aggregate functions
(COUNT,AVG,MIN,MAX,SUM),Group by, Order by.
Employee(E_id, E_name, Age, Salary)
a) Create Employee table containing all Records E_id,
E_name, Age, Salary.
b) Count number of employee names from employeetable
c) Find the Maximum age from employee table.
d) Find the Minimum age from employeetable.
e) Find salaries of employee in Ascending Order.
f) 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)

[ DEPARTMENT OF CYBER] 2
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [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. 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 Database MangoDB & perform
basic CRUD (Create, Read, Update & Delete) operations.
Execute MangoDB basic Queries using CRUD operations.

[ DEPARTMENT OF CYBER] 3
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

EXPERIMENT 1: Create a table called Employee & execute the


following. Employee(EMPNO,ENAME,JOB, MANAGER_NO, SAL,
COMMISSION)
a) Create a user and grant all permissions to the user.
b) Insert any three records in the employee table containing
attributes EMPNO,ENAME JOB, MANAGER_NO, SAL,
COMMISSION and use rollback. Check the result.
c) Add primary key constraint and not null constraint to the
employee table.
d) Insert null values to the employee table and verify the result.

******************** SQL commands *********************


-- Step 1: Create a User and Grant Permissions
CREATE USER employee_user IDENTIFIED BY password123;
GRANT CONNECT, RESOURCE, DBA TO employee_user;

-- Step 2: Connect as the new user and create the Employee table
CONNECT employee_user/password123;
CREATE TABLE Employee (
EMPNO NUMBER,
ENAME VARCHAR2(50),
JOB VARCHAR2(50),
MANAGER_NO NUMBER,
SAL NUMBER,
COMMISSION NUMBER
);

[ DEPARTMENT OF CYBER] 4
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

-- Insert three records and rollback


INSERT INTO Employee (EMPNO, ENAME, JOB,
MANAGER_NO, SAL, COMMISSION) VALUES (101, 'Alice',
'Manager', 1001, 60000, 5000);
INSERT INTO Employee (EMPNO, ENAME, JOB,
MANAGER_NO, SAL, COMMISSION) VALUES (102, 'Bob',
'Developer', 1002, 55000, 3000);
INSERT INTO Employee (EMPNO, ENAME, JOB,
MANAGER_NO, SAL, COMMISSION) VALUES (103, 'Charlie',
'Analyst', 1003, 50000, 2000);
ROLLBACK;
SELECT * FROM Employee; -- Verify that rollback was successful

-- Step 3: Add Primary Key and Not Null Constraints


ALTER TABLE Employee
ADD CONSTRAINT PK_EMPNO PRIMARY KEY (EMPNO);
ALTER TABLE Employee
MODIFY (EMPNO NOT NULL, ENAME NOT NULL, JOB NOT
NULL, MANAGER_NO NOT NULL, SAL NOT NULL);

-- Step 4: Attempt to insert null values


INSERT INTO Employee (EMPNO, ENAME, JOB,
MANAGER_NO, SAL, COMMISSION) VALUES (104, NULL,
'Clerk', 1004, 40000, NULL); -- This should fail

-- Verify that the insert with null values failed


SELECT * FROM Employee;

[ DEPARTMENT OF CYBER] 5
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

OUTPUT:

[ DEPARTMENT OF CYBER] 6
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

[ DEPARTMENT OF CYBER] 7
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

EXPERIMENT 2: Create a table called Employee that contain


attributes EMPNO,ENAME,JOB, MGR,SAL & execute the following.
a) Add a column commission with domain to the Employeetable.
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.
Delete the employee whose Empno is 105.

***************** SQL Commands ********************


-- Step 1: Create the Employee Table
CREATE TABLE Employee
(EMPNO NUMBER,
ENAME VARCHAR2(50),
JOB VARCHAR2(50),
MGR NUMBER,
SAL NUMBER
);

-- Step 2: Add a column commission with domain


ALTER TABLE Employee
ADD COMMISSION NUMBER;

-- Step 3: Insert five records into the table


INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, SAL,
COMMISSION) VALUES (101, 'Alice', 'Manager', 1001, 60000,
5000);

[ DEPARTMENT OF CYBER] 8
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, SAL,


COMMISSION) VALUES (102, 'Bob', 'Developer', 1002, 55000,
3000);
INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, SAL,
COMMISSION) VALUES (103, 'Charlie', 'Analyst', 1003, 50000,
2000);
INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, SAL,
COMMISSION) VALUES (104, 'David', 'Clerk', 1004, 40000, 1000);
INSERT INTO Employee (EMPNO, ENAME, JOB, MGR, SAL,
COMMISSION) VALUES (105, 'Eve', 'Sales', 1005, 45000, 1500);

-- Step 4: Update the column details of job


UPDATE Employee
SET JOB = 'Senior Developer'
WHERE ENAME = 'Bob';

-- Step 5: Rename the column commission to bonus


ALTER TABLE Employee
RENAME COLUMN COMMISSION TO BONUS;

-- Step 6: Delete the employee whose Empno is 105


DELETE FROM Employee
WHERE EMPNO = 105;

-- Verify the final state of the table


SELECT * FROM Employee;

[ DEPARTMENT OF CYBER] 9
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

OUTPUT:

[ DEPARTMENT OF CYBER] 10
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

[ DEPARTMENT OF CYBER] 11
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

EXPERIMENT 3: Queries using aggregate functions (COUNT,


AVG, MIN, MAX, SUM), Group by, Order by. Employee(E_id,
E_name, Age, Salary)
a) Create Employee table containing all Records E_id, E_name,
Age, Salary.
b) Count number of employee names from employeetable
c) Find the Maximum age from employee table.
d) Find the Minimum age from employeetable.
e) Find salaries of employee in Ascending Order.
f) Find grouped salaries of employees.
***************** SQL Commands ********************
-- Step 1: Create the Employee Table and Insert Records

CREATE TABLE Employee


( E_id NUMBER PRIMARY
KEY,E_name VARCHAR2(50),
Age NUMBER,
Salary NUMBER
);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (1,
'Alice', 30,
60000);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (2,
'Bob', 25,
55000);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (3,
'Charlie',
35, 70000);

[ DEPARTMENT OF CYBER] 12
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

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


'David', 28,
50000);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (5,
'Eve', 40,
75000);

-- Step 2: Count number of employee names

SELECT COUNT(E_name) AS EmployeeCount FROM Employee;

-- Step 3: Find the maximum age from the employee table

SELECT MAX(Age) AS MaxAge FROM Employee;

-- Step 4: Find the minimum age from the employee table

SELECT MIN(Age) AS MinAge FROM Employee;

-- Step 5: Find salaries of employees in ascending order


SELECT E_name, Salary FROM Employee ORDER BY Salary
ASC;

-- Step 6: Find grouped salaries of employees


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

[ DEPARTMENT OF CYBER] 13
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

OUTPUT:

[ DEPARTMENT OF CYBER] 14
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

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)
***************** SQL Commands ********************

-- Creating the CUSTOMERS table


CREATE TABLE CUSTOMERS
(ID INT PRIMARY KEY,
NAME VARCHAR(50),
AGE INT,
ADDRESS VARCHAR(255),
SALARY NUMBER
);
SET SERVEROUTPUT ON;

-- Creating the row-level trigger


CREATE OR REPLACE TRIGGER salary_difference_trigger
BEFORE INSERT OR UPDATE OR DELETE ON CUSTOMERS
FOR EACH ROW
DECLARE
old_salary NUMBER;
new_salary NUMBER;
BEGIN

[ DEPARTMENT OF CYBER] 15
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

IF INSERTING OR UPDATING THEN


old_salary := :OLD.SALARY;
new_salary := :NEW.SALARY;
ELSIF DELETING THEN
old_salary := :OLD.SALARY;
new_salary := NULL; -- Set to NULL for deleted rows
END IF;
IF old_salary IS NOT NULL AND new_salary IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE('Salary difference: ' || (new_salary -
old_salary));
ELSIF old_salary IS NULL AND new_salary IS NOT NULL THEN
DBMS_OUTPUT.PUT_LINE('New salary: ' || new_salary);
ELSIF old_salary IS NOT NULL AND new_salary IS NULL THEN
DBMS_OUTPUT.PUT_LINE('Salary deleted');
ELSE
DBMS_OUTPUT.PUT_LINE('No salary changes');
END IF;
END;
/
-- Inserting some sample data
INSERT INTO CUSTOMERS (ID, NAME, AGE, ADDRESS,
SALARY) VALUES (1, 'John Doe', 30, '123 Main St', 50000);
INSERT INTO CUSTOMERS (ID, NAME, AGE, ADDRESS,
SALARY) VALUES (2, 'Jane Smith', 25, '456 Elm St', 60000);

[ DEPARTMENT OF CYBER] 16
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

-- Updating a record to test the trigger


UPDATE CUSTOMERS SET SALARY = 55000 WHERE ID = 1;
-- Deleting a record to test the trigger
DELETE FROM CUSTOMERS WHERE ID = 2;
OUTPUT:

[ DEPARTMENT OF CYBER] 17
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

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. Employee(E_id, E_name,
Age, Salary)
***************** SQL Commands ********************
-- Creating the Employee table
CREATE TABLE Employee (
E_id NUMBER PRIMARY KEY,
E_name VARCHAR2(100),
Age NUMBER,
Salary NUMBER
);
SET SERVEROUTPUT ON;

-- Inserting the sample data values in the table


INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (1,
'Alice', 30, 50000);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (2,
'Bob', 25, 45000);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (3,
'Charlie', 35, 55000);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (4,
'David', 28, 48000);
INSERT INTO Employee (E_id, E_name, Age, Salary) VALUES (5,
'Eve', 32, 52000);

COMMIT;

[ DEPARTMENT OF CYBER] 18
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

-- Create and Use the Cursor in PL/SQL


-- Declare a PL/SQL block to work with the cursor
DECLARE

-- Declare the cursor


CURSOR emp_cursor IS
SELECT E_id, E_name, Age, Salary FROM Employee;

-- Declare variables to hold the values from the cursor


v_E_id Employee.E_id%TYPE;
v_E_name Employee.E_name%TYPE;
v_Age Employee.Age%TYPE;
v_Salary Employee.Salary%TYPE;
BEGIN

-- Open the cursor


OPEN emp_cursor;

-- Loop through the rows returned by the cursor


LOOP

-- Fetch each row into the variables


FETCH emp_cursor INTO v_E_id, v_E_name, v_Age,
v_Salary;

[ DEPARTMENT OF CYBER] 19
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

-- Exit the loop when no more rows are found


EXIT WHEN emp_cursor%NOTFOUND;

-- Display the values (or you can process them as needed)


DBMS_OUTPUT.PUT_LINE('E_id: ' || v_E_id || ', E_name: ' ||
v_E_name || ', Age: ' || v_Age || ', Salary: ' || v_Salary);
END LOOP;

-- Close the cursor


CLOSE emp_cursor;
END;
/
OUTPUT:

[ DEPARTMENT OF CYBER] 20
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

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.
***************** SQL Commands ********************
-- Create the new roll call table
CREATE TABLE N_RollCall
( ID NUMBER PRIMARY
KEY,Name VARCHAR2(100),
Roll_Number NUMBER
);

-- Create the old roll call table


CREATE TABLE O_RollCall
( ID NUMBER PRIMARY
KEY,Name VARCHAR2(100),
Roll_Number NUMBER
);
SET SERVEROUTPUT ON;

-- Insert sample data into N_RollCall


INSERT INTO N_RollCall (ID, Name, Roll_Number) VALUES (1,
'Alice', 101);
INSERT INTO N_RollCall (ID, Name, Roll_Number) VALUES (2,
'Bob', 102);

[ DEPARTMENT OF CYBER] 21
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

INSERT INTO N_RollCall (ID, Name, Roll_Number) VALUES (3,


'Charlie', 103);

-- Insert sample data into O_RollCall


INSERT INTO O_RollCall (ID, Name, Roll_Number) VALUES (2,
'Bob', 102);
INSERT INTO O_RollCall (ID, Name, Roll_Number) VALUES (4,
'David', 104);
COMMIT;

-- Create the PL/SQL Block with Parameterized Cursor


DECLARE
-- Declare a cursor with parameters
CURSOR n_rollcall_cursor IS
SELECT ID, Name, Roll_Number FROM N_RollCall;

-- Declare variables to hold the values from the cursor


v_ID N_RollCall.ID%TYPE;
v_Name N_RollCall.Name%TYPE;
v_Roll_Number N_RollCall.Roll_Number%TYPE;
BEGIN

-- Open the cursor


OPEN n_rollcall_cursor;

[ DEPARTMENT OF CYBER] 22
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

-- Loop through the rows returned by the cursor


LOOP

-- Fetch each row into the variables


FETCH n_rollcall_cursor INTO v_ID, v_Name,
v_Roll_Number;

-- Exit the loop when no more rows are found


EXIT WHEN n_rollcall_cursor%NOTFOUND;

-- Check if the record already exists in O_RollCall


BEGIN
DECLARE
v_count NUMBER;
BEGIN
SELECT COUNT(*) INTO v_count
FROM O_RollCall
WHERE ID = v_ID;

-- If the record does not exist, insert it into O_RollCall


IF v_count = 0 THEN
INSERT INTO O_RollCall (ID, Name, Roll_Number)
VALUES (v_ID, v_Name, v_Roll_Number);
END IF;
END;

[ DEPARTMENT OF CYBER] 23
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

END;
END LOOP;

-- Close the cursor


CLOSE n_rollcall_cursor;

-- Commit the changes


COMMIT;

-- Output the results


DBMS_OUTPUT.PUT_LINE('Data merged successfully.');

-- Display the merged data from O_RollCall


FOR rec IN (SELECT ID, Name, Roll_Number FROM
O_RollCall ORDER BY ID) LOOP
DBMS_OUTPUT.PUT_LINE('ID: ' || rec.ID || ', Name: ' ||
rec.Name || ', Roll_Number: ' || rec.Roll_Number);
END LOOP;
END;
/
OUTPUT:

[ DEPARTMENT OF CYBER] 24
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

EXPERIMENT 7: Install an Open Source NoSQL Database


MangoDB & perform basic CRUD (Create, Read, Update & Delete)
operations. Execute MangoDB basic Queries using CRUD operations.
***************** SQL Commands ********************
Resource:
✓ https://www.mongodb.com/try/download/community
✓ https://www.mongodb.com/try/download/shell?jmp=docs
✓ https://www.mongodb.com/docs/manual/tutorial/install-
mongodb-on-windows/
Procedure:
1. Install MangoDB Community Edition NoSQL database
(windows 64-bit):
a. Download the MongoDB Community .msi installer from
the following link:
https://www.mongodb.com/try/download/community
b. Run the installer.
c. Choose the complete setup.
d. Select Install MongoD as a Service followed by Run
service as Network Service user.

e. Install MongoDB Compass and when ready click Install.

[ DEPARTMENT OF CYBER] 25
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

f. Download mongosh .msi installer from the


link:
https://www.mongodb.com/try/download/shell

g. Double click the downloaded installer file and follow the


prompts to install mongosh.
h. Add the mongosh binary to your PATH environment
variable.
i. Open control panel and in system and security
category click system.
ii. Click Advanced system settings then click
Environment Variables.
iii. In the System variables section, select Path and click
Edit. The Edit environment variable modal displays.
iv. Click New and add the filepath to your mongosh
binary(bin). Here in this case filepath was
“C:\Program Files\MongoDB\Server\7.0\bin”
v. Click OK on each modal to confirm your changes.
i. Verify that your PATH environment variable is correctly
configured to find mongosh, open command prompt and
enter:
✓ mongod.exe
✓ mongosh.exe

[ DEPARTMENT OF CSE ] 27
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

2. Initial basic commands


a. To create a database and switch to it: use <database name>
// e.g. use mydatabase
b. To show database list: show dbs
c. To create collection: db.createCollection(“<name>”)
//e.g. db.createCollection(“users”)
d. To see collections: show collections
e. To verify CRUD operations: db.<collection_name>.find()
// e.g. db.users.find()
Perform basic CRUD (create, read, update & delete) operations.
Insert a document
db.users.insertOne({ name: "Alice", age: 25, city:
"New York" })
db.users.insertMany([{ name: "Bob", age: 30, city:
"Los Angeles" }, { name: "Charlie", age: 35, city:
"Chicago" }])
db.users.find() --Verify
Read
db.users.findOne({ name: "Alice" })
db.users.find({ age: { $gt: 25 } })
c. Update
i. db.users.updateOne({ name: "Alice" }, { $set: { age:
26 } })
ii. db.users.updateMany({ age: { $lt: 30 } }, { $set: { city:
"San Francisco" } })
iii. db.users.find() --Verify
d. Delete
i. db.users.deleteOne({ name: "Alice" })
ii. db.users.deleteMany({ age: { $gte: 30 } })
iii. db.users.find() --Verify

[ DEPARTMENT OF CSE ] 28
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

OUTPUT:
Basic commands

Create/Insert commands

[ DEPARTMENT OF CSE ] 29
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

Read Commands:

Update commands:

[ DEPARTMENT OF CSE ] 30
Cambridge Institute of Technology Lab Manual Database Management System
North Campus [BCS403]

Delete commands:

[ DEPARTMENT OF CSE ] 31

You might also like