DBMS - LAB Manual
DBMS - LAB Manual
Lab Manual
Program : B.Sc [MPCs/MSCs/MECs] II Year Semester III
Course : Relational Database Management System Lab
Course code : BS.06.201.21.P
B.Sc. (Computer Science)
II Year/ III Semester
Theory Paper - III
Relational Database Management System
Scheme of Instruction Scheme of Examination
Total durations Hrs : 60 Max. Marks : 100
Hours/Week : 06(4T+2P) Internal Examination :30
Credits : 5 SBT : 10
Instruction Mode: Lecture +Practical External Examination :60
Course Code : BS.06.201.13.T Exam Duration : 3 Hrs
Course Objectives:
To impart the students with the knowledge on the database management systems, design
models, Normalization, Transaction management and Oracle in Creation and maintenance of
databases.
Course Outcomes:
At the end of the course the student will be able to
CO1: Understand and evaluate the database environment in an organization.
CO 2: Design and Develop database using SQL & PL/SQL.
CO 3:Understand and Design the ER Model utilized for developing a database.
CO 4:Applythe Normalization techniques to evaluate and correct table structures.
CO 5:Design real time databases using the concepts of Transaction Management, Concurrency
Control& Distributed Databases.
TextBook:
Thomas M. Connolly, Carolyn E. Begg, Database Systems–A Practical Approach to Design,
Implementation, and Management.
References:
1. Modern Database Management: Fred R. McFadden
2. Database Systems: Design, Implementation, and Management: C Coronel, S Morris,
Peter Rob
3. SQL, Pl/SQL: The Programming Language of Oracle: Ivan Bayross
6. Employee Database
An Enterprise wishes to maintain a database to automate its operations. Enterprise is divided into
certain departments and each department consists of employees. The following two tables
describes the automation schemas
Dept (deptno, dname, loc)
Emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
a) Update the employee salary by 15%, whose experience is greater than 10 years.
b) Delete the employees, who completed 30 years of service.
c) Display the manager who is having maximum number of employees working under him?
d) Create a view, which contain employee names and their manager
Employees table:
Query:
create table Employees (Emp_id NUMBER(6),First_name CHAR(25),Last_name
CHAR(20),Phone_No NUMBER(12),Hire_date DATE,Job_Id NUMBER(5),Emp_Salary
NUMBER(7),Comission_Pct NUMBER(5),manager_id NUMBER(5),Department_id
NUMBER(5));
1 row created.
1 row created.
SQL> insert into employees values(47403,'Raja','Shekhar',9848002255,'12-aug-
2004',303,58451,603,103,80);
1 row created.
1 row created.
1 row created.
Query:
sql>select * from employees;
a) Find out the employee id, names, salaries of all the employees
Query:
sql>select Emp_id,First_Name,Last_Name,Emp_Salary from employees;
c) Find the names of the employees who have a salary greater than or equal to 4800
Query:
sql>select * from employees where EMP_SALARY>=4800;
e) Find the names of the employees who works in departments 60,70 and 80
Query: sql>select * from employees where DEPARTMENT_ID IN(60,70,80);
Query:
sql>select DISTINCT(MANAGER_ID) from employees;
Lab Practical No: 2
Question:
Query:
create table Client_Master(Client_no varchar(6),Client_Name char(25),Client_Address
varchar(25),Client_City varchar(20),Client_State varchar(20),Balance_Due number(20));
Query:
sql>insert INTO CLIENT_MASTER Values('C123','Ramesh','L B Nagar', 'Hyderabad',
'Telangana', 7000);
sql>insert INTO CLIENT_MASTER Values('C124', 'Suresh', 'Dilsuknagar', 'Hyderabad',
'Telangana',6000);
sql>insert INTO CLIENT_MASTER Values('C125','Vignesh','Saroor nagar', 'Hyderabad',
'Telangana',3500);
sql>insert INTO CLIENT_MASTER Values('C126','Rajiv','A S Rao Nagar','Hyderabad',
'Telangana',4500);
sql>insert INTO CLIENT_MASTER Values('C127','Ranga', 'Vanasthalipuram','Hyderabad',
'Telangana',5478);
Create Teacher table with the following fields(Id,Name, DeptNo, Date of joining, DeptName,
Location, Salary)
Query :
SQL> create table teacher(Id number(2) primary key, name varchar2(20) not null, Deptno
number(2) not null, Deptname varchar2(20) not null, joinDate date not null, location varchar2(20)
not null, salary number(10,2) not null);
Sql> update teacher set salary= salary+(salary * 0.25) where Deptname= ‘mathematics';
Sql>commit;
Lab Practical No: 4
Question:
4 . (Exercise on order by and group by clauses) Create Sales table with the following fields( Sales
No, Salesname, Branch, Salesamount, DOB)
( a ) Insert five records
( b ) Calculate total salesamount in each branch
( c ) Calculate average salesamount in each branch .
( d ) Display all the salesmen, DOB who are born in the month of December as day in character
format i.e. 21-Dec-09
( e ) Display the name and DOB of salesman in alphabetical order of the month.
Sales Table:
Query:
Query:
Sql> insert into Sales VALUES(1020,'AutoMobiles','Hyderabad',68452,'28-JUL-1985');
Sql> insert into Sales VALUES(1021,'Electronics','Secunderabad',47850,'22-DEC-1995');
Sql> insert into Sales VALUES(1022,'Electronics','Secunderabad',44500,'03-JUN-1986');
Sql> insert into Sales VALUES(1023,'AutoMobiles','Hyderabad',74200,'28-SEP-1996');
Sql> insert into Sales VALUES(1024,'AutoMobiles','Hyderabad',54500,'28-OCT-1984');
Display the Sales table:
Query: sql>select * from Sales;
( d ) Display all the salesmen, DOB who are born in the month of December as day in character
format i.e. 21-Dec-09
Query:
SQL> select Sales_Name, DOB from sales where SUBSTR(DOB,4,3)='DEC';
( e ) Display the name and DOB of salesman in alphabetical order of the month.
Query:
sql>select Sales_Name,to_char(DOB,'MONTH') from sales Order by to_Char(DOB,'Day');
5. Create an Emp table with the following fields:(EmpNo, EmpName, Job,Basic, DA, HRA,PF,
GrossPay, NetPay)
(Calculate DA as 30% of Basic and HRA as 40% of Basic)
( a ) Insert Five Records and calculate GrossPay and NetPay.
( b ) Display the employees whose Basic is lowest in each department .
( c ) If NetPay is less than <Rs. 10,000 add Rs. 1200 as special allowances .
( d ) Display the employees whose GrossPay lies between 10,000 & 20,000
( e ) Display all the employees who earn maximum salary .
Employee Table
Query:
Sql> update employee set emp_netpay=emp_netpay+1200 where emp_netpay<30000;
( d ) Display the employees whose GrossPay lies between 10,000 & 20,000
Query: sql>select * from employee where emp_grosspay between 10000 and 20000;
Sql> create table dept(deptno number(3) primary key, dname varchar2(30) not null, loc
varchar2(30) not null);
Create Dept table : Emp (empno, ename, job, mgr, hiredate, sal, comm, deptno)
Sql>create table emp(empno number(3) primary key, ename varchar2(20) not null, job
varchar2(20) not null, mgr number(3) references emp(empno), hiredate date not null, sal
number(10,2) not null, comm Number(10,2), deptno number(3));
Sql> insert into emp (empno, ename ,job, hiredate, sal, deptno) values
(68319,’KAYLING’,’PRESIDENT’,’18-Nov-1991’,6000.00,101);
Sql> insert into emp (empno, ename ,job,mgr, hiredate, sal, deptno) values
(66928,’ BLAZE’,’ MANAGER’,68319,’09 -Jun-1991’, 2750.00,103);
Sql> insert into emp (empno, ename ,job,mgr, hiredate, sal, deptno) values
(67832,’ CLARE’,’ MANAGER’,68319,’18-Nov-1991’, 2550.00,101);
Sql> insert into emp (empno, ename ,job,mgr, hiredate, sal, deptno) values
(65646,’ JONAS’,’ MANAGER’,68319,’02-Apr-1991’, 2957.00,102);
Sql> insert into emp (empno, ename ,job,mgr, hiredate, sal, deptno) values
(67858,’ SCARLET’,’ ANALYST’, 65646,’19-Apr-1997’, 3100.00,102);
Sql> insert into emp (empno, ename ,job,mgr, hiredate, sal, deptno) values
(69062,’ FRANK’,’ ANALYST’, 65646,’03-Dec-1991’, 3100.00,102);
Sql> insert into emp (empno, ename ,job,mgr, hiredate, sal, deptno) values
(63679,’ SANDRINE’,’ CLERK’, 69062,’18-Dec-1990’, 900.00,102);
Sql> insert into emp (empno, ename ,job,mgr, hiredate, sal,comm, deptno) values
(64989,’ ADELYN’,’ SALESMAN’, 66928,’20-Feb-1991’, 1700.00,400, 103);
Sql> insert into emp (empno, ename ,job,mgr, hiredate, sal,comm, deptno) values
(65271,’ WADE’,’ SALESMAN’, 66928,’22-Feb-1991’, 1350.00,600, 103);
Sql> insert into emp (empno, ename ,job,mgr, hiredate, sal,comm, deptno) values
(66564,’ MADDEN’,’ SALESMAN’, 66928,’28-Sep-1991’, 1350.00,1500, 103);
Sql> insert into emp (empno, ename ,job,mgr, hiredate, sal,comm, deptno) values
(68454,’ TUCKER’,’ SALESMAN’, 66928,’08-Sep-1991’, 1600.00, 0, 103);
Sql> insert into emp (empno, ename ,job,mgr, hiredate, sal, deptno) values
(68736,’ ADNRES’,’ CLERK’, 67858,’23-May-1997’, 1200.00,102);
Sql> insert into emp (empno, ename ,job,mgr, hiredate, sal, deptno) values
(69000,’ JULIUS’,’ CLERK’, 66928,’03-Dec-1991’, 1050.00,103);
Sql> insert into emp (empno, ename ,job,mgr, hiredate, sal, deptno) values
(69324,’ MARKER’,’ CLERK’, 67832,’23-Jan-1992’, 1400.00,101);
Sql> insert into emp (empno, ename ,job,mgr, hiredate, sal, deptno) values
(69924,’ MARKER’,’ CLERK’, 67832,’23-Jan-1992’, 1400.00,104);
a)Update the employee salary by 15%, whose experience is greater than 30 years.
Query
Sql> update emp set sal=sal+(sal*0.15) where (sysdate-hiredate)/365>30;
c)Display the manager who is having maximum number of employees working under him?
Create view
Sql> create view mgrcount as select mgr, count(empno) total from emp group by mgr;
Sql> select mgr from mgrcount where total in (select max(total) from mgrcount);
Sql> create view employee_manager as select e1.ename ,e2.ename from emp e1, emp e2
where e1.mgr=e2.empno;
a)Determine the names of employee, who earn more than their managers.
sql>select e1.ename from emp e1, emp e2 where e1.mgr=e2.empno and e1.sal>e2.sal;
b)Determine the names of employees, who take highest salary in their departments.
SQL> create view maxsalaries as select max(sal) maxsalary, deptno from emp group by deptno;
Sql>select ename, sal from emp, maxsalaries where emp.deptno= maxsalaries.deptno and
sal=maxsalary;
d)Determine the employees, whose total salary is like the minimum Salary of any
department.
SQL> select empno, ename, sal from emp where sal in(select max(sal) from emp group by
deptno);
SQL> select dname from dept where deptno not in(select deptno from emp);
a)Display the employee details, departments that the departments are same in both the emp
and dept.
b)Display the employee name and Department name by implementing a left outer join.
d)Display the details of those who draw the salary greater than the average salary.
SQL> Select empno,sal from emp where sal> (select avg(sal) from emp);
Lab Practical No:9
Question:
DECLARE
Eid emp.empno%type:= 69000;
name emp.ename%type;
BEGIN
SELECT empno,ename INTO eid,name
FROM emp
WHERE empno = eid;
DBMS_OUTPUT.PUT_LINE ('empno: ' || eid);
DBMS_OUTPUT.PUT_LINE ('Name: '|| name);
EXCEPTION
WHEN no_data_found THEN
dbms_output.put_line('No such employee!');
WHEN others THEN
dbms_output.put_line('Error!');
END;
/
Output
empno: 69000
Name: JULIUS
Output
empno ename
69000 JULIUS
63679 SANDRINE
68736 ADNRES
69324 MARKER
69924 MARKER
67832 CLARE
66928 BLAZE
65646 JONAS
67858 SCARLET
SQL> /
Function created.
Program to invoke a function
SQL>
declare
x number;
begin
x:=fname(23,x);
dbms_output.put_line(x);
end;
Output
SQL> /
23
Executing Procedure:
procedure emp_proc2
is
begin
update emp
set sal=sal+sal*0.10
where comm<>sal*0.09;
DBMS_OUTPUT.PUT_LINE ('I am a procedure ');
end emp_proc2;
Output
2020
I am a procedure
Trigger created.
Trigger gets fired when update is performed on EMP table
SQL> update emp
set sal=500
where empno=69000;
Output
trigger fired
1 row updated.