DBMS Lab Manual - BIT
DBMS Lab Manual - BIT
BCS403
Manual
IV-SEMESTER
Prepared By:
Prof. Sahana M.
DATABASE MANAGEMENT SYSTEM LABORATORY
SEMESTER – IV
Course Code: BCS403 IA Marks: 25
Number of Lecture Hours/Week 0:2:0 Total Hours: 20
CREDITS – 02
Course Learning objectives:
Lab Programs:
Sl.
Name of Program
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 contain attributes EMPNO, ENAME
1
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 a table called Employee that contains attributes EMPNO, ENAME, JOB, MGR,
SAL & execute the following.
1. Add a column commission with domain to the Employee table.
2 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.
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
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 INSERT or UPDATE
or DELETE operations performed on the CUSTOMERS table. This trigger will display
4
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. Declare the
5 variables, Open the cursor & extract the values from the cursor. Close the cursor.
Employee(E_id, E_name, Age, Salary)
Write a PL/SQL block of code using parameterized Cursor that will merge the data
6 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.
Install an Open Source NoSQL Data base MongoDB & perform basic CRUD (Create,
7
Read, Update & Delete) operations. Execute MongoDB basic Queries using CRUD.
Laboratory outcomes:
operations.
25 marks lab component is split up into 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.
To create a new user
Login in to sql.
1
Program 1
Create a table called Employee & execute the following.
Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
2. Insert any three records in the employee table contain attributes EMPNO, ENAME JOB,
MANAGER_NO, SAL, COMMISSION and use rollback. Check the result.
• INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL,COMMISSION)
VALUES (1, 'John Doe', 'Manager', NULL, 5000, 1000);
• INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
VALUES (2, 'Jane Smith', 'Clerk', 1, 3000, NULL);
2
• INSERT INTO Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
VALUES (3, 'Alice Johnson', 'Analyst', 1, 4000, 500);
ROLLBACK;
Since none of the row entry was committed all the entry got rolled back.
To save the inserted values or any other operations use Commit; command
If we perform rollback after commit we can only roll back till the commit point.
3
3. Add primary key constraint and not null constraint to the employee table.
• ALTER TABLE Employee ADD CONSTRAINT PK_Employee PRIMARY KEY (EMPNO);
• ALTER TABLE Employee MODIFY (ENAME NOT NULL, JOB NOT NULL, SAL NOT
NULL);
4. Insert null values to the employee table and verify the result.
4
Program 2
Create a table called Employee that contains attributes EMPNO, ENAME, JOB, MGR, SAL &
execute the following.
5
3. Update the column details of job.
• UPDATE Employee SET JOB = 'Sales Manager' WHERE ENAME = 'Mike';
6
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.
CREATE TABLE Employee (
E_id INT,
E_name VARCHAR(50),
Age INT,
Salary DECIMAL(10, 2) );
7
3. Find the Maximum age from employee table.
8
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)
First on serveroutput and then write trigger after trigger is successful do insert delete and update.
The SET SERVEROUTPUT command specifies whether output from the DBMS_OUTPUT message
buffer is redirected to standard output.
10
Program 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.
DECLARE
CURSOR emp_cursor IS
SELECT E_id, E_name, Age, Salary FROM Employee;
-- Process the fetched data (you can perform any actions here)
DBMS_OUTPUT.PUT_LINE('Employee ID: ' || v_E_id || ', Name: ' || v_E_name || ', Age: ' || v_Age ||
', Salary: ' || v_Salary);
END LOOP;
-- Close the cursor
CLOSE emp_cursor;
END;
/
12
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.
id int,
data varchar(50));
id int,
data varchar(50));
13
DECLARE
CURSOR n_rollcall_cursor IS
SELECT * FROM N_RollCall;
v_n_id N_RollCall.id%TYPE;
v_n_data N_RollCall.data%TYPE;
v_exists NUMBER := 0;
BEGIN
OPEN n_rollcall_cursor;
LOOP
FETCH n_rollcall_cursor INTO v_n_id, v_n_data;
EXIT WHEN n_rollcall_cursor%NOTFOUND;
CLOSE n_rollcall_cursor;
END;
/
14
Program 7
Install an Open Source NoSQL Data base MongoDB & perform basic CRUD (Create, Read, Update
& Delete) operations. Execute MongoDB basic Queries using CRUD operations.
https://www.youtube.com/watch?v=dEm2AS5amyA
Create operation
15
// To display data from collection
> db.details.find()
{ "_id" : ObjectId("5d7d3daf315728b4998f522e"), "rno" : 1, "name" : "Bhavana" }
{ "_id" : ObjectId("5d7d3f28315728b4998f522f"), "name" : "Amit", "rno" : 2 }
{ "_id" : ObjectId("5d7d3f56315728b4998f5230"), "rno" : 3, "email_id" : "[email protected]" }
_id is 12 bytes hexadecimal number unique for every document in a collection.
// trying to insert data with duplicate _id, it will not accept as _id is primary key field
> db.details.insert({_id:1,rno:5,name:"Reena"})
E11000 duplicate key error index: db1.emp.$_id_ dup key: { : 1.0 }
> db.emp.insert({rno:10,name:'Ankit',hobbies:['singing','cricket','swimming',’music’],age:21})
Find operation
//To find document having city as Nashik(as city is key of address key specify "address.city"
> db. details.find({"address.city":"Nashik"})
{ "_id" : ObjectId("5d83c04aa44331f62bcd8370"), "rno" : 8, "address" : { "area" : "College Road", "city" :
"Nashik", "state" : "MH" }, "name" : "Arya" }
16
Update operation
//upsert use to update document if condition found otherwise insert document with updates
values.
> db. details.update({rno:50},{$set:{rno:55}},{upsert:true})
db. details.update({rno:50},{$set:{rno:55,location:’kerala’}},{upsert:true})
Delete operation
db. details.deleteMany({})
db. details.deleteMany( { location: "chennai" } )
db. details.deleteOne( { location: "chennai" } )
17