Dbms Lab Rec PDF
Dbms Lab Rec PDF
Data Types:-
Commands:-
1
20. drop database <db_name>; Used to delete a database completely.
7. select * from <table To view selected rows and all columns
21. name> w h e
d r o p r e based on gUivsend ctonddeitlieotne. a table
t<aebxlep><;table completely including its schema.
8. To retrieve all the distinct fields in the table.
name>;
22. Used to delete a table leaving behind its
select distinct(col1, clo2, …) schema.
92. fr o m < ta bl e n a TGoracnretsa tehetasbpl ecfirfoiemd
t r unc a te t ab l e apnriveixlgisetsinognt abplea.rticular
3. m e > ;
< t a ble name>;
table to the users.
crgeratnet
24. t a b le < Used to revoke the granted privileges on a
< s y st emtabplreivni table Sforrotms the tuasbelersi.n ascending
10. a m ( c ol
le ge> o n order based on given expression.
na<mtaeb)lea>s
25. tsoel<eucste(rc>o;l1, col2, Groups all the rows with same
column value
11. …) from <old table Sboarstsedthoenta bgleivei
name>; n
dceosncdeintidoinn.g order based on
revoke <system given expression.
privilege> on
se<lteacbtle*>froom
<<taubsler>n;ame>
order by (col);
select (column) from
<table senlaemcte>*
fgromup<btayb<leconlan
mame>e>
orhdaevrinbgye(xcpo;l) desc;
16 alter table <table name> add Adds a new column to the table.
.
(col data type);
Aggregate Functions:-
3
S.no. Funtion Usage
1. AVG Used to find the average of the values in a given
column.
2. MAX Used to find the maximum or the largest of all
the
values in a given column.
4
month.
It is used to adjust the time according to
the
6. NEW_TIME desired time zone.
5
Number Functions:-
6
9. FLOOR(n) Returns the greatest integer smaller than or
equal to that of the argument passed.
10. SIGN(n) Judges whether a given no. is positive or
negative.
String Functions:-
8
occurs.
Conversion Functions:-
S.no. Function Usage
/* TABLES INVOLVED */
/* SUPPLIER DATABASE */
s1 smith london
9
s2 jones paris
s3 blak paris
e
s4 clark london
s5 adams athens
/* PARTS TABLE */
/*ORDERS TABLE */
SID PI QUANTITY
D
s1 p1 300
s1 p2 200
s1 p3 400
s1 p4 200
s1 p5 100
s1 p6 100
s2 p1 300
s2 p2 400
s3 p2 200
10
11
s4 p2 200
s4 p4 300
/* QUERIES */
EMPLOYEE DATABASE
/* TABLES INVOLVED */
1) EMP Table
12
EMPNO NOT NULL
NUMBER(5) ENAME
VARCHAR2(10)
JOB VARCHAR2(9)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(4)
COMM NUMBER(4)
DEPTNO NUMBER(6)
2) DEPT Table
3) SALGRADE Table
Name Null? Type
GRADE NUMBER(5)
LOSAL NUMBER(6)
HISAL NUMBER(6)
/* DEPT TABLE */
/* EMP TABLE */
13
EMP ENA JOB M 2 HIR SAL 800
NO ME CLE GR ED 08-
SEP- 1600
736 SMIT RK 790 AT 81
9 H 2 E 1250
SALES 23-
MAN 17- MAY
7499 ALLE 769 -87 2975
N 8 DE
SALES
7521 MAN C- 03- 1250
WAR 769 DEC
D 8 80 -81
7566 MANA 2850
GER
JONE 783 20- 03-
7654 S 9 FEB DEC 2450
SALES -81 -81
MAN
7698 MAR 769 3000
TIN 8 22- 23- JA
MANA FEB N-
7782 GER -81 8 5000
BLAK 783 2
E 9
7788 MANA 02- 1500
GER APR
CLAR 783 -81
7839 K 9 1100
ANALY
ST 28-
7844 SCOT 756 SEP 950
T 6 -81
PRESID
7876 ENT 3000
KING 01-
MA
7900 SALES Y- 1300
TUR MAN 769 81
NER 8
7902
CLERK 09-
ADA 778 JUN
7934 MS 8 -81
CLERK
FORD 756
CLERK 6 17-
NO
MILL V-
ER 778 81
14
COMM D 0 20
0 E
30 3
P
1400 0 1 30
T 0
300 N 30
O 1 20
0 3
500 2 20 0
0 10
2
30
/* SALGRADE TABLE */
15
GRA LOSA HISAL
DE L
1 70 1200
0
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999
/* QUERIES */
3. List the names of employees who have “th” or “ll” in their names.
4. List the names, jobs and salaries of all employees who have a manager.
10. Find average salary and average total remuneration of all employees
other than sales man
11. Find maximum, minimum and average salaries in each department.
12. Find the maximum, minimum and average salaries of each job.
13. Find the departments which have more than 3 employees.
14. Display employee names and their respective department numbers.
15. Give the salary grades for all the employees.
16. Display the employee names who earn highest salary in each job.
17. Find the employee details whose salary is greater than Blake’s salary.
18. Find employee details of employees who have the same job and
salary as that of Scott.
19. Display the maximum salaries in accounting and research department.
21. Give the names and salaries of the employees whose salary is
maximum in their respective departments.
22. List the employees whose salary is greater than the salaries of all employees
who are working as salesmen.
16
23. Write a query which will return the day of the week entered in
the format of sysdate.
24. Find the difference between highest and lowest salaries.
25. Generate the output as Smith – clerk.
26. Generate the output as Smith (Clerk).
27. Give the details of all employees those who r working as manager.
28. List the departments where there are no employees
29. Generate the following list :
EMPLOYEE NAME JOB SAL GRADE
30. List the information of those employees in department number 10.
31. Find the department location of James.
32. Get the manager of Jones.
33. Get the sub-ordinates of Jones.
34. Write a query to calculate the length of time of all employees
with the company.
35. List out name, job, salary, grade, department name of all employees who are
not clerks.
36. List out name, job, salary, grade, department name of all
employees who are clerks.
37. Display the most recently employed employee in each department.
38. Display each employee with name, hiredate and review date –
1 year from now.
39. Display the year in which most people joined.
40. List out minimum salary of those employees under manager.
17
53. Count the people in department 30 who receive both
salary and commission.
54. List all the employees who were hired between “ 1st
January 1981” and “15th April 1985”. In the order of their
seniority.
PL/SQL Programs
18
2) Sum of first ‘n’ natural numbers.
8) Reverse of a number.
11)Prime Numbers
up to ‘n’.
CURSORS
19
1) Cursor programs to find :-
2) Details of employees
4) Hike in salary.
TRIGGERS
1) Trigger on Insertion.
2) Trigger on Updating.
5) Trigger on Deletion.
VNRVJIET
INDEX
20
S.N Exp Name Of The Dat Remarks
O. No. Experiment e
SQL
II PL/SQL
16
III CURSORS
Cursor programs to find :-
SUPPLIER – PARTS DATABASE
/* TABLES INVOLVED */
/* DDL STATEMENTS */
Table created.
Table created.
17
ER DIAGRAM FOR SUPPLIER - PART DATABASE
18
SQL> describe orders;
/* DML STATEMENTS */
/* SUPPLIER DATABASE */
19
SQL> select * from parts;
/* PARTS TABLE */
PI PNAME COLOR
D
p1 nut red
p2 bolt green
p3 screw blue
p4 screw red
p5 cam blue
p6 cog red
6 rows selected.
/*ORDERS TABLE */
20
/* QUERIES */
s1 smith
s4 clark
s5 adams
s1 smith london
s2 jones paris
s3 blake paris
s4 clark london
s5 adams Athens
orders; SUPPLIERS
MIN(QUANTITY)
400 200
21
5. For each part supply gets the part no. and total shipment quantity.
SUM(QUANTITY)
p1 600
p2 1000
p3 400
p4 500
p5 500
p6 100
6 rows selected.
6. Get part number for all parts supplied by more than one supplier.
count(sid)>1; PID
p
1
p
2
p
4
p
5
7. Get supplier names for suppliers who supplied at least one red part.
SNAME
smi
th
jone
s
clark
8. Get the names of the suppliers who supply the part p2.
22
2 (select distinct(sid)from orders where pid='p2');
SNAME
23
smit
h
jone
s
blak
e
clark
9. Get the supplier names for suppliers who do not supply part p2.
SNAME
blake
adams
SNAME
smith
11. Get pname for parts supplied by xyz supplier and by no one else.
PNAME
screw
cog
12. Find the sid of suppliers who supply red or green parts.
SID
---
s
1
s
2
s
3
24
s4
13. Find the sid of suppliers who supply both red and green parts.
SID
---
s
1
s
2
s4
25
EMPLOYEE DATABASE
/* TABLES INVOLVED */
/* DDL STATEMENTS*/
Table created.
Table created.
EMP
Table
Null? Type
Name
EMPNO NOT NULL NUMBER(5)
ENAME VARCHAR2(1
0)
JOB VARCHAR2(9
)
MGR NUMBER(4)
HIREDAT DATE
E
SAL NUMBER(4)
COMM NUMBER(4)
DEPTNO NUMBER(6)
DEPT Table
26
Name Null? Type
-
DEPTNO NOT NULL
NUMBER(6) DNAME
VARCHAR2(20)
LOC VARCHAR2(20)
SALGRADE Table
GRADE NUMBER(5)
LOSAL NUMBER(6)
HISAL NUMBER(6)
/*DML STATEMENTS */
/* DEPT TABLE */
4 rows selected.
1 row created.
SQL> insert into emp values ( 7499, ‘ALLEN’, ‘SALESMAN’, 7698, ’20-
FEB-81’, 1600, 300, 30);
27
1 row created.
NO ME O R DEC-80
FORD MANA 7566
736 SMIT B GER 790
20-FEB-81
9 H MILLER 2 7782
ANALY
C ST 22-FEB-81
7499 ALLE 769
N L 8
PRESID 02-APR-81
7521 E ENT
WAR 769
D R 8 28-SEP-81
7566 SALES
K MAN
JONE 783 01-MAY-81
7654 S 9
SA CLERK
LE 09-JUN-81
7698 MAR S 769
TIN M CLERK 8
AN 19-APR-87
7782
BLAK ANALY 783
E SA ST 9 17-NOV-81
7788 LE
S
CLAR M CLERK 783 08-SEP-81
7839 K AN 9
23-MAY-87
7844 SCOT M 756
T AN 6
AG 03-DEC-81
7876 ER
KING
03-DEC-81
7900 SA
TUR LE 769
NER S 8 23- JAN-82
7902 M
AN
ADA 778
7934 MS 8
M
AN
JAME AG 769
28
SAL 800 CO D
MM E
P
1600
T
N
1250 O
300
2
2975 500 0
1250
30
2850 1400
30
2450
20
3000
30
5000 0
30
1500
10
1100
20
950
10
3000
30
1300
20
30
20
10
29
SQL> insert into salgrade values ( 1, 700, 1200);
1 row created.
1 row created.
5 rows
selected.
30
ER DIAGRAM FOR EMPLOYEE DATABASE
31
/* QUERIES */
32
JOB
ANALYST
CLERK
MANAGER
PRESIDENT
SALESMAN
8 rows selected
.
3. List the names of employees who have “th” or “ll” in their names.
SQL> select ename from emp where ename like ('%TH%') or ename
like('%LL%');
33
ENAME
SMITH
ALLEN
MILLER
4. List the names, jobs and salaries of all employees who have a manager.
SQL> select ename, job, sal from emp where mgr is not null;
ENAME JOB SAL
34
SMITH 800
ALLEN 1900
WARD 1750
JONES 2975
MARTIN 2650
BLAKE 2850
CLARK 2450
SCOTT 3000
KING 5000
TURNER 1500
ADAMS 1100
JAMES 950
FORD 3000
MILLER 1300
14 rows selected.
35
ENAME INCREASED_SALARY
SMITH 920
ALLEN 1840
WARD 1437.5
3421.25
JONES
1437.5
MARTIN
3277.5
BLAKE
2817.5
CLARK
3450
SCOTT
5750
KING
1725
TURNER
1265
ADAMS
1092.5
JAMES
3450
FORD
1495
MILLER
14 rows selected.
ENAME
MILLER
8. Display name, annual salary, commission of all salesmen whose monthly salary
is greater than commission.
36
ENAME ANUAL_SALARY ANUAL_COMMISSION
9. Produce the output as “Smith has held the position of clerk in department 20
since 17-dec-80”.
SQL> select ename || ' HAS HELD THE POSITION OF ' || job || ' IN
DEPARTMENT ' 2 || deptno || ' SINCE ' || hiredate details_of_employees
from emp;
DETAILS_OF_EMPLOYEES
SMITH HAS HELD THE POSITION OF CLERK IN DEPARTMENT 20
SINCE 17-DEC-80
37
ADAMS HAS HELD THE POSITION OF CLERK IN DEPARTMENT 20
SINCE 23-MAY-87
10. Find average salary and average total remuneration of all employees other
than sales man
AVG(SAL) AVG_RENUMERATION
2073.21429 2230.35714
SQL> select deptno, max(sal), min(sal), avg(sal) from emp group by deptno;
12. Find the maximum, minimum and average salaries of each job.
38
SQL> select job, max(sal), min(sal) , avg(sal) from emp group by job;
DEPTNO
20
30
39
NAME DEPARTMENT
SMITH 20
ALLEN 30
WARD 30
JONES 20
MARTIN 30
BLAKE 30
CLARK 10
SCOTT 20
KING 10
TURNER 30
ADAMS 20
JAMES 30
FORD 20
MILLER 10
14 rows selected.
40
ENAME GRADE
SMITH 1
ADAMS 1
JAMES 1
WARD 2
MARTIN 2
MILLER 2
ALLEN 3
TURNER 3
JONES 4
BLAKE 4
CLARK 4
SCOTT 4
FORD 4 14 ROWS
KING 5 SELECTED.
16. Display the employee names who earn highest salary in each job.
41
SQL> select ename, job from emp where sal in
2 (select max(sal) from emp group by job);
ENAME JOB
MILLER CLERK
ALLEN SALESMAN
JONES MANAGER
SCOTT ANALYST
FORD ANALYST
KING PRESIDENT
6 rows selected.
17. Find the employee details whose salary is greater than Blake’s salary.
SQL> select * from emp where sal>(select sal from emp where ename='BLAKE');
4 rows selected.
18. Find employee details of employees who have the same job and salary as that
of Scott.
42
SQL> select * from emp where
2 (sal=(select sal from emp where ename='SCOTT') and
3 job=(select job from emp where ename='SCOTT'));
MAX(SAL)
5000
20. Display salary grades of all employees except of those employees whose salary
grade is 3 and 4.
ENAME GRADE
SMITH 1
ADAMS 1
JAMES 1
WARD 2
MARTIN 2
MILLER 2
KING 5
7 rows selected.
21. Give the names and salaries of the employees whose salary is maximum in
their respective departments.
43
2 (select max(sal) from emp group by deptno);
NAME DEPARTMENT
BLAKE 30
SCOTT 20
FORD 20
KING 10
22. List the employees whose salary is greater than the salaries of all employees
who are working as salesmen.
ENAME
JONES
BLAKE
CLARK
SCOTT
KING
FORD
6 rows selected.
23. Write a query which will return the day of the week entered in the format of
sysdate.
SQL> select ename || ' - ' || job from emp where ename=’smith’;
44
ENAME||'-'||JOB
SMITH - CLERK
SQL> select ename || ' ( ' || job || ' ) ' emp_job_details from emp;
EMP_JOB_DETAILS
SMITH ( CLERK )
27. Give the details of all employees those who are working as manager.
DEPTNO DNAME
40 OPERATIONS
45
2 where e.sal between s.losal and s.hisal;
14 rows selected.
46
2 where deptno=10 and job not in('MANAGER', 'CLERK');
LOC
DALLAS
MANAGER_OF_JONES
KING
ENAME
SCOTT
FORD
35. Write a query to calculate the length of time of all employees with the
company.
ENAME LENGTH_OF_TIME
47
SMITH 290
ALLEN 288
WARD 288
JONES 286
MARTIN 280
BLAKE 285
CLARK 284
SCOTT 214
KING 279
TURNER 281
ADAMS 213
JAMES 278
FORD 278
MILLER 277
14 rows selected.
36. List out name, job, salary, grade, department name of all employees who are
not clerks.
SQL> select ename, job, sal, grade, dname from emp e, dept d, salgrade s
2 where e.deptno=d.deptno and
3 e.sal between s.losal and s.hisal and
4 job not in('CLERK');
10 rows selected.
37. List out name, job, salary, grade, department name of all employees who are
clerks.
48
SQL> select ename, job, sal, grade, dname from emp e, dept d, salgrade s
2 where e.deptno=d.deptno and
3 e.sal between s.losal and s.hisal and
4 job in('CLERK');
MILLER 23-JAN-82 10
ADAMS 23-MAY-87 20
JAMES 03-DEC-81 30
49
39. Display each employee with name, hiredate and review date – 1 year
from now.
14 rows selected.
50
YEAR
1981
7 rows selected.
2 -1,'below target',
3 1,'above target')
ENAME DETAILS
51
SMITH below target
14 rows selected.
52
43. Display ename, loc and dnames of the employees who earn more than 1500
salary.
7 rows selected.
ENAME SAL
SCOTT 3000
KING 5000
FORD 3000
45. Write a query to display a ‘*’ against the employee who joined most recently.
SQL> (select empno, ename||' * ' name , job , mgr, hiredate, deptno from emp
53
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7782
14 rows selected.
54
46. Find the most recently joined employee in the company in each department
in order of hiredate.
2 e.mgr=m.empno and
3 m.hiredate>e.hiredate;
ENAME
SMITH
ALLEN
WARD
JONES
BLAKE
CLARK
6 rows selected.
47. Find all the employees who joined before their manager.
ENAME
SMITH
ALLEN
WARD
JONES
BLAKE
CLARK
6 rows selected.
55
48. List jobs and department names of employees whose names are 5 letters long
and must begin with ‘ALL’ and end with ‘N’.
ALLEN SALESMAN 30
49. List employees whose commission is greater than 25% of their salary.
ENAME
WARD
MARTIN
DEPTNO
20
51. List employees in dept 30 with job not found in dept 10.
ENAME
ALLEN
WARD
MARTIN
TURNER
56
52. Who earns the second highest salary?
ENAME
SCOTT
FORD
HIREDATE
Wednesday, 17-dec-80
Friday , 20-feb-81
Sunday , 22-feb-81
Thursday , 02-apr-81
Monday , 28-sep-81
Friday , 01-may-81
Tuesday , 09-jun-81
Sunday , 19-apr-87
Tuesday , 17-nov-81
Tuesday , 08-sep-81
Saturday , 23-may-87
Thursday , 03-dec-81
Thursday , 03-dec-81
Saturday , 23-jan-82
14 rows selected.
57
54. Count the people in department 30 who receive both salary and commission.
SQL> select count(empno) from emp where nvl(comm,0)!=0 and sal!=0 and
deptno=30;
COUNT(EMPNO)
3
55. List all the employees who were hired between “ 1st January 1981” and “15th
April 1985”. In the order of their seniority.
ENAME
ALLEN
WARD
JONES
BLAKE
CLARK
TURNER
MARTIN
KING
JAMES
FORD
MILLER
11 rows selected.
58
2 || ' MONTHS ' service_period from emp order by hiredate;
ENAME SERVICE_PERIOD
SMITH 290 MONTHS
14 rows selected.
59
PL/SQL PROGRAMS
SQL> ed add.sql;
declare
a number :=&a;
b number :=&b;
begin
dbms_output.put_line(' Sum of ' || a||' and '|| b || ' is '||(a+b));
dbms_output.put_line(‘Product of ‘ || a || ‘and’ || b || ‘is’ || a*b);
end;
SQL> @add.sql;
Input :-
Enter the value of a : 4
Enter the value of b : 5
Output :-
Sum of 4 and 5 is 9
Product of 4 and 5 is 20
60
2. Program to find sum of first ‘n’ natural no.s.
SQL> ed sum.sql;
declare
n number := &n;
y number :=0;
begin
for j in 1..n
loop
y := y + j;
end loop;
dbms_output.put_line(‘ Sum of first ‘ ||n|| ‘ Natural no.s is ’|| y);
end;
SQL> @ sum.sql;
Input:-
Enter the value of n : 5
Output:-
Sum of first 5 natural no.s is 15.
61
3. Program to print 10 numbers in the reverse order.
SQL> ed rev.sql;
begin
for i in reverse 1..10
loop
dbms_output.put_line(i);
end loop;
end;
SQL> @rev.sql
Output:-
10
9
8
7
6
5
4
3
2
1
PL/SQL procedure successfully completed
62
4. Program to print even numbers upto 100.
SQL> ed even.sql;
declare
n number :=&n;
c number;
begin
for j in 1..n
loop
if j mod 2 =0 then
dbms_output.put_line(j);
end if;
end loop;
end;
SQL> @even.sql;
Input :-
Enter the value of n: 100
Output :-
2 22 42 62 82
4 24 44 64 84
6 26 46 66 86
8 28 48 68 88
10 30 50 70 90
12 32 52 72 92
14 34 54 74 94
16 36 56 76 96
18 38 58 78 98
20 40 60 80 100
63
5. Program to find sum of digits of a number.
SQL> ed digit.sql;
declare
n number := &n;
s number := 0;
begin
while n>0
loop
s := n mod 10 + s;
n := floor(n/10);
end loop;
dbms_output.put_line(‘The Sum of digits of given number is ‘||s);
end;
SQL> @digit.sql;
Input :-
Enter the value of n : 111
Output :-
The sum of digits of given number is 3.
64
6. Program to print multiplication table of a given number.
SQL> ed multi.sql;
declare
n number := &n;
product number;
begin
for j in 1..10
loop
product := n*j;
dbms_output.put_line( n || ‘ * ’ || j || ‘ = ‘ || product);
end loop;
end;
SQL> @multi.sql;
Input :-
Enter the value of n: 5
Output :-
5*1 = 5
5 * 2 = 10
5 * 3 = 15
5 * 4 = 20
5 * 5 = 25
5 * 6 = 30
5 * 7 = 35
5 * 8 = 40
5 * 9 = 45
5 * 10 = 50
65
7. Program to print n multiplication tables.
SQL> ed nmulti.sql;
declare
n number := &n;
product number;
begin
for i in 1..n
loop
for j in 1..10
loop
product := i * j;
dbms_output.put_line( i || ' * ' || j || ' = ' || product);
end loop;
end loop;
end;
SQL> @nmulti.sql;
Input :-
Enter the value of n : 5
Output:-
66
8. Program to find reverse of a number.
SQL> ed reverse.sql;
declare
n number := &n;
r number;
s number :=0;
begin
while (n != 0)
loop
r := mod(n , 10);
s := (s*10) + r;
n := floor(n/10);
end loop;
dbms_output.put_line( ' The reverse of given number is '|| s);
end;
SQL> @reverse.sql;
Input :-
Enter the value of n : 1234
Output :-
The reverse of given number is 4321.
67
9. Program to check whether a given number is prime or not.
SQL> ed prime.sql;
declare
a number := &a;
r number := 0;
begin
for i in 2..a-1
loop
if a mod i = 0 then
r:=1;
end if;
end loop;
if r = 0 then
dbms_output.put_line(' It is a prime number ' );
else
dbms_output.put_line(' It is not a prime number ' );
end if;
end;
SQL> @prime.sql;
Input :-
Enter the value of n : 13
Output :-
It is a prime number.
68
10. Program to generate Fibonacci series.
SQL> ed fibonacci.sql ;
declare
a number :=0;
b number :=1;
c number;
begin
while b<=1000
loop
c :=a+b;
dbms_output.put_line(b);
dbms_output.put_line(c);
a:=c;
b := b+c;
end loop;
end;
SQL> @fibonacci.sql;
Output :-
1
1
2
3
5
8
13
21
34
55
89
144
233
377
610
987
PL/SQL procedure successfully completed
69
11. Program to generate prime numbers upto 100.
SQL> ed prime.sql;
declare
flag number :=1;
i number;
begin
for j in 2..100
loop
flag:=1;
i :=2;
while i<=(j/2)
loop
if j mod i =0 then
flag :=0;
end if;
i:=i+1;
end loop;
if flag =1 then
dbms_output.put_line(j);
end if;
end loop;
end;
SQL> @ prime.sql;
Output:-
1 23 59
2 29 61
3 31 67
5 37 71
7 41 73
11 43 79
13 47 83
17 53 89
19 97
PL/SQL procedure successfully completed
70
12. Program to find factorial of a given number.
SQL> ed factorial.sql;
declare
n number := &n;
p number :=1;
begin
for j in 1..n
loop
p := p * j;
end loop;
dbms_output.put_line( ' Factorial of '|| n || ' is ' || p);
end;
SQL> @ factorial.sql;
Input:-
Enter the value of n : 5
Output:-
Factorial of 5 is 120.
71
13. Program to find the area of a circle given the radius.
SQL> ed acircle.sql;
declare
r number := &radius;
area number;
begin
SQL> @ acircle.sql;
Input :-
Enter the value of radius : 5
Output:-
78.525
72
14. Insert the values of areas of a circle into a table called areas taking radius
values from 2 to 8.
Table created.
SQL> ed areas.sql
declare
area number;
begin
for radius in 2..8
loop
area := 3.14* radius*radius;
insert into areas values(radius,area);
end loop;
end;
SQL> @ areas.sql;
RADIUS AREA
2 12.56
3 28.26
4 50.24
5 78.5
6 113.04
7 153.86
8 200.96
7 rows selected.
73
15. Program to find whether the given number is an Armstrong number or not.
SQL> ed armstrong.sql;
declare
a number := &a;
s number :=0;
n number :=a;
re number;
begin
while a>0
loop
re := a mod 10;
re := re*re*re;
s := s+re;
a := floor(a/10);
end loop;
if n = s then
dbms_output.put_line(' It is an armstrong number ');
else
dbms_output.put_line(' It is not an armstrong number ');
end if;
end;
SQL> @ armstrong.sql;
Input :-
Enter the value of a : 153
Output :-
It is an Armstrong number.
74
16. Program to generate Armstrong numbers below 1000.
SQL> ed armstrongn.sql;
declare
s number :=0;
a number ;
re number;
begin
for n in 1..1000
loop
s:=0;
a :=n;
while a>0
loop
re := a mod 10;
re := re*re*re;
s := s+re;
a := floor(a/10);
end loop;
if s = n then
dbms_output.put_line(n);
end if;
end loop;
end;
SQL> @armstrongn.sql;
Output:-
1
153
370
371
407
75
17. Program to find greatest of 2 numbers.
SQL> ed greatest.sql;
declare
a number := &a;
b number := &b;
begin
if a>b then
dbms_output.put_line(a || ' is greater than ' || b);
else
dbms_output.put_line(b || ' is greater than ' || a);
end if;
end;
SQL> @ greatest.sql;
Input:-
Enter the value of a: 5
Enter the value of b: 15
Output:-
15 is greater than 5
76
18. Program to check whether the given number is palindrome number or not.
SQL> ed palindrome.sql;
declare
n number := &n;
a number := n ;
r number;
s number :=0;
begin
while (n != 0)
loop
r := mod(n , 10);
s := (s*10) + r;
n := floor(n/10);
end loop;
if(s=a) then dbms_output.put_line( ' It is a palindrome number');
else dbms_output.put_line(' It is not a palindrome number');
end if;
end;
SQL> @palindrome.sql;
77
19. Program to find the greatest of 3 numbers.
SQL> ed great.sql;
declare
a number :=&a;
b number :=&b;
c number :=&c;
begin
if a>b then
if(a>c) then dbms_output.put_line(a||' is the greatest of the
given numbers. ' );
else dbms_output.put_line(c||' is the greatest of the given
numbers. ' );
end if;
else
if(c>b) then dbms_output.put_line(c||' is the greatest of the
given numbers. ' );
else dbms_output.put_line(b||' is the greatest of the given
numbers. ' );
end if;
end if;
end;
SQL> @ great.sql;
Input :-
Enter the value of a: 5
Enter the value of b: 7
Enter the value of c: 4
Output :-
7 is the greatest of given numbers.
78
20. Program to check whether the given number is Adams number or not!
SQL> ed adams.sql;
declare
a number:=&a;
re1 number;
re2 number;
n number ;
s1 number :=0;
s2 number :=0;
begin
n:=a;
n:=n*n;
while (n!=0)
loop
re1 :=n mod 10;
s1 := re1 + (10*s1);
n := floor(n/10);
end loop;
s1 := sqrt(s1);
while (s1!=0)
loop
re2 :=s1 mod 10;
s2 := re2 + (10*s2);
s1 := floor(s1/10);
end loop;
if s2=a then dbms_output.put_line(' It is ADAMS number ' );
else dbms_output.put_line(' It is not an ADAMS number ');
end if;
end;
SQL> @adams.sql;
Input :- Enter the value of a: 12
Output :- It is an adams number.
79
CURSOR’S PROGRAMS:
1. Write a cursor program to retrieve the details of all the employees using
cursors.
SQL> ed cursor1.sql;
declare
cursor cemp is select empno, ename, job, sal from emp;
vemp cemp%rowtype;
begin
open cemp;
dbms_output.put_line(' Details of the employees :- ');
loop
fetch cemp into vemp ;
exit when (cemp% notfound);
dbms_output.put_line(vempno.empno || ' ' || vempno.ename || ' ' ||
vemp.job || ‘ ‘ || vempno.sal);
end loop;
close cemp;
end;
SQL> @cursor1.sql;
7369 SMITH CLERK 800
7499 ALLEN SALESMAN 1600
7521 WARD SALESMAN 1250
7566 JONES MANAGER 2975
7654 MARTIN SALESMAN 1250
7698 BLAKE MANAGER 2850
7782 CLARK MANAGER 2450
7788 SCOTT ANALYST 3000
7839 KING PRESIDENT 5000
7844 TURNER SALESMAN 1500
7876 ADAMS CLERK 1100
7900 JAMES CLERK 950
7902 FORD ANALYST 3000
7934 MILLER CLERK 1300
80
PL/SQL Procedure successfully completed.
SQL> ed cursor2.sql;
declare
cursor cemp2 is select ename from emp where deptno=20;
vemp cemp2%rowtype;
begin
open cemp2;
dbms_output.put_line('Employees in department 20:-');
loop
fetch cemp2 into vemp;
exit when(cemp2% notfound);
dbms_output.put_line(vemp.ename);
end loop;
close cemp2;
end;
SQL> @cursor2.sql;
Output :-
81
SMITH
JONES
SCOTT
ADAMS
FORD
Table created.
SQL> ed cursor3.sql;
declare
cursor cempr is select empno, sal from emp where deptno=20;
vemp cempr%rowtype;
begin
open cempr;
loop
fetch cempr into vemp;
exit when cempr% notfound;
update emp set sal = vemp.sal+(vemp.sal*.20) where
empno=vemp.empno;
insert into empraise values(vemp.empno, sysdate, vemp.sal*0.20);
end loop;
commit;
close cempr;
end;
82
SQL> @cursor3.sql;
SQL> ed cursor4.sql;
declare
c number(5);
cursor cemp3 is select count(empno) c from emp where deptno=20;
vemp cemp3%rowtype;
begin
open cemp3;
dbms_output.put_line('Employees in department 20:-');
loop
fetch cemp3 into vemp;
exit when(cemp3% notfound);
dbms_output.put_line(vemp.c);
end loop;
close cemp3;
end;
SQL> @cursor4.sql;
Output :-
Employees in department 20:-
5
83
PL/SQL procedure successfully completed.
5. Write a PL/SQL block to count the no. of employees in each job type.
SQL> ed cursor5.sql;
declare
c number(5);
cursor cemp3 is select count(empno) c,job from emp group by job;
vemp cemp3%rowtype;
begin
open cemp3;
loop
fetch cemp3 into vemp;
exit when(cemp3% notfound);
dbms_output.put_line(vemp.job||' - ' ||vemp.c);
end loop;
close cemp3;
end;
SQL> @cursor5.sql;
Output :-
ANALYST - 2
CLERK - 4
MANAGER - 3
84
PRESIDENT - 1
SALESMAN - 4
TRIGGERS’s PROGRAMS :
1. Trigger on Insertion.
Table created.
Trigger created.
Output :-
CONGRATULATIONS!!!
85
1 row created.
2. Trigger on updation.
Trigger created.
Output :-
Success!!!
1 row updated.
86
3. Trigger on updation (Statement Level Trigger).
Trigger created.
Output :-
Success!!!
3 rows updated.
87
02 – APR – 2005 One row successfully updated CLASS
02 – APR – 2005 1 row successfully updated CLASS
02 – APR – 2005 1 row successfully inserted CLASS
SQL> create or replace trigger trigger4 after update on class for each row
2 begin
3 dbms_output.put_line ('Success!!!');
4 insert into log values(sysdate, '1 row Updated ',' CLASS ');
5 end;
6 /
Trigger created.
Output :-
Success!!!
Success!!!
Success!!!
3 rows updated.
88
02 – APR – 2005 1 row updated CLASS
02 – APR – 2005 1 row updated CLASS
02 – APR – 2005 1 row updated CLASS
02 – APR – 2005 One row successfully updated CLASS
02 – APR – 2005 1 row successfully updated CLASS
02 – APR – 2005 1 row successfully inserted CLASS
5. Trigger on Deletion.
Trigger created.
Output :-
A row deleted!!!
1 row deleted.
89
02 – APR – 2005 1 row successfully deleted CLASS
02 – APR – 2005 1 row updated CLASS
02 – APR – 2005 1 row updated CLASS
02 – APR – 2005 1 row updated CLASS
02 – APR – 2005 One row successfully updated CLASS
02 – APR – 2005 1 row successfully updated CLASS
02 – APR – 2005 1 row successfully inserted CLASS
90