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

Database Management Systems Lab Manual

The document outlines the curriculum for the Database Management Systems (BCS403) course at ACS College of Engineering, detailing various experiments and tasks related to database creation and manipulation. It includes instructions for creating tables, inserting records, using aggregate functions, and performing CRUD operations in both SQL and MongoDB. The document serves as a laboratory guide for students in the Computer Science and Engineering department.

Uploaded by

nishashanx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Database Management Systems Lab Manual

The document outlines the curriculum for the Database Management Systems (BCS403) course at ACS College of Engineering, detailing various experiments and tasks related to database creation and manipulation. It includes instructions for creating tables, inserting records, using aggregate functions, and performing CRUD operations in both SQL and MongoDB. The document serves as a laboratory guide for students in the Computer Science and Engineering department.

Uploaded by

nishashanx
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Database Management Systems(BCS403)

ACS COLLEGE OF ENGINEERING


DEPARTMENT OF CSE
LABORATORY OF CSE [2022 SCHEME]

SUBJECT NAME & CODE: DATABASE MANAGEMENT SYSTEMS[BCS403]

SCHEME-B.E : 2022 SCHEME

SEMESTER : IV

FACULTY INCHARGE: Charan M S

Department of CSE Page 1


Database Management Systems(BCS403)

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

INDEX

SL.NO EXPERIMENT PAGE


NO

Create a table called employee and execute the following EMPLOYEE


1. (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION) 3-7
Create a table called employee that contains attributes EMPNO, ENAME, JOB,
2. MGR, SAL & execute the following 8-11
Queries using aggregate functions (COUNT, AVG, MIN, MAX, SUM), Group
3. by, Order by, Employee (E_id, E_name, Age, Salary) 12-15
Create a row level trigger for the customers table that would fire for INSERT or
4. UPDATE or DELETE operations performed on the CUSTOMERS table. This 16-20
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. Declare the
5. Variables, Open the cursor & extract the values from the cursor. Close the cursor 21-23
Employee (E_id, E_name, Age, Salary)
Write a PL/SQL block of code using parameterized Cursor, that will merge the
6. data available in the newly created table N_RollCall with the data available in 24-30
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 &


7. perform basic CRUD (Create,
Read, Update & Delete) operations. Execute MongoDB basic
31-33
Queries using CRUD
operations.

Department of CSE Page 2


Database Management Systems(BCS403)

Experiment.1

Create a table called Employee & execute the following.

Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISION)


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:
Create a table called Employee & execute the following.
Employee (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
SQL> CREATE TABLE EMPLOYEE (
2 EMPNO NUMBER,
3 ENAME VARCHAR2(20),
4 JOB VARCHAR2(20),
5 MANAGER_NO NUMBER,
6 SAL NUMBER,
7 COMMISSION NUMBER);
Table created.

SQL> DESC EMPLOYEE;


Name Null? Type
----------------------------------------- -------------------- -----------------------------------------------
EMPNO NUMBER
ENAME VARCHAR2(20)
JOB VARCHAR2(20)
MANAGER_NO NUMBER
SAL NUMBER
COMMISSION NUMBER

Department of CSE Page 3


Database Management Systems(BCS403)

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


Login in as
User: system
Psw: ***
------------------------------------------------------------------------------------------------------------------------------------------------
SQL>sho user
USER is “SYSTEM”
------------------------------------------------------------------------------------------------------------------------------------------------
SQL>create user super1 identified by abcd1;
User created.
------------------------------------------------------------------------------------------------------------------------------------------------
* To grant permission to super1 user from system
SQL>grant create cluster, create indextype,
2 create operator, create procedure,
3 create sequence, create session, create synonym,
4 create table, create trigger, create type,
5 create view to super1;
Grant succeeded.

SQL> exit;

 exit from system user


----------------------------------------------------------------------------------------------------------------------------------------------
Login in as
User: super1
Psw: abcd1
---------------------------------------------------------------------------------------------------------------------------------------------
SQL>sho user
USER is “SUPER1”

SQL>select * from session_privs


2 order by privilege;

Department of CSE Page 4


Database Management Systems(BCS403)

PRIVILEGE
-------------------------------------
CREATE CLUSTER
CREATE INDEXTYPE
CREATE OPERATOR
CREATE PROCEDURE
CREATE SEQUENCE
CREATE SESSION
CREATE SYNONYM
CREATE TABLE
CREATE TRIGGER
CREATE TYPE
CREATE VIEW
11 rows selected.
----------------------------------------------------------------------------------------------------------------------------------------------
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.

SQL> INSERT INTO EMPLOYEE (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
2 VALUES (1, 'JAY', 'SALES', 100, 1000, 50);
1 row created.

SQL> INSERT INTO EMPLOYEE (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
2 VALUES (2, 'TIM', 'FINANCE', 100, 2000, 50);
1 row created.

SQL> INSERT INTO EMPLOYEE (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
2 VALUES (3, 'SAM', ’HR', 100, 5000, 50);
1 row created.

Department of CSE Page 5


Database Management Systems(BCS403)

SQL> SELECT * FROM EMPLOYEE;


EMPNO ENAME JOB MANAGER_NO SAL COMMISSION
---------- -------------------- -------------------- ---------------------- ---------- -------------------
1 JAY SALES 100 1000 50
2 TIM FINANCE 100 2000 50
3 SAM HR 100 5000 50

SQL>ROLLBACK;
Rollback complete.
SQL> SELECT * FROM EMPLOYEE;
No rows selected

3. Add primary key constraint and not constraint to the employee table.
SQL> ALTER TABLE EMPLOYEE ADD CONSTRAINT PK_EMPNO PRIMARY KEY(EMPNO);
Table altered.
SQL> ALTER TABLE EMPLOYEE MODIFY ENAME NOT NULL;
Table altered.

SQL>DESC EMPLOYEE;
Name Null? Type
--------------------------------------------------------- ---------------------- -------------------------------------------------------
EMPNO NOT NULL NUMBER
ENAME NOT NULL VARCHAR2(20)
JOB VARCHAR2(20)
MANAGER_NO NUMBER
SAL NUMBER
COMMISSION NUMBER

Department of CSE Page 6


Database Management Systems(BCS403)

4.Insert null values to the employee table and verify the result.
SQL> INSERT INTO EMPLOYEE (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
2 VALUES (NULL,’KIM’, ‘HR’, 100, 1000, 50);
VALUES (NULL, ‘KIM’, ‘HR’, 100, 5000, 50)
*
ERROR at line 2:
ORA-01400: cannot insert NULL into (“SCOTT”. “EMPLOYEE”.” EMPNO”)

SQL> INSERT INTO EMPLOYEE (EMPNO, ENAME, JOB, MANAGER_NO, SAL, COMMISSION)
2 VALUES (5, NULL, ‘HR’, 100, 5000, 50);
VALUES (5, NULL, ‘HR’, 100, 5000, 50)
*
ERROR at line 2:
ORA-01400: cannot insert NULL into (“SCOTT”. “EMPLOYEE”. “ENAME”)

Department of CSE Page 7


Database Management Systems (BCS403)

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

Solution:

Create a table called Employee that contain attributes EMPNO, ENAME, JOB, MGR, SAL &
execute the following.
SQL> CREATE TABLE EMPLOYEE (
2 EMPNO NUMBER,
3 ENAME VARCHAR2(20),
4 JOB VARCHAR2(20),
5 MGR NUMBER,
6 SAL NUMBER);
Table created.

SQL> DESC EMPLOYEE;


Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NUMBER
ENAME VARCHAR2(20)
JOB VARCHAR2(20)
MGR NUMBER
SAL NUMBER

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

Department of CSE Page 8


Database Management Systems (BCS403)

SQL> ALTER TABLE EMPLOYEE ADD COMMISSION NUMBER;


Table altered.

SQL> DESC EMPLOYEE;


Name Null? Type
----------------------------------------- --------------- ----------------------------------------
EMPNO NUMBER
ENAME VARCHAR2(20)
JOB VARCHAR2(20)
MGR NUMBER
SAL NUMBER
COMMISSION NUMBER

2. Insert any five records into the table.


SQL>INSERT INTO EMPLOYEE (EMPNO, ENAME, JOB, MGR, SAL, COMMISSION)
2 VALUES (101, ‘JAY’, ‘Sales’, 101, 1000, 500);
1 row created
SQL> INSERT INTO EMPLOYEE (EMPNO, ENAME, JOB, MGR, SAL, COMMISSION)
2 VALUES (102, ’TIM’,’Finance’,101,2000,500);
1 row created
SQL> INSERT INTO EMPLOYEE (EMPNO, ENAME, JOB, MGR, SAL, COMMISSION)
2 VALUES(103,’SAM’,’HR’,101,5000,500);
1 row created
SQL>INSERT INTO EMPLOYEE (EMPNO, ENAME, JOB, MGR, SAL, COMMISSION)
2 VALUES (104,’JOHN’, ‘Sales’,101,6000,500);
1 row created
SQL>INSERT INTO EMPLOYEE (EMPNO, ENAME, JOB, MGR, SAL, COMMISSION)
2 VALUES (105,’SIMI’, ‘FINANCE’,101,7000,500);
1 row created

Department of CSE Page 9


Database Management Systems (BCS403)

SQL>SELECT * FROM EMPLOYEE;


EMPNO ENAME JOB MGR SAL COMMISION
-------------- --------------- ------------ -------------------- ------------ ------------------------------------
101 JAY SALES 101 1000 500
102 TIM FINANCE 101 2000 500
103 SAM HR 101 5000 500
104 JOHN SALES 101 6000 500
105 SIMI FINANCE 101 7000 500

3. Update the column details of job

SQL>UPDATE EMPLOYEE SET JOB=’SALES’;


5 rows updated.
SQL>COMMIT;
Commit complete

SQL> SELECT * FROM EMPLOYEE;


EMPNO ENAME JOB MGR SAL COMMISSION
------------------ ------------------ ----------------- ------------- ------------------ -----------------------------
101 JAY SALES 101 1000 500
102 TIM SALES 101 2000 500
103 SAM SALES 101 5000 500
104 JOHN SALES 101 6000 500
105 SIMI SALES 101 7000 500

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

SQL> ALTER TABLE EMPLOYEE RENAME COLUMN ENAME TO EMP_NAME;


Table altered.

SQL> DESC EMPLOYEEE;

Department of CSE Page 10


Database Management Systems (BCS403)

Name Null? Type


--------------------------------------------- --------------- ----------------------------------------
EMPNO NUMBER
EMP_NAME VARCHAR2(20)
JOB VARCHAR2(20)
MGR NUMBER
SAL NUMBER
COMMISSION NUMBER

SQL> SELECT * FROM EMPLOYEE;


EMPNO EMP_NAME JOB MGR SAL COMMISSION
------------------ ------------------ ----------------- ------------- ------------------ -----------------------------
101 JAY SALES 101 1000 500
102 TIM SALES 101 2000 500
103 SAM SALES 101 5000 500
104 JOHN SALES 101 6000 500
105 SIMI SALES 101 7000 500

5.Delete the employee whose Empno is 105


SQL>DELETE FROM EMPLOYEE WHERE EMPNO=105;
1 row deleted.
SQL>COMMIT;
Commit complete.

SQL> SELECT * FROM EMPLOYEE;


EMPNO EMP_NAME JOB MGR SAL COMMISSION
------------------ ------------------ ----------------- ------------- ------------------ -----------------------------
101 JAY SALES 101 1000 500
102 TIM SALES 101 2000 500
103 SAM SALES 101 5000 500
104 JOHN SALES 101 6000 500

Department of CSE Page 11


Database Management Systems (BCS403)

Experiment – 3
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. 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.

Solution:
1.Create Employee table containing all Records E_id, E_name, Age, Salary.
SQL>CREATE TABLE EMPLOYEE3(
2 E_ID NUMBER,
3 E_NAME VARCHAR2(20),
4 AGE NUMBER,
5 SALARY NUMBER);
Table created.
SQL>DESC EMPLOYEE3;
NAME NULL? TYPE
------------------------------ ----------------------------------- ----------------------------------
E_ID NUMBER
E_NAME VARCHAR2(20)
AGE NUMBER
SALARY
NUMBER
SQL>INSERT INTO EMPLOYEE3 (E_ID, E_NAME, AGE, SALARY)
2 VALUES (101,’JAY’,20, 10000);
1 row created
SQL>INSERT INTO EMPLOYEE3 (E_ID, E_NAME, AGE, SALARY)
2 VALUES (102,’TIM’,30,20000);
1 row created

Department of CSE Page 12


Database Management Systems (BCS403)

SQL>INSERT INTO EMPLOYEE3 (E_ID, E_NAME, AGE, SALARY)


2 VALUES (103,’SAM’,35, 25000);
1 row created
SQL>INSERT INTO EMPLOYEE3 (E_ID, E_NAME, AGE, SALARY)
2 VALUES (104, ‘JOHN’, 40, 30000);
1 row created
SQL> INSERT INTO EMPLOYEE3 (E_ID, E_NAME, AGE, SALARY)
2 VALUES (105,’SIMI’, 45, 50000)
1 row created

SQL> SELECT * FROM EMPLOYEE3;


EMPNO EMP_NAME AGE SALARY
------------------ --------------------- ----------------- -----------------
101 JAY 20 10000
102 TIM 30 20000
103 SAM 35 25000
104 JOHN 40 30000
105 SIMI 45 50000

2. Count number of employee names from employee table

SQL> SELECT COUNT(E_NAME) FROM EMPLOYEE3;


COUNT(E_NAME)
----------------------------------
5
3. Find the Maximum age from employee table

SQL> SELECT MAX(AGE) FROM EMPLOYEE3;


MAX(AGE)
-----------------------------
45

Department of CSE Page 13


Database Management Systems (BCS403)

4. Find the Minimum age from employee table


SQL> SELECT MIN(AGE) FROM EMPLOYEE3;
MIN(AGE)
-------------------------
20
5. Find salaries of employee in Ascending Order.
SQL> SELECT * FROM EMPLOYEE3 ORDER BY SALARY;
EMPNO EMP_NAME AGE SALARY
------------------ --------------------- ----------------- -----------------
101 JAY 20 10000
102 TIM 30 20000
103 SAM 35 25000
104 JOHN 40 30000
105 SIMI 45 50000
SQL>SELECT * FROM EMPLOYEE3 ORDER BY SALARY DESC;
EMPNO EMP_NAME AGE SALARY
--------------------- ------------------------- ----------------- ----------------------
105 SIMI 45 50000
104 JOHN 40 30000
103 SAM 35 25000
102 TIM 30 20000
101 JAY 20 10000
SQL> SELECT * FROM EMPLOYEE3 ORDER BY SALARY ASC;
EMPNO EMP_NAME AGE SALARY
------------------ --------------------- ----------------- -----------------
101 JAY 20 10000
102 TIM 30 20000
103 SAM 35 25000
104 JOHN 40 30000
105 SIMI 45 50000

Department of CSE Page 14


Database Management Systems (BCS403)

6. Find grouped salaries of employees.


SQL>SELECT SUM(SALARY) FROM EMPLOYEE3;
SUM(SALARY)
----------------------------------
135000
SQL> SELECT SALARY
FROM EMPLOYEE 3
GROUP BY SALARY;
SALARY
----------------------
10000
20000
25000
30000

Department of CSE Page 15


Database Management Systems (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)

Solution:
CUSTOMERS (ID, NAME, AGE, ADDRESS, SALARY)

SQL>CREATE TABLE customers (

2 ID NUMBER,

3 NAME VARCHAR2(20),

4 AGE NUMBER,

5 ADDRESS VARCHAR2(20),

6 SALARY NUMBER);

Table created

SQL> DESC customers;

Name NULL? Type

------------------------------------------------ ---------------------------- -------------------------

ID NUMBER

NAME VARCHAR2(20)

AGE NUMBER

ADDRESS VARCHAR2(20)

SALARY NUMBER

SQL> INSERT INTO customers (ID, NAME, ADDRESS, SALARY)

VALUES(1001,’JAY’,20,’BANGALORE’,10000);

1 row created.

Department of CSE Page 16


Database Management Systems (BCS403)

SQL> INSERT INTO customers (ID, NAME, ADDRESS, SALARY)

VALUES(1002,’RIM’,30,’MYSORE’,20000);

1 row created.

SQL> INSERT INTO customers (ID, NAME, ADDRESS, SALARY)

VALUES(1003,’SAM’,35,’BANGALORE’,30000);

1 row created.

SQL> INSERT INTO customers (ID, NAME, ADDRESS, SALARY)

VALUES(1004,’JOHN’,25,’BANGALORE’,40000);

1 row created.

SQL> INSERT INTO customers (ID, NAME, ADDRESS, SALARY)

VALUES(1005,’SIMI’,40,’MYSORE’,15000);

1 row created.

SQL>COMMIT;

Commit complete.

SQL> SET LINESIZE 150;

SQL> SELECT * FROM customers;

ID NAME AGE ADDRESS SALARY

------ ---------------------- ----------- --------------------------------- ----------------------

1001 JAY 20 BANGALORE 10000

1002 TIM 30 MYSORE 20000

1003 SAM 35 BANGALORE 30000

1004 JOHN 25 BANGALORE 40000

1005 SIMI 40 MYSORE 15000

Department of CSE Page 17


Database Management Systems (BCS403)

SQL> ALTER TABLE customers ADD OLD_SALARY NUMBER;


Table altered.
SQL> ALTER TABLE customers ADD NEW_SALARY NUMBER;
Table altered.
SQL> ALTER TABLE customers ADD SAL_DIFF NUMBER;
Table altered.
SQL> DESC customers;
Name NULL? Type

------------------------------------------------ -------------- -------------------------

ID NUMBER

NAME VARCHAR2(20)

AGE NUMBER

ADDRESS VARCHAR2(20)

SALARY NUMBER
OLD_SALARY NUMBER
NEW_SALARY NUMBER
SAL_DIFF NUMBER

SQL>SELECT * FROM customers;


ID NAME AGE ADDRESS SALARY OLD_SALARY NEW_SALARY SAL_DIFF
------ ---------- --------- ----------------- ------------ --------------- ---------------------- ---------------

1001 JAY 20 BANGALORE 10000

1002 TIM 30 MYSORE 20000

1003 SAM 35 BANGALORE 30000

1004 JOHN 25 BANGALORE 40000

1005 SIMI 40 MYSORE 15000

Department of CSE Page 18


Database Management Systems (BCS403)

SQL> set server output on size 30000;


SQL> CREATE OR REPLACE TRIGGER display_salary_changes
2 BEFORE DELETE OR INSERT OR UPDATE ON customers
3 FOR EACH ROW
4 WHEN (NEW.ID > 0)
5 DECLARE
6 sal_diff number;
7 BEGIN
8 sal_diff := :NEW.Salary - :OLD. Salary;
9 :NEW.OLD_SALARY := :OLD.salary;
10 :NEW.NEW_SALARY := :NEW.salary;
11 :NEW.SAL_DIFF := sal_diff;
12 dbms_output.put_line('Old salary: ' || :OLD.salary);
13 dbms_output.put_line('New salary: ' || :NEW.salary);
14 dbms_output.put_line('Salary difference: ' || sal_diff);
15 END;
16 /
Trigger created.
SQL> UPDATE customers SET salary = 35000 WHERE ID = 1001;
Old salary: 10000
New salary: 35000
Salary difference: 25000
1 row updated.

SQL> UPDATE customers SET salary = 40000 WHERE ID = 1002;


Old salary: 20000
New salary: 40000
Salary difference: 20000
1 row updated.

Department of CSE Page 19


Database Management Systems (BCS403)

SQL> UPDATE customers SET salary = 20000 WHERE ID = 1001;


Old salary: 35000
New salary: 20000
Salary difference: -15000
1 row updated.

SQL> UPDATE customers SET salary = 20000 WHERE ID = 0;


0 rows updated.

SQL> commit;
Commit complete.
SQL> SELECT * FROM customers;
ID NAME AGE ADDRESS SALARY OLD_SALARY NEW_SALARY SAL_DIFF
---------- ---------- ---------- --------------- ------------ ------------------ --------------------- -----------------
1001 JAY 20 Bangalore 20000 35000 20000 -15000
1002 TIM 30 Mysore 40000 20000 20000 20000
1003 SAM 35 Bangalore 30000
1004 John 25 Bangalore 40000
1005 Simi 40 Mysore 15000

Department of CSE Page 20


Database Management Systems (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)
Solution:
SQL> CREATE TABLE Employee3(
2 E_name VARCHAR2(20),
3 E_id NUMBER,
4 Age NUMBER,
5 Salary NUMBER
6) ;
Table created.
SQL> DESC Employee3;
Name Null? Type
---------------------------------- --------------- ----------------------------
E_NAME VARCHAR2(20)
E_ID NUMBER
AGE NUMBER
SAL NUMBER

SQL> INSERT INTO Employee3 (E_id, E_name, Age, Salary) VALUES (101, 'JAY', 20, 10000);
1 row created.

SQL> INSERT INTO Employee3 (E_id, E_name, Age, Salary) VALUES (102, 'TIM', 30, 20000);
1 row created.

SQL> INSERT INTO Employee3 (E_id, E_name, Age, Salary) VALUES (103, 'SAM', 35, 25000);
1 row created.

SQL> INSERT INTO Employee3 (E_id, E_name, Age, Salary) VALUES (104, 'John', 40, 30000);
1 row created.

Department of CSE Page 21


Database Management Systems (BCS403)

SQL> INSERT INTO Employee3 (E_id, E_name, Age, Salary) VALUES (105, 'Simi', 45, 50000);
1 row created.

SQL> COMMIT;
Commit complete.
ID NAME AGE SALARY

------ ---------------------- ---------------- ----------------------------

101 JAY 20 10000

102 TIM 30 20000

103 SAM 35 30000

104 JOHN 25 40000

105 SIMI 40 50000

SQL> set server output on size 30000;


SQL> DECLARE

2 v_id employee3.E_id%type;
3 v_name employee3.E_name%type;
4 v_age employee3.age%type;
5 v_salary employee3.salary%type;
6

7 CURSOR cur_employee3 is

8 SELECT Eid, E_name, age, salary FROM employee3;


9 BEGIN
10 OPEN cur_employee3;
11 LOOP
12 FETCH cur_employee3 into v_id, v_name, v_age, v_salary;
13 EXIT WHEN cur_employee3%notfound;
14
15 dbms_output.put_line ('E_id:'||v_id||' E_name:'||v_name||' age:'||v_age||' salary:'||v_salary);

Department of CSE Page 22


Database Management Systems (BCS403)

16
17 END LOOP;
18 CLOSE cur_employee3;
19 END;
20 /
E_id :101 E_name: JAY age :20 salary :10000
E_id :102 E_name: TIM age :30 salary :20000
E_id :103 E_name: SAM age :35 salary :25000
E_id :104 E_name: John age :40 salary :30000
E_id :105 E_name: Simi age :45 salary :50000

PL/SQL procedure successfully completed.

Department of CSE Page 23


Database Management Systems (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.

Solution :

SQL> CREATE TABLE O_rollcall (

2 rno number PRIMARY KEY,

3 name varchar2(20),

4 address varchar2(30));

Table created.

SQL> INSERT INTO O_rollcall VALUES (1, 'Arun', 'Bengaluru');

1 row created.

SQL> INSERT INTO O_rollcall VALUES (2, 'Prashanth', 'Hosur');

1 row created.

SQL> INSERT INTO O_rollcall VALUES (3, 'Rekha', 'Mysore');

1 row created.

SQL> INSERT INTO O_rollcall VALUES (4, 'Prema', 'Bengaluru');

1 row created.

SQL> INSERT INTO O_rollcall VALUES (5, 'Priya', 'Mysore');

1 row created.

SQL> COMMIT;

Commit complete.

Department of CSE Page 24


Database Management Systems (BCS403)

SQL> SET LINESIZE 250;

SQL> SELECT * FROM O_rollcall;


RNO NAME ADDRESS
------------ -------------- ------------------------------
1 Arun BANGALORE
2 Prashanth HOSUR
3 Rekha MYSORE
4 Prema BANGALORE
5 Priya MYSORE

SQL> CREATE TABLE N_rollcall(


2 rno number PRIMARY KEY,
3 name varchar2(20),
4 address varchar2(30));
Table created.

SQL> SELECT * FROM N_rollcall;


no rows selected

SQL> SET serveroutput ON;


SQL> DECLARE
2 v_rno O_RollCall.rno%TYPE;
3 v_name O_RollCall.name%TYPE;
4 v_address O_RollCall.address%TYPE;
5
6 v_count number;
7 v_cursor_param O_RollCall.rno%TYPE:= 3;
8
9 /* Specify the rno to be copied from O_rollcall into N_rollcall table, or
10 Specify NULL if all the rno's needs to be copied which are not existing in N_rollcall table */

Department of CSE Page 25


Database Management Systems (BCS403)

11
12 --Parameterized cursor
13 CURSOR c_O_RollCall (p_rollnumber NUMBER)
14 IS SELECT rno,name,address
15 FROM O_RollCall a
16 WHERE (rno = p_rollnumber OR p_rollnumber IS NULL);
17
18 BEGIN
19 -- Open cursor
20 OPEN c_O_RollCall(v_cursor_param);
21 LOOP
22 FETCH c_O_RollCall INTO v_rno, v_name, v_address;
23 EXIT WHEN c_O_RollCall%NOTFOUND;
24
25 -- check if rno already exists in N_RollCall table
26 SELECT COUNT (*)
27 INTO v_count
28 FROM N_RollCall
29 WHERE rno = v_rno;
30
31 IF v_count = 0 THEN
32
33 INSERT INTO N_RollCall (rno, name, address)
34 VALUES (v_rno, v_name, v_address);
35
36 COMMIT;
37
38 DBMS_OUTPUT.PUT_LINE (‘Data merged successfully: rno:’||v_no);
39 ELSE
40 DBMS_OUTPUT.PUT_LINE ('rno skipped: '||v_rno);
41 END IF
42 END LOOP;

Department of CSE Page 26


Database Management Systems (BCS403)

43 END;
44 /
Data merged successfully! Rno:3
PL/SQL procedure successfully completed.
SQL> SELECT * FROM N_rollcall;
RNO NAME ADDRESS
--------- ------------------------------ ---------------------------
3 Rekha MYSORE
SQL> DECLARE
2 v_rno O_RollCall.rno%TYPE;
3 v_name O_RollCall.name%TYPE;
4 v_address O_RollCall.address%TYPE;
5
6 v_count number;
7 v_cursor_param O_RollCall.rno%TYPE:= 4;
8
9 /* Specify the rno to be copied from O_rollcall into N_rollcall table, or
10 Specify NULL if all the rno's needs to be copied which are not existing in N_rollcall table */
11
12 --Parameterized cursor
13 CURSOR c_O_RollCall (p_rollnumber NUMBER)
14 IS SELECT rno,name,address
15 FROM O_RollCall a
16 WHERE (rno = p_rollnumber OR p_rollnumber IS NULL);
17
18 BEGIN
19 -- Open cursor
20 OPEN c_O_RollCall(v_cursor_param);
21 LOOP
22 FETCH c_O_RollCall INTO v_rno, v_name, v_address;
23 EXIT WHEN c_O_RollCall%NOTFOUND;
24

Department of CSE Page 27


Database Management Systems (BCS403)

25 -- check if rno already exists in N_RollCall table


26 SELECT COUNT (*)
27 INTO v_count
28 FROM N_RollCall
29 WHERE rno = v_rno;
30
31 IF v_count = 0 THEN
32
33 INSERT INTO N_RollCall (rno, name, address)
34 VALUES (v_rno, v_name, v_address);
35
36 COMMIT;
37
38 DBMS_OUTPUT.PUT_LINE (‘Data merged successfully: rno:’||v_no);
39 ELSE
40 DBMS_OUTPUT.PUT_LINE ('rno skipped: '||v_rno);
41 END IF
42 END LOOP;
43 END;
44 /
Data merged successfully! Rno:4
PL/SQL procedure successfully completed.
SQL> SELECT * FROM N_rollcall;
RNO NAME ADDRESS-
--------- ------------------------------ ---------------------------
3 Rekha MYSORE
4 Prema BANGALORE

SQL> DECLARE
2 v_rno O_RollCall.rno%TYPE;
3 v_name O_RollCall.name%TYPE;
4 v_address O_RollCall.address%TYPE;
5

Department of CSE Page 28


Database Management Systems (BCS403)

6 v_count number;
7 v_cursor_param O_RollCall.rno%TYPE:= 3;
8
9 /* Specify the rno to be copied from O_rollcall into N_rollcall table, or
10 Specify NULL if all the rno's needs to be copied which are not existing in N_rollcall table */
11
12 --Parameterized cursor
13 CURSOR c_O_RollCall (p_rollnumber NUMBER)
14 IS SELECT rno, name, address
15 FROM O_RollCall a
16 WHERE (rno = p_rollnumber OR p_rollnumber IS NULL);
17
18 BEGIN
19 -- Open cursor
20 OPEN c_O_RollCall(v_cursor_param);
21 LOOP
22 FETCH c_O_RollCall INTO v_rno, v_name, v_address;
23 EXIT WHEN c_O_RollCall%NOTFOUND;
24
25 -- check if rno already exists in N_RollCall table
26 SELECT COUNT (*)
27 INTO v_count
28 FROM N_RollCall
29 WHERE rno = v_rno;
30
31 IF v_count = 0 THEN
32
33 INSERT INTO N_RollCall (rno, name, address)
34 VALUES (v_rno, v_name, v_address);
35
36 COMMIT;
37

Department of CSE Page 29


Database Management Systems (BCS403)

38 DBMS_OUTPUT.PUT_LINE (‘Data merged successfully: rno:’||v_no);


39 ELSE
40 DBMS_OUTPUT.PUT_LINE ('rno skipped: '||v_rno);
41 END IF
42 END LOOP;
43 END;
44 /
Data merged successfully! rno:1
Data merged successfully! rno:2
rno skipped :3
rno skipped :4
Data merged successfully! rno:5
SQL> SELECT * FROM N_rollcall;
RNO NAME ADDRESS
--------- ---------------- ------------------------------
3 Rekha MYSORE
4 Prema BANGALORE
1 Arun BANGALORE
2 Prashanth HOSUR
5 Priya MYSORE

Department of CSE Page 30


Database Management Systems (BCS403)

Experiment 7:

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

//Create operation

Department of CSE Page 31


Database Management Systems (BCS403)

//Read Operation

//Update operations

Department of CSE Page 32


Database Management Systems (BCS403)

//Delete operation

Department of CSE Page 33

You might also like