You are on page 1of 45

DBMS ( CS 592)

Introduction to SQL
WEEK 1:
In This Course ou !i"" "e#rn:
B#sic SQL
$d%#nced SQL
&L'SQL Conce(ts
$ (ro)ect on *r#c"e usin+ SQL #nd
&L'SQL(With ,B -ront end)
Structured Quer L#n+u#+e ( SQL ) is # co.(uter "#n+u#+e /or co..unic#tion !ith
d#t#0#se1
SQL is # o(en "#n+u#+e de%e"o(ed 0 IBM !hose ru"es h#%e 0een set 0 # st#nd#rds
co..ittee1
SQL h#s .#n c#(#0i"ities2 0ut the .ost co..on needs in 0usiness #re to:
3e#d e4istin+ d#t#
Cre#te ne! records ho"din+ d#t#
Ch#n+e e4istin+ d#t#
De"ete d#d#
SQL is not # (ro+r#. or # de%e"o(.ent en%iron.ent such #s $ccess or ,B1 SQL is #
(ure "#n+u#+e1
-ront5end so/t!#re inter/#ces !ith the user1 B#c65end so/t!#re ho"ds d#t#1 SQL is the
st#nd#rd "#n+u#+e /or the t!o (ieces o/ so/t!#re to co..unic#te !ith e#ch other1
D#t#: A set of information with some aspect in common.
For example data for employees might include social security numbers:
7 12895:;<9 3#)# 3o2 2895:;<9= Shi"# Sen2 895:;<9=1 $"o6e D#s 7
Met#d#t#: Information about the structure and organization of data in a database.
Metadata typically contains descriptions of the tables and their constraints, the fields and
their sizes and rules, and the relationships between the tables. Some people hae phrased
metadata as ! data about the data !. An example of metadata for an employee database:
T#0"e >1 n#.e? E.("oees /ie"d count? 8
-ie"d >1 n#.e? E.("oeeID t(e? inte+er si@e? 11 di+its ( e4#ct" )
-ie"d >2 n#.e? A#.e-irst t(e? strin+ si@e? B?8= ch#r#cters
-ie"d >1 n#.e? n#.eL#st t(e? strin+ si@e? B?8= ch#r#cters
This .et#d#t# descri0es th#t !e h#%e # set o/ d#t# #0out e.("oees th#t is or+#ni@ed
into # t#0"e1 Within th#t t#0"e !e ho"d three 6inds o/ in/or.#tion #0out e#ch e.("oee2
their ID nu.0er2 #nd their /irst #nd "#st n#.e1
D#t#0#se: $ d#t#0#se is the d#t# #nd the .et#d#t#2 in other !ords # d#t#0#se is # set
o/ re"#ted in/or.#tion #s !e"" #s # descri(tion o/ ho! th#t in/or.#tion is or+#ni@ed1 $
d#t#0#se /or e.("oees !ou"d 0e the .et#d#t# !e )ust "oo6ed #t2 ("us the in/or.#tion
descri0ed #0o%e under d#t#
E.("oee ID A#.e-irst A#.eL#st
12895:;<9 3#)# 3o
2895:;<9= Shi"# Sen
895:;<9=1 $"o6e D#s
D#t# ser%er: A data Serer manages data and is typically one or more many serers in an
enironment.
D#t#0#se M#n#+e.ent Sste. (DBMS): Software which handles most aspects of data
management including physical storage, reading and writing data, security, replication, error
correction and other functions.
SQL C*MM$ADS
DDL( D$T$ DE-IAITI*A L$ACD$CE )
$/ter co.("etin+ this "esson2 ou shou"d 0e #0"e to do the /o""o!in+:
Descri0e the .#in d#t#0#se o0)ects
Cre#te t#0"es
$"ter t#0"e de/initions
Dro(2 ren#.e2 #nd trunc#te t#0"es
Descri0e constr#ints
Cre#te #nd .#int#in constr#ints
D#t#0#se *0)ects or Sche.#
*0)ect Descri(tion
T#0"e B#sic unit o/ stor#+eE co.(osed o/ ro!s #nd co"u.ns
,ie! Lo+ic#"" re(resents su0sets o/ d#t# /ro. one or .ore t#0"es
SeFuence Au.eric %#"ue +ener#tor
Inde4 I.(ro%es the (er/or.#nce o/ so.e Fueries
Snon. Ci%es #"tern#ti%e n#.es to o0)ects
A#.in+ 3u"es /or t#0"es #nd co"u.ns
Must 0e+in !ith # "etter
Must 0e 1G8= ch#r#cters "on+
Must cont#in on" $GH2 #G@2 =G92 I2 J2 #nd >
Must not du("ic#te the n#.e o/ #nother o0)ect o!ned 0 the s#.e user
Must not 0e #n *r#c"e ser%er reser%ed !ord
11 C3E$TE
SQLKCre#te t#0"e e.( (
Eno nu.0er(5)2
En#.e %#rch#r2(2=)2
E#dd %#rch#r2(8=)2
EI)oinId d#te)E
Constr#ints
1. Not Null, 2. Check
1. Primary key, 2. Unique key
Foreign key
11 SQLKCre#te t#0"e e.("
Eno nu.0er(5) constr#int enoInotInu"" Aot Au""2
En#.e %#rch#r2(2=)2
E#dd %#rch#r2(8=)2
EI)oinId d#te)E
21 SQLKCre#te t#0"e e.((
Eno nu.0er(5) constr#int enoIchec6 Chec6 ( eno K 1== )2
5555555555555555
5555555555555555
5555555555555555)E
81 SQLKCre#te t#0"e e.("
Eno nu.0er(5) constr#int uniIeno DniFue2
55555555555555
55555555555555
55555555555555)E
or
SQLKCre#te t#0"e e.("
Eno nu.0er(5)2
En#.e %#rch#r2(2=)2
E#dd %#rch#r2(8=)2
EI)oinId d#te2 constr#int uniIeno DniFue(eno))E
91 SQLKCre#te t#0"e e.("
Eno nu.0er(5) constr#int (Ieno &ri.#r 6e2
5555555555555555
5555555555555555
5555555555555555)E
or
SQLKCre#te t#0"e e.("
Eno nu.0er(5)2
En#.e %#rch#r2(2=)2
DserIcre#ted
Co"u.n n#.e
E#dd %#rch#r2(8=)2
EI)oinId d#te2 constr#int (Ieno &ri.#r 6e( eno))E
51 SQLKCre#te t#0"e e.(1(
#no number"$% constr#int /6Ieno re/erences e.((eno),
#&type char"'%,
#&salary number"(,'%%)
*r
SQLKCre#te t#0"e e.(1(
#no number"$%,
#&type char"'%,
#&salary number"(,'%,constr#int /6Ieno /orei+n 6e(eno) re/erences e.((eno))
21 $LTE3
Adding columns in an exiting table.
11 S*+,$"ter t#0"e e.( #dd ( EI(incode ch#r(:)2 EI te"e(hone ch#r(;)2 111111111)E
Modifying columns in an exiting table.
21 S*+,$"ter t#0"e e.( .odi/ ( Eno nu.0er(9)2 E#dd %#rch#r2(2=)2 111111111)E
-o add .onstraints after creating a table.
81 SQLK $"ter t#0"e e.( #dd constr#int enoIchec6 Chec6 ( eno K 1== ))
91 SQLK $"ter t#0"e e.( #dd constr#int (Ieno &ri.#r 6e (eno)E
51 SQLK $"ter t#0"e e.( #dd constr#int uniIeno DniFue (eno)E
:1 SQLK $"ter t#0"e e.(1 #dd constr#int /6Ieno /orei+n 6e(eno)
re/erences e.((eno)%)
/nly for 0ot 0ull constraint it will be
;1 SQLK $"ter t#0"e e.( .odi/ eno constr#int enoInotInu"" Aot Au""E
For delete the constraints
<1 SQLK $"ter t#0"e e.( dro( constr#int (IenoE
81 D3*&
11 SQLK Dro( t#0"e e.(E
21 SQLK #"ter t#0"e e.( D3*& constr#int enoInotInu""E
91 T3DAC$TE
11 SQLK Trunc#te t#0"e e.(E
B#se T#0"e n#.e
Co"u.n o/ the
e.( t#0"e !hich
contents (ri.#r
6e
Co"u.n o/ the e.(1 t#0"e
Eno !#s Au.0er(5) #nd
E#dd !#s %#rch#r2(8=)
$ttention
Constr#int n#.e
T#0"e n#.e
De"ete #"" records /ro. the e.( t#0"e
$SSICAMEAT51
1ou should create the following table with your class roll number li2e emp&'3 where class roll number 4 '3.
5. .reate table #M6 with the following structure.
'. 7isplay the structure of #M6 table.
3. Modify the #M6 table add a new column named !7#6-0/8 with data type integer
9. .reate table 7#6- with the following structure.
$. Modify the #M6 table add referential integrity on !7#6-0/8 with the parent 2ey 7#6-"7eptno%.
:. Modify the 7#6- table delete column !.ity8.
;. Modify the #M6 table delete the constraint from s#".
(. create a duplicate table of 7ept with the name !7#6-&7<68
=. change the name !7#6-&7<68 to !7<6&7#6-8
5>. delete the table 7<6&7#6-
Name Constraint Type
Deptno PRIMARY KEY Number(2)
Dname BLOCK LETTER Varcar2(!")
Locat#on CAL $ M%M $ DEL Varcar2(&)
C#t' Varcar2((")
Name Constraint Type
Empno PRIMARY KEY Number())
Ename BLOCK LETTER Varcar2(!")
*ob Varcar2(&)
M+r Number())
,#re-ate Date
.a/ 01!""" AND 21!"""" Number(342)
Comm Number(342)
WEEK 2:
DML( D$T$ M$AI&DL$TI*A L$ACD$CE )
11 IASE3T
The Insert co..#nd is used to #dd one or .ore ro!s to # t#0"e1Whi"e usin+ this co..#nd the %#"ues
#re se(#r#ted 0 co..#s #nd the d#t#t(es ch#r2%#rch#r2 #nd d#te #re enc"osed in #(ostro(hes1 The
%#"ues .ust 0e entered in the s#.e order #s the #re de/ined in the t#0"e1
11 SQLK Insert into e.( %#"ues ( 12 L$3DAL2 LC$LCDTT$L2 L125M$A59<L)E
21 SQLK Insert into e.( %#"ues ( N$2 LNBL2 LNCL2 LNDL)E
S*+, ?A+<# /F $: 1
S*+, ?A+<# /F B: $3DA
S*+, ?A+<# /F C: C$LCDTT$
S*+, ?A+<# /F D: 125M$A59<
For 'nd record enter
SQLK '
se"ect n#.e /ro. e.(+h81 SQLK Insert into 444 ( Se"ect O /ro. e.( )E
21 SELECT
To (er/or. # Fuer !e use the se"ect co..#nd1 The Fuer is # reFuest /or in/or.#tion1
11 SQLK Se"ect O /ro. e.(E
21 SQLK Se"ect eno2en#.e /ro. e.(E
81 SQLK Se"ect distinct eno /ro. e.(E
91 SQLK cre#te t#0"e 444 #s Se"ect O /ro. e.(E
aboe example create a new table xxx from the exiting table emp along with its records.
ART!"#TC $P#RAT$R%
To (er/or. c#"cu"#tions 0#sed on nu.0er %#"ues2 !e inc"ude #rith.etic e4(ressions in SQL co..#nd1
The #rith.etic o(er#tors #re P (#ddition)2 5(su0tr#ction)2 O(.u"ti("ic#tion) #nd
'(di%ision)1
51 SQLK Se"ect (rodid2 std(rice2 .in(rice2 std(rice P .in(rice /ro. (rice
!here endd#te ? L95)un5<9LE
Say price is a table and columns are prodid, stdprice, minprice, enddate. So
std(riceP.in(rice is not a column in the table price, yet S*+@6+<S would display it as a
separate column.
:1 SQLK Se"ect ordid2 ite.id2 1==O( #ctu#"(rice P Ft ) /ro. ite.
!here (rodid ? 1==E
C$"PAR%$N $P#RAT$R%
Co(ies #"" the ro!s /ro. e.( t#0"e to 4441 The structure
o/ the t#0"e 444 is the s#.e #s th#t o/ e.(1
Se"ection o/ du("ic#te ro!s2
here eno is the co"u.n n#.e
Co.(#tison o(er#tors #re used in conditions to co.(#re one e4(ression !ith #nother1 The co.(#rison
o(er#tors #re ? 2 Q? 2 B 2 K 2 B? 2 K? 2 0et!een ( to chec6 0et!een #n t!o %#"ues)2
In ( to .#tch !ith #n o/ the %#"ues in the "ist )2 "i6e ( to .#tch # ch#r#cter (#ttern ) 1
;1 SQLK Se"ect O /ro. orderIin/o !here tot#" K ;===E
<1 SQLK se"ect O /ro. orderIin/o !here not ( custid ? 2 or custid ? 9 )E
91 SQL K Se"ect O /ro. orderIin/o !here shi(d#te
in ( L125)#n5<<L2 L8=5)#n5<9L2 L155)#n59=L )E
-he IA operator can be used to select rows that match one of the alues in the list.
1=1 SQLK Se"ect n#.e2 #ddress2 cit /ro. custo.er !here n#.e "i6e L.RLE
7isplay the columns name, address, city whose name begins with AmA.
111SQLK Se"ect O /ro. custo.er !here n#.e "i6e L%I(LE
+ists all the columns customer whose names are three letters long, starting with A%L and
ending with A(L.
&$'CA& $P#RAT$R%
$ "o+ic#" o(er#tor is used to co.0ine the resu"ts o/ t!o conditions to (roduce # sin+"e resu"t1
The "o+ic#" o(er#tors #re $AD2 A*T2 *31
121 SQLK Se"ect O /ro. orderIin/o
!here shi(d#te K L=95)#n5<<L $AD tot#" B 9===E
81 D&D$TE
D(d#te co..#nd is used to #"ter the co"u.n %#"ues in # t#0"e1 The u(d#te co..#nd consists o/ #
LsetL c"#use #nd #n o(tion#" L!hereL c"#use1
11SQLK D(d#te e.( set En#.e ? LT$3DAL !here Eno ? 1=E
91 DELETE
$/ter insertin+ ro!s in # t#0"e !e c#n #"so de"ete the. i/ reFuired1 The de"ete co..#nd consists
o/ # L/ro. c"#useL /o""o!ed 0 #n o(tion#" L!here c"#useL1
11SQLK De"ete /ro. e.( !here Eno ? 1=E
The co..#nd !i""
dis("# #"" ro!s !here
custid is neither 2 nor 9
T#0"e A#.e
$SSICAMEAT52
5. Insert all the following data into #M6 table.
'. 7isplays all columns and all rows of the #M6 table.
3. 7isplay specific columns of the #M6 table li2e employee name, salary B department number.
9. .alculate a salary increase of 3>> for all employees and displays a new SalC3>> column in the output.
$. insert a new record and gie the data into empno, ename, sal and deptno columns only
:. 7isplays the employee name B annual compensation of employees. It calculates the annual
compensation as 5' multiplied by the monthly salary, plus a one time bonus of 5>>.
;. 7isplays employee details as DI0E is a 6F#SI7#0- with column heading #mployee 7etails. " using
concatenation operator, GG B column alias %
(. 7isplays distinct department number from #M6 table.
=. 7isplays employee name, Hob title B department number of all employees whose Hob title is .+#FD.
5>. 7isplays name, salary B commission from the #M6 table except MA0AE#F.
55. 7isplays name B salary whose salary is between 5>>> and '>>>.
5'. 7isplays employee number, name, salary B managerIs employee number of all the employees whose
managerIs employee number are ;=>', ;$:: or ;;((.
53. 7isplays all employee names starting with S.
59. 7isplays all employee names ending with F.
5$. Increase the salary of all employees with $J of sal.
5:. Increase the salary of all managers with 5$J of sal.
5;. update all the comm. Kith '>> whose comm. Are 0<++.
Empno Ename *ob M+r ,#re-ate .a/ Comm Deptno
;(3= DI0E 6F#SI7#0- 5;L0/?L(5 $>>> 5>
;:=( M+AD# MA0AE#F ;(3= >5LMA1L(5 '($> 3>
;;(' .+AFD MA0AE#F ;(3= >=LN<0L(5 '9$> 5>
;$:: N/0#S MA0AE#F ;(3= >'LA6FL(5 '=;$ '>
;:$9 MAF-I0 SA+#SMA0 ;:=( '(LS#6L(5 5'$> 59>> 3>
$9== A++#0 SA+#SMA0 ;:=( '>LF#ML(5 5:>> 3>> 3>
;(99 -<F0#F SA+#SMA0 ;:=( >(LS#6L(5 5$>> > 3>
;=>> NAM#S .+#FD ;:=( >3L7#.L(5 =$> 3>
;$'5 KAF7 SA+#SMA0 ;:=( ''LF#ML(5 5'$> $>> 3>
;=>' F/F7 A0A+1S- ;$:: >3L7#.L(5 3>>> '>
;3:= SMI-O .+#FD ;=>' 5;L7#.L(5 (>> '>
;;(( S./-- A0A+1S- ;$:: >=L7#.L(' 3>>> '>
;(;: A7AMS .+#FD ;;(( 5'LNA0L(3 55>> '>
;=39 MI++#F .+#FD ;;(' '3LNA0L(' 53>> 5>
WEEK 8 :
SQLO&"us -unctions
Sin+"e53o! -unctions
-hese functions operate on single rows only and return one result per row. -here are different types of single
row functions.
5. Ch#r#cter /unctions: accept character input and return both character and number
alues.
'. Au.0er /unctions: Accept numeric input and return numeric alues.
3. D#te /unctions: /perate on alues of the date datatype. All date functions return a
alue of date datatype except the M*ATSSIBETWEEA function, which return a
number.
9. Con%ersion /unctions: .onert a alue from one datatype to another.
$. Cener#" /unctions:
L 0?+ function
L 7#./7# function
-unctions
Sin+"e5ro!
/unctions
Mu"ti("e5ro!
/unctions
Sin+"e5ro!
/unctions
Ch#r#cter
Cener#"
Au.0er
D#te Con%ersion
C#se con%ersion /unctions
-unction 3esu"t
L*WE3( T SQL Course T )
D&&E3 (T SQL Course T )
IAITC$& (T SQL Course T )
sF" course
SQL C*D3SE
SF" Course
Ch#r#cter .#ni(u"#tion /unctions
-unction 3esu"t
C*AC$T( T CoodU2 T Strin+ T )
SDBST3 (T Strin+ T 2 1 2 8 )
LEACTS (T Strin+ T )
IAST3 ( T Strin+ T2 TrU )
L&$D ( s#" 2 1=2 TOU)
CoodStrin+
Str
:
8
OOOOOO5=== ( e4#.("e on" )
3*DAD: Founds alue to specified decimal
3*DAD" 9$.=':,' % 9$.=3
T3DAC: -runcates alue to specified decimal
T3DAC" 9$.=':,' % 9$.='
M*D: Feturns remainder of diision
M*D" 5:>>, 3>> % 5>>
Ch#r#cter /unctions
C#se con%ersion
/unctions
Ch#r#cter .#ni(u"#tion
/unctions
L*WE3
D&&E3
IAITC$&
C*AC$T
SDBST3
LEACTS
IAST3
L&$D
Au.0er -unctions
Wor6in+ !ith D#tes
/racle stores dates in an internal numeric format: .entury,
year, month, day, hours, minutes, seconds.
-he default date format is 77LM/0L11.
S1S7A-# is a function returning date and time.
7<A+ is a dummy table used to iew S1S7A-#.
,#"id *r#c"e d#tes #re 0et!een M#nu#r 12 9;12 B1C1 2 #nd Dece.0er 812 9999 $1D1
S1S7A-# is a date function that returns the current date and time. 1ou can use S1S7A-# Hust as you would
use any other column name. For example, select S1S7A-# from 7<A+. " dummy table %
-he 7<A+ table is owned by the user S1S and can be accessed by all users. It contains one column,
7<MM1, and one row with the alue P. -he 7<A+ table is useful when you want to return a alue once only
Q for instance, the alue of a constant, pseudocolumn or expression that is not deried from a table with user
data.
$ddI.onths
The /or.#t is #ddI.onths(d2n)2 !here d is the d#te #nd n re(resents the nu.0er o/ .onths1
SQLK Se"ect #ddI.onths(ssd#te22) /ro. du#"E
-he result will be A'$LmayL==A, if sysdate is A'$LmarL==A.
MonthsI0et!een
To /ind out the nu.0er o/ .onths 0et!een t!o d#tes2 !e use the .onthsI0et!een /unction1
The /or.#t is .onthsI0et!een(d12d2)2 !here d12d2 #re d#tes1
SQLK Se"ect .onthsI0et!een(toId#te(L=15=55;;L2Ldd5..5L)2
ToId#te(L=15=85;;L2Ldd5..5L)) Vd#teV /ro. du#"E
-he output will be a number and is '.
Ae4tId#
The /or.#t is ne4tId#(d2d#) !here d re(resents d#te #nd d# i.("ies #n !ee6d#1
SQLK Se"ect ne4tId#(ssd#te2LTuesd#L) /ro. du#"E
If sysdate is A'$LmarL==A, then A3>LmarL==A will be displayed which is the next -uesday.
The e4#.("es th#t /o""o! .ost" uses the sste. t#0"e Ldu#"L1 It is # t#0"e !hich is
#uto.#tic#"" cre#ted 0 *r#c"e #"on+ !ith the d#t# diction#r1 Du#" t#0"e h#s one co"u.n
de/ined to 0e o/ %#rch#r2 t(e #nd cont#ins on" one ro! !ith %#"ue L4L1
3ound
This /unction returns the d#te !hich is rounded to the unit s(eci/ied 0 the /or.#t .ode"1
The /or.#t is round(d2W/.tX) !here d is the d#te #nd /.t is the /or.#t .ode"1
SQLK Se"ect round(toId#te(L285oct59:L2Ldd5.on5L)2Le#rL) /ro. du#"E
A>5LHanL=;A will be the result.
SQLK Se"ect round(toId#te(L285oct59:L2Ldd5.on5L)2L.onthL) /ro. du#"E
A>5LnoL=:A will be the result.
Trunc#te
This /unction returns the d#te !ith the ti.e (ortion o/ the d# trunc#ted to the unit s(eci/ied 0 the
-or.#t .ode"1The /or.#t is trunc(d2W/.tX) !here d is the d#te #nd /.t is the /or.#t .ode"1
SQLK Se"ect trunc(toId#te(L285oct59:L2Ldd5.on5L)2Le#rL) /ro. du#"E
A>5LHanL=:A will be the result.
SQLK Se"ect trunc(toId#te(L285oct59:L2Ldd5.on5L)2L.onthL) /ro. du#"E
A>5LoctL=:A will be the result.
C!ARACT#R FUNCT$N%
-DACTI*A IA&DT *DT&DT
Initc#((ch#r) Se"ect initc#((Lhe""oL) /ro. du#"E Se""o
Lo!er(ch#r) Se"ect "o!er(L-DAL) /ro. du#"E /un
D((er(ch#r) Se"ect u((er(Lsu.L) /ro. du#"E SDM
Ltri.(ch#r2set) Se"ect "tri.(L4@#d#.sL2L4@L)
/ro. du#"E
$d#.s
3tri.(ch#r2set) Se"ect rtri.(L4@#d#.sL2L#.sL)
/ro. du#"E
Y@#d
Tr#ns"#te(ch#r2/ro.2t
o)
Se"ect tr#ns"#te(L)#c6L2L)L2L0L) /ro.
du#"E
B#c6
3e("#ce(ch#r2se#rchs
5trin+2Wre( strin+X)
Se"ect re("#ce(L)#c6 #nd )ueL2L)L2L0"L)
/ro. du#"E
B"#c6 #nd
0"ue
Su0str(ch#r2.2n) Se"ect su0str(L#0cde/+L2822) /ro. du#"E Cd
Soundex, which is also a character function compares words that are spelled differently,
but sound ali2e..onsider the following example.
SQLK Se"ect n#.e /ro. custo.er !here sounde4(n#.e) ? sounde4(Ls.theL)E
/ther two functions are "(#d B r(#d.
SQLK Se"ect "(#d(L#0cL2152L?L) /ro. du#"E
-his function returns the sign A4A before the character string AabcA. -he output gies the
sign A4A before the word function. -he entire string is 5$ in length after the padding is
done.
SQLK Se"ect r(#d(L#0cL2152L?L) /ro. du#"E
-he rpad function pads the alue gien to the right of the string.
NU"#RC FUNCT$N%
-DACTI*A IA&DT *DT&DT
$0s Se"ect #0s(515) /ro. du#"E 15
Cei"(n) Se"ect cei"(991;;<) /ro. du#"E 95
Cosh(n) Se"ect cosh(=) /ro. du#"E 1
E4((n) Se"ect e4((9) /ro. du#"E 59159<15
-"oor(n) Se"ect /"oor(1==12) /ro. du#"E 1==
&o!er(.2n) Se"ect (o!er(922) /ro. du#"E 1:
Mod(.2n) Se"ect .od(1=28) /ro. du#"E 1
3ound(.2n) Se"ect round(1==125:22) /ro. du#"E 1==12:
Trunc(.2n) Se"ect trunc(1==125:22) /ro. du#"E 1==125
SFrt(n) Se"ect sFrt(9) /ro. du#"E 2
C$N(#R%$N FUNCT$N%
ToIch#r( )
-he function is to&char"d,RfmtS%, where d is the date, fmt is the format model which
specifies the format of date. -o&char conersion function conerts date to a alue of
archar' datatype in a form specified by date format fmt.
SQLK Se"ect toIch#r(ssd#te2 Lddth Vo/V /.onth L) /ro. du#"E
-he date will be displayed as ';th of /ctober 5===, if sysdate is A';LoctL==A.
ToId#te( )
-he format is to&date"char,RfmtS%. -his conerts char or archar' datatype to date
datatype.
SQLK Se"ect toId#te(L)#nu#r 15 1999L 2 L.onth5dd5L ) /ro. du#"E
A5$LHanL==A will be the result.
'R$UP FUNCT$N
C3*D& -unctions
<nli2e single row functions, group functions operate on sets of rows to gie one result per group. -hese sets
may be the whole table or the table split into groups.
-ypes of Eroup Functions
$,C
C*DAT
M$Y
MIA
SDM
Euidelines for using Eroup Functions
DISTIACT .#6es the /unction consider on" nondu("ic#te %#"ues1
$"" +rou( -unctions e4ce(t C*DAT(O) i+nore nu"" %#"ues1 To su0stitute # %#"ue /or nu"" %#"ues2 use the
A,L /unction1
Zou c#n use $,C2 SDM2 MIA2 #nd M$Y /unctions #+#inst co"u.ns th#t c#n store nu.eric d#t#1
Zou c#n use M$Y #nd MIA /unctions /or #n d#t#t(e1
C*DAT(O) returns the nu.0er o/ ro!s in # t#0"e2 inc"udin+ du("ic#te ro!s #nd ro!s cont#inin+ nu""
%#"ues1
C*DAT(e4(r) returns the nu.0er o/ nonnu"" ro!s1
SQLK se"ect C*DAT(co..) /ro. e.(E
Crou( -unctions i+nore nu"" %#"ues in the co"u.n1
SQLK se"ect $,C(co..1) /ro. e.(E
Dse the A,L /unction !ith Crou( /unction to inc"ude nu"" %#"ues1
SQLK se"ect $,C(A,L(co..2=)) /ro. e.(E
.reating Eroups of 7ata : C3*D& BZ C"#use1
1ou can use the EF/<6 M1 clause to diide the rows in a table into groups.
$"" co"u.ns in the SELECT "ist th#t #re not in +rou( /unctions .ust 0e in the C3*D& BZ c"#use1
SQLK se"ect de(tno2 $,C(s#") /ro. e.( C3*D& BZ de(tnoE
The C3*D& BZ co"u.n does not h#%e to 0e in the se"ect "ist1
SQLK se"ect $,C(s#") /ro. e.( C3*D& BZ de(tnoE
Dsin+ the C3*D& BZ C"#use on Mu"ti("e Co"u.ns
SQLK se"ect de(tno2 )o02 SDM(s#") /ro. e.( C3*D& BZ de(tno2 )o0 E
1ou can return summary results for groups and subgroups by listing more than one EF/<6 M1 column.
Oere is how the S#+#.- statement aboe, containing a EF/<6 M1 clause, is ealuated :
-he S#+#.- clause specifies the column to be retrieed:
7epartment number in the #M6 table
Nob title in the #M6 table
-he sum of all the salaries in the group you specified in the EF/<6 M1 clause
-he FF/M clause specifies the tables that the database must access : the #M6 table
-he EF/<6 M1 clause specifies how you must group the rows :
First the rows are grouped by department number
Second within the department number groups, the rows are grouped by Hob title
So the S<M function is being applied to the salary column for all Hob titles within each department number
group.
S*+, select deptno, count"ename% from emp)
Kheneer you use a mixture of indiidual items"7#6-0/% and group functions"./<0-% in the same
S#+#.- statement, you must include a EF/<6 M1 clause that specifies the indiidual items " in this case,
7#6-0/%. If the EF/<6 M1 clause is missing, then the error message ! not a singleLgroup group function !
appears.
S#%in+ c"#use
-he AOaing clauseA is used to specify certain conditions on rows, retrieed by using
group by clause. -he result of the following example will be same as the aboe
except that the count should be greater than '.
SQLK Se"ect .#4(custid)2re(id /ro. custo.er +rou( 0 re(id h#%in+
Count(O) K ? 2E
7#6-0/ N/M S<M"SA+%
LLLLLLLLLLLL LLLLLLLLLLLLLLLLLLLLLL LLLLLLLLLLLLLLLLLLLLLLLL
5> .+#FD 53>>
5> MA0AE#F '9$>
5> 6F#SI7#0- $>>>
'> A0A+1S- :>>>
'> .+#FD 5=>>
TTTT.
TTTT.
TTTT.
= rows selected
1ou cannot use the KO#F# clause to restrict groups.
1ou use the OA?I0E clause to restrict groups.
S*+, S#+#.- deptno, A?E"sal%
FF/M emp
KO#F# A?E"sal% , '>>>
EF/<6 M1 deptno)
S*+, S#+#.- deptno, A?E"sal%
FF/M emp
EF/<6 M1 deptno
OA?I0E A?E"sal% , '>>>
Aestin+ C3*D& /unction
C3*D& /unctions c#n 0e nested to #n de(th1
Su..#r
/rder of ealuation of the clauses :
WSE3E c"#use
C3*D& BZ c"#use
S$,IAC c"#use
$SSICAMEAT58
11 Dsin+ the C#se Con%ersion -unctions +ener#te the re(ort #s
EM&L*ZEE DET$ILS
55555555555555555555555555555555555555555555555555555555555555555555555
The )o0 tit"e /or Kin+ is (resident
The )o0 tit"e /or B"#6e is .#n#+er
[[[1
21 Dsin+ the Ch#r#cter M#ni(u"#tion -unctions dis("#s e.("oee n#.e #nd )o0 to+ether2 "en+th
o/ the e.("oee n#.e #nd the nu.eric (osition o/ the "etter $ in the e.("oee n#.e2 /or #""
e.("oees !ho #re in s#"es1
3. Dis("# the record o/ e.("oee !ho e#rns 2
nd
hi+hest s#"#r1
91 -or #"" e.("oees e.("oed /or /e!er th#n 2== .onths2 dis("# the e.("oee nu.0er2 hire d#te2
nu.0er o/ .onths e.("oed2 si4 .onth re%ie! d#te2 /irst -rid# #/ter hire d#te #nd the "#st d#
o/ the .onth !hen hired1
51 Co.(#re the hire d#tes /or #"" e.("oees !ho st#rted in 19<;1 Dis("# the e.("oee nu.0er2
hire d#te2 #nd .onth st#rted usin+ the 3*DAD #nd T3DAC /unctions1
SELECT co"u.n2 +rou(I/unction ( co"u.n )
-3*M t#0"e
W WSE3E condition X
W C3*D& BZ +rou(I0Ie4(ression X
W S$,IAC +rou(Icondition X
W *3DE3 BZ co"u.n XE
:1 Dis("#s the n#.e #nd hire d#tes /or #"" the e.("oees1 The hire d#te "oo6s "i6e 1; Ao%e.0er
19<11
;1 Dis("#s the n#.e #nd hire d#tes /or #"" the e.("oees1 The hire d#te "oo6s "i6e Se%enth o/
-e0ru#r 19<1 =<:==:== $M1
<1 Dis("# the n#.es #nd hire d#tes o/ #"" the e.("oees !ho )oined on -e0ru#r 222 19<11
=. Dis("# the d#te o/ the ne4t -rid# th#t is si4 .onths /ro. the hire d#te1 The resu"t#nt d#te
shou"d "oo6 "i6e -rid#2 M#rch 12
th
2 19<21 *rder the resu"ts 0 hire d#te1
1=1 Dis("#s #"" e.("oees2 )o02 de(tno2 hired#te /ro. the e.( t#0"e in the sorted order o/ hired#te1
111 Sortin+ in descendin+ order ( #0o%e Fuer )
121 Dis("#s e.(no2 en#.e2 #nnu#" s#"#r /ro. e.( t#0"e in the sorted order o/ #nnu#" s#"#r1
181 Dis("# n#.e #nd s#"#r o/ #"" e.("oees1 *rder the resu"t 0 de(#rt.ent nu.0er #nd then
descendin+ order 0 s#"#r1
191 Dis("#s the #%er#+e2 hi+hest2 "o!est #nd su. o/ .onth" s#"#ries /or #"" s#"es.en1
151 Dis("#s the nu.0er o/ e.("oees in de(#rt.ent 8=1
1:1 Dis("#s the nu.0er o/ e.("oees in de(#rt.ent 8= !ho c#n e#rn # co..ission1
1;1 Dis("# the nu.0er o/ de(#rt.ents in the EM& t#0"e1
1<1 Dis("# the nu.0er o/ distinct de(#rt.ents in the EM& t#0"e1
WEEK 9 :
*0t#inin+ D#t# /ro. Mu"ti("e T#0"es ()oin)
EM& DE&T
7ata from Multiple -ables
Sometimes you need to use data from more than one table. In the example aboe, the report displays data from
two separate tables.
5. EM&A* exits in the EM& table.
'. DE&TA* exits in both the EM& and DE&T tables.
3. L*C exits in DE&T table.
-o produce the report, you need to lin2 #M6 and 7#6- tables and access data from both of them.
Wh#t is # Moin\
<se a Hoin to Uuery data from more than one table.
Krite the Hoin condition in the KO#F# clause.
6refix the column name with the table name when the same column name appears in more than one table.
Fows in one table can be Hoined to rows in another table according to common alues existing in
corresponding columns, that is usually, primary and foreign 2ey columns.
SELECT t#0"e11co"u.n2 t#0"e21co"u.n
-3*M t#0"e12 t#0"e2
WSE3E t#0"e11co"u.n1 ? t#0"e21co"u.n2E
EM&A* EA$ME DE&TA*
55555555555 5555555555555555555555 [1 55555555555555
;<89 KIAC [1 1=
;:9< BL$KE [1 8=
[[[[
[[[[
;989 MILLE3 [1 1=
DE&TA* DA$ME L*C
55555555555 5555555555555555555555 [1 55555555555555
1= $CC*DATIAC [1 AEW Z*3K
2= 3ESE$3CS [1 D$LL$S
8= S$LES [1 CSIC$C*
9= *&E3$TI*AS [1 B*ST*A
EM&A* DE&TA* L*C
55555555555 555555555555 555555555555555555555
;<89 1= AEW Z*3K
;:9< 8= CSIC$C*
;;<2 1= AEW Z*3K
;5:: 2= D$LL$S
[[[
[[[
19 ro!s se"ected
T(es o/ )oin
#UuiHoin 0onLeUuiHoin /uter Hoin Self Hoin
-here are two main types of Hoin conditions:
EFui)oins
Aon5eFui)oins
Additional Hoin methods include the following:
*uter )oins
Se"/ )oins
Set o(er#tors
EFui)oin
A Hoin which is based on eUualities is called an eUuiHoin.
SQLK Se"ect e.(1e.(no2 e.(1en#.e2 e.(1de(tno2 de(t1de(tno2 de(t1"oc
-ro. e.(2 de(t
Where e.(1de(tno ? de(t1de(tnoE
In the aboe statement e.(1de(tno ? de(t1de(tno performs the Hoin operation.
It retriees rows from both the tables proided they both hae the same de(tno column as specified in the
AwhereA clause. Since the AwhereA clause uses comparison operator eUual to "4%, to perform a Hoin, it is said an
eFui5)oin.
EFui)oins #re #"so c#""ed si.("e )oins or inner )oins1
$ddition#" Se#rch Conditions Dsin+ the $AD *(er#tor1
For example, to display employee number, name, department number and department location, you need an
additional condition in the KO#F# clause.
SQLK SELECT e.(no2 en#.e2 e.(1de(tno2 "oc
-3*M e.(2 de(t
WSE3E e.(1de(tno ? de(t1de(tno
$AD IAITC$& ( en#.e ) ? T Kin+U E
<sing -able Aliases
Without $"i#ses
SQLK Se"ect e.(1e.(no2 e.(1en#.e2 e.(1de(tno2 de(t1de(tno2 de(t1"oc
-ro. e.(2 de(t
Where e.(1de(tno ? de(t1de(tnoE
With $"i#ses
SQLK Se"ect e1e.(no2 e1en#.e2 e1de(tno2 d1de(tno2 d1"oc
-ro. e.( e2 de(t d
Where e1de(tno ? d1de(tnoE
Zou c#n use t#0"e #"i#ses inste#d o/ t#0"e n#.es1 The t#0"e #"i#s is %#"id on" /or the current SELECT
st#te.ent1 T#0"e #"i#ses c#n 0e u( to 8= ch#r#cters in "e#n+th1
Moinin+ More Th#n T!o T#0"es1
SQLK SELECT c1n#.e2 o1ordid2 i1ite.id2 i1ite.tot2 o1tot#"
-3*M custo.er c2 ord o2 ite. i
WSE3E c1custid ? o1custid
$AD o1ordid ? i1ordid
$AD c1n#.e ? T TKB S&*3T SS*&U E

Aon5EFui)oins
$ non eFui5)oin s(eci/ies the re"#tionshi( 0et!een co"u.ns 0e"on+in+ to di//erent t#0"es 0 .#6in+ use
o/ the re"#tion#" o(er#tors ( K2B2B?2K?2BK ) other th#n ? 1
EM& S$LC3$DE
The re"#tionshi( 0et!een the EM& t#0"e #nd the S$LC3$DE t#0"e is # non5eFui)ion2 .e#nin+ th#t no
co"u.n in the EM& t#0"e corres(onds direct" to # co"u.n in the S$LC3$DE t#0"e1 The re"#tionshi(
0et!een the t!o t#0"es is th#t the S$L co"u.n in the EM& t#0"e is 0et!een the L*S$L #nd SIS$L
co"u.n o/ the S$LC3$DE t#0"e1 The re"#tionshi( is o0t#ined usin+ #n o(er#tor other th#n eFu#" ( ? )1
*uter Moins
Zou use #n outer )oin to #"so see ro!s th#t do not usu#"" .eet the )oin condition1
*uter )oin o(er#tor is the ("us si+n ( P )1
Missin+ ro!(s) c#n 0e returned i/ #n outer )oin o(er#tor is used in the )oin condition1 It is ("#ced on the
side o/ the )oin th#t is de/icient in in/or.#tion1 This o(er#tor h#s the e//ect o/ cre#tin+ one or .ore nu""
ro!s2 to !hich one or .ore ro!s /ro. the nonde/icient t#0"e c#n 0e )oined1
$ condition in%o"%in+ #n outer )oin c#nnot use the IA o(er#tor or 0e "in6ed to #nother condition 0 the
*3 o(er#tor1
Se"/ Moins
So.eti.es ou need to )oin # t#0"e to itse"/1 To /ind the n#.e o/ e#ch e.("oeeUs .#n#+er ou need to
)oin the EM& t#0"e to itse"/1 -or e4#.("e2 to /ind the n#.e o/ BL$KEUs .#n#+er2 ou need to :
-ind BL$KE in the EM& t#0"e 0 "oo6in+ #t the EA$ME co"u.n1
-ind the .#n#+er nu.0er /or BL$KE 0 "oo6in+ #t the MC3 co"u.n1 BL$KEUs .#n#+er nu.0er is
;<891
-ind the n#.e o/ the .#n#+er !ith EM&A* ;<89 0 "oo6in+ #t the EA$ME co"u.n1 KIACUs e.("oee
nu.0er is ;<891 So2 KIAC is BL$KEUs .#n#+er1
In this (rocess2 ou "oo6 in the t#0"e t!ice1 The /irst ti.e ou "oo6 in the t#0"e to /ind BL$KE in the
EA$ME co"u.n #nd MC3 %#"ue o/ ;<891 The second ti.e ou "oo6 in the EM&A* co"u.n to /ind ;<89
#nd the EA$ME co"u.n to /ind KIAC1
EM&A* EA$ME S$L
55555555555 55555555555555555555 5555555555555555
;<89 KIAC 5===
;:9< BL$KE 2<5=
;;<2 CL$3K 295=
;5:: M*AES 29;5
;:5= M$3TIA 125=
;999 $LLEA 1:==
;<99 TD3AE3 15==
;9== M$MES 95=
[[[1
19 ro!s se"ected
C3$DE L*S$L SIS$L
55555555555 555555555555555 55555555555555
1 ;== 12==
2 12=1 19==
8 19=1 2===
9 2==1 8===
5 8==1 9999
EM&A* EA$ME MC3
55555555555 555555555555555555555555 555555555555555
;(3= KIAC
;:9< BL$KE ;<89
TTTTT..
TTTTT...

Set *(er#tors
Set operators combine the results of two Uueries into a single one.
Dnion
Dnion #""
Intersect
Minus
The queries )hich are relate* +y a set operator shoul* ha,e the same num+er
o- columns an* the correspon*ing columns must +e o- the same *atatype.
Dnion
-he union operator returns all distinct rows selected by either Uuery. -he following
example combines the result of two Uueries with the union operator which eliminates
duplicate rows.
SQLK Se"ect custid /ro. custo.er union se"ect ordid /ro. orderIin/oE
Dnion #""
-he union all operator returns all rows selected by either Uuery including duplicates.
SQLK Se"ect custid /ro. custo.er union #"" se"ect ordid /ro. orderIin/oE
Intersect
Intersect operator returns only rows that are common to both the Uueries.
SQLK Se"ect ordid /ro. orderIin/o intersect se"ect ite.id /ro. ite.E
Minus
Minus operator returns all distinct rows selected only by the first Uuery and
not by the second.
SQLK Se"ect ordid /ro. ite. .inus se"ect ordid /ro. orderIin/oE
Khile using the Aorder byA clause, it must follow the last select statement and we
must order by interger and not by column name. .onsider the following example.
SQLK Se"ect ordid2custid /ro. orderIin/o union se"ect ite.id2(rodid
/ro. ite. order 0 2E
-he aboe example displays distinct rows selected by either Uuery, ordered by the
second column stated in both the Uueries. Since the column names are different in
the aboe Uueries, we use an integer in the Aorder byA clause instead of a column name.
$SSICAMEAT59
$1 Cre#te t#0"e de(tIBour ro"" nu.0erK !ith /o""o!in+ structure1
DE&TA* A*T ADLL ADMBE3 (2)
DA$ME ,$3CS$32 (19)
L*C ,$3CS$32 (18)
6ut the alues.
DE&TA* DA$ME L*C
55555555555 5555555555555555555555 55555555555555
1= $CC*DATIAC AEW Z*3K
2= 3ESE$3CS D$LL$S
8= S$LES CSIC$C*
9= *&E3$TI*AS B*ST*A
B1 Cre#te t#0"e S#"+r#deIBour ro"" nu.0erK !ith /o""o!in+ structure1
C3$DE ADMBE3 (2)
L*S$L ADMBE3(9)
SIS$L ADMBE3(:)
6ut the alues.
C3$DE L*S$L SIS$L
55555555555 555555555555555 55555555555555
1 ;== 12==
2 12=1 19==
8 19=1 2===
9 2==1 8===
5 8==1 9999
91 E%#"u#te #n e.("oeeUs s#"#r +r#de1
51 Dis("#s EA$ME2 DE&TA*2 DA$ME /or #"" the de(#rt.ents1 The *&E3$TI*AS
de(#rt.ent2 !hich does not h#%e #n e.("oees2 is #"so dis("#ed2 sorted 0 DE&TA*1
:1 Dis("# the /o""o!in+1
3E&*3T
BL$KE !or6s /or KIAC
CL$3K !or6s /or KIAC
M*AES !or6s /or KIAC
M$3TIA !or6s /or BL$KE
[[[[[1
[[[[[1
WEEK 5 :
Say this is a problem.
7 Who h#s # s#"#r +re#ter th#n MonesUs \ 7
-o sole this problem, you need two Uueries : one Uuery to find what Nones earns and a second Uuery to find
who earns more than that amount.
1ou can sole this problem by combining the two Uueries, placing one Uuery inside the other Uuery.
Su0Fueries
$ su0Fuer is # SELECT st#te.ent th#t is e.0edded in # c"#use o/ #nother SELECT st#te.ent1
In the snt#4:
*(er#tor inc"udes # co.(#rison o(er#tor such #s K2 ?2 or IA
Co.(#rison o(er#tors /#"" into t!o c"#sses: sin+"e5ro! o(er#tors ( K2 ?2 K?2 B2 BK2 B?) #nd .u"ti("e5ro!
o(er#tors ( IA2 $AZ2 $LL )
The su0Fuer is o/ten re/erred to #s # nested SELECT2 su05SELECT2 or inner SELECT st#te.ent1 The
su0Fuer +ener#"" e4ecutes /irst #nd its out(ut is used to co.("ete the Fuer condition /or the .#in or
outer Fuer1
M#in Quer
7Which e.("oees h#%e # s#"#r +re#ter th#n MonesUs s#"#r\]
Su0Fuer
7Wh#t is MonesUs s#"#r\]
SELECT se"ectI"ist
-3*M t#0"e
WSE3E e4(r o(er#tor
( SELECT se"ectI"ist
-3*M t#0"e
WSE3E e4(r ) E
E4#.("e
SQLK SELECT en#.e
-3*M e.(
WSE3E s#" K
( SELECT [[[11
-3*M [[[
WSE3E [[[[ ) E
Cuide"ines /or Dsin+ Su0Fueries
11 Enc"ose su0Fueries in (#rentheses1
21 &"#ce su0Fueries in the ri+ht side o/ the co.(#rison o(er#tor1
81 Do not #dd #n *3DE3 BZ c"#use to # su0Fuer1
91 Dse sin+"e5ro! o(er#tors !ith sin+"e5ro! suFueries1
51 Dse .u"ti("e5ro! o(er#tors !ith .u"ti("e5ro! suFueries1
T(es o/ Su0Fueries
Sin+"e5ro! su0Fueries: Queries th#t return on" one ro! /ro. the inner SELECT st#te.ent1
Mu"ti("e5ro! su0Fueries: Queries th#t return .ore th#n one ro! /ro. the inner SELECT
st#te.ent1
Mu"ti("e5co"u.n su0Fueries: Queries th#t return .ore th#n one co"u.n /ro. the inner
SELECT st#te.ent1
3eturn on" one ro!
Dse sin+"e5ro! co.(#rison o(er#tors
/perator Meaning
4 #Uual to
, Ereater than
,4 Ereater than or eUual to
V +ess than
V4 +ess than or eUual to
V, 0ot eUual to
If the inner Uueries return single alues, the S*+ statement is called a singleLrow subUuery. -he outer and the
inner Uueries can get data from different tables.
Zou c#n use Crou( /unctions in # Su0Fuer1
S*+, S#+#.- TTTTTT.
FF/M TTT.
KO#F# TT. ,
" S#+#.- MAP"TT.%
FF/M TTT % )
S$,IAC C"#use !ith Su0Fueries
11 The *r#c"e Ser%er e4ecutes su0Fueries /irst
21 The *r#c"e Ser%er returns resu"ts into the .#in FuerUs S$,IAC c"#use1
S*+, S#+#.- TTTTTTTTTTT..
FF/M TTTT
EF/<6 M1 TTTT.
OA?I0E MI0"TT% ,
" S#+#.- MI0"TT%
FF/M TTT..
KO#F# TTTTT.. % )
Wh#t is Wron+ !ith This St#te.ent\
SQLK SELECT e.(no2 en#.e
-3*M e.(
WSE3E s#" ?
( SELECT MIA(s#")
-3*M e.(
C3*D& BZ de(tno ) E
E33*3:
Sin+"e5ro! su0Fuer returns .ore th#n one ro!1
In the SQL st#te.ent #0o%e2 the su0Fuer cont#ins # C3*D& BZ ( de(tno ) c"#use2 !hich i.("ies th#t
the su0Fuer !i"" return .u"ti("e ro!s2 one /or e#ch +rou( it /inds1 In this c#se2 the resu"t o/ the
su0Fuer !i"" 0e <==2 18==2 95=1
The outer Fuer t#6es the resu"ts o/ the su0Fuer ( <==2 18==2 95= ) #nd uses these resu"ts in its WSE3E
c"#use1 The WSE3E c"#use cont#ins #n eFu#" ( ? ) o(er#tor2 # sin+"e5ro! co.(#rison o(er#tor e4(ectin+
on" one %#"ue1 The ? o(er#tor c#nnot #cce(t .ore th#n one %#"ue /ro. the su0Fuer #nd hence
+ener#tes the error1
To correct this error2 ch#n+e the ? o(er#tor to IA1
3eturn .ore th#n one ro!
Dse .u"ti("e5ro! co.(#rison o(er#tors
*(er#tor Me#nin+
IA EFu#" to #n .e.0er in the "ist
$AZ Co.(#re %#"ue to e#ch %#"ue returned 0 the su0Fuer
$LL Co.(#re %#"ue to e%er %#"ue returned 0 the su0Fuer
<sing A01 /perator in MultipleLFow SubUuery.
S*+, S#+#.- TTT., TTT, TTT
FF/M TTT
KO#F# TT.. V A01
" S#+#.- TTT
FF/M TTT
KO#F# TTT 4 WTTTI %)
In singleLrow subUueries and multipleLrow subUueries only one column was compared in th e KO#F# clause
or OA?I0E clause of the S#+#.- statement. If you want to compare two or more columns, you must write a
compound KO#F# clause using logical operators.
Snt#4
D#t#0#se *0)ects
Snon.
A synonym is a database obHect, which is used as an alias" alternatie name % for a table,
iew or seUuence.
SQLK Cre#te snon. cust /or custo.erE
SeFuences
A seUuence is a database obHect which can generate uniUue, seUuential integer alues. It can
be used to automatically generate primary 2ey or uniUue 2ey alues.
-he syntax for the creating a seUuence is as follows.
Cre#te seFuence B seFIn#.e K incre.ent 0 n st#rt !ith n W .#4%#"ue n X
W .in%#"ue n X W cc"e ' nocc"e X W c#che ' noc#che XE
Incre.ent 0 n : AnA is an integer which specifies the interal between seUuence
numbers.
St#rt !ith n : Specifies the first seUuence number to be generated.
Min%#"ue n : Specifies the minimum alue of the seUuence.
M#4%#"ue n : It specifies the maximum alue that the seUuence can generate.
Cc"e : Specifies that the seUuence continues to generate alues after reaching
either its max or min alue.
Aocc"e : Specifies that the seUuence cannot generate more alues after reaching
either its max or min alue.
C#che : Allows faster generation of seUuence number. /racle preallocates
seUuence numbers and 2eeps them in memory for faster access.
-he cache alue must be less than maxalue minus minalue.
SELECT co"u.n2 co"u.n2 [11
-3*M t#0"e
WSE3E ( co"u.n2 co"u.n2 [1) IA
( SELECT co"u.n2 co"u.n2 [11
-3*M t#0"e
WSE3E condition ) E
Aoc#che : 7oes not preallocate seUuence numbers for faster access.
SQLK Cre#te seFuence custseF incre.ent 0 1 st#rt !ith 1 .#4%#"ue 1=
Min%#"ue 1 cc"e c#che 91
After creating a seUuence we can access its alues with the help of pseudocolumns li2e
curr%#" and ne4t%#". A pseudocolumn behaes li2e a table column, but it not actually stored
in the table.
Ae4t%#" : 0extal will return inital alue of the seUuence, when referred to, for
the first time. +ater reference to nextal will increment the seUuence alue
by I0.F#M#0- M1 clause and return the new alue.
Curr%#" : .urral returns the cuttent alue of the seUuence which is the alue returned
My the last reference to nextal.
SQLK Cre#te t#0"e te.( ( id nu.0er(8)2 n#.e %#rch#r2(2=))E
SQLK Insert into te.( %#"ues ( custseF1ne4t%#"2 L%i)#L)E
-o find out the current seUuence number
SQLK Se"ect custseF1curr%#" /ro. te.(E
$"ter seFuence
Ke can alter the seUuence when we want to do the following :
Set or eliminate min or max alue.
.hange the increment alue.
.hange the number of the cached seUuence numbers.
-he following example will change the maxalue of the seUuence from 5> to 5$.
SQLK #"ter seFuence custseF .#4%#"ue 15E
$SSICAMEAT5 5
5. Kho has a salary greater than NonesIsX
'. Kho has a salary greater than the salary of employee ;$::.
3. 7isplay the employees whose Hob title is the same as that of employee ;3:=.
9. 7isplay the employees whose Hob title is the same as that of employee ;3:= and whose salary is
greater than of employee ;(;:.
$. 7isplays the employee name, Hob title and salary of all employees whose salary is eUual to the
minimum salary.
:. 7isplays all the departments that hae a minimum salary greater than that of department '>.
;. Find the Hob with the lowest aerage salary.
(. Find the employees who earn the same salary as the minimum salary for department.
=. 7isplays the employees whose salary is less than any cler2 and who are not cler2s." hint: -he max
salary that a cler2 earns is 53>> so the S*+ statement displays all the employees who are not cler2s
but earn less than 53>>%
5>. 7isplays employees whose salary is greater than the aerage salaries of all the departments. " hint: the
highest aerage salary of a department is '=5:.::, so the Uuery returns those employees whose salary
is greater than '=5:.::%
55. 7isplay the name, department number, salary and commission of any employee whose salary and
commission matches both the commission and salary of any employee in department 3>.
WEEK 6
PL/SQL Tutorial
PL/SQL BLOCK : The pl/sql block contains the following section:--
The DECLARE section.
The Master BEGIN and END section that contains the
EXCEPTION section.
The declare section contains declaration of memory variables, constants,
cursors etc. The begin section contains sql eecutable statements and
pl/sql eecutable statements. The eception section contains code to
handle errors that may arise during the eecution of the code block. The
end declares the end of pl/sql block.
! bit about it"s working. #hen you typed out the pl/sql block for
eecution. $t is sent to the pl/sql engine, where procedural statements
are eecuted% and sql statements are sent to the sql eecutor in the
oracle engine. &ince pl/sql engine resides in the oracle engine, the codes
eecutes smoothly and efficiently.
PL/SQL DATA-TYPE :
This is easy since it includes almost all the data types which u have used
in sql such as date, varchar, number, char etc etc.. &ome of the
attributes such as 'T()* is also used. This attribute automatically takes
in the default data type of the sql table from which u have passed the
query. #e will discuss this later.
+emember in pl/sql a variable name must begin with a character and can
be followed by maimum of ,- other characters. +eserved words can"t
be used unless enclosed within double quotes. .ariables must be
separated from each other by at least one space or by a punctuation
mark. (ou can assign values of operator using :/ operator. $ won"t
discuss about logical comparisons operators such as 0, 1 , 1/, 23T,
T+4*, !25, 3+, 2466 etc since they r quite easy to understand.
OW TO DISPLAY !ESSAGES ON SCREEN :
DB!S"O#TP#T : is a $a%&a'( that includes a number of procedure and functions
that accumulate information in a buffer so that it can be retrieved later. These functions
can also be used to display messages to the user.
P#T"LINE : )ut a piece of information in the package buffer followed by an end-of-line
marker. $t can also be used to display message to the user. )ut7line epects a single
parameter of character data type. $f used to display a message, it is the message
"string".
*8: dbms7output.put7line9:%
RE!E!BER: To display messages to the user the &*+.*+34T)4T should be set to 32.
&*+.*+34T)4T is a sql ;plus environment parameter that displays the information on
screen passed as a parameter to the )4T76$2* function.
EG: SET SER)ERO#TP#T ON
PL/SQL %o**(+t, :
! comment can have , forms i.e.
-- The comment line begins with a double hyphen 9--:. The entire line will be treated as
a comment.
-- The < style comment such as /; i am a comment ;/
EXA!PLE :
--ADDITION O- TWO N#!BER
5eclare
a number%
b number%
c number%
=egin
a:/>a%
b:/>b%
c:/a?b%
dbms7output.put7line9"&um of " @@ a @@ " and " @@ b @@ " is " @@ c:%
*nd%
Aere B>C symbol is used to take user input at runtime.....
CONDITIONAL CONTROL :
S.+ta/ :
$D --<ondition TA*2
--!ction
*6&*$D --<ondition TA*2
--!ction
*6&*
--!ction
*25 $D%
E/a*$l( 0: 1!AXI!#! O- 2 N#!BERS3
5eclare
a number%
b number%
c number%
d number%
=egin
dbms7output.put7line9"*nter a:":%
a:/>a%
dbms7output.put7line9"*nter b:":%
b:/>b%
dbms7output.put7line9"*nter c:":%
c:/>b%
if 9a1b: and 9a1c: then
dbms7output.putline9"! is Maimum":%
elsif 9b1a: and 9b1c: then
dbms7output.putline9"= is Maimum":%
else
dbms7output.putline9"< is Maimum":%
end if%
*nd%
E/a*$l( 4: 1CALC#LATION O- NET SALARY3
declare
ename varchar,9EF:%
basic number%
da number%
hra number%
pf number%
netsalary number%
begin
ename:/>ename%
basic:/>basic%
da:/basic ; 9GE/EHH:%
hra:/basic ; 9EF/EHH:%
if 9basic 0 IHHH:
then
pf:/basic ; 9F/EHH:%
elsif 9basic 1/ IHHH and basic 0/ FHHH:
then
pf:/basic ; 9J/EHH:%
elsif 9basic 1/ FHHH and basic 0/ KHHH:
then
pf:/basic ; 9K/EHH:%
else
pf:/basic ; 9EH/EHH:%
end if%
netsalary:/basic ? da ? hra -pf%
dbms7output.put7line9"*mployee name : " @@ ename:%
dbms7output.put7line9")rovidend Dund : " @@ pf:%
dbms7output.put7line9"2et salary : " @@ netsalary:%
end%
ITERATI)E CONTROL AND SEQ#ENTIAL CONTROL
5. SI!PLE LOOP
S.+ta/ :
loop
-- &equence of statements%
-- the loop ends when u use *L$T #A*2 statement --condition
end loop%
(/a*$l( :
5eclare n number:/E%
6oop
5bms7output.put7line 9n:%
n:/n?E%
*L$T #A*2 n1F%
*nd loop%
45 WILE LOOP
S.+ta/ :
#hile --condition
loop
--sequence of statements
end loop%
E/a*$l( :
--S#! O- ODD N#!BER WITIN 67
declare
n number%
sumE number default H%
begin
n:/E%
while 9n 0 FH:
loop
sumE:/sumE?n%
n:/n?,%
end loop%
dbms7output.put7line9"&um of odd numbers :" @@ sumE:%
end%
$SSICAMEAT5:
5. Krite a program using 6+YS*+ to input an integer and chec2 whether a year is +eap year or not.
'. Krite a program to chec2 the salary of WM+AD#I from #M6 table. If it is V5>>> then increment comm
with :>> else show message ! 0/ I0.F#M#0- 8 .
3. Krite a program to display 5
st
'> odd numbers " suing simple loop%
9. Krite a program using to input an integer and display it in reerse order"using Khile loop%
$. Krite a program using to input an integer and chec2 whether it is prime or not.
WEEK 8
25 -OR LOOP
S.+ta/ :
Dor i in E..EH
loop
--sequence of statements
end loop%
E/a*$l( 0 :
5eclare
begin
for i in E..EH
loop
dbms7output.put7line9i:%
end loop%
end%
E/a*$l( 4 : 1S#! O- ODD N#!BERS WITIN 673
declare
n number%
sumE number default H%
begin
n:/E%
for n in E.. FH
loop
if mod9n,,:/E then
sumE:/sumE?n%
end if
end loop%
dbms7output.put7line9"sum / " @@ sumE:%
end%

95 GOTO : 1SEQ#ENTIAL CONTROL3
S.+ta/ :
83T3 L%
00 L 11
declare
--takes the default datatype of the column of the table price
msal emp.sal'type%
begin
select sal into msal from emp where empno/J-HH%
if msal 1 JHHH then
goto 4pd%
else
update emp set sal/sal?FHH where empno/J-HH%
end if%
00 4pd 11
dbms7output.put7line 9M $2<+*M*2T $& 23T <32&$5*+*5N :%
end%
EXCEPTION ANDLING IN PLSQL
*rrors in pl/sql block can be handled...error handling refers to the way
we handle the errors in pl/sql block so that no crashing stuff of code
takes place...This is eactly the same as we do in <?? or Oava..rightPP
TYPES O- EXCEPTION :
A5 Pr(:(;i+(: (/%($tio+,
B1 #,(r :(;i+(: (/%($tio+,

$r(:(;i+(: (/%($tio+,: are already defined within data
dictionary. Dollowing are some eamples of )redefined eceptions
EXCEPTION
2o-data-found // when no rows are returned
<ursor-already-open // when a cursor is opened in advance
5up-val-3n-inde // for duplicate entry of inde..
&torage-error // if memory is damaged
)rogram-error // internal problem in pl/sql
Qero-divide // divide by Rero
invalid-cursor // if a cursor is not open and u r trying to close it
6ogin-denied // invalid user name or password
$nvalid-number // if u r inserting a string datatype for a
number datatype which is already
declared
Too-many-rows // if more rows are returned by select statement
SYNTAX:
begin
sequence of statements%
eception
when --eception name then
sequence of statements%
end%
EY$M&LES
E/a*$l( 0: 1IS S!IT EARNING ENO#G <3
declare
sE emp.sal 'type%
begin
select sal into sE from emp where ename / "&M$TA"%
if9no7data7found:
then
raise7application7error9,HHHE,"smith is not present":%
end if%
if9sE 1 EHHHH:
then
raise7application7error9,HHH,,"smith is earning enough":%
end if%
update emp set sal/sal ? FHH where ename/"&M$TA"%
end%
EXA!PLE 4: 1#SER DE-INED EXCEPTION 3
5*<6!+*
e7rec emp'+3#T()*%
eE *L<*)T$32%
salE emp.sal'T()*%
=*8$2
&*6*<T sal $2T3 salE D+3M emp #A*+* deptno / IH !25 ename
/"Sohn"%
$D salE 0 FHHH TA*2
+!$&* eE%
salE :/ KFHH%
4)5!T* emp &*T sal / salE #A*+* deptno / IH !25 ename / "Sohn"%
*25 $D%
*L<*)T$32
#A*2 no7data7found TA*2
+!$&*7!))6$<!T$327*++3+ 9-,HHHE, "Sohn is not there.":%
#A*2 eE TA*2
+!$&*7!))6$<!T$327*++3+ 9-,HHH,, "6ess &alary.":%
*25%
WEEK =
CD3S*3: <ursor is a work area in pl/sql which is used by sql server used to store
the result of a query. *ach column value is pointed using pointer. (ou can independently
manipulate cursor values. ! bit about it"s working. suppose you ask for a query stored
in the server, at first a cursor consisting of query result is created in server. 2ow the
cursor is transferred to the client where again cursor is created and hence the result is
displayed.
Cur,or, ar( o; 4 t.$(,:
I*$li%it Cur,or
E/$li%it Cur,or
I*$li%it %ur,or, are created by oracle engine itself while (/$li%it %ur,or, are created
by the users......cursors are generally used in such a case when a query returns more
than one rows....normal )6/&T6 returning more than one rows givens error but using
cursor this limitation can be avoided....so cursors are used....
Cur,or Attri>ut(, Pur$o,(
'$&3)*2 // returns true if cursor is open, false otherwise
'D3425 // returns true if record was fetched successfully, false
otherwise
'23TD3425 // returns true if record was not fetched successfully,
false otherwise
'+3#<342T // returns number of records processed from the cursor.
.ery important: <ursor can be controlled using following I control
statements. They are O$(+? -(t%@? Clo,(.....open statement identifies
the active set...i.e. query returned by select statement...close statement
closes the cursor...and fetch statement fetches rows into the
variables...<ursors can be made into use using cursor for loop and fetch
statement...we will see the corresponding eamples...
EXA!PLES
--EXA!PLE O- SQLA-O#ND 1I!PLICIT C#RSORS3
begin
update emp set sal/sal ;H.EF where empno / >emp7code%
if sql'found then
dbms7output.put7line9"employee record modified successfully":%
else
dbms7output.put7line9"employee does not eist":%
end if%
end%
--EXA!PLE -OR SQLANOT-O#ND 1I!PLICIT C#RSORS3
begin
update emp set sal / sal;H.EF where empno / >emp7code%
if sql'notfound then
dbms7output.put7line9"employee does not eist":%
else
dbms7output.put7line9"employee record modified successfully":%
end if%
end%
--EXA!PLE -OR SQLAROWCO#NT 1I!PLICIT C#RSORS3
declare
rows7affected char9G:%
begin
update employee set salary / salary;H.EF where Oob/"programmers"%
rows7affected :/ to7char9sql'rowcount:%
if sql'rowcount 1 H then
dbms7output.put7line9rows7affected @@ "employee records modified
successfully":%
else
dbms7output.put7line9"There are no employees working as
programmers":%
end if%
end%
S.+ta/ of eplicit cursor: <ursor cursorname is sql select statement%
S.+ta/ of fetch : fetch cursorname into variableE, variable,...%
S.+ta/ of close% close cursorname%
S.+ta/ of open cursor% open cursorname%
--EXPLICIT C#RSOR
5*<6!+*
<4+&3+ cE $& &*6*<T ; D+3M emp%
e7rec emp'rowtype%
=*8$2
3)*2 cE%
633)
D*T<A cE $2T3 e7rec%
*L$T #A*2 cE'23TD3425%
5=M&734T)4T.)4T76$2*9"2umber: " @@ " " @@ e7rec.empno:%
5=M&734T)4T.)4T76$2*9"2ame : " @@ " " @@ e7rec.ename:%
5=M&734T)4T.)4T76$2*9"&alary: " @@ " " @@ e7rec.sal:%
*25 633)%
<63&* cE%
*25%
--RE-ERENCE TYPE C#RSOR
5*<6!+*
T()* ecursor $& +*D <4+&3+ +*T4+2 emp'+3#T()*%
ecur ecursor%
e7rec emp'+3#T()*%
dn 24M=*+%
=*8$2
dn :/ >deptno%
3)*2 ecur D3+ &*6*<T ; D+3M emp #A*+* deptno / dn%
D3+ e7rec $2 ecur
633)
5=M&734T)4T.)4T76$2* 9"*mployee 2o : " @@ e7rec.empno:%
5=M&734T)4T.)4T76$2* 9"*mployee &alary: " @@ e7rec.salary:%
*25 633)%
*25%
TRIGGER:
Trigger is a stored procedure which is called implicitly by oracle
engine whenever a insert, update or delete statement is fired.
A:Ba+ta'(, o; :ata>a,( tri''(r,:
---1 5ata is generated on it"s own
---1 +eplicate table can be maintained
---1 To enforce comple integrity constraints
---1 To edit data modifications
---1 To auto increment a field
etc..
S.+ta/: <reate or replace trigger Utrigger name-- Vbefore/afterW
Vinsert/update/deleteW on Utable name-- Vfor each statement/ for each
rowW Vwhen --condition--W plusX.begin and eception
Tri''(r, ar( o; ;olloCi+' t.$(,:
>(;or( or a;t(r tri''(r
;or (a%@ roC tri''(r
;or (a%@ ,tat(*(+t tri''(r
B(;or( tri''(r is fired before insert/update/delete statement while
a;t(r tri''(r is fired after insert/update/delete statement. Dor each row
and for each statements triggers are self eplanatory.
EXA!PLE 0:
--23 <A!28*& <!2 =* 532* 32 ! )!+T$<46!+ T!=6* 32 &425!(
!25 &!T4+5!(
&ql1*5 T+$88*+2!M* 9type from sql prompt:
<reate or replace trigger change before on emp for each row when
9to7char9sysdate,"dy": in 9"&!T","&42"::
begin
raise7application7error9-,HHHHE, "u cannot enter data in saturnday and
sunday":%
end%
save the program file.. and then sql1 D tri''(r+a*( 9to run the
program:
EXA!PLE 4:
--(ou have , tables with the same structure. $f u delete a record from
one table, it will be inserted into ,nd table
&ql1*5 T+$88*+2!M*
<reate or replace trigger backup after delete on *M) fro each row
=egin
$nsert into emp7dup9ename,Oob,sal: values
9:old.ename,:old.Oob,:old.sal:% end%
save the file and then sql1 Y triggername
EXA!PLE 2:
--To +*&T+$<T $2 &!6 D$*65 =( T+$88*+ M*!2& #A*2 4 *2T*+
8+*!T*+ TA!2 FHHH, TA*2 TA$& T+$88*+ $& *L*<4T*5
<reate or replace trigger check before insert on emp for each row when
92ew.sal 1 FHHH:%
=egin
raise7application7error9-,HHHH, "your no is greater than FHHH":%
end%
WEEK E
PROCED#RES AND -#NCTIONS
procedure is a subprogram...which consists of a set of sql statement.
)rocedures are not very different from functions. ! procedure or function
is a logically grouped set of &T6 and )6/&T6 statements that perform a
specific task. ! stored procedure or function is a named pl/sql code block
that have been compiled and stored in one of the oracle engineCs system
tables.
To make a procedure or function dynamic either of them can be passed
parameters before eecution. ! procedure or function can then change
the way it works depending upon the parameters passed prior to its
eecution.
Pro%(:ur(, a+: ;u+%tio+ ar( *a:( u$ o;
5eclaration part
*ecutable part
*ception-handling part93ptional:
! declaration part consists of declarations of variables. !
eecutable part consists of the logic i.e. sql statements....and
eception handling part handles any error during run-time
T@( ora%l( (+'i+( $(r;or*, t@( ;olloCi+' ,t($, to (/(%ut( a
$ro%(:ur( or ;u+%tio+5555
.erifies user access
.erifies procedure or function validity
eecutes the procedure or function
So*( o; t@( a:Ba+ta'(, o; u,i+' $ro%(:ur(, a+: ;u+%tio+, ar(:
&ecurity
)erformance
memory allocation
productivity
integrity.
!o,t i*$orta+t: 1the :i;;(r(+%( between $ro%(:ur(, and
;u+%tio+,:
! function must return a value back to the caller. ! function can
return only one value to the calling pl/sql block. =y defining multiple
out parameters in a procedure, multiple values can be passed to the
caller. The out variable being global by nature, its value is accessible
by any pl/sql code block including the calling pl/sql block.
S.+ta/ ;or ,tor(: $ro%(:ur(:
<+*!T* 3+ +*)6!<* )+3<*54+* VschemaW procedure name
9argument Z $2, 34T, $2 34T[ data type, ..: Z$&, !&[
variable declarations%
constant declarations%
=*8$2
pl/sql subprogram body%
*L<*)T$32
eception pl/sql block%
*25%
S.+ta/ ;or ,tor(: ;u+%tio+:
<+*!T* 3+ +*)6!<* D42<T$32 VschemaW function name9argument
$2 data type, ..: +*T4+2 data type Z$&, !&[
variable declarations%
constant declarations%
=*8$2
pl/sql subprogram body%
*L<*)T$32
eception pl/sql block%
*25%
The above synta $ think is self eplanatory...but $ will give you some
details...IN : specifies that a value for the argument must be
specified when calling the procedure or function. Ar'u*(+t: is the
name of an argument to the procedure or function. )arentheses can
be omitted if no arguments are present. O#T: specifies that the
procedure passes a value for this argument back to its calling
environment after eecution. $2 34T: specifies that a value for the
argument must be specified when calling the procedure and that the
procedure passes a value for this argument back to its calling
environment after eecution. =y default it takes $2. Data t.$( : is
the data type of an argument.
EXA!PLE 0:
--)+3<*54+* 4&$28 23 !+84M*2T !25 4&$28 <4+&3+
<+*!T* 3+ +*)6!<* )+3<*54+* ), $&
cursor curE is select ; from emp%
begin
for erec in curE
loop
dbms7output.put7line9erec.ename:%
end loop%
end%
EXA!PLE 4:
--)+3<*54+* 4&$28 !+84M*2T
<+*!T* 3+ +*)6!<* )+3<*54+* square 9 L $2 24M=*+:
$&
=*8$2
dbms7output.put7line9;:%
end%
sql1 eec me9I:% 9 T3 *L*<4T* TA* )+3<*54+* :
EXA!PLE 2:
--D42<T$32 using argument
<+*!T* 3+ +*)6!<* D42<T$32 square 9L $2 24M=*+:
+*T4+2 24M=*+ $&
=egin
return 9;:%
end%
(ou can make a procedure and functions similarly.....also if u want to
drop a function then use drop function function7name and for
procedure use drop procedure procedure7name
WEEK 07
PACKAGE:
! package is an oracle obOect, which holds other obOects within it.
3bOects commonly held within a package are procedures, functions,
variables, constants, cursors and eceptions. )ackages in plsql are
very much similar to those packages which we use in S!.!.
Part, o; $a%&a'( :
Pa%&a'( ,$(%i;i%atio+
Pa%&a'( >o:.
! $a%&a'( ,$(%i;i%atio+ part consists of all sort of declaration
of functions and procedures
#hile package body consists of group of code and logic of declared
functions and procedures.
EXA!PLE
--package specification 9save into file packE:
create or replace package pack, is
Dunction rmt9 in number: return number%
procedure rmtE9 in number:%
end%
--body 9save into file p,:
create or replace package body pack, is
Dunction rmt9 in number: return number is
begin
return 9;:%
end%
procedure rmtE9 in number: is
begin
dbms7output.put7line9;:%
end%
end%
-- To eecute the package at first run the pE file and then p, file
and then from &T6 prompt type eec pack,.rmtE9I:%
!s shown above u can put in complicated procedures and functions
inside the package. $ have Oust shown a simple eample. (ou can
easily modify the above code to fit your requirement. Sust try out
packages which include cursors, procedures and functions etc.
+emember pl/sql supports overloading. i.e. you can use the same
function or procedure name in your application but with different
no or type of arguments.

You might also like