You are on page 1of 27

Issue No.

: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 1 of 51
Lab Maual

DIT UNIVERSITY DEHRADUN


DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

Lab Manual for the Academic Year 2018-19

Subject : Advanced DBMS Lab


Subject code : CS604
Course coordinator : Dr. Srabanti Maji
HOD : Dr.Vishal Bharti

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 2 of 51
Lab Maual

Table of Contents

S. No Content Page No.

1 System Requirements 4

2 Lab Objectives 4

Experiment Manual

Experiment Title of experiment


Page No.
No.
Create a database in Oracle and perform Insert Operation on the table
1 5
Create a database in Oracle and perform Update Operation on the
2 table 7

Create a database in Oracle and perform Delete Operation on the table


3 9
Create a database in Oracle and perform Rename Operation on the
4 table 10

Create a table in the already existing database and perform various


5 join operations 11

Write a program in PLSQL to show the initialization of variables


6 14
Write a Program in PLSQL to show the scope of variables
7 16
Write a Program in PLSQL to show the constants of PLSQL
8 17
Write a Program in PLSQL to create a function and call the function
9 18
Write a Program in PLSQL to print the maximum of 2 numbers
10 20
Write a Program in PLSQL to print the Control Statements.
11 22

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 3 of 51
Lab Maual

GENERAL INSTRUCTIONS FOR LABORATORY CLASSES:-


DO’S

(1) Without Prior permission do not enter into the Laboratory.

(2) While entering into the LAB students should carry ID cards.

(3) The Students should come with proper uniform.

(4) Students should come with the record note book into the laboratory.

(5) Students should maintain silence inside the laboratory.

(6) After completing the laboratory exercise, make sure to shut-down the system properly.

DONT’S

(1) Students bringing the bags inside the laboratory.

(2) Students wearing slippers/shoes insides the laboratory.

(3) Students using the computers in an improper way.

(4) Students bringing pen drive or other secondary storage device inside the laboratory.

(5) Students using mobile phones inside the laboratory.

(6) Students making noise inside the laboratory.

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 4 of 51
Lab Maual

System Requirements

1. Intel based desktop PC of Intel Core I-5 or faster processor with at least 2-4 GB RAM and

10-500 GB free disk space.

2. Latest Oracle11g supporting Database.

Lab Objectives

1. Create database and table using Oracle.

2. Build Database and then table, perform update, rename and delete operation on that data .

3. Perform join operation on multiple tables.

4. Implementing PL/SQL.

5. Creating functions in PL/SQL.

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 5 of 51
Lab Maual

Practical – 1

Create a Database and create a table “Employee” with the fields (empno, ename, job, mgr,
hiredate,sal,comm, deptno) and perform INSERT operation

Solution:There are two ways to insert values in a table. In the first


method there is no need to specify the column name where the data
will be inserted, you need only their values. Second method specifies
both the column name and the values to be inserted

Step1: Create a table named as „EMP‟

CREATE TABLE emp(


empno int(4) not null primary key,
ename varchar(50) not null,
job varchar(50) not null,
mgr int(4),
hiredate date,
sal decimal(10,2),
comm decimal(10,2),
deptno int(2) not null);

Step2: Insert values to the fields of the table

insert into emp values (7369,'SMITH','CLERK',7902,TO_DATE('93/6/13','YY/MM/DD'),800,0.00,20);


insert into emp values
(7499,'ALLEN','SALESMAN',7698,TO_DATE('1998/8/15','YY/MM/DD'),1600,300,30);
insert into emp values
(7521,'WARD','SALESMAN',7698,TO_DATE('96/3/26','YY/MM/DD'),1250,500,30);
insert into emp values
(7566,'JONES','MANAGER',7839,TO_DATE('95/10/31','YY/MM/DD'),2975,null,20);
insert into emp values
(7698,'BLAKE','MANAGER',7839,TO_DATE('92/6/11','YY/MM/DD'),2850,null,30);
insert into emp values
(7782,'CLARK','MANAGER',7839,TO_DATE('93/5/14','YY/MM/DD'),2450,null,10);
desc emp;

Output: Displaying the structure of the table

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 6 of 51
Lab Maual

Step3: Perform SELECT Operation on the records of the table

SELECT * FROM emp;

Output: Displaying all records from the table

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 7 of 51
Lab Maual

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 8 of 51
Lab Maual

Practical- 2

Create a Database and create a table “Employee” with the fields (empno, ename, job, mgr,
hiredate,sal,comm, deptno) and perform INSERT operation

Solution:SQL UPDATE statement is used to change the data of the records held by tables.
Which rows is to be update, it is decided by a condition. To specify condition, we use WHERE
clause.

Step1:Perform SELECT Operation on the records of the table

SELECT * FROM emp;

Step2: Perform UPDATE operation on the emp table at attribute Salary

SET sal = 3500


WHERE empno=7566;

Step3: Perform select operation to see the updated records

SELECT empno,ename,sal
FROM emp;

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 9 of 51
Lab Maual

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 10 of 51
Lab Maual

Practical- 3

Create a Database and create a table “Employee” with the fields (empno, ename, job, mgr,
hiredate,sal,comm, deptno) and perform Delete operation.

Solution:The DELETE statement is used to delete rows from a table. If you want to remove a
specific row from a table you should use WHERE condition.

Step1: Perform SELECT Operation on the records of the table

Step2: Perform DELETE operation on the Attribute EMP no

DELETE FROM emp


WHERE empno=7698;
SELECT * FROM emp;

OUTPUT: Display the updated table

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 11 of 51
Lab Maual

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 12 of 51
Lab Maual

Practical – 4

Create a Database and create a table “Employee2” with the fields (empno, ename, job,
mgr, hiredate,sal,comm, deptno) and perform Rename operation.

Solution:SQL RENAME TABLE syntax is used to change the name of a table. Sometimes, we
choose non-meaningful name for the table. So it is required to be changed.

Step1: Perform SELECT Operation on the records of the table

Step2: Perform Rename operation on the table EMPLOYEE2

desc EMPLOYEE2;
alter table EMPLOYEE2 rename to EMP;
desc EMP;

OUTPUT:

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 13 of 51
Lab Maual

Practical- 5
Create a table “Employee”with the fields (empno, ename, job, mgr, hiredate,sal,comm,
deptno) in the already existing database and perform various join operations of the
database

Solution: The SQL JOIN clause takes records from two or more tables in a
database and combines it together. In the SQL outer JOIN all the content of the
both tables are integrated together either they are matched or not.Left outer
join (also known as left join): this join returns all the rows from left table combine
with the matching rows of the right table. If you get no matching in the right table it
returns NULL values.Right outer join (also known as right join): this join returns
all the rows from right table are combined with the matching rows of left table .If
you get no column matching in the left table .it returns null value.

Step1: Create Table:

Create table Employee (emp_id int, e_name varchar(30),age int, dep_id int,Salary int,);
desc Employee;
Output: Showing structure of the table

Step2: Insert values into the table

Insert into Employee(emp_id,e_name,age,dep_id,Salary)values('101','Alex','23','121','23000');


Insert into Employee(emp_id,e_name,age,dep_id,Salary)values('102','Alexa','25','131','25000');
Insert into Employee(emp_id,e_name,age,dep_id,Salary)values('103','Sam','23','141','23000');
Insert into Employee(emp_id,e_name,age,dep_id,Salary)values('104','John','25','151','27000');
Insert into Employee(emp_id,e_name,age,dep_id,Salary)values('105','Gold','23','161','22000');

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 14 of 51
Lab Maual

Step3: Display the records

Select* from Employee;


desc Employee;

Step4: Create another table “Department”

Create table Department(dept_id int,dep_name varchar(30),employee_name varchar(30));


desc Department;

Step5: Insert values into the table

Insert into Department(dept_id, dep_name)values('121','CSE');


Insert into Department(dept_id, dep_name)values('131','ME');
Insert into Department(dept_id, dep_name)values('141','EE');

Step6: Display the records and perform Inner Join Operation

Select Employee.emp_id,Employee.e_name,Department.dep_name
From Employee
INNER JOIN Department
ON Employee.dep_id=Department.dept_id;

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 15 of 51
Lab Maual

Step7: Create table College

Create table College(Colg_id int,Colg_name varchar(30),Department varchar(30));


desc College;
Insert into College(Colg_id,Colg_name,Department)values('101','DIT','CSE');
Insert into College(Colg_id,Colg_name,Department)values('121','GBIT','ME');
Insert into College(Colg_id,Colg_name,Department)values('131','DU','Pharmacy');
Insert into College(Colg_id,Colg_name,Department)values('141','GBU','EE');
Insert into College(Colg_id,Colg_name,Department)values('151','DT','EN');

Create table Student(Stu_id int,College_id, S_name varchar(30),Collge varchar(30));


desc Student;
Drop table Student;

Step8: Create table Student

Create table Student(Stu_id int,College_id int, S_name varchar(30),Collge varchar(30));


desc Student;
Insert into Student(Stu_id, College_id,S_name,College)values('161','121','Alexa','DIT');
Insert into Student(Stu_id, College_id,S_name,College)values('171','131','Alex','GBIT');
Insert into Student(Stu_id, College_id,S_name,College)values('181','121','Sam','DIT');
Insert into Student(Stu_id, College_id,S_name,College)values('191','141','Gold','BHU');

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 16 of 51
Lab Maual

Step9: Perform Left Outer Join

Select College.Colg_id,College.Colg_name, Student.S_name


From College
LEFT OUTER JOIN Student
ON College.Colg_id=Student.College_id;

Output:

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 17 of 51
Lab Maual

Practical-6
Write a program in PLSQL to perform the initialization of Variables in PL/SQL

Solution:A variable is a meaningful name which facilitates a programmer to store data


temporarily during the execution of code. It helps you to manipulate data in PL/SQL programs

Declare
a integer:=30;
b integer:=40;
c integer;
f real;
Begin
c:=a+b;
dbms_output.put_line('Value of c: ' || c);
f := 100.0/3.0;
dbms_output.put_line('Value of f: ' || f);
END;

Output:

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 18 of 51
Lab Maual

Practical-7
Write a Program in PLSQL to show the Scope of Global and Local Variables

Solution: PL/SQL allows nesting of blocks. A program block can contain another
inner block. If you declare a variable within an inner block, it is not accessible to
an outer block. There are two types of variable scope:
Local Variable: Local variables are the inner block variables which are not
accessible to outer blocks.
Global Variable: Global variables are declared in outermost block.

DECLARE
-- Global variables
num1 number := 195;
num2 number := 285;
BEGIN
dbms_output.put_line('Outer Variable num1: ' || num1);
dbms_output.put_line('Outer Variable num2: ' || num2);
DECLARE
-- Local variables
num1 number := 395;
num2 number := 185;
BEGIN
dbms_output.put_line('Inner Variable num1: ' || num1);
dbms_output.put_line('Inner Variable num2: ' || num2);
END;
END;

Output:

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 19 of 51
Lab Maual

Practical- 8
Write a Program in PLSQL by using PLSQL Constants

Solution:A constant is a value used in a PL/SQL block that remains unchanged throughout the
program. It is a user-defined literal value. It can be declared and used instead of actual values.

Syntax: constant_name CONSTANT Datatype: = VALUE;


DECLARE
-- Constant declaration
pi constant number := 3.141592654;
-- Other declarations
radius number(5,2);
dia number(5,2);
Circumference number (7, 2);
area number (10, 2);
BEGIN
-- processing
radius := 9.5;
dia := radius * 2;
circumference := 2.0 * pi * radius;
area := pi * radius * radius;
-- output
dbms_output.put_line('Radius: ' || radius);
dbms_output.put_line('Diameter: ' || dia);
dbms_output.put_line('Circumference: ' || circumference);
dbms_output.put_line('Area: ' || area);
END;

Output:

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 20 of 51
Lab Maual

Practical -9
Write a Program in PLSQL to create a Function and perform calling of the function

Solution: The PL/SQL Function is very similar to PL/SQL Procedure. The main difference
between procedure and a function is, a function must always return a value

Syntax: CREATE [OR REPLACE] FUNCTION function_name [parameters]


[(parameter_name [IN | OUT | IN OUT] type [, ...])]
RETURN return_datatype
{IS | AS}
BEGIN
< function_body >
END [function_name];

Program to Create a Function:


create or replace function adder(n1 in number, n2 in number)
return number
is
n3 number(8);
begin
n3 :=n1+n2;
return n3;
end;

Output:

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 21 of 51
Lab Maual

Program to Call a Function:


DECLARE
n3 number(2);
BEGIN
n3 := adder(11,22);
dbms_output.put_line('Addition is: ' || n3);
END;

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 22 of 51
Lab Maual

Practical-10

Write a Program in PLSQL to print the Maximum of the Two Numbers using Functions:
Solution:
DECLARE
a number;
b number;
c number;
FUNCTION findMax(x IN number, y IN number)
RETURN number
IS
z number;
BEGIN
IF x > y THEN
z:= x;
ELSE
Z:= y;
END IF;

RETURN z;
END;
BEGIN
a:= 23;
b:= 45;

c := findMax(a, b);
dbms_output.put_line(' Maximum of (23,45): ' || c);
END;

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 23 of 51
Lab Maual

Output:

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 24 of 51
Lab Maual

Practical- 11

Write a PLSQL Program to illustrate the concept of various Control Statements (Loops).

Solution:The PL/SQL loops are used to repeat the execution of one or more
statements for specified number of times. These are also known as iterative
control statements. There are 4 types of PL/SQL Loops.
 Basic Loop / Exit Loop
 While Loop
 For Loop
 Cursor For Loop

Program for Exit Loop:

PL/SQL exit loop is used when a set of statements is to be executed at least once
before the termination of the loop. There must be an EXIT condition specified in
the loop, otherwise the loop will get into an infinite number of iterations.

DECLARE
i NUMBER := 1;
BEGIN
LOOP
EXIT WHEN i>10;
DBMS_OUTPUT.PUT_LINE(i);
i := i+1;
END LOOP;
END;

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 25 of 51
Lab Maual

Output:

Program of For Loop:


PL/SQL for loop is used when when you want to execute a set of statements for a predetermined
number of times. The loop is iterated between the start and end integer values.

BEGIN
FOR k IN 1..10 LOOP
-- note that k was not declared
DBMS_OUTPUT.PUT_LINE(k);
END LOOP;
END;

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 26 of 51
Lab Maual

Output:

Program for While Loop;


PL/SQL while loop is used when a set of statements has to be executed as long as a condition is
true, the While loop is used. The condition is decided at the beginning of each iteration and
continues until the condition becomes false.

DECLARE
i INTEGER := 1;
BEGIN
WHILE i <= 10 LOOP
DBMS_OUTPUT.PUT_LINE(i);
i := i+1;
END LOOP;
END;

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji
Issue No.: Date:
Advanced DBMS (CS604) Rev No.: Nil Rev. Date: Nil
Clause: Nil Page: 27 of 51
Lab Maual

Output:

Prepared by: Reviewed by: Approved by:


Dr.Srabanti Maji

You might also like