Professional Documents
Culture Documents
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 1
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
Company database
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 2
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
desc department2;
DEPT_NAME DEPT_NO
HR 11
RESEARCH 12
MANAGEMENT 13
devpmt 14
recovery 15
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 3
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
desc works_on1;
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 4
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
desc project1;
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 5
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
desc deptloc;
DEPT_NO DEPT_LOC
11hyderabad
12chennai
12hyderabad
13bangalore
14Goa
15Jaipur
6 rows selected.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 6
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
Bank database
desc accounts;
7 rows selected.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 7
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
desc customer;
10 rows selected.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 8
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
desc loan;
7 rows selected.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 9
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
desc borrower;
CUST_NAME LOAN_NO
anand 16
giri 93
shashank 15
sreekanth 14
kishore 17
karthik 11
karthik 23
babu 17
8 rows selected.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 10
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
desc depositors;
CUST_NAME ACC_NO
shashank 102
prided 101
pradeep 201
kishore 217
chanu 222
karthik 215
saleem 305
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 11
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
desc branch;
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 12
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
Sailors_reserves_boats database
desc reserves;
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 13
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 14
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
FNAME LNAME
shashank g
Anand m
prashanth m
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 15
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
FNAME LNAME
babu n
FNAME LNAME
nano t
babu n
Anand m
EMP_ID
111
116
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 16
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
COUNT(DISTINCTSALARY)
5
COUNT(EMP_ID)
7
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 17
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
COUNT(*)
1
FNAME LNAME
shashank g
nano t
babu n
Anand m
sreekanth t
prashanth m
karthik d
7 rows selected.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 18
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
SNAME
bhatt
SID
1111
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 19
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
SNAME
name1
Ans:
BCOLOR
cream
Ans:
SNAME
bhatt
Bob
name1
nano
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 20
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
Ans:
SAGE
20
Ans:
SNAME
bhatt
Bob
name1
nano
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 21
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
Ans:
SNAME
name1
Ans:
SID
1111
1114
Ans:
SNAME
nano
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 22
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
1. Find all loan_no’s for for loans made at ‘hyd’ branch, with loan
amount>1000.
Ans:
LOAN_NO
15
16
2. Find all customers who have a loan from the bank and
find their names and loan_no’s.
Ans:
CUST_NAME LOAN_NO
karthik 11
sreekanth 14
shashank 15
anand 16
babu 17
kishore 17
karthik 23
Giri 93
8 rows
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 23
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
3. Find the names & loan_no’s of all customers who have a loan at branch
‘SBI’.
Ans:
borrower b,loan l
LOAN_NO CUST_NAME
14sreekanth
17babu
17kishore
4. Find the names of all branches that have assets greater than at least one
branch located in ‘bangalore’
Ans:
having(b_city='banglore');
B_NAME
sb3
sb1
sb4
sb7
sb2
sb8
sb5
7 rows selected.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 24
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
5. Find the names of all customers where street address includes sub string
’main’
Ans
CUST_NAME
kishore
shashank
Ans
CUST_NAME
anand
giri
karthik
shashank
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 25
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
UNION
10 rows selected.
Ans
INTERSECT
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 26
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
MINUS
CUST_NAME
chanu
pradeep
saleem
AVG(BALANCE)
807.5
B_NAME AVG(BALANCE)
sb1 500
sb2 400
sb3 807.5
sb4 700
sb5 700
sb6 350
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 27
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
select b.b_name,count(d.cust_name)
group by b.b_name;
B_NAME COUNT(D.CUST_NAME)
sb1 1
sb2 1
sb3 2
sb4 1
sb5 1
sb6 1
6 rows selected.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 28
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
AGREEGATE FUNCTIONS
AVERAGE (AVG()):-
Average Balance
24250
MINIMUM (min()):-
Minimum Balance
2500
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 29
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
MAXIMUM (max()):-
Maximum Balance
65000
COUNT():-
No. of Account
4
COUNT(*):-
No. of Account
4
SUM():-
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 30
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
Total Balance
97000
Second Highest
Max(salary)
15000
Fifth Highest
Max(salary)
7000
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 31
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
Table created;
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 32
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
Table created;
EXIST clause:-
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 33
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
CUST_NO MNAME
B777 Venkatlakshmi
C666 Diwakar
SQL> select cust_no,mname from cust_mstr E where not exists(select *from emp_mstr
where emp_no=E.cust_no);
CUST_NO MNAME
A111 naga
D123 setty
E321 pantulu
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 34
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
IN lists: -
NOT IN lists: -
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 35
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
Allen Salesman 30
Blake Manager 30
King President 10
Ford Annalyst 10
ORDER BY clause:-
GROUPBY clause:-
DEPT_NO COUNT(DEPT_NO)
10 2
20 4
30 2
HAVING
we want to see total salary department wise where the dept wise total salary is above 5000.
For this you have to use HAVING clause. Remember HAVING clause is used to filter groups
and WHERE clause is used to filter rows. You cannot use WHERE clause to filter groups.
select deptno,sum(sal)
from emp
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 36
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
group by deptno
We want to see those departments and the number of employees working in them where the
number of employees is more than 2.
from emp
group by deptno
NO NAME SALARY
201 Venkatlakshmi 30000
202 Diwakar 35000
203 Rohan 15000
204 Monty 25000
NO NAME SALARY
101 Sushma 25000
102 Diwakar 25000
103 Lakshman 10000
104 Krishna 15000
MAXIMUM SALARY:-
SQL>select max(salary)from(select salary from sal1 UNION select salary from sal2);
MAX(SALARY)
35000
MINIMUM SALARY:-
SQL>select min(salary)from(select salary from sal1 UNION select salary from sal2);
MIN(SALARY)
10000
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 38
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 39
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
ACCT_FD_NO CUST_NO
CA612 CA221
CA335 CA120
SB112 SB340
FS771 FS110
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 40
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
UNION CLAUSE: -
SQL>select cust_no"ID",fname||''||lname"Customer|Employee"from
cust_mstr,add_detail where cust_mstr.cust_no=add_detail.code_no AND
add_detail.city='Hyd' AND
add_detail. code_no like'A%'
UNION
select emp_no"ID",fname ||''||lname "Customer|Employee"from
emp_mstr,add_detail where emp_mstr.emp_no=add_detail.code_no AND
add_detail.city='Hyd' AND add_detail.code_no like 'A%';
ID CUSYOMER/EMPLOYEE
A666 Aaaravi
A666 Madhavvenu
INTERSECT CLAUSE: -
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 41
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
CUST_NO
CA120
MINUS: -
CUST_NO
CA120
Joins:
Explanation of joins with an example
Table created
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 42
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
Table created
Inner join:
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 43
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
Outer join:
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 44
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
Cross join
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 45
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
----------
5
10*5
----------
50
10/5
----------
2
Numeric Functions:
ABS(num): Returns absolute value of the given number.(i.e. Always positive value)
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 46
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
CEIL(num): It returns the smallest integer greater than the given number.
CEIL(123.456)
-------------
124
FLOOR(num): It returns the largest integer smaller than the given value.
SQL> SELECT FLOOR(123.456) FROM DUAL;
FLOOR(123.456)
--------------
123
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 47
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
ROUND(m, n): It rounds the given float-valued number “m” to the “n” places after the decimal
ROUND(1.23456,3)
----------------
1.235
SQRT(9)
----------
3
TRUNC(m, n): It truncates given float-valued number “m” to “n” places after the decimal.
TRUNC(1.23456,3)
----------------
1.234
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 48
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
GREATEST(expr1, expr2, …) : It finds the greatest value among the given expressions.
SQL> SELECT GREATEST(4,7,3,5,9,2) FROM DUAL;
GREATEST(4,7,3,5,9,2)
---------------------
9
LEAST(expr1, expr2, …): It finds the Lowest value among the given expressions.
SQL> SELECT LEAST(4,7,3,5,9,2) FROM DUAL;
LEAST(4,7,3,5,9,2)
------------------
2
CHARACTER FUNCTIONS:
UPPER( str) : It converts all letters in the given string “str” into Upper case.
LOWER(str): It converts all the letters in the given string “str” into Lower Case.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 49
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
-------
abcdefg
INITCAP(str): It converts first letter of every word in the given string “str” into Upper Case and
remaining letters into lower case. It is like proper function in FoxPro.
INITCAP
------
Abcdef
LENGTH(str) : This function returns the number of characters in the given string (including
spaces)
LENGTH('ABCD')
--------------
4
LENGTH('ABCD')
--------------
5
SUBSTR(str, m, n) : Will extract “n” characters from the given string starting from
“m th” position.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 50
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
INSTR('
-----------------------------------------------------
27
INSTR(string, str, m, n): It displays nth occurrence of “str” in the string “string” starting from
“m”.
DUMMY
------------
X
LPAD() : This function is used to left pad the the given string with specified character or string.
LPAD
----
ABCD
Explanation: To the given string "BCD" add "A" to the left necessary number of times to make it
a string of 4 characters.
LPAD(
-----
AABCD
Explanation: To the given string "BCD" add "A" to the left necessary number of times to make it
a string of 5 characters.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 51
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
LPA
---
BCD
Explanation: To the given string "BCD" add "A" to the left necessary number of times to make it
a string of 3 characters.
RPAD(): This function is used to left pad the the given string with specified character or string.
RPAD('BILL',
------------
BILL CLINTON
LTRIM(): This function removes specified string from the given string if it is there to the left of
given string.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 52
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
LTRIM
-----
BUSH
RTRIM(): This function removes specified string from the given string if it is there to the right of
given string.
RTRIM('
-------
TONY BL
ASCII('A')
----------
65
TRAN
----
JONN
OTHER FUNCTIONS:
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 53
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
COSH(0)
----------
1
SIN(0)
----------
0
SINH(0)
----------
0
TAN(x): It returns tangent of x.
TAN(0)
----------
0
TANH(0)
----------
0
DATEFUNCTIONS:
ADD_MONTHS(date, n) : Adds n months to the specified date .
ADD_MONTHS
------------------
01-JUN-05
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 54
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
MONTHS_BETWEEN('31-DEC-05','1-JAN-05')
--------------------------------------
11.9677419
MONTHS_BETWEEN('31-JUL-05','1-JUL-05')
--------------------------------------
.967741935
NEXT_DAY(date, ‘day’ ) : It gives date of the next occurrence of the specified day after
the given date.
SQL> SELECT NEXT_DAY('01-JAN-05','FRI') FROM DUAL;
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 55
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
TO_DATE('
---------
01-MAR-05
USER
------------------------------
SCOTT
SYSDATE
---------
1-JAN-05
CONVERSION FUNCTIONS:
TO_CHAR: This function is used to convert a date or number to character string.
SQL> SELECT TO_CHAR(SYSDATE,'DAY DD MONTH YYYY') FROM DUAL
TO_CHAR (SYSDATE,'DAYDDMONTH
---------------------------
SATURDAY 01 JANUARY 2005
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 56
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
TO_CHAR (SYSDA
-------------
01 SAT 01 05
NVL() Function: This function is used to substitute any null value with a user-defined value.
In the above table except for 7499, 7521, 7654 and 7844 all others commissions are null
To display their commission as “0” (zero)
SQL> SELECT EMPNO, ENAME, SAL, NVL (COMM, 100) FROM EMP
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 57
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
SQL> SELECT EMPNO, ENAME, SAL, NVL (COMM, 888) FROM EMP
In above queries we have seen how to substitute a value when the comm is null.
If one want to display "Commission not payed" against the employees who have no
commission we can write the following query.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 58
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
SUBQUERIES
A query nested within a query is known as subquery.
For example, you want to see all the employees whose salary is above average
salary. For this you have to first compute the average salary using AVG function
and then compare employees salaries with this computed salary. This is possible
using subquery. Here
the sub query will first compute the average salary and then main query will
execute.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 59
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
Select * from emp where sal > (select avg(sal) from emp);
Similarly we want to see the name and empno of that employee whose salary is
maximum.
We want to see how many employees are there whose salary is above average.
We want to see those employees who are working in Hyderabad. Remember emp
and dept are joined on deptno and city column is in the dept table. Assuming that
wherever the department is located the employee is working in that city.
For example the following query returns the top 5 salaries from employees table.
Select sal from (select sal from emp order sal desc)
where rownum <= 5;
To see the sum salary deptwise you can give the following query.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 60
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
Now to see the average total salary deptwise you can give a sub query in FROM
clause.
WITH
The above average total salary department wise can also be achieved in 9i using
WITH clause given below
WITH DEPTOT AS (select sum(sal) as dsal from emp
group by deptno)
select avg(dsal) from deptot;
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 61
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
OUTPUT:
1
2
3
4
5
6
7
8
9
10
area number(14,2);
begin
radius:=3;
while radius<=7
loop
area:=pi*power(radius,2);
insert into areas values(radius,area);
radius:=radius+1;
end loop;
end;
OUT PUT:-
RADIUS AREA
3 28.26
4 50.24
5 78.5
6 113.04
7 153.86
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 64
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
dbms_output.put_line('fibbonacci series');
dbms_output.put_line(n);
dbms_output.put_line(n1);
while n2 < 20
loop
n2:=n+n1;
dbms_output.put_line(n2);
n:=n1;
n1:=n2;
end loop;
End;
OUTPUT:-
fibbonacci series
0
1
1
2
3
5
8
13
21
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 65
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
OUTPUT:
1
3
5
7
9
11
Output:-
old 5: n:=&n;
new 5: n:=30;
enter a number
display the numbers from 30 to 40:
30
31
32
33
34
35
36
37
38
39
40
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 67
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
fact number(10):=1;
temp number(10);
begin
num:=#
temp:=num;
while num>0
loop
fact:=fact*num;
num:=num-1;
end loop;
insert into facto values(temp,fact);
end;
OUTPUT:-
NUM FACT
5 120
set serverout on
declare
i number:=1;
n number(10):=&n;
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 68
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
counts number:=0;
begin
while i<=n
loop
if n mod i=0
then
counts:=counts+1;
endif;
I:=I+1;
End loop;
If counts=2
Then
dbms_output.put_line(‘prime’);
else
dbms_output.put_line(‘not prime’);
end if;
end;
OUT PUT:-
old 3: n number(10):=&n;
new 3: n number(10):=6;
not prime
PL/SQL procedure successfully completed.
Function:-
8) Write a PL/SQL program to find the area of the circle with the
function
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 69
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
return number
is
pi constant number(9,7):=3.14;
area number(14,2);
begin
area:=pi*power(radius,2);
return area;
end;
OUT PUT:-
CASE
ELSE
return 'Invalid score';
END CASE;
end Get_Grade1;
2.
create or replace function Get_Grade2(score IN NUMBER)
RETURN VARCHAR2
is
grade VARCHAR2(15);
begin
grade := CASE
WHEN score BETWEEN 80 AND 100 THEN 'A'
WHEN score BETWEEN 65 AND 79 THEN 'B'
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 71
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
return grade;
end Get_Grade2;
Procedure:
EXCEPTION HANDLING:
set serveroutput on
DECLARE
Num_a NUMBER := 6;
Num_b NUMBER;
BEGIN
Num_b := 0;
Num_a := Num_a / Num_b;
Num_b := 7;
dbms_output.put_line(' Value of Num_b ' || Num_b);
EXCEPTION
WHEN ZERO_DIVIDE
THEN
dbms_output.put_line('Trying to divide by zero');
dbms_output.put_line(' Value of Num_a ' || Num_a);
bms_output.put_line(' Value of Num_b ' || Num_b);
END;
OUT PUT:
Trying to divide by zero
Value of num_a 6
Value of num_b 0
Pl/sql procedure successfully completed.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 73
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
O/P:
CONVERSION TO STRING TO NUMBER FAILED
PL/SQL PROCEDURE SUCCESFULLY COMPLETED.
Triggers:-
Syntax of Triggers
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 74
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 75
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
1) BEFORE UPDATE, Statement Level: This trigger will insert a record into
the table 'product_check' before an sql update statement is executed, at the
statement level.
CREATE or REPLACE TRIGGER Before_Update_Stat_product
BEFORE
UPDATE ON product
Begin
INSERT INTO product_check
Values('Before update, statement level',sysdate); END;
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 77
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
2) BEFORE UPDATE, Row Level: This trigger will insert a record into the
table 'product_check' before each row is updated.
CREATE or REPLACE TRIGGER Before_Upddate_Row_product
BEFORE
UPDATE ON product
FOR EACH ROW
BEGIN
INSERT INTO product_check
Values('Before update row level',sysdate);
END;
3) AFTER UPDATE, Statement Level: This trigger will insert a record into
the table 'product_check' after a sql update statement is executed, at the
statement level.
CREATE or REPLACE TRIGGER After_Update_Stat_product
AFTER
UPDATE ON product
BEGIN
INSERT INTO product_check
Values('After update, statement level', sysdate);
End;
4) AFTER UPDATE, Row Level: This trigger will insert a record into the
table 'product_check' after each row is updated.
CREATE or REPLACE TRIGGER After_Update_Row_product
AFTER
insert On product
FOR EACH ROW
BEGIN
INSERT INTO product_check
Values('After update, Row level',sysdate);
END;
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 78
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
/
Now lets execute a update statement on table product.
UPDATE PRODUCT SET unit_price = 800
WHERE product_id in (100,101);
Lets check the data in 'product_check' table to see the order in
which the trigger is fired.
SELECT * FROM product_check;
Output:
Mesage Current_Date
------------------------------------------------------------
Before update, statement level 26-Nov-2008
Before update, row level 26-Nov-2008
After update, Row level 26-Nov-2008
Before update, row level 26-Nov-2008
After update, Row level 26-Nov-2008
After update, statement level 26-Nov-2008
The above result shows 'before update' and 'after update' row level events
have occured twice, since two records were updated. But 'before update' and
'after update' statement level events are fired only once per sql statement.
The above rules apply similarly for INSERT and DELETE statements.
DESC USER_TRIGGERS;
NAME Type
--------------------------------------------------------
TRIGGER_NAME VARCHAR2(30)
TRIGGER_TYPE VARCHAR2(16)
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 79
VIGNAN INSTITUTE OF TECHNOLOGY & SCEINCE.DESHMUKHI DBMS Lab Manual
The above sql query provides the header and body of the trigger
'Before_Update_Stat_product'.
Dept of Computer Science & Engineering LAB Manual for 2008 -2009 (II – II) 80