0% found this document useful (0 votes)
26 views22 pages

B SC III Sem DBMS Record Programs

Good

Uploaded by

taheerali156
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)
26 views22 pages

B SC III Sem DBMS Record Programs

Good

Uploaded by

taheerali156
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/ 22

II B SC (DBMS Record Programs)

SQL Programs

1 Draw E-R diagrams for train services in a railway station.

2 Draw E-R diagram for hospital administration.

Creation of college database and establish relationships between


3 tables

4 Write a view to extract details from two or more tables

5 Write a program to demonstrate of Aggregate functions

6 Creation of Reports based on different queries

PL/SQL Programs

7 Write a PL/SQL program to check the given number is Armstrong


are not.
Write a PL/SQL program to check the given number is a
8
palindrome or not.
Write a PL/SQL program to generate multiplication of tables
9
for 2, 4,6.
Write a stored procedure to process students results
10

Write a program to demonstrate function


11
Write a program to generate employee pay slip using
12 PL/SQL.

S V Degree College Page-1


II B SC (DBMS Record Programs)

Problem 1: -

Draw ER diagrams for train services in a railway station.

Procedure:-

S.NO ENTITY ATTRIBUTES

1 USER U_id, password, Security_quest

2 Train_ Status Status_id, Booked_seat, Avail_seat, Wait_seat

3 Train Train_id, T_name, Train_type, Avail_class

4 Station Station_id, Stattion_name

5 Route Stop_no, Arr_time, Depart_time

S V Degree College Page-2


II B SC (DBMS Record Programs)

E-R Diagram:-

S V Degree College Page-3


II B SC (DBMS Record Programs)

Problem 2: -

Draw ER diagram for hospital administration.

Procedure: -

S.NO ENTITY ATTRIBUTES

1 Hospital Hosp-id, HAddress, Hos-Name, Pat-id, Doc-id

Pat-id, PName, PAddress, PDiagnosis, Record-


2 Patient
id, Hosp-id

3 Doctor Doc-id, DName, Qualification, Salary, Hosp-id

Medical Record-id, Problem, Date_of_examination, Pat-


4
Record id

S V Degree College Page-4


II B SC (DBMS Record Programs)

E-R Diagram: -

S V Degree College Page-5


II B SC (DBMS Record Programs)

Problem 3: -
Creation of college database and establish relationships between tables

College whishes to computerize their operations by using the following relations.


Student (Snum:Integer, Sname:String, Level :String, Age:Integer)
Class (Cname:String, Hour:Integer, Room:String, Fid:Integer)
Enrolled (Snum:Integer, Cname:String)
Faculty (Fid:Integer, Fname:String, Deptid:Integer)
Depart (Deptid:Integer, Dname:String, Loc:String)

Solve the following Queries: -


1) Find the names of all juniors (Level=Jr) who are enrolled in a class taught by Smith.
2) Print the level and Average Age of the Students for that level for each level.
3) Print the level and Average Age of the Students for that level, for all levels except juniors.
4) Find the names of the Student who are not enrolled in any class.

Procedure: -

Table Creation: -

 SQL> Create Table Student(Snum number(10) Primary Key,Sname varchar2(10), Level1


varchar2(3),Age number(2));
 SQL> Create Table Depart(Deptid number(10) Primary Key,Dname varchar2(10), Loc
varchar2(10));
 SQL> Create Table Faculty(Fid number(10) Primary Key,Fname varchar2(10), Deptid
number(10) References Depart(Deptid));
 SQL> Create Table Class(Cname varchar2(10) Primary Key, Hour number(10), Room
varchar2(10),Fid number(10) References Faculty(Fid));
 SQL> Create Table Enrolled(Snum number(10) References Student(Snum), Cname
varchar2(10) References Class(Cname),Primary Key (Snum,Cname));

Insert Rows into Student Table: -


SQL> Insert into Student values (1,'aa','Jr',20);
SQL> Insert into Student values (2,'bb','Jr',18);
SQL> Insert into Student values (3,'cc','Sr',21);
SQL> Insert into Student values (4,'dd','SSr',22);
SQL> Select * From Student;
SNUM SNAME LEVEL1 AGE
---------- ---------- ---------- ----------
1 aa Jr 20
2 bb Jr 18
3 cc Sr 21
4 dd SSr 22

Insert Rows into Depart Table: -


S V Degree College Page-6
II B SC (DBMS Record Programs)

SQL> Insert into Depart values (10,'Computers','Bangalore');


SQL> Insert into Depart values (20,'Physics','Hyderabad');
SQL> Select * From Depart;
DEPTID DNAME LOC
---------- ---------- ----------
10 Computers Bangalore
20 Physics Hyderabad

Insert Rows into Faculty Table: -


SQL> Insert into Faculty values (5000,'Smith', 10);
SQL> Insert into Faculty values (6000,'Mithra', 10);
SQL> Select * From Faculty;
FID FNAME DEPTID
-------- ---------- ----------
5000 Smith 10
6000 Mithra 10
Insert Rows into Class Table: -
SQL> Insert into Class values (‘c1’,9,’C100’,5000);
SQL> Insert into Class values (‘c2’,10,’C101’,5000);
SQL> Select * From Class;
CNAME HOUR ROOM FID
---------- - --------- ---------- ----------
c1 9 C100 5000
c2 10 C101 5000
Insert Rows into Enrolled Table: -
SQL> Insert into Enrolled values (1,’c1’);
SQL> Insert into Enrolled values (2,’c2’);
SQL> Insert into Enrolled values (1,’c2’);
SQL> Select * From Enrolled;
SNUM CNAME
---------- ----------
1 c1
1 c2
2 c2

1) Find the names of all juniors (Level=Jr) who are enrolled in a class taught by ‘Smith’.
SQL> Select Distinct(Sname) From Student,Faculty,Class,Enrolled where Faculty.Fid=Class.Fid and
Class.Cname=Enrolled.Cname and Enrolled.Snum=Student.Snum and Level1='Jr' and
Fname='Smith';
SNAME
----------
aa
bb

2) Print the level and Average Age of the Students for that level for each level.
SQL> Select Distinct(Level1),Avg(Age) From Student Group By Level1;
LEV AVG(AGE)
------ ---------
S V Degree College Page-7
II B SC (DBMS Record Programs)

Jr 19
Sr 21
SSr 22
3) Print the level and Average Age of the Students for that level, for all levels except juniors.
SQL> Select Distinct(Level1),Avg(Age) From Student Group By Level1 Having Level1!='Jr';
LEV AVG(AGE)
------ -------------
Sr 21
SSr 22

4) Find the names of the Student who are not enrolled in any class.
SQL> Select Distinct(Sname) From Student,Enrolled Where Student.Snum not in (Select
Enrolled.Snum from Enrolled);
SNAME
----------
cc
dd

S V Degree College Page-8


II B SC (DBMS Record Programs)

Problem 4: -
Write a view to extract details from two or more tables.
Procedure: -

Creating table Dept: -

SQL> Create Table Dept (Deptno number(2) Primary Key, Dname varchar2(10));

Insert Rows into an Dept Table: -

SQL> Insert into Dept Values (&Deptno,'&Dname');

DEPTNO DNAME
------------ -----------
10 Accounting
20 Marketing
30 Finance
40 Sales

Creating table Employee: -

Create Table Employee (Empno number(4) Primary Key, Ename varchar2(10), Job varchar2(10), Hiredate
date, Sal number(7,2),deptno number(2) references dept(deptno) );

Insert Rows into an Employee Table: -

SQL> Insert into Emp1 Values (101,’Sudheer’,’Clerk’,’17-Dec-1980’,10000,10);


SQL> Insert into Emp1 Values (102,’Ramesh’,’Salesman’,’20-Feb-1991’,16000,20);
SQL> Insert into Emp1 Values (103,’Rajesh’,’Salesman’,’22-Feb-1995’,12000,30);
SQL> Insert into Emp1 Values (104,’Naveen’,’Manager’,’02-April-1981’,14000,40);

Creating View: -

SQL> Create view empview as (select empno,ename,sal, Employee.deptno,dname from


Employee,dept where Employee.deptno=dept.deptno;

SQL> select * from empview;

EMPNO ENAME SAL DEPTNO DNAME


101 keerthi 10000 10 Accounting
102 Ramesh 16000 20 Marketing
103 Rajesh 12000 30 Finance
104 Naveen 14000 40 Sales

S V Degree College Page-9


II B SC (DBMS Record Programs)

Problem 5: - Write a program to demonstrate of Aggregate functions.

Procedure: -
Table Creation: -
SQL> Create Table Employees (Empno number(4) Primary Key, Ename varchar2(10), Job varchar2(10),
Hiredate date, Sal number(7,2));

Insert Rows into an Employees Table: -


SQL> Insert into Emp1 Values (101,’Sudheer’,’Clerk’,’17-Dec-1980’,10000);
SQL> Insert into Emp1 Values (102,’Ramesh’,’Salesman’,’20-Feb-1991’,16000);
SQL> Insert into Emp1 Values (103,’Rajesh’,’Salesman’,’22-Feb-1995’,12000);
SQL> Insert into Emp1 Values (104,’Naveen’,’Manager’,’02-April-1981’,14000);

SQL> Select * From Employees;


EMPNO ENAME JOB HIREDATE SAL
----------- ---------- ---------- --------------- ----------
101 Sudheer Clerk 17-DEC-80 10000
102 Ramesh Salesman 20-FEB-91 16000
103 Rajesh Salesman 22-FEB-95 12000
104 Naveen Manager 02-APR-81 14000

Aggregate functions: -

Count: SELECT COUNT (*) FROM Employees;

Output: 4

Sum: SELECT SUM (SAL) FROM EMPLOYEES;

Output: 52000

Avg: SELECT AVG(SAL) FROM EMPLOYEES;

Output: 13000

Max: SELECT MAX(SAL) FROM EMPLOYEES;

Output: 16000

Min : SELECT MAX(SAL) FROM EMPLOYEES;

Output: 10000

S V Degree College Page-10


II B SC (DBMS Record Programs)

Problem 6: -
Creation of Reports based on different queries

Procedure : -

Table Creation: -

SQL> Create Table Emp (Empno number(4) Primary Key, Ename varchar2(10), Job varchar2(10), Hiredate
date, Sal number(7,2));

Insert Rows into an Emp Table: -

SQL> Insert into Emp1 Values (101,’Sudheer’,’Clerk’,’17-Dec-1980’, 10000);


SQL> Insert into Emp1 Values (102,’Ramesh’,’Salesman’,’20-Feb-1991’, 16000);
SQL> Insert into Emp1 Values (103,’Rajesh’,’Salesman’,’22-Feb-1995’, 12000);
SQL> Insert into Emp1 Values (104,’Naveen’,’Manager’,’02-April-1981’, 14000);

SQL> Select * From Emp;

EMPNO ENAME JOB HIREDATE SAL


----------- ---------- ---------- --------------- ----------
101 Sudheer Clerk 17-DEC-80 10000
102 Ramesh Salesman 20-FEB-91 16000
103 Rajesh Salesman 22-FEB-95 12000
104 Naveen Manager 02-APR-81 14000

1) List All Employees Names and their Salaries, Whose Salary lies between 12000/- and 16000/- both
inclusive.
SQL> Select Ename,Sal From Emp Where Sal between 12000 and 16000;
ENAME SAL
---------- ----------
Ramesh 16000
Rajesh 12000
Naveen 14000

2) List all Employees, which starts with either ‘P’ or ‘S’.

SQL> Select Ename From Emp where Ename like 'P%' or Ename like 'S%';

ENAME
Sudheer

3) List all Jobs available in Employee table.

SQL> Select Distinct (job) from Emp1;


JOB
----------
Clerk
S V Degree College Page-11
II B SC (DBMS Record Programs)

Manager
Salesman

4) List Minimum, Maximum, Average Salaries of Employee.

SQL> Select Min (Sal), Max (Sal), Avg (Sal) From Emp;

MIN (SAL) MAX (SAL) AVG (SAL)


------------- -------------- -------------
10000 16000 13000

S V Degree College Page-12


II B SC (DBMS Record Programs)

Problem 7: -

Pl/sql program to check the given number is Armstrong are not.

Declare

R number;
S number:=0;
N number;
Temp number;

Begin

N :=&number;
Temp:=n;
While N > 0 loop
R:=mod(N,10);
S:=S+(R*R*R);
N:=floor(N/10);
End loop;
If S=Temp then
Dbms_output.put_line(‘The Given Number is an Armstrong Number’);
Else
Dbms_output.put_line(‘The Given Number is Not an Armstrong Number’);
End if;
End;
/

Output:

S V Degree College Page-13


II B SC (DBMS Record Programs)

Problem 8: -
pl/sql program to check the given number is a palindrome or not.

Declare

X number;
Rev number;
N number;
Temp number;

Begin

N :=#
Rev :=0;
Temp:=N;
While N>0 loop
X:=mod(N,10);
Rev :=(Rev*10)+X;
N:=floor(N/10);
End loop;
If Temp=Rev then
Dbms_output.put_line(‘The given number is a palindrome number’);
Else
Dbms_output.put_line(‘The given number is not a palindrome number’);
End if;

End;
/

Output:

S V Degree College Page-14


II B SC (DBMS Record Programs)

Problem 9: -
pl/sql program to generate multiplication of tables for 2, 4,6.

Declare

I number;
J number;
Begin

I :=2;
While i<=6 loop
For j in 1..10 loop
Dbms_output.put_line(I||’ ‘||’*’||’ ‘||J||’ ‘||’=’||I*J);
End loop;
I:=I+2;
Dbms_output.put_line(‘------------‘);
End loop;

End;
/

Output:

S V Degree College Page-15


II B SC (DBMS Record Programs)

Problem 10: -
Write a stored procedure to process student’s results.

Procedure: -

I. For Pass: all marks>=40.


II. For ‘I class’: total %> 59.
III. For ‘II class’: total% between >40 and <60.
IV. For ‘III class’: total%=40.

Table Used: std

Name Null? Type


------------------------------------------------- -------- ----------------------
STDNO NOT NULL NUMBER (3)
STDNAME VARCHAR2(15)
M1 NUMBER(3)
M2 NUMBER(3)
M3 NUMBER(3)
TOT NUMBER(3)
PERCENT NUMBER(4)
RESULT VARCHAR2(5)
CLASS VARCHAR2(12)
GRADE VARCHAR2(3)

Table Creation:-

SQL> Create table std (stdno number (3) primary key,


stdname varchar2(15),
m1 number(3),
m2 number(3),
m3 number(3),
tot number(3),
percent number(4),
result varchar2(5),
class varchar2(12),
grade varchar2(3));

Insert Rows into an employee table:-

SQL> Insert into std values(100,’a’,80,90,80,null,null,null,null,null);

SQL> Insert into std values(101,’b’,30,50,50,null,null,null,null,null);

SQL> Insert into std values(102,’c’,45,50,60,null,null,null,null,null);

S V Degree College Page-16


II B SC (DBMS Record Programs)

SQL> Insert into std values(103,’d’,40,40,40,null,null,null,null,null);

PL/SQL program:-

create or replace procedure studentgrade is


cursor c2 is select * from std;
r c2%rowtype;
begin
open c2;
loop
fetch c2 into r;
exit when c2%notfound;
r.tot:=(r.m1+r.m2+r.m3);
r.percent:=(r.tot*100/300);
if (r.m1>=40 and r.m2>=40 and r.m3>=40) then
if(r.percent>59) then
r.result:=’pass’;
r.class:=’I class’;
r.grade:=’A’;
elsif(r.percent>40 and r.percent<60 ) then
r.result:=’pass’;
r.class:=’II class’;
r.grade:=’B’;
elsif (r.percent=40) then
r.result:=’pass’;
r.class:=’III class’;
r.grade:=’C’;
End if;
else
r.class:=’--’;
r.result:=’fail’;
r.grade:=’--’;
End if;
update std set tot=r.tot,percent=r.percent,result=r.result,class=r.class,
grade=r.grade where stdno=r.stdno;
End loop;
commit;
close c2;
end;
/

S V Degree College Page-17


II B SC (DBMS Record Programs)

Output:

STDNO STDNAME M1 M2 M3 TOT PERCENT RESUL CLASS GRADE


100 a 80 90 80 250 83 pass I class A
101 b 30 50 50 130 43 fail -- --
102 c 45 50 60 155 52 pass II class B
103 d 40 40 40 120 40 pass III class C

S V Degree College Page-18


II B SC (DBMS Record Programs)

Problem 11: -

Write a program to demonstrate function.


Procedure: -

Creating 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;
/

Calling Function: -

Set Serveroutput on;

DECLARE
n3 number(2);
BEGIN
n3 := adder(11,22);
dbms_output.put_line('sum of two numbers =: ' || n3);
END;
/

Output:

Sum of two numbers =: 33

S V Degree College Page-19


Problem 12: -
Write a program to fuel manufactureimg PL/SQL.

Procedure : -

STATEMENT TO CREATE BASE TABLE EMP IS :

SQL> CREATE TABLE EMP (ENO NUMBER, ENAME VARCHAR2(30),


BASIC NUMBER, DA NUMBER, HRA NUMBER,
ITAX NUMBER, GSAL NUMBER, NETSAL NUMBER);

STATEMENT TO INSERT RECORDS INTO THE BASE TABLE EMP IS:

SQL> INSERt fuel detailFUELBASIC ) VALUES


(&ENO,’&ENAME’,&BASIC);
Enter value for eno: 1
Enter value for ename:fueal
Enter value for basic: 5000
old 1: INSERT INTO EMP (ENO, ENAME, BASIC ) VALUES (&ENO,'&ENAME',&BASIC)
new 1: INSERT INTO EMP (ENO, ENAME, BASIC ) VALUES
(1,'VARA LAKSHMI',5000)
1 row created.
SQL> /
Enter value for eno: 2
Enter value for ename: fuel
manufacturing Enter value for
old 1:8000
basic: INSERT INTO EMP (ENO, ENAME, BASIC ) VALUES (&ENO,'&ENAME',&BASIC)
new 1: INSERT INTO EMP (ENO, ENAME, BASIC ) VALUES
(2,'GOWTHAMI',8000)
2 row created.
SQL> /
Enter value for eno: 3
Enter value for ename: fuelI
Enter value for basic: 4000
old 1: INSERT INTO EMP (ENO, ENAME, BASIC ) VALUES (&ENO,'&ENAME',&BASIC)
new 1: INSERT INTO EMP (ENO, ENAME, BASIC ) VALUES
(3,'GOPI',4000)
3 row created.
SQL> /
Enter value for eno: 4
Enter value for ename: fueal manufacturing
Enter value for basic: 4000
old 1: INSERT INTO EMP (ENO, ENAME, BASIC ) VALUES (&ENO,'&ENAME',&BASIC)
new 1: INSERT INTO EMP (ENO, ENAME, BASIC ) VALUES
(4,'GOWRI',4000)
4 row created.
PL / SQL PROGRAM: -
create or replace procedure emp_sql is
CURSOR Q IS SELECT*FROM EMP;
R Q%ROWTYPE;
BEGIN
OPEN Q;
LOOP
FETCH Q INTO R;
EXIT WHEN Q%NOTFOUND;
IF R.BASIC>6000 THEN
R.DA:=(R.BASIC)*(20/100);
R.HRA:=(R.BASIC)*(20/100);
R.ITAX:=(R.BASIC)*(10/100);
ELSIF R.BASIC>4000 AND R.BASIC<6000 THEN
R.DA:=(R.BASIC)*(10/100);
R.HRA:=(R.BASIC)*(10/100);
R.ITAX:=(R.BASIC)*(5/100);
ELSE
R.DA:=(R.BASIC)*(5/100);
R.HRA:=(R.BASIC)*(5/100);
R.ITAX:=0;
END IF;
R.GSAL:=R.BASIC+R.DA+R.HRA;
R.NETSAL:=R.GSAL-R.ITAX;
UPDATE EMP SET DA=R.DA,HRA=R.HRA,ITAX=R.ITAX,GSAL=R.GSAL,
NETSAL=R.NETSAL WHERE ENO=R.ENO;
END LOOP;
CLOSE Q;
EXCEPTION
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('NO DATA FOUND’);
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(SQLCODE||SQLERRM);
END;

SQL> Exec Emp_sql

PL/SQL procedure successfully completed.

S V Degree College Pagl


II B SC (DBMS Record Programs)

OUTPUT:-
SQL> select * from emp;

ENO ENAME BASIC DA HRA ITAX GSAL NETSAL


--------- ------------------------------ --------- --------- --------- --------- ---------- ---------
1 VARA LAKSHMI 5000 500 500 250 6000 5750
2 GOWTHAMI 8000 1600 1600 800 11200 10400
3 GOPI 4000 200 200 0 4400 4400
4 GOWRI 4000 200 200 0 4400 4400

S V Degree College Page-22

You might also like