Professional Documents
Culture Documents
3rdbsc Dbms Lab
3rdbsc Dbms Lab
3rdbsc Dbms Lab
The order tracking database consists of the following defined six relation schemes.
1. Employees(eno,ename,zip,hdate)
2. Parts(pno,pname,qoh,price,level)
3. Customers(cno,cname,street,zip,phone)
4. Orders(ono,cno,eno,received_date,shipped_date)
5.Odetails(ono,pno,qty)
6.Zipcodes(zip,city)
Creating Tables:
Table1:
CREATE TABLE Employees
(ENO VARCHAR2(5) PRIMARYKEY,
ENAME VARCHAR2(30),
ZIP NUMBER(10) REFERENCES ZIPCODES,
HDATE DATE);
To insert the rows in the table type the following command at SQL Prompt.
SQL>INSERT INTO Employees
Values(&ENO,&ENAME,&ZIP,&HDATE);
Table2:
SQL>CREATE TABLE PARTS
(PNO VARCHAR2(5) PRIMARYKEY,
PNAME VARCHAR2(30),
QOH NUMBER(5),
PRICE NUMBER(8,2),
1
LEVEL1 NUMBER(5));
Insert the following data:
To insert the rows in the table type the following command at SQL Prompt.
Table3:
To insert the rows in the table type the following command at SQL Prompt.
2
SQL>INSERT INTO Customers
Values(&CNO,&CNAME,&STREET,&ZIP,&PHONE);
Table4:
Table5:
3
Insert the following data:
ONO PNO QTY
O3 P2 6
O3 P8 7
O2 P5 5
O2 P7 9
O4 P1 4
O5 P1 10
O1 P8 2
O1 P7 10
O2 P2 5
To insert the rows in the table type the following command at SQL Prompt.
Table6:
CREATE TABLE ZIPCODES
(ZIP NUMBER(10)PRIMARYKEY,
CITY VARCHAR2(30));
4
2.UNIVERSITY DATABASE
1. Student(snum:Integer,sname:string,major:string,level:string,age:integer)
2. Class(name:string,Hour:integer,room:string,fid:integer)
3. Enrolled(sum:integer,cname:string)
4. Faculty(fid:integer,fname:string,depyid:integer)
5. Depart(deptid:integer,dname:string,loc:string)
Insert Data:
SQL>INSERT INTO Student VALUES (&snum,&sname,&major,&level1,&age);
5
2.Class Table
CREATE TABLE class
(cname varchar2(20) primary key,
hour number(3),
room varchar2(20),
fid number(4) references faculty);
Insert Data:
3.Enrolled Table:
Insert Data:
SQL>INSERT INTO Enrolled VALUES(&snum,&cname);
SNUM CNAME
1 TELUGU
1 COMPUTERS
2 MATHS
3 PHYSICS
6
3 ENGLISH
4 COMMERCE
5 HISTORY
6 TELUGU
7 STATISTICS
8 STATISTICS
9 COMMERCE
10 ENGLISH
11 TELUGU
12 TELUGU
13 COMPUTERS
14 MATHS
18 HISTORY
15 TELUGU
4.Faculty Table:
Insert data:
7
5.Depart Table:
Insert data:
8
1. Write a PL/SQL program to check the given number is strong or not.
Strong number:-
The sum of the factorials of digits of a number is equal to the original number.
Ex:n=145=>1!+4!+5!=1+4+120=145
So it is strong number.
AIM: To Write a PL/SQL program to check the given number is strong or not.
9
Program:
DECLARE
N NUMBER;
N1 NUMBER;
FSUM NUMBER;
F NUMBER;
A NUMBER;
REM NUMBER;
BEGIN
N:=&N;
N1:=N;
FSUM:=0;
WHILE N>0
LOOP
REM:=MO(N,10);
N:=FLOOR(N/10);
F:=1;
WHILE REM>0
LOOP
F:=F*REM;
RE:=REM-1;
END LOOP;
FSUM:=FSUM+F;
END LOOP;
IF N1=FSUM THEN
DBMS_OUTPUT.PUT_LINE(N1||IS STRONG NUMBER);
ELSE
DBMS_OUTPUT.PUT_LINE(N1||IS NOT STRONG NUMBER);
END IF
END;
10
Input&Output:
SQL>SET SERVER OUTPUT ON
Case 1:
SQL>/
Enter value for n:145
145 IS STRONG NUMBER
Case 2:
SQL>/
Enter value for n:155
155 IS NOT STRONG NUMBER
11
2. Write a PL/SQL program to check the given string is palindrome or not.
Definition:
A palindrome is a string that can be read from right to left, and yet still remains
the same as the string read left to right.
For example, racecar and level are both palindromes because if you read the
strings from right to left they are equivalent to the strings read left to right.
AIM: To Write a PL/SQL program to check the given string is palindrome or not.
12
Program:
Declare
Strvarchar2(30);
Len number:=0;
Cnt number:=0;
J number:=0;
Begin
Str:=&string;
Len:=length(str);
For I in 1..len loop
J:=i-1;
If substr(str,I,1)=substr(str,len-j,1)then
Cnt:=cnt+1;
End if;
End loop;
If cnt=len then
Dbms_output.put_line(str|| is palindrome string);
Else
Dbms_output.put_line(str|| is not palindrome string)
End if;
End;
13
Output:
Case 1:
Enter Str Value: madam
Madam is palindrome string
Case 2:
Enter Str value: nani
Nani is not palindrome string
14
3. Write a pl/sql program to swap two numbers without using third variable.
AIM: To Write a pl/sql program to swap two numbers without using third variable.
15
Program:
------------
Declare
A number;
B number;
Begin
A;=&a;
B:=&b;
Dbms_output.put_line(before exchange:);
Dbms_output.put_line(a=||a||b=||b);
A:=a+b;
B:=a-b;
A:=a-b;
Dbms_ouput.put_line(After exchange:);
Dbms_output.put_line(a=||a||b=||b);
End;
16
Input&Output:
SQL/
Enter value for a:=10
Enter value for b:=15
Before exchange:
a=10 b=15
After exchange:
a=15 b=10
17
4. Write a PL/SQL program to generate multiplication tables for 2,4,6
18
Program:
------------
declare
m number;
a number;
tn number;
begin
tn:=2;
loop
for a in 1..10
loop
m:=tn*a;
dbms_output.put_line(tn||x||a||=||m);
end loop;
tn:=tn+2;
exit when tn>6;
end loop;
end;
19
output:
20
5. Write a PL/SQL program to display sum of even numbers and sum of odd
numbers in the given range.
AIM: To Write a PL/SQL program to display sum of even numbers and sum of odd
numbers in the given range.
21
Program
Declare
M number;
N number;
Esum number;
Osum number;
Begin
M:=&m;
N:=&n;
Esum:=0;
Osum:=0;
For a in m..n
Loop
If(mod(a,2)=0)then
Esum:=esum+a;
Else
Osum:=osum+a;
End if;
End loop;
Dbms_output.put_line(even numbers sum=||esum);
Dbms_output.put_line(odd numbers sum=||osum);
End;
22
Input & Output:
Enter value for m:=5
Enter value for n:=9
Even numbers sum=14
Odd numbers sum=21
23
6. Write a PL/SQL program to check the given number is palindrome or not.
AIM : To Write a PL/SQL program to check the given number is palindrome or not.
24
Program
Declare
N number;
N1 number;
Rnum number(15);
Rem number;
Begin
N:=&n;
N1:=n;
Rnum:=0;
Dbms_output.put_line(given number=||n);
While n>0
Loop
Rem:=mod(n,10);
Rnum:=rnum*10+rem;
N:=floor(n/10);
End loop;
If(n1=rnum)then
Dbms_output.put_line(n1||is palindrome number);
Else
Dbms_output.put_line(n1||is not palindrome number);
End if;
End;
25
Input&Output:
SQL>RUN
Enter value for n:111
Given number=111
111 is palindrome number
SQL>Run
Enter value for n:153
Given number=153
153 is not palindrome number
26
Update Employee salary
7. The hrd manager has decided to raise the employee salary by 15%.Write a
PL/SQL block to accept the employee number and update the salary of that
employee.Display appropriate message based on the existence of the record
in emp table.
AIM: To The hrd manager has decided to raise the employee salary by 15%.Write a
PL/SQL block to accept the employee number and update the salary of that
employee.Display appropriate message based on the existence of the record in emp
table.
27
Program:
Declare
E1rec emp%rowtype;
Eno number;
Begin
Eno:=&eno;
SELECT *INTO e1rec From emp Where empno=eno;
Dbms_output.put_line(Before updating employee details:);
Dbms_output.put_line(e1rec.empno||-||e1rec.ename||-||e1rec.job||-
||e1rec.mgr||-||e1rec.hiredate||-||e1rec.sal||-||e1rec.comm||-
||e1rec.deptno);
E1rec.sal:=e1rec.sal+e1rec.sal*15/100;
UPDATE emp Set sal=e1rec.sal Where empno=eno;
SELECT *INTO e1rec FROM emp Where empno=eno;
Dbms_output.put_line(After updating employee details:);
Dbms.output.put_line(e1rec.empno||-||e1rec.ename||-||e1rec.job||-
||e1rec.mgr||-||e1rec.hiredate||-||e1rec.sal||-||e1rec.comm||-
||e1rec.deptno);
End;
28
Input&Output:
SQL/
Enter value for eno:7934
Before updating employee details:
7934-MILLER-CLERK-7782-23-JAN-82-130010
After updating employee details:
7934-MILLER-CLERK-7782-23-JAN-82-149510
29
Display Employee table details
8. Write a PL/SQL program to display top 10 rows in emp table based on their
job and salary.
Aim : To Write a PL/SQL program to display top 10 rows in emp table based on
their job and salary.
30
Program:
Declare
Cursor c1 is SELECT * FROM emp ORDER BY job,sal;
E1rec emp%rowtype;
Rcount number;
Begin
Open c1;
Rcount:=1;
Loop
Fetch c1 into e1rec;
Exit when rcount>10;
Dbms_output.put_line(e1rec.empno||-||e1rec.ename||-||e1rec.job||-
||e1rec.mgr||-||e1rec.hiredate||-||e1rec.sal||-||e1rec.comm||-
||e1rec.deptno);
Rcount:=rcount+1;
End loop;
Close c1;
End;
31
Output:
7782 SCOTT ANALYST 7566 19-APR-87 3000 20
7902 FORM ANALYST 7566 03-DEC-81 3000 20
7369 SMITH CLERK 7902 17-DEC-80 800 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7839 KING PRESIDENT 7849 17-NOV-81 5000 20
32
9. Write a procedure to update the salary of employee, who are not getting
commission by 10%
Aim: To Write a procedure to update the salary of employee, who are not getting
commission by 10%
33
Program:
Declare
Cursor c1 is SELECT * FROM emp;
E1rec emp%rowtype;
Rsal number(7,2);
Begin
Open c1;
Loop
Fetch c1 into e1rec;
Exit when c1%notfound;
If(nvl(e1rec.comm,0)=0)then
Rsal:=e1rec.sal+e1rec.sal*10/100;
Else
Rsal:=e1rec.sal;
End if;
Dbms_output.put_line(e1rec.empno||-||e1rec.ename||-||e1rec.sal||-
||rsal||-||e1rec.comm);
End loop;
Close c1;
End;
34
Output:
EMPNO ENAME SAL RSAL COMM
7369 SMITH 800 880
7499 ALLEN 1600 1600 300
7521 WARD 1250 1250 500
7566 JONES 2975 3272.5
7654 MARTIN 1250 1250 1400
7698 BLAKE 2850 3135
7782 CLARK 2450 2695
7788 SCOTT 3000 3300
7839 KING 5000 5500
7844 TURNER 1500 1650 0
7876 ADAMS 1100 1210
7900 JAMES 950 1045
7902 FORM 3000 3300
7934 MILLER 1300 1430
35
Electricity Bill
Assumption:
No_units=cur_read-prev_read
If no_units>=1000 then
Rate per unit(rpu)=Rs.5.25
Otherwise
Rate per unit(rpu)=3.25
Amount=no_units*rpu
If amount>=1500 then
Service tax(ser_tax)=25
Otherwise
Service tax(ser_tax)=15
Net amount(net_amt)=amount+ser_tax
36
Step2:Insert the following data into ELECT table
MNO CNAME CUR_READ PREV_READ
101 G.SITADEVI 1500 1000
102 G.MADHAVILATHA 1700 750
105 M.SUBBAREDDY 1750 350
104 J.KAVITHARANI 3500 2000
103 K.SAMBASIVARAO 1500 500
INSERT INTO ELECT (mno,cname,cur_read,prev_read)
Values(&mno,&cname,&cur_read,&prev_read);
37
Step 3:Wrie PL/SQL code:
Declare
Cursor c1 is SELECT * FROM ELECT;
Rec elect%rowtype;
Begin
Open c1;
Loop
Fetch c1 into rec;
Exit when c1%notfound;
Rec.no_units:=rec.cur_read-rec.prev_read;
If rec.no_units>=1000 then
Rec.amount:=rec.no_units*5.25;
Else
Rec.amount:=rec.no_units*3.25;
End if;
If rec.amount>=1500 then
Rec.ser_tax:=25;
Else
Rec.ser_tax:=15;
End if;
Rec.net_amt:=rec.amount+rec.ser_tax;
Update elect Set no_units=rec.no_units, amount=rec.amount,
er_tax=rec.ser_tax,net_amt=rec.net_amt Where mno=rec.mno;
End loop;
Close c1;
End;
Step 3:
SELECT * FROM ELECT;
38
Output:
39
Telephone Bill
Assumption:
Net_units=cur_read-pr_read
If net_units>=1000then rate per unit(rpu)=1.20
If net_units>=500then rate per unit(rpu)=0.75
Otherwise rpu=0.49
Tot_amt=net_units*rpu
40
Step 3:Write PL/SQL program
Declare
Cursor c1 is SELECT * FROM phone Rec phone%rowtype;
Begin
Open c1;
Loop
Fetch c1 into rec;
Exit when c1%notfound;
Rec.net_units:=rec.cur_read-rec.pr_read;
If rec.net_units>=1000then
Rec.tot_amt:=rec.net_units*1.20;
Elseifrec.net_units>=500then
Rec.tot_amt:=rec.net_units*0.75;
Else
Rec.tot_amt:=rec.net_units*0.49;
End if;
UPDATE PHONE
SET net_units=rec.net_units,tot_amt=rec.tot_amt
Where tel_no=rec.tel_no;
End loop;
Close c1;
End;
Step 3:
SELECT * FROM PHONE;
41
Output:
TEL_N CNAME CITY PR_REA CUR_REA NET_UNI TOT_A
O D D TS MT
22900 G.SITADEVI TIRUPATHI 1000 1500 500 375
33545 G.MADHAVILAT ANANTHAP 750 1700 950 712.5
HA UR
27765 M.SUBBAREDDY GUNTUR 350 1750 1400 1680
65222 J.KAVITHARANI VIJAYAWAD 2000 3500 1500 1800
A
66464 K.SAMBASIVA NELLORE 500 1500 1000 1200
42