INTRODUCTION
SQL is divided into the following
Data Definition Language (DDL)
Data Manipulation Language (DML)
Data Retieval Language (DRL)
Tansa!tion Contol Language (TCL)
Data Contol Language (DCL)
DDL "" !eate# alte# dop# tun!ate# ena$e
DML "" inset# update# delete
DRL "" sele!t
TCL "" !o$$it# oll%a!&# savepoint
DCL "" gant# evo&e
CR'(T' T()L' S*NT(+
Ceate ta%le ,table_name- (col1 datatype1, col2 datatype2 coln datatypen).
'/0
SQL- !eate ta%le student (no nu$%e (1)# na$e va!ha (23)# $a&s nu$%e (4)).
Ta%le !eated5
INS'RT
This will %e used to inset the e!ods into ta%le5
6e have two $ethods to inset5
)7 value $ethod
)7 addess $ethod
a) USIN8 9(LU' M'T:OD
s7nta/0
inset into ,table_name) values (value1, value2, value3 . Valuen).
'/0
SQL- inset into student values (2# ;sudha;# 233).
1
2 ow !eated5
SQL- inset into student values (1# ;sað# 133).
2 ow !eated5
To inset a new e!od again 7ou have to t7pe entie inset !o$$and# if thee ae lot of
e!ods this will %e diffi!ult5
This will %e avoided %7 using addess $ethod5
%) USIN8 (DDR'SS M'T:OD
s7nta/0
inset into ,table_name) values (&col1, &col2, &col3 . &coln).
This will po$pt 7ou fo the values %ut fo eve7 inset 7ou have to use fowad slash5
'/0
SQL- inset into student values (<no# =<na$e=# <$a&s).
'nte value fo no0 2
'nte value fo na$e0 >agan
'nte value fo $a&s0 433
old 20 inset into student values(<no# =<na$e=# <$a&s)
new 20 inset into student values(2# =>agan=# 433)
2 ow !eated5
SQL- ?
'nte value fo no0 1
'nte value fo na$e0 Naen
'nte value fo $a&s0 @33
old 20 inset into student values(<no# =<na$e=# <$a&s)
new 20 inset into student values(1# =Naen=# @33)
2 ow !eated5
!) INS'RTIN8 D(T( INTO SA'CIBI'D COLUMNS USIN8 9(LU' M'T:OD
s7nta/0
inset into ,table_name)(col1, col2, col3 Coln) values (value1, value2, value3 . Valuen).
2
'/0
SQL- inset into student (no# na$e) values (4# ;Ra$esh;).
2 ow !eated5
SQL- inset into student (no# na$e) values (@# ;Madhu;).
2 ow !eated5
d) INS'RTIN8 D(T( INTO SA'CIBI'D COLUMNS USIN8 (DDR'SS M'T:OD
s7nta/0
inset into ,table_name)(col1, col2, col3 coln) values (&col1, &col2, &col3 . &coln).
This will po$pt 7ou fo the values %ut fo eve7 inset 7ou have to use fowad slash5
'/0
SQL- inset into student (no# na$e) values (<no# =<na$e=).
'nte value fo no0 C
'nte value fo na$e0 9isu
old 20 inset into student (no# na$e) values(<no# =<na$e=)
new 20 inset into student (no# na$e) values(C# =9isu=)
2 ow !eated5
SQL- ?
'nte value fo no0 D
'nte value fo na$e0 Rattu
old 20 inset into student (no# na$e) values(<no# =<na$e=)
new 20 inset into student (no# na$e) values(D# =Rattu=)
2 ow !eated5
S'L'CTIN8 D(T(
S7nta/0
Sele!t E fo$ ,table_name-. "" hee E indi!ates all !olu$ns
o
3
Sele!t col1, col2, coln fo$ ,table_name-.
'/0
SQL- sele!t E fo$ student.
NO N(M' M(RFS
""" """""" """"""""
2 Sudha 233
1 Sað 133
2 >agan 433
1 Naen @33
4 Ra$esh
@ Madhu
C 9isu
D Rattu
G ows sele!ted5
SQL- sele!t no# na$e# $a&s fo$ student.
NO N(M' M(RFS
""" """""" """"""""
2 Sudha 233
1 Sað 133
2 >agan 433
1 Naen @33
4 Ra$esh
@ Madhu
C 9isu
D Rattu
G ows sele!ted5
SQL- sele!t no# na$e fo$ student.
NO N(M'
""" """""""
2 Sudha
1 Sað
2 >agan
4
1 Naen
4 Ra$esh
@ Madhu
C 9isu
D Rattu
G ows sele!ted5
CONDITION(L S'L'CTIONS (ND OA'R(TORS
6e have two !lauses used in this
6hee
Ode %7
USIN8 6:'R'
s7nta/0
5
sele!t E fo$ ,table_name- whee ,condition-.
the following ae the diffeent t7pes of opeatos used in whee !lause5
(ith$eti! opeatos
Co$paison opeatos
Logi!al opeatos
(ith$eti! opeatos "" highest pe!eden!e
H# "# E# ?
Co$paison opeatos
I# JI# -# ,# -I# ,I# ,-
%etween# not %etween
in# not in
null# not null
li&e
Logi!al opeatos
(nd
O "" lowest pe!eden!e
not
a) USIN8 I# -# ,# -I# ,I# JI# ,-
'/0
SQL- sele!t E fo$ student whee no I 1.
NO N(M' M(RFS
""" """"""" """""""""
1 Sað 133
1 Naen @33
1 ows sele!ted5
SQL- sele!t E fo$ student whee no , 1.
NO N(M' M(RFS
""" """"""" """"""""""
2 Sudha 233
2 >agan 433
6
1 ows sele!ted5
SQL- sele!t E fo$ student whee no - 1.
NO N(M' M(RFS
""" """"""" """"""""""
4 Ra$esh
@ Madhu
C 9isu
D Rattu
@ ows sele!ted5
SQL- sele!t E fo$ student whee no ,I 1.
NO N(M' M(RFS
""" """"""" """"""""""
2 Sudha 233
1 Sað 133
2 >agan 433
1 Naen @33
@ ows sele!ted5
SQL- sele!t E fo$ student whee no -I 1.
NO N(M' M(RFS
""" """"""" """""""""
1 Sað 133
1 Naen @33
4 Ra$esh
@ Madhu
C 9isu
D Rattu
D ows sele!ted5
SQL- sele!t E fo$ student whee no JI 1.
7
NO N(M' M(RFS
""" """"""" """"""""""
2 Sudha 233
2 >agan 433
4 Ra$esh
@ Madhu
C 9isu
D Rattu
D ows sele!ted5
SQL- sele!t E fo$ student whee no ,- 1.
NO N(M' M(RFS
""" """"""" """"""""""
2 Sudha 233
2 >agan 433
4 Ra$esh
@ Madhu
C 9isu
D Rattu
D ows sele!ted5
%) USIN8 (ND
This will gives the output when all the !onditions %e!o$e tue5
s7nta/0
sele!t E fo$ ,table_name- whee ,condition1- and ,condition2- and 55 ,conditionn-.
'/0
SQL- sele!t E fo$ student whee no I 1 and $a&s -I 133.
NO N(M' M(RFS
""" """"""" """"""""
1 Sað 133
1 Naen @33
8
1 ows sele!ted5
!) USIN8 OR
This will gives the output when eithe of the !onditions %e!o$e tue5
s7nta/0
sele!t E fo$ ,table_name- whee ,condition1- and ,condition2- o 55 ,conditionn-.
'/0
SQL- sele!t E fo$ student whee no I 1 o $a&s -I 133.
NO N(M' M(RFS
""" """"""" """""""""
1 Sað 133
2 >agan 433
1 Naen @33
4 ows sele!ted5
d) USIN8 )'T6''N
This will gives the output %ased on the !olu$n and its lowe %ound# uppe%ound5
s7nta/0
sele!t E fo$ ,table_name- whee ,col- %etween ,lower bound- and ,upper bound-.
'/0
SQL- sele!t E fo$ student whee $a&s %etween 133 and @33.
NO N(M' M(RFS
""" """"""" """""""""
1 Sað 133
2 >agan 433
1 Naen @33
4 ows sele!ted5
e) USIN8 NOT )'T6''N
This will gives the output %ased on the !olu$n whi!h values ae not in its lowe %ound#
uppe%ound5
s7nta/0
sele!t E fo$ ,table_name- whee ,col- not %etween ,lower bound- and ,upper bound-.
'/0
SQL- sele!t E fo$ student whee $a&s not %etween 133 and @33.
9
NO N(M' M(RFS
""" """"""" """""""""
2 Sudha 233
2 ow sele!ted5
f) USIN8 IN
This will gives the output %ased on the !olu$n and its list of values spe!ified5
s7nta/0
sele!t E fo$ ,table_name- whee ,col- in ( value1, value2, value3 valuen).
'/0
SQL- sele!t E fo$ student whee no in (2# 1# 4).
NO N(M' M(RFS
""" """"""" """""""""
2 Sudha 233
1 Sað 133
2 >agan 433
1 Naen @33
4 Ra$esh
C ows sele!ted5
g) USIN8 NOT IN
This will gives the output %ased on the !olu$n whi!h values ae not in the list of values
spe!ified5
s7nta/0
sele!t E fo$ ,table_name- whee ,col- not in ( value1, value2, value3 valuen).
'/0
SQL- sele!t E fo$ student whee no not in (2# 1# 4).
NO N(M' M(RFS
""" """"""" """""""""
@ Madhu
C 9isu
10
D Rattu
4 ows sele!ted5
h) USIN8 NULL
This will gives the output %ased on the null values in the spe!ified !olu$n5
s7nta/0
sele!t E fo$ ,table_name- whee ,col- is null.
'/0
SQL- sele!t E fo$ student whee $a&s is null.
NO N(M' M(RFS
""" """"""" """""""""
4 Ra$esh
@ Madhu
C 9isu
D Rattu
@ ows sele!ted5
i) USIN8 NOT NULL
This will gives the output %ased on the not null values in the spe!ified !olu$n5
s7nta/0
sele!t E fo$ ,table_name- whee ,col- is not null.
'/0
SQL- sele!t E fo$ student whee $a&s is not null.
NO N(M' M(RFS
""" """"""" """""""""
2 Sudha 233
1 Sað 133
2 >agan 433
1 Naen @33
@ ows sele!ted5
K) USIN8 LIF'
This will %e used to sea!h though the ows of data%ase !olu$n %ased on the patten 7ou
spe!if75
s7nta/0
sele!t E fo$ ,table_name- whee ,col- li&e ,pattern-.
11
'/0
i) This will give the ows whose $a&s ae 2335
SQL- sele!t E fo$ student whee $a&s li&e 233.
NO N(M' M(RFS
""" """"""" """""""""
2 Sudha 233
2 ow sele!ted5
ii) This will give the ows whose na$e stat with LS;5
SQL- sele!t E fo$ student whee na$e li&e =SM=.
NO N(M' M(RFS
""" """"""" """""""""
2 Sudha 233
1 Sað 133
1 ows sele!ted5
iii) This will give the ows whose na$e ends with Lh;5
SQL- sele!t E fo$ student whee na$e li&e =Mh=.
NO N(M' M(RFS
""" """"""" """""""""
1 Sað 133
4 Ra$esh
1 ows sele!ted5
i9) This will give the ows whose na$e;s se!ond lette stat with La;5
SQL- sele!t E fo$ student whee na$e li&e =NaM=.
NO N(M' M(RFS
12
""" """"""" """"""""
1 Sað 133
2 >agan 433
1 Naen @33
4 Ra$esh
@ Madhu
D Rattu
D ows sele!ted5
9) This will give the ows whose na$e;s thid lette stat with Ld;5
SQL- sele!t E fo$ student whee na$e li&e =NNdM=.
NO N(M' M(RFS
""" """"""" """""""""
2 Sudha 233
@ Madhu
1 ows sele!ted5
9i) This will give the ows whose na$e;s se!ond lette stat with Lt; fo$ ending5
SQL- sele!t E fo$ student whee na$e li&e =MNtM=.
NO N(M' M(RFS
""" """"""" """""""""
1 Sað 133
D Rattu
1 ows sele!ted5
9ii) This will give the ows whose na$e;s thid lette stat with Le; fo$ ending5
SQL- sele!t E fo$ student whee na$e li&e =MeNNM=.
NO N(M' M(RFS
""" """"""" """""""""
1 Sað 133
4 Ra$esh
13
1 ows sele!ted5
9iii) This will give the ows whose na$e !otains 1 a;s5
SQL- sele!t E fo$ student whee na$e li&e =MaM a M=.
NO N(M' M(RFS
""" """"""" """"""""""
2 >agan 433
2 ow sele!ted5
E *ou have to spe!if7 the pattens in like using undes!oe ( N )5
USIN8 ORD'R )*
This will %e used to odeing the !olu$ns data (as!ending o des!ending)5
S7nta/0
Sele!t E fo$ ,table_name- ode %7 ,col- des!.
)7 default oa!le will use as!ending ode5
If 7ou want output in des!ending ode 7ou have to use desc &e7wod afte the !olu$n5
'/0
SQL- sele!t E fo$ student ode %7 no.
NO N(M' M(RFS
""" """"""" """""""""
2 Sudha 233
2 >agan 433
1 Sað 133
1 Naen @33
4 Ra$esh
@ Madhu
C 9isu
D Rattu
G ows sele!ted5
SQL- sele!t E fo$ student ode %7 no des!.
NO N(M' M(RFS
""" """"""" """""""""
14
D Rattu
C 9isu
@ Madhu
4 Ra$esh
1 Sað 133
1 Naen @33
2 Sudha 233
2 >agan 433
G ows sele!ted5
USIN8 DML
USIN8 UAD(T'
This !an %e used to $odif7 the ta%le data5
S7nta/0
Update ,table_name- set ,col1- I value2# ,col2- I value1 whee ,condition-.
'/0
SQL- update student set $a&s I C33.
If 7ou ae not spe!if7ing an7 !ondition this will update entie ta%le5
SQL- update student set $a&s I C33 whee no I 1.
SQL- update student set $a&s I C33# na$e I =9enu= whee no I 2.
USIN8 D'L'T'
This !an %e used to delete the ta%le data te$poail75
S7nta/0
Delete ,table_name- whee ,condition-.
'/0
SQL- delete BROM student.
If 7ou ae not spe!if7ing an7 !ondition this will delete entie ta%le5
SQL- delete BROM student whee no I 1.
15
USIN8 DDL
USIN8 (LT'R
This !an %e used to add o e$ove !olu$ns and to $odif7 the pe!ision of the datat7pe5
a) (DDIN8 COLUMN
s7nta/0
alte ta%le ,table_name- add ,col datatype-.
'/0
SQL- alte ta%le student add sdo% date.
%) R'MO9IN8 COLUMN
s7nta/0
alte ta%le ,table_name- dop ,col datatype-.
'/0
SQL- alte ta%le student dop !olu$n sdo%.
!) INCR'(SIN8 OR D'CR'(SIN8 AR'CISION OB ( COLUMN
s7nta/0
alte ta%le ,table_name- $odif7 ,col datatype-.
'/0
SQL- alte ta%le student $odif7 $a&s nu$%e(C).
E To de!ease pe!ision the !olu$n should %e e$pt75
d) M(FIN8 COLUMN UNUS'D
s7nta/0
alte ta%le ,table_name- set unused !olu$n ,col-.
'/0
SQL- alte ta%le student set unused !olu$n $a&s.
16
'ven though the !olu$n is unused still it will o!!up7 $e$o75
d) DROAAIN8 UNUS'D COLUMNS
s7nta/0
alte ta%le ,table_name- dop unused !olu$ns.
'/0
SQL- alte ta%le student dop unused !olu$ns.
E *ou !an not dop individual unused !olu$ns of a ta%le5
e) R'N(MIN8 COLUMN
s7nta/0
alte ta%le ,table_name- ena$e !olu$n ,old_col_name- to ,new_col_name-.
'/0
SQL- alte ta%le student ena$e !olu$n $a&s to s$a&s.
USIN8 TRUNC(T'
This !an %e used to delete the entie ta%le data pe$anentl75
S7nta/0
tun!ate ta%le ,table_name-.
'/0
SQL- tun!ate ta%le student.
USIN8 DROA
This will %e used to dop the data%ase o%Ke!t.
S7nta/0
Dop ta%le ,table_name-.
'/0
SQL- dop ta%le student.
USIN8 R'N(M'
This will %e used to ena$e the data%ase o%Ke!t.
S7nta/0
ena$e ,oldNtable_name- to ,new_table_name-.
'/0
17
SQL- ena$e student to stud.
USIN8 TCL
USIN8 COMMIT
This will %e used to save the wo&5
Co$$it is of two t7pes5
I$pli!it
'/pli!it
a) IMALICIT
This will %e issued %7 oa!le intenall7 in two situations5
6hen an7 DDL opeation is pefo$ed5
6hen 7ou ae e/iting fo$ SQL E ALUS5
%) '+ALICIT
This will %e issued %7 the use5
S7nta/0
Co$$it o !o$$it wo&.
E 6hen eve 7ou !o$$itted then the tansa!tion was !o$pleted5
USIN8 ROLL)(CF
This will undo the opeation5
This will %e applied in two $ethods5
Upto pevious !o$$it
Upto pevious oll%a!&
S7nta/0
Roll o oll wo&.
O
Roll%a!& o oll%a!& wo&.
E 6hile po!ess is going on# if suddenl7 powe goes then oa!le will oll%a!& the tansa!tion5
USIN8 S(9'AOINT
18
*ou !an use savepoints to oll%a!& potions of 7ou !uent set of tansa!tions5
S7nta/0
Savepoint ,savepoint_name-.
'/0
SQL- savepoint s2.
SQL- inset into student values(2# La;# 233).
SQL- savepoint s1.
SQL- inset into student values(1# L%;# 133).
SQL- savepoint s4.
SQL- inset into student values(4# L!;# 433).
SQL- savepoint s@.
SQL- inset into student values(@# Ld;# @33).
)efoe oll%a!&
SQL- sele!t E fo$ student.
NO N(M' M(RFS
""" """"""" """"""""""
2 a 233
1 % 133
4 ! 433
@ d @33
@ ows sele!ted5
SQL- oll%a!& to savepoint s4.
O
SQL- oll%a!& to s4.
This will oll%a!& last two e!ods5
SQL- sele!t E fo$ student.
NO N(M' M(RFS
""" """"""" """"""""""
2 a 233
1 % 133
1 ows sele!ted5
19
USIN8 DCL
DCL !o$$ands ae used to ganting and evo&ing the pe$issions5
USIN8 8R(NT
This is used to gant the pivileges to othe uses5
S7nta/0
8ant ,privilees- on ,ob!ect_name- to ,user_name- Owith gant optionP.
'/0
SQL- gant sele!t on student to sudha. "" 7ou !an give individual pivilege
SQL- gant sele!t# inset on student to sudha. "" 7ou !an give set of pivileges
SQL- gant all on student to sudha. "" 7ou !an give all pivileges
The sudha use has to use dot $ethod to a!!ess the o%Ke!t5
SQL- sele!t E fo$ sað5student.
The sudha use !an not gant pe$ission on student ta%le to othe uses5 To get this t7pe of
option use the following5
SQL- gant all on student to sudha with gant option.
Now sudha use also gant pe$issions on student ta%le5
USIN8 R'9OF'
This is used to evo&e the pivileges fo$ the uses to whi!h 7ou ganted the pivileges5
S7nta/0
Revo&e ,privilees- on ,ob!ect_name- fo$ ,user_name-.
'/0
SQL- evo&e sele!t on student fo$ sudha. "" 7ou !an evo&e individual pivilege
SQL- evo&e sele!t# inset on student fo$ sudha. "" 7ou !an evo&e set of pivileges
SQL- evo&e all on student fo$ sudha. "" 7ou !an evo&e all pivileges
USIN8 (LI(S'S
20
CR'(T' 6IT: S'L'CT
6e !an !eate a ta%le using e/isting ta%le Oalong with dataP5
S7nta/0
Ceate ta%le ,new_table_name- Ocol1, col2, col3 ... colnP as sele!t E fo$ ,old_table_name-.
'/0
SQL- !eate ta%le student2 as sele!t E fo$ student.
Ceating ta%le with 7ou own !olu$n na$es5
SQL- !eate ta%le student1(sno# sna$e# s$a&s) as sele!t E fo$ student.
Ceating ta%le with spe!ified !olu$ns5
SQL- !eate ta%le student4 as sele!t no#na$e fo$ student.
Ceating ta%le with out ta%le data5
SQL- !eate ta%le student1(sno# sna$e# s$a&s) as sele!t E fo$ student whee 2 I 1.
In the a%ove whee !lause give an7 !ondition whi!h does not satisf75
INS'RT 6IT: S'L'CT
Using this we !an inset e/isting ta%le data to a anothe ta%le in a single tip5 )ut the ta%le stu!tue should %e sa$e5
S7nta/0
Inset into ,table1- sele!t E fo$ ,table2-.
'/0
SQL- inset into student2 sele!t E fo$ student.
Inseting data into spe!ified !olu$ns
SQL- inset into student2(no# na$e) sele!t no# na$e fo$ student.
COLUMN (LI(S'S
S7nta/0
"elect #orinal_col$ #alias_name$ %rom #tableNna$e-.
'/0
SQL- sele!t no sno fo$ student.
o
SQL- sele!t no QsnoR fo$ student.
21
T()L' (LI(S'S
If 7ou ae using ta%le aliases 7ou !an use dot $ethod to the !olu$ns5
S7nta/0
Sele!t ,alias_name-5,col1-# ,alias_name-5,col2- S ,alias_name-5,coln- fo$ ,table_name- ,alias_name-.
'/0
SQL- sele!t s5no# s5na$e fo$ student s.
USIN8 M'R8'
M'R8'
*ou !an use $ege !o$$and to pefo$ inset and update in a single !o$$and5
'/0
SQL- Mege into student2 s2
22
Using (sele!t EBo$ student1) s1
On(s25noIs15no)
6hen $at!hed then
Update set $a&s I s15$a&s
6hen not $at!hed then
Inset (s25no#s25na$e#s25$a&s)
9alues(s15no#s15na$e#s15$a&s).
In the a%ove the two ta%les ae with the sa$e stu!tue %ut we !an $ege diffeent stu!tued
ta%les also %ut the datat7pe of the !olu$ns should $at!h5
(ssu$e that student2 has !olu$ns li&e no#na$e#$a&s and student1 has !olu$ns li&e no#
na$e# hno# !it75
SQL- Mege into student2 s2
Using (sele!t EBo$ student1) s1
On(s25noIs15no)
6hen $at!hed then
Update set $a&s I s15hno
6hen not $at!hed then
Inset (s25no#s25na$e#s25$a&s)
9alues(s15no#s15na$e#s15hno).
MULTI)L' INS'RTS
6e have ta%le !alled D'AT with the following !olu$ns and data
D'ATNO DN(M' LOC
"""""""" """""""" """"
23 a!!ounting new 7o&
13 esea!h dallas
43 sales Chi!ago
@3 opeations %oston
23
a) CR'(T' STUD'NT T()L'
SQL- Ceate ta%le student(no nu$%e(1)#na$e va!ha(1)#$a&s nu$%e(4)).
%) MULTI INS'RT 6IT: (LL BI'LDS
SQL- Inset all
Into student values(2#;a;#233)
Into student values(1#;%;#133)
Into student values(4#;!;#433)
Sele!t Efo$ dept whee deptnoI23.
"" This insets 4 ows
!) MULTI INS'RT 6IT: SA'CIBI'D BI'LDS
SQL- inset all
Into student (no#na$e) values(@#;d;)
Into student(na$e#$a&s) values(;e;#@33)
Into student values(4#;!;#433)
Sele!t Efo$ dept whee deptnoI23.
"" This insets 4 ows
d) MULTI INS'RT 6IT: DUALIC(T' RO6S
SQL- inset all
Into student values(2#;a;#233)
Into student values(1#;%;#133)
Into student values(4#;!;#433)
Sele!t Efo$ dept whee deptno - 23.
"" This insets T ows %e!ause in the sele!t state$ent etieves 4 e!ods (4 insets fo ea!h
ow etieved)
e) MULTI INS'RT 6IT: CONDITIONS )(S'D
SQL- Inset all
6hen deptno - 23 then
Into student2 values(2#;a;#233)
6hen dna$e I LS(L'S; then
Into student1 values(1#;%;#133)
24
6hen lo! I LN'6 *ORF; then
Into student4 values(4#;!;#433)
Sele!t Efo$ dept whee deptno-23.
"" This insets @ ows %e!ause the fist !ondition satisfied 4 ti$es# se!ond !ondition
satisfied on!e and the last none5
f) MULTI INS'RT 6IT: CONDITIONS )(S'D (ND 'LS'
SQL- Inset all
6hen deptno - 233 then
Into student2 values(2#;a;#233)
6hen dna$e I LS; then
Into student1 values(1#;%;#133)
6hen lo! I LN'6 *ORF; then
Into student4 values(4#;!;#433)
'lse
Into student values(@#;d;#@33)
Sele!t Efo$ dept whee deptno-23.
"" This insets 4 e!ods %e!ause the else satisfied 4 ti$es
g) MULTI INS'RT 6IT: CONDITIONS )(S'D (ND BIRST
SQL- Inset fist
6hen deptno I 13 then
Into student2 values(2#;a;#233)
6hen dna$e I LR'S'(RC:; then
Into student1 values(1#;%;#133)
6hen lo! I LN'6 *ORF; then
Into student4 values(4#;!;#433)
Sele!t Efo$ dept whee deptnoI13.
"" This insets 2 e!od %e!ause the fist !lause avoid to !he!& the e$aining !onditions
on!e the !ondition is satisfied5
h) MULTI INS'RT 6IT: CONDITIONS )(S'D# BIRST (ND 'LS'
SQL- Inset fist
6hen deptno I 43 then
25
Into student2 values(2#;a;#233)
6hen dna$e I LR; then
Into student1 values(1#;%;#133)
6hen lo! I LN'6 *ORF; then
Into student4 values(4#;!;#433)
'lse
Into student values(@#;d;#@33)
Sele!t Efo$ dept whee deptnoI13.
"" This insets 2 e!od %e!ause the else !lause satisfied on!e
i) MULTI INS'RT 6IT: MULTI)L' T()L'S
SQL- Inset all
Into student2 values(2#;a;#233)
Into student1 values(1#;%;#133)
Into student4 values(4#;!;#433)
Sele!t Efo$ dept whee deptnoI23.
"" This insets 4 ows
EE *ou !an use $ulti ta%les with spe!ified fields# with dupli!ate ows# with !onditions# with
fist and else !lauses5
BUNCTIONS
Bun!tions !an %e !ategoiUed as follows5
Single ow fun!tions
8oup fun!tions
SIN8L' RO6 BUNCTIONS
Single ow fun!tions !an %e !ategoiUed into five5 These will %e applied fo ea!h ow and podu!es individual output fo
ea!h ow5
Nu$ei! fun!tions
Sting fun!tions
Date fun!tions
Mis!ellaneous fun!tions
26
Convesion fun!tions
NUM'RIC BUNCTIONS
(%s
Sign
SVt
Mod
Nvl
Aowe
'/p
Ln
Log
Ceil
Bloo
Round
Tun&
)itand
8eatest
Least
Coales!e
a) ()S
(%solute value is the $easue of the $agnitude of value5
(%solute value is alwa7s a positive nu$%e5
S7nta/0 a%s (value)
'/0
SQL- sele!t a%s(C)# a%s("C)# a%s(3)# a%s(null) fo$ dual.
()S(C) ()S("C) ()S(3) ()S(NULL)
"""""""""" """""""""" """""""""" """""""""""""
C "C 3
%) SI8N
Sign gives the sign of a value5
S7nta/0 sign (value)
'/0
SQL- sele!t sign(C)# sign("C)# sign(3)# sign(null) fo$ dual.
27
SI8N(C) SI8N("C) SI8N(3) SI8N(NULL)
"""""""""" """""""""" """""""""" """"""""""""""
2 "2 3
!) SQRT
This will give the sVuae oot of the given value5
S7nta/0 sVt (value) "" hee value $ust %e positive5
'/0
SQL- sele!t sVt(@)# sVt(3)# sVt(null)# sVt(2) fo$ dual.
SQRT(@) SQRT(3) SQRT(NULL) SQRT(2)
"""""""""" """""""""" """"""""""""""" """"""""""
1 3 2
d) MOD
This will give the e$ainde5
S7nta/0 $od (value, divisor)
'/0
SQL- sele!t $od(W#@)# $od(2#C)# $od(null#null)# $od(3#3)# $od("W#@) fo$ dual.
MOD(W#@) MOD(2#C) MOD(NULL#NULL) MOD(3#3) MOD("W#@)
"""""""""""" """""""""" """"""""""""""""""""" """"""""""" """""""""""""
4 2 3 "4
e) N9L
This will su%stitutes the spe!ified value in the pla!e of null values5
S7nta/0 nvl (null_col, replacement_value)
'/0
SQL- sele!t E fo$ student. "" hee fo 4
d
ow $a&s value is null
NO N(M' M(RFS
""" """"""" """""""""
2 a 233
1 % 133
4 !
28
SQL- sele!t no# na$e# nvl($a&s#433) fo$ student.
NO N(M' N9L(M(RFS#433)
""" """"""" """""""""""""""""""""
2 a 233
1 % 133
4 ! 433
SQL- sele!t nvl(2#1)# nvl(1#4)# nvl(@#4)# nvl(C#@) fo$ dual.
N9L(2#1) N9L(1#4) N9L(@#4) N9L(C#@)
"""""""""" """""""""" """""""""" """"""""""
2 1 @ C
SQL- sele!t nvl(3#3)# nvl(2#2)# nvl(null#null)# nvl(@#@) fo$ dual.
N9L(3#3) N9L(2#2) N9L(null#null) N9L(@#@)
"""""""""" """""""""" """"""""""""""""" """"""""""
3 2 @
f) AO6'R
Aowe is the a%ilit7 to aise a value to a given e/ponent5
S7nta/0 powe (value, e&ponent)
'/0
SQL- sele!t powe(1#C)# powe(3#3)# powe(2#2)# powe(null#null)# powe(1#"C) fo$
dual.
AO6'R(1#C) AO6'R(3#3) AO6'R(2#2) AO6'R(NULL#NULL) AO6'R(1#"C)
"""""""""""""" """""""""""""" """"" """"""""" """"""""""""""""""""""" """""""""""""""
41 2 2 53421C
g) '+A
This will aise e value to the give powe5
S7nta/0 e/p (value)
'/0
SQL- sele!t e/p(2)# e/p(1)# e/p(3)# e/p(null)# e/p("1) fo$ dual.
29
'+A(2) '+A(1) '+A(3) '+A(NULL) '+A("1)
"""""""" """"""""" """""""" """"""""""""" """"""""""
15W2G1G2G4 W54GT3CD2 2 524C44C1G4
h) LN
This is %ased on natual o %ase e logaith$5
S7nta/0 ln (value) "" hee value $ust %e geate than Ueo whi!h is positive onl75
'/0
SQL- sele!t ln(2)# ln(1)# ln(null) fo$ dual.
LN(2) LN(1) LN(NULL)
""""""" """"""" """"""""""""
3 5DT42@W2G2
Ln and '/p ae e!ipo!al to ea!h othe5
'+A (4) I 1353GCC4DT
LN (1353GCC4DT) I 4
i) LO8
This is %ased on 23 %ased logaith$5
S7nta/0 log (23# value) "" hee value $ust %e geate than Ueo whi!h is positive onl75
'/0
SQL- sele!t log(23#233)# log(23#1)# log(23#2)# log(23#null) fo$ dual.
LO8(23#233) LO8(23#1) LO8(23#2) LO8(23#NULL)
""""""""""""""" """"""""""" """""""""""" """""""""""""""""
1 543231TTTD 3
LN (value) I LO8 ('+A(2)# value)
SQL- sele!t ln(4)# log(e/p(2)#4) fo$ dual.
LN(4) LO8('+A(2)#4)
""""""" """""""""""""""""
253TGD211T 253TGD211T
30
K) C'IL
This will podu!e a whole nu$%e that is geate than o eVual to the spe!ified value5
S7nta/0 !eil (value)
'/0
SQL- sele!t !eil(C)# !eil(C52)# !eil("C)# !eil( "C52)# !eil(3)# !eil(null) fo$ dual.
C'IL(C) C'IL(C52) C'IL("C) C'IL("C52) C'IL(3) C'IL(NULL)
""""""""" """"""""""" """""""""" """""""""""" """""""" """"""""""""""
C D "C "C 3
&) BLOOR
This will podu!e a whole nu$%e that is less than o eVual to the spe!ified value5
S7nta/0 floo (value)
'/0
SQL- sele!t floo(C)# floo(C52)# floo("C)# floo( "C52)# floo(3)# floo(null) fo$ dual.
BLOOR(C) BLOOR(C52) BLOOR("C) BLOOR("C52) BLOOR(3) BLOOR(NULL)
""""""""""" """"""""""""" """""""""""" """""""""""""" """"""""""" """"""""""""""""
C C "C "D 3
l) ROUND
This will ounds nu$%es to a given nu$%e of digits of pe!ision5
S7nta/0 ound (value, precision)
'/0
SQL- ound(214514@C#1)# ound(sele!t ound(214514@C)# 214514C@#1) fo$ dual.
ROUND(214514@C) ROUND(214514@C#3) ROUND(214514@C#1) ROUND(214514C@#1)
""""""""""""""""""""" """""""""""""""""""""""" """"""""""""""""""""""" """""""""""""""""""""""
214 214 214514 21451@
SQL- sele!t ound(214514@C#"2)# ound(214514@C#"1)# ound(214514@C#"4)#
ound(214514@C#"@) fo$ dual.
ROUND(214514@C#"2) ROUND(214514@C#"1) ROUND(214514@C#"4) ROUND(214514@C#"@)
"""""""""""""""""""""""" """"""""""""""""""""""""" """""""""""""""""""""""" """"""""""""""""""""""""
213 233 3 3
SQL- sele!t ound(214#3)# ound(214#2)# ound(214#1) fo$ dual.
31
ROUND(214#3) ROUND(214#2) ROUND(214#1)
""""""""""""""""" """"""""""""""""" """"""""""""""""
214 214 214
SQL- sele!t ound("214#3)# ound("214#2)# ound("214#1) fo$ dual.
ROUND("214#3) ROUND("214#2) ROUND("214#1)
"""""""""""""""""" """"""""""""""""" """""""""""""""""""
"214 "214 "214
SQL- sele!t ound(214#"2)# ound(214#"1)# ound(214#"4)# ound("214#"2)# ound("214#"
1)# ound("214#"4) fo$ dual.
ROUND(214#"2) ROUND(214#"1) ROUND(214#"4) ROUND("214#"2) ROUND("214#"1)
ROUND("214#"4)
""""""""""""" """"""""""""" """"""""""""" """""""""""""" """""""""""""" """"""""""""""
213 233 3 "213 "233 3
SQL- sele!t ound(null#null)# ound(3#3)# ound(2#2)# ound("2#"2)# ound("1#"1) fo$
dual.
ROUND(NULL#NULL) ROUND(3#3) ROUND(2#2) ROUND("2#"2) ROUND("1#"1)
""""""""""""""""""""""" """""""""""""" """""""""""""" """""""""""""""" """"""""""""""""
3 2 3 3
$) TRUNC
This will tun!ates o !hops off digits of pe!ision fo$ a nu$%e5
S7nta/0 tun! (value, precision)
'/0
SQL- sele!t tun!(214514@C)# tun!(214514@C#1)# tun!(214514C@#1) fo$ dual.
TRUNC(214514@C) TRUNC(214514@C#1) TRUNC(214514C@#1)
""""""""""""""""""""" """"""""""""""""""""""" """""""""""""""""""""""
214 214514 214514
SQL- sele!t tun!(214514@C#"2)# tun!(214514@C#"1)# tun!(214514@C#"4)#
tun!(214514@C#"@) fo$ dual.
32
TRUNC(214514@C#"2) TRUNC(214514@C#"1) TRUNC(214514@C#"4) TRUNC(214514@C#"@)
"""""""""""""""""""""""" """""""""""""""""""""""" """"""""""""""""""""""" """"""""""""""""""""""""
213 233 3 3
SQL- sele!t tun!(214#3)# tun!(214#2)# tun!(214#1) fo$ dual.
TRUNC(214#3) TRUNC(214#2) TRUNC(214#1)
"""""""""""""""" """""""""""""""" """""""""""""""""
214 214 214
SQL- sele!t tun!("214#3)# tun!("214#2)# tun!("214#1) fo$ dual.
TRUNC("214#3) TRUNC("214#2) TRUNC("214#1)
""""""""""""""""" """"""""""""""""" """""""""""""""""
"214 "214 "214
SQL- sele!t tun!(214#"2)# tun!(214#"1)# tun!(214#"4)# tun!("214#"2)# tun!("214#1)#
tun!("214#"4) fo$ dual.
TRUNC(214#"2) TRUNC(214#"1) TRUNC(214#"4) TRUNC("214#"2) TRUNC("214#1) TRUNC("
214#"4)
""""""""""""" """"""""""""" """"""""""""" """""""""""""" """"""""""""" """"""""""""""
213 233 3 "213 "214 3
SQL- sele!t tun!(null#null)# tun!(3#3)# tun!(2#2)# tun!("2#"2)# tun!("1#"1) fo$ dual.
TRUNC(NULL#NULL) TRUNC(3#3) TRUNC(2#2) TRUNC("2#"2) TRUNC("1#"1)
""""""""""""""""""""""" """"""""""""" """"""""""""" """"""""""""""" """"""""""""""""
3 2 3 3
n) )IT(ND
This will pefo$ %itwise and opeation5
S7nta/0 %itand (value1, value2)
'/0
33
SQL- sele!t %itand(1#4)# %itand(3#3)# %itand(2#2)# %itand(null#null)# %itand("1#"4) fo$
dual.
)IT(ND(1#4) )IT(ND(3#3) )IT(ND(2#2) )IT(ND(NULL#NULL) )IT(ND("1#"4)
"""""""""""""" """"""""""""""" """""""""""""" """""""""""""""""""""""" """""""""""""""""
1 3 2 "@
o) 8R'(T'ST
This will give the geatest nu$%e5
S7nta/0 geatest (value1, value2, value3 valuen)
'/0
SQL- sele!t geatest(2# 1# 4)# geatest("2# "1# "4) fo$ dual.
8R'(T'ST(2#1#4) 8R'(T'ST("2#"1#"4)
"""""""""""""""""""" """""""""""""""""""""""
4 "2
If all the values ae Ueos then it will displa7 Ueo5
If all the paa$etes ae nulls then it will displa7 nothing5
If an7 of the paa$etes is null it will displa7 nothing5
p) L'(ST
This will give the least nu$%e5
S7nta/0 least (value1, value2, value3 valuen)
'/0
SQL- sele!t least(2# 1# 4)# least("2# "1# "4) fo$ dual.
L'(ST(2#1#4) L'(ST("2#"1#"4)
"""""""""""""""""""" """""""""""""""""""""""
2 "4
If all the values ae Ueos then it will displa7 Ueo5
If all the paa$etes ae nulls then it will displa7 nothing5
If an7 of the paa$etes is null it will displa7 nothing5
V) CO(L'SC'
This will etun fist non"null value5
S7nta/0 !oales!e (value1, value2, value3 valuen)
34
'/0
SQL- sele!t !oales!e(2#1#4)# !oales!e(null#1#null#C) fo$ dual.
CO(L'SC'(2#1#4) CO(L'SC'(NULL#1#NULL#C)
""""""""""""""""""" """""""""""""""""""""""""""""""
2 1
STRIN8 BUNCTIONS
Init!ap
Uppe
Lowe
Length
Rpad
Lpad
Lti$
Rti$
Ti$
Tanslate
Repla!e
Sounde/
Con!at ( L XX L Con!atenation opeato)
(s!ii
Ch
Su%st
Inst
De!ode
8eatest
Least
Coales!e
a) INITC(A
This will !apitaliUe the initial lette of the sting5
S7nta/0 init!ap (strin)
'/0
SQL- sele!t init!ap(=!o$pute=) fo$ dual.
35
INITC(A
"""""""""""
Co$pute
%) UAA'R
This will !onvet the sting into uppe!ase5
S7nta/0 uppe (strin)
'/0
SQL- sele!t uppe(=!o$pute=) fo$ dual.
UAA'R
"""""""""""
COMAUT'R
!) LO6'R
This will !onvet the sting into lowe!ase5
S7nta/0 lowe (strin)
'/0
SQL- sele!t lowe(=COMAUT'R=) fo$ dual.
LO6'R
"""""""""""
!o$pute
d) L'N8T:
This will give length of the sting5
S7nta/0 length (strin)
'/0
SQL- sele!t length(=!o$pute=) fo$ dual.
L'N8T:
"""""""""""
G
e) RA(D
This will allows 7ou to pad the ight side of a !olu$n with an7 set of !haa!tes5
S7nta/0 pad (strin, lent' (, paddin_c'ar))
'/0
SQL- sele!t pad(=!o$pute=#2C#=E=)# pad(=!o$pute=#2C#=EY=) fo$ dual.
36
RA(D(=COMAUT'R= RA(D(=COMAUT'R=
"""""""""""""""""""""" """"""""""""""""""""""
!o$puteEEEEEEE !o$puteEYEYEYE
"" Default padding !haa!te was %lan& spa!e5
f) LA(D
This will allows 7ou to pad the left side of a !olu$n with an7 set of !haa!tes5
S7nta/0 lpad (strin, lent' (, paddin_c'ar))
'/0
SQL- sele!t lpad(=!o$pute=#2C#=E=)# lpad(=!o$pute=#2C#=EY=) fo$ dual.
LA(D(=COMAUT'R= LA(D(=COMAUT'R=
""""""""""""""""""""" """""""""""""""""""""
EEEEEEE!o$pute EYEYEYE!o$pute
"" Default padding !haa!te was %lan& spa!e5
g) LTRIM
This will ti$ off unwanted !haa!tes fo$ the left end of sting5
S7nta/0 lti$ (strin (,unwanted_c'ars))
'/0
SQL- sele!t lti$(=!o$pute=#=!o=)# lti$(=!o$pute=#=!o$=) fo$ dual.
LTRIM( LTRIM
"""""""" """""""""
$pute pute
SQL- sele!t lti$(=!o$pute=#=pute=)# lti$(=!o$pute=#=o$pute=) fo$ dual.
LTRIM(=C LTRIM(=C
"""""""""" """"""""""
!o$pute !o$pute
"" If 7ou haven;t spe!if7 an7 unwanted !haa!tes it will displa7 entie sting5
h) RTRIM
This will ti$ off unwanted !haa!tes fo$ the ight end of sting5
37
S7nta/0 ti$ (strin (, unwanted_c'ars))
'/0
SQL- sele!t ti$(=!o$pute=#=e=)# ti$(=!o$pute=#=te=) fo$ dual.
RTRIM( RTRIM
"""""""" """""""""
!o$put !o$pu
SQL- sele!t ti$(=!o$pute=#=!o$put;)# ti$(=!o$pute=#=!o$pute=) fo$ dual.
RTRIM(=C RTRIM(=C
"""""""""" """"""""""
!o$pute !o$pute
"" If 7ou haven;t spe!if7 an7 unwanted !haa!tes it will displa7 entie sting5
i) TRIM
This will ti$ off unwanted !haa!tes fo$ the %oth sides of sting5
S7nta/0 ti$ (unwanted_c'ars fo$ strin)
'/0
SQL- sele!t ti$( =i= fo$ =indiani=) fo$ dual.
TRIM(
"""""
ndian
SQL- sele!t ti$( leading=i= fo$ =indiani=) fo$ dual. "" this will wo& as LTRIM
TRIM(L
""""""
ndiani
SQL- sele!t ti$( tailing=i= fo$ =indiani=) fo$ dual. "" this will wo& as RTRIM
TRIM(T
""""""
indian
K) TR(NSL(T'
This will epla!e the set of !haa!tes# !haa!te %7 !haa!te5
38
S7nta/0 tanslate (strin, old_c'ars, new_c'ars)
'/0
SQL- sele!t tanslate(=india=#=in=#=/7=) fo$ dual.
TR(NS
""""""""
/7d/a
&) R'AL(C'
This will epla!e the set of !haa!tes# sting %7 sting5
S7nta/0 epla!e (strin, old_c'ars (, new_c'ars))
'/0
SQL- sele!t epla!e(=india=#=in=#=/7=)# epla!e(Lindia;#;in;) fo$ dual.
R'AL(C' R'AL(C'
""""""""""" """""""""""
+7dia dia
l) SOUND'+
This will %e used to find wods that sound li&e othe wods# e/!lusivel7 used in whee !lause5
S7nta/0 sounde/ (strin)
'/0
SQL- sele!t E fo$ e$p whee sounde/(ena$e) I sounde/(=SMIT=).
'MANO 'N(M' >O) M8R :IR'D(T' S(L D'ATNO
"""""""" """""""" """"" """"" """""""""""" """"""""" """"""""""
W4DT SMIT: CL'RF WT31 2W"D'C"G3 C33 13
$) CONC(T
This will %e used to !o$%ine two stings onl75
S7nta/0 !on!at (strin1, strin2)
'/0
SQL- sele!t !on!at(=!o$pute=#= opeato=) fo$ dual.
CONC(T(=COMAUT'R=
"""""""""""""""""""""""""
!o$pute opeato
If 7ou want to !o$%ine $oe than two stings 7ou have to use !on!atenation opeato (XX)5
39
SQL- sele!t =how= XX = ae= XX = 7ou= fo$ dual.
=:O6=XX=(R'
"""""""""""""""
how ae 7ou
n) (SCII
This will etun the de!i$al epesentation in the data%ase !haa!te set of the fist
!haa!te of the sting5
S7nta/0 as!ii (strin)
'/0
SQL- sele!t as!ii(=a=)# as!ii(=apple=) fo$ dual.
(SCII(=(=) (SCII(=(AAL'=)
"""""""""""" """"""""""""""""""
TW TW
o) C:R
This will etun the !haa!te having the %ina7 eVuivalent to the sting in eithe the
data%ase !haa!te set o the national !haa!te set5
S7nta/0 !h (number)
'/0
SQL- sele!t !h(TW) fo$ dual.
C:R
"""""
a
p) SU)STR
This will %e used to e/ta!t su%stings5
S7nta/0 su%st (strin, start_c'r_count (, no_o%_c'ars))
'/0
SQL- sele!t su%st(=!o$pute=#1)# su%st(=!o$pute=#1#C)# su%st(=!o$pute=#4#W) fo$
dual.
SU)STR( SU)ST SU)STR
"""""""""" """"""" """"""""
o$pute o$put $pute
40
If no_o%_c'ars paa$ete is negative then it will displa7 nothing5
If %oth paa$etes e/!ept strin ae null o Ueos then it will displa7 nothing5
If no_o%_c'ars paa$ete is geate than the length of the sting then it ignoes and !al!ulates %ased on
the oginal sting length5
If start_c'r_count is negative then it will e/ta!t the su%sting fo$ ight end5
2 1 4 @ C D W G
C O M A U T ' R
"G "W "D "C "@ "4 "1 "2
V) INSTR
This will allows 7ou fo sea!hing though a sting fo set of !haa!tes5
S7nta/0 inst (strin, searc'_str (, start_c'r_count (, occurrence) ))
'/0
SQL- sele!t inst(=info$ation=#=o=#@#2)# inst(=info$ation=#=o=#@#1) fo$ dual.
INSTR(=INBORM(TION=#=O=#@#2) INSTR(=INBORM(TION=#=O=#@#1)
"""""""""""""""""""""""""""""""""""" """""""""""""""""""""""""""""""""""""
@ 23
If 7ou ae not spe!if7ing start_c'r_count and occurrence then it will stat sea!h fo$
the %eginning and finds fist o!!uen!e onl75
If %oth paa$etes start_c'r_count and occurrence ae null# it will displa7 nothing5
) D'COD'
De!ode will a!t as value %7 value su%stitution5
Bo eve7 value of field# it will !he!&s fo a $at!h in a seies of if?then tests5
S7nta/0 de!ode (value, i%1, t'en1, i%2, t'en2, . else).
'/0
SQL- sele!t sal# de!ode(sal#C33#=Low=#C333#=:igh=#=Mediu$=) fo$ e$p.
S(L D'COD'
""""" """""""""
C33 Low
1C33 Mediu$
1333 Mediu$
41
4C33 Mediu$
4333 Mediu$
C333 :igh
@333 Mediu$
C333 :igh
2G33 Mediu$
2133 Mediu$
1333 Mediu$
1W33 Mediu$
1133 Mediu$
4133 Mediu$
2@ ows sele!ted5
SQL- sele!t de!ode(2#2#4)# de!ode(2#1#4#@#@#D) fo$ dual.
D'COD'(2#2#4) D'COD'(2#1#4#@#@#D)
""""""""""""""""" """"""""""""""""""""""""
4 D
If the nu$%e of paa$etes ae odd and diffeent then de!ode will displa7 nothing5
If the nu$%e of paa$etes ae even and diffeent then de!ode will displa7 last
value5
If all the paa$etes ae null then de!ode will displa7 nothing5
If all the paa$etes ae Ueos then de!ode will displa7 Ueo5
s) 8R'(T'ST
This will give the geatest sting5
S7nta/0 geatest (strn1, strin2, strin3 strinn)
'/0
SQL- sele!t geatest(=a=# =%=# =!=)# geatest(=satish=#=sinu=#=sað=) fo$ dual.
8R'(T 8R'(T
""""""" """""""
! sinu
If all the paa$etes ae nulls then it will displa7 nothing5
If an7 of the paa$etes is null it will displa7 nothing5
42
t) L'(ST
This will give the least sting5
S7nta/0 geatest (strn1, strin2, strin3 strinn)
'/0
SQL- sele!t least(=a=# =%=# =!=)# least(=satish=#=sinu=#=sað=) fo$ dual.
L'(ST L'(ST
""""""" """""""
a sað
If all the paa$etes ae nulls then it will displa7 nothing5
If an7 of the paa$etes is null it will displa7 nothing5
u) CO(L'SC'
This will gives the fist non"null sting5
S7nta/0 !oales!e (strn1, strin2, strin3 strinn)
'/0
SQL- sele!t !oales!e(=a=#=%=#=!=)# !oales!e(null#=a=#null#=%=) fo$ dual.
CO(L'SC' CO(L'SC'
""""""""""" """""""""""
a a
D(T' BUNCTIONS
S7sdate
CuentNdate
CuentNti$esta$p
S7sti$esta$p
Lo!alti$esta$p
D%ti$eUone
43
Sessionti$eUone
ToN!ha
ToNdate
(ddN$onths
MonthsN%etween
Ne/tNda7
LastNda7
'/ta!t
8eatest
Least
Round
Tun!
NewNti$e
Coales!e
Oa!le default date fo$at is DD"MON"**5
6e !an !hange the default fo$at to ou desied fo$at %7 using the following !o$$and5
SQL- alte session set nlsNdateNfo$at I LDD"MONT:"****;.
)ut this will e/pie on!e the session was !losed5
a) S*SD(T'
This will give the !uent date and ti$e5
'/0
SQL- sele!t s7sdate fo$ dual.
S*SD(T'
"""""""""""
1@"D'C"3D
%) CURR'NTND(T'
This will etuns the !uent date in the session;s ti$eUone5
'/0
SQL- sele!t !uentNdate fo$ dual.
CURR'NTND(T'
""""""""""""""""""
1@"D'C"3D
44
!) CURR'NTNTIM'ST(MA
This will etuns the !uent ti$esta$p with the a!tive ti$e Uone info$ation5
'/0
SQL- sele!t !uentNti$esta$p fo$ dual.
CURR'NTNTIM'ST(MA
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1@"D'C"3D 345@15@254G44DT (M H3C043
d) S*STIM'ST(MA
This will etuns the s7ste$ date# in!luding fa!tional se!onds and ti$e Uone of the
data%ase5
'/0
SQL- sele!t s7sti$esta$p fo$ dual.
S*STIM'ST(MA
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1@"D'C"3D 345@T5425G433TT (M H3C043
e) LOC(LTIM'ST(MA
This will etuns lo!al ti$esta$p in the a!tive ti$e Uone info$ation# with no ti$e Uone
info$ation shown5
'/0
SQL- sele!t lo!alti$esta$p fo$ dual.
LOC(LTIM'ST(MA
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1@"D'C"3D 345@@52G5C31GW@ (M
f) D)TIM'ZON'
This will etuns the !uent data%ase ti$e Uone in UTC fo$at5 (Coodinated Univesal Ti$e)
'/0
SQL- sele!t d%ti$eUone fo$ dual.
D)TIM'ZON'
"""""""""""""""
"3W033
g) S'SSIONTIM'ZON'
This will etuns the value of the !uent session;s ti$e Uone5
'/0
SQL- sele!t sessionti$eUone fo$ dual.
45
S'SSIONTIM'ZON'
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
H3C043
h) TONC:(R
This will %e used to e/ta!t vaious data fo$ats5
The availa%le date fo$ats as follows5
S7nta/0 toN!ha (date# %ormat)
D(T' BORM(TS
D "" No of da7s in wee&
DD "" No of da7s in $onth
DDD "" No of da7s in 7ea
MM "" No of $onth
MON "" Thee lette a%%eviation of $onth
MONT: "" Bull7 spelled out $onth
RM "" Ro$an nu$eal $onth
D* "" Thee lette a%%eviated da7
D(* "" Bull7 spelled out da7
* "" Last one digit of the 7ea
** "" Last two digits of the 7ea
*** "" Last thee digits of the 7ea
**** "" Bull fou digit 7ea
S**** "" Signed 7ea
I "" One digit 7ea fo$ ISO standad
I* "" Two digit 7ea fo$ ISO standad
I** "" Thee digit 7ea fo$ ISO standad
I*** "" Bou digit 7ea fo$ ISO standad
*# *** "" *ea with !o$$a
*'(R "" Bull7 spelled out 7ea
CC "" Centu7
Q "" No of Vuates
6 "" No of wee&s in $onth
66 "" No of wee&s in 7ea
I6 "" No of wee&s in 7ea fo$ ISO standad
:: "" :ous
46
MI "" Minutes
SS "" Se!onds
BB "" Ba!tional se!onds
(M o AM "" Displa7s (M o AM depending upon ti$e of da7
(5M o A5M "" Displa7s (5M o A5M depending upon ti$e of da7
(D o )C "" Displa7s (D o )C depending upon the date
(5D o )5C "" Displa7s (D o )C depending upon the date
BM "" Aefi/ to $onth o da7# suppesses padding of $onth o da7
T: "" Suffi/ to a nu$%e
SA "" suffi/ to a nu$%e to %e spelled out
SAT: "" Suffi/ !o$%ination of T: and SA to %e %oth spelled out
T:SA "" sa$e as SAT:
'/0
SQL- sele!t toN!ha(s7sdate#=dd $onth 7777 hh0$i0ss a$ d7=) fo$ dual.
TONC:(R(S*SD(T'#=DD MONT: ****::0MI
""""""""""""""""""""""""""""""""""""""""""""""""""""
1@ de!e$%e 133D 31034014 p$ sun
SQL- sele!t toN!ha(s7sdate#=dd $onth 7ea=) fo$ dual.
TONC:(R(S*SD(T'#=DDMONT:*'(R=)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""
1@ de!e$%e two thousand si/
SQL- sele!t toN!ha(s7sdate#=dd f$$onth 7ea=) fo$ dual.
TONC:(R(S*SD(T'#=DD BMMONT: *'(R=)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""
1@ de!e$%e two thousand si/
SQL- sele!t toN!ha(s7sdate#=ddth DDT:=) fo$ dual.
TONC:(R(S
""""""""""""
1@th 1@
T:
47
SQL- sele!t toN!ha(s7sdate#=ddspth DDSAT:=) fo$ dual.
TONC:(R(S*SD(T'#=DDSAT:DDSAT:
""""""""""""""""""""""""""""""""""""""""""
twent7"fouth T6'NT*"BOURT:
SQL- sele!t toN!ha(s7sdate#=ddsp Ddsp DDSA =) fo$ dual.
TONC:(R(S*SD(T'#=DDSADDSADDSA=)
""""""""""""""""""""""""""""""""""""""""""""""""
twent7"fou Twent7"Bou T6'NT*"BOUR
i) TOND(T'
This will %e used to !onvet the sting into data fo$at5
S7nta/0 toNdate (date)
'/0
SQL- sele!t toN!ha(toNdate(=1@?de!?133D=#=dd?$on?7777=)# =dd E $onth E da7=) fo$
dual.
TONC:(R(TOND(T'(=1@?D'C?13
""""""""""""""""""""""""""
1@ E de!e$%e E Sunda7
"" If 7ou ae not using toN!ha oa!le will displa7 output in default date fo$at5
K) (DDNMONT:S
This will add the spe!ified $onths to the given date5
S7nta/0 addN$onths (date, no_o%_mont's)
'/0
SQL- sele!t addN$onths(toNdate(=22"Kan"2TT3=#=dd"$on"7777=)# C) fo$ dual.
(DDNMONT:S
""""""""""""""""
22">UN"T3
SQL- sele!t addN$onths(toNdate(=22"Kan"2TT3=#=dd"$on"7777=)# "C) fo$ dual.
48
(DDNMONT:
"""""""""""""""
22"(U8"GT
If no_o%_mont's is Ueo then it will displa7 the sa$e date5
If no_o%_mont's is null then it will displa7 nothing5
&) MONT:SN)'T6''N
This will give diffeen!e of $onths %etween two dates5
S7nta/0 $onthsN%etween (date1, date2)
'/0
SQL- sele!t $onthsN%etween(toNdate(=22"aug"2TT3=#=dd"$on"7777=)# toNdate(=22"Kan"
2TT3=#=dd"$on"7777=)) fo$ dual.
MONT:SN)'T6''N(TOND(T'(=22"(U8"2TT3=#=DD"MON"****=)#TOND(T'(=22">(N"2TT3=#=DD"MON"****=))
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
W
SQL- sele!t $onthsN%etween(toNdate(=22"Kan"2TT3=#=dd"$on"7777=)# toNdate(=22"aug"
2TT3=#=dd"$on"7777=)) fo$ dual.
MONT:SN)'T6''N(TOND(T'(=22">(N"2TT3=#=DD"MON"****=)#TOND(T'(=22"(U8"2TT3=#=DD"MON"****=))
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"W
l) N'+TND(*
This will podu!e ne/t da7 of the given da7 fo$ the spe!ified date5
S7nta/0 ne/tNda7 (date, day)
'/0
SQL- sele!t ne/tNda7(toNdate(=1@"de!"133D=#=dd"$on"7777=)#=sun=) fo$ dual.
N'+TND(*(
"""""""""""""
42"D'C"3D
"" If the da7 paa$ete is null then it will displa7 nothing5
$) L(STND(*
49
This will podu!e last da7 of the given date5
S7nta/0 lastNda7 (date)
'/0
SQL- sele!t lastNda7(toNdate(=1@"de!"133D=#=dd"$on"7777=)#=sun=) fo$ dual.
L(STND(*(
"""""""""""""
42"D'C"3D
n) '+TR(CT
This is used to e/ta!t a potion of the date value5
S7nta/0 e/ta!t ((7ea X $onth X da7 X hou X $inute X se!ond)# date)
'/0
SQL- sele!t e/ta!t(7ea fo$ s7sdate) fo$ dual.
'+TR(CT(*'(RBROMS*SD(T')
""""""""""""""""""""""""""""""""""""
133D
"" *ou !an e/ta!t onl7 one value at a ti$e5
o) 8R'(T'ST
This will give the geatest date5
S7nta/0 geatest (date1, date2, date3 daten)
'/0
SQL- sele!t geatest(toNdate(=22"Kan"T3=#=dd"$on"77=)#toNdate(=22"$a"T3=#=dd"$on"
77=)#toNdate(=22"ap"T3=#=dd"$on"77=)) fo$ dual.
8R'(T'ST(
"""""""""""""
22"(AR"T3
p) L'(ST
This will give the least date5
S7nta/0 least (date1, date2, date3 daten)
'/0
SQL- sele!t least(toNdate(=22"Kan"T3=#=dd"$on"77=)#toNdate(=22"$a"T3=#=dd"$on"
77=)#toNdate(=22"ap"T3=#=dd"$on"77=)) fo$ dual.
50
L'(ST(
"""""""""""""
22">(N"T3
V) ROUND
Round will ounds the date to whi!h it was eVual to o geate than the given date5
S7nta/0 ound (date, (da7 X $onth X 7ea*)
If the se!ond paa$ete was year then ound will !he!&s the $onth of the given date in the
following anges5
>(N "" >UN
>UL "" D'C
If the $onth falls %etween >(N and >UN then it etuns the fist da7 of the !uent 7ea5
If the $onth falls %etween >UL and D'C then it etuns the fist da7 of the ne/t 7ea5
If the se!ond paa$ete was mont' then ound will !he!&s the da7 of the given date in the
following anges5
2 "" 2C
2D "" 42
If the da7 falls %etween 2 and 2C then it etuns the fist da7 of the !uent $onth5
If the da7 falls %etween 2D and 42 then it etuns the fist da7 of the ne/t $onth5
If the se!ond paa$ete was day then ound will !he!&s the wee& da7 of the given date in
the following anges5
SUN "" 6'D
T:U "" SUN
If the wee& da7 falls %etween SUN and 6'D then it etuns the pevious sunda75
If the wee&da7 falls %etween T:U and SUN then it etuns the ne/t sunda75
If the se!ond paa$ete was null then it etuns nothing5
If the 7ou ae not spe!if7ing the se!ond paa$ete then ound will esets the ti$e to the
51
%egining of the !uent da7 in !ase of use spe!ified date5
If the 7ou ae not spe!if7ing the se!ond paa$ete then ound will esets the ti$e to the
%egining of the ne/t da7 in !ase of s7sdate5
'/0
SQL- sele!t ound(toNdate(=1@"de!"3@=#=dd"$on"77=)#=7ea=)# ound(toNdate(=22"$a"
3D=#=dd"$on"77=)#=7ea=) fo$ dual.
ROUND(TON ROUND(TON
"""""""""""" """""""""""""""
32">(N"3C 32">(N"3D
SQL- sele!t ound(toNdate(=22"Kan"3@=#=dd"$on"77=)#=$onth=)# ound(toNdate(=2G"Kan"
3@=#=dd"$on"77=)#=$onth=) fo$ dual.
ROUND(TON ROUND(TON
""""""""""""" """""""""""""""
32">(N"3@ 32"B')"3@
SQL- sele!t ound(toNdate(=1D"de!"3D=#=dd"$on"77=)#=da7=)# ound(toNdate(=1T"de!"
3D=#=dd"$on"77=)#=da7=) fo$ dual.
ROUND(TON ROUND(TON
"""""""""""""" """"""""""""""
1@"D'C"3D 42"D'C"3D
SQL- sele!t toN!ha(ound(toNdate(=1@"de!"3D=#=dd"$on"77=))# =dd $on 7777 hh0$i0ss a$=)
fo$ dual.
TONC:(R(ROUND(TOND(T'(=
"""""""""""""""""""""""""""""""""
1@ de! 133D 21033033 a$
) TRUNC
Tun! will !hops off the date to whi!h it was eVual to o less than the given date5
S7nta/0 tun! (date, (da7 X $onth X 7ea*)
If the se!ond paa$ete was year then it alwa7s etuns the fist da7 of the !uent 7ea5
52
If the se!ond paa$ete was mont' then it alwa7s etuns the fist da7 of the !uent $onth5
If the se!ond paa$ete was day then it alwa7s etuns the pevious sunda75
If the se!ond paa$ete was null then it etuns nothing5
If the 7ou ae not spe!if7ing the se!ond paa$ete then tun& will esets the ti$e to the
%egining of the !uent da75
'/0
SQL- sele!t tun!(toNdate(=1@"de!"3@=#=dd"$on"77=)#=7ea=)# tun!(toNdate(=22"$a"
3D=#=dd"$on"77=)#=7ea=) fo$ dual.
TRUNC(TON TRUNC(TON
""""""""""""" """"""""""""""
32">(N"3@ 32">(N"3D
SQL- sele!t tun!(toNdate(=22"Kan"3@=#=dd"$on"77=)#=$onth=)# tun!(toNdate(=2G"Kan"
3@=#=dd"$on"77=)#=$onth=) fo$ dual.
TRUNC(TON TRUNC(TON
""""""""""""" """""""""""""
32">(N"3@ 32">(N"3@
SQL- sele!t tun!(toNdate(=1D"de!"3D=#=dd"$on"77=)#=da7=)# tun!(toNdate(=1T"de!"3D=#=dd"
$on"77=)#=da7=) fo$ dual.
TRUNC(TON TRUNC(TON
""""""""""""" """"""""""""""
1@"D'C"3D 1@"D'C"3D
SQL- sele!t toN!ha(tun!(toNdate(=1@"de!"3D=#=dd"$on"77=))# =dd $on 7777 hh0$i0ss a$=)
fo$ dual.
TONC:(R(TRUNC(TOND(T'(=
"""""""""""""""""""""""""""""""""
1@ de! 133D 21033033 a$
s) N'6NTIM'
This will give the desied ti$eUone;s date and ti$e5
S7nta/0 newNti$e (date, current_time+one, desired_time+one)
53
(vaila%le ti$eUones ae as follows5
TIM'ZON'S
(ST?(DT "" (tlanti! standad?da7 light ti$e
)ST?)DT "" )eing standad?da7 light ti$e
CST?CDT"" Cental standad?da7 light ti$e
'ST?'DT "" 'asten standad?da7 light ti$e
8MT "" 8eenwi!h $ean ti$e
:ST?:DT"" (las&a":awaii standad?da7 light ti$e
MST?MDT "" Mountain standad?da7 light ti$e
NST "" Newfoundland standad ti$e
AST?ADT "" Aa!ifi! standad?da7 light ti$e
*ST?*DT "" *u&on standad?da7 light ti$e
'/0
SQL- sele!t toN!ha(newNti$e(s7sdate#=g$t=#=7st=)#=dd $on 7777 hh0$i0ss a$=) fo$ dual.
TONC:(R(N'6NTIM'(S*SD(T
"""""""""""""""""""""""""""""""""""
1@ de! 133D 310C2013 p$
SQL- sele!t toN!ha(newNti$e(s7sdate#=g$t=#=est=)#=dd $on 7777 hh0$i0ss a$=) fo$ dual.
TONC:(R(N'6NTIM'(S*SD(T
"""""""""""""""""""""""
1@ de! 133D 3D0C201D p$
t) CO(L'SC'
This will give the fist non"null date5
S7nta/0 !oales!e (date1, date2, date3 daten)
'/0
SQL- sele!t !oales!e(=21"Kan"T3=#=24"Kan"TT=)# !oales!e(null#=21"Kan"T3=#=14"$a"TG=#null)
fo$ dual.
CO(L'SC'( CO(L'SC'(
""""""""""""" """"""""""""
21"Kan"T3 21"Kan"T3
54
MISC'LL(N'OUS BUNCTIONS
Uid
Use
9siUe
a) UID
This will etuns the intege value !oesponding to the use !uentl7 logged in5
'/0
SQL- sele!t uid fo$ dual.
UID
""""""""""
42T
%) US'R
This will etuns the login;s use na$e5
'/0
SQL- sele!t use fo$ dual.
US'R
""""""""""""""""
S(F'T:
!) 9SIZ'
This will etuns the nu$%e of %7tes in the e/pession5
'/0
SQL- sele!t vsiUe(214)# vsiUe(=!o$pute=)# vsiUe(=21"Kan"T3=) fo$ dual.
9SIZ'(214) 9SIZ'(=COMAUT'R=) 9SIZ'(=21">(N"T3=)
""""""""""""" """"""""""""""""""""""" """"""""""""""""""""""
4 G T
55
CON9'RSION BUNCTIONS
)inNtoNnu$
Chatoowid
Rowidto!ha
ToNnu$%e
ToN!ha
ToNdate
a) )INNTONNUM
This will !onvet the %ina7 value to its nu$ei!al eVuivalent5
S7nta/0 %inNtoNnu$( binary_bits)
'/0
SQL- sele!t %inNtoNnu$(2#2#3) fo$ dual.
)INNTONNUM(2#2#3)
""""""""""""""""""""""""
D
If all the %its ae Ueo then it podu!es Ueo5
If all the %its ae null then it podu!es an eo5
%) C:(RTORO6ID
This will !onvet a !haa!te sting to a!t li&e an intenal oa!le ow identifie o owid5
!) RO6IDTOC:(R
This will !onvet an intenal oa!le ow identifie o owid to !haa!te sting5
d) TONNUM)'R
This will !onvet a !ha o va!ha to nu$%e5
e) TONC:(R
This will !onvet a nu$%e o date to !haa!te sting5
56
f) TOND(T'
This will !onvet a nu$%e# !ha o va!ha to a date5
8ROUA BUNCTIONS
Su$
(vg
Ma/
Min
Count
8oup fun!tions will %e applied on all the ows %ut podu!es single output5
a) SUM
This will give the su$ of the values of the spe!ified !olu$n5
S7nta/0 su$ (column)
'/0
SQL- sele!t su$(sal) fo$ e$p.
SUM(S(L)
""""""""""
4GD33
%) (98
This will give the aveage of the values of the spe!ified !olu$n5
S7nta/0 avg (column)
'/0
SQL- sele!t avg(sal) fo$ e$p.
(98(S(L)
"""""""""""""""
1WCW52@1GD
!) M(+
This will give the $a/i$u$ of the values of the spe!ified !olu$n5
S7nta/0 $a/ (column)
'/0
57
SQL- sele!t $a/(sal) fo$ e$p.
M(+(S(L)
""""""""""
C333
d) MIN
This will give the $ini$u$ of the values of the spe!ified !olu$n5
S7nta/0 $in (column)
'/0
SQL- sele!t $in(sal) fo$ e$p.
MIN(S(L)
""""""""""
C33
e) COUNT
This will give the !ount of the values of the spe!ified !olu$n5
S7nta/0 !ount (column)
'/0
SQL- sele!t !ount(sal)#!ount(E) fo$ e$p.
COUNT(S(L) COUNT(E)
"""""""""""""" """"""""""""
2@ 2@
58
CONSTR(INTS
Constaints ae !ategoiUed as follows5
Do$ain integit7 !onstaints
Not null
Che!&
'ntit7 integit7 !onstaints
UniVue
Ai$a7 &e7
Refeential integit7 !onstaints
Boeign &e7
Constaints ae alwa7s atta!hed to a !olu$n not a ta%le5
6e !an add !onstaints in thee wa7s5
Colu$n level "" along with the !olu$n definition
Ta%le level "" afte the ta%le definition
(lte level "" using alte !o$$and
6hile adding !onstaints 7ou need not spe!if7 the na$e %ut the t7pe onl7# oa!le will intenall7 na$e the !onstaint5
If 7ou want to give a na$e to the !onstaint# 7ou have to use the !onstaint !lause5
NOT NULL
This is used to avoid null values5
6e !an add this !onstaint in !olu$n level onl75
'/0
SQL- !eate ta%le student(no nu$%e(1) not null# na$e va!ha(23)# $a&s nu$%e(4)).
SQL- !eate ta%le student(no nu$%e(1) !onstaint nn not null# na$e va!ha(23)# $a&s
nu$%e(4)).
C:'CF
59
This is used to inset the values %ased on spe!ified !ondition5
6e !an add this !onstaint in all thee levels5
'/0
COLUMN L'9'L
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4) !he!&
($a&s - 433)).
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4) !onstaint !h
!he!&($a&s - 433)).
T()L' L'9'L
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)# !he!&
($a&s - 433)).
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)# !onstaint
!h !he!&($a&s - 433)).
(LT'R L'9'L
SQL- alte ta%le student add !he!&($a&s-433).
SQL- alte ta%le student add !onstaint !h !he!&($a&s-433).
UNIQU'
This is used to avoid dupli!ates %ut it allow nulls5
6e !an add this !onstaint in all thee levels5
'/0
COLUMN L'9'L
SQL- !eate ta%le student(no nu$%e(1) uniVue# na$e va!ha(23)# $a&s nu$%e(4)).
SQL- !eate ta%le student(no nu$%e(1) !onstaint un uniVue# na$e va!ha(23)# $a&s
nu$%e(4)).
T()L' L'9'L
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
uniVue(no)).
60
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)# !onstaint
un uniVue(no)).
(LT'R L'9'L
SQL- alte ta%le student add uniVue(no).
SQL- alte ta%le student add !onstaint un uniVue(no).
ARIM(R* F'*
This is used to avoid dupli!ates and nulls5 This will wo& as !o$%ination of uniVue and not null5
Ai$a7 &e7 alwa7s atta!hed to the paent ta%le5
6e !an add this !onstaint in all thee levels5
'/0
COLUMN L'9'L
SQL- !eate ta%le student(no nu$%e(1) pi$a7 &e7# na$e va!ha(23)# $a&s nu$%e(4)).
SQL- !eate ta%le student(no nu$%e(1) !onstaint p& pi$a7 &e7# na$e va!ha(23)#
$a&s nu$%e(4)).
T()L' L'9'L
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
pi$a7 &e7(no)).
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)# !onstaint
p& pi$a7 &e7(no)).
(LT'R L'9'L
SQL- alte ta%le student add pi$a7 &e7(no).
SQL- alte ta%le student add !onstaint p& pi$a7 &e7(no).
BOR'I8N F'*
This is used to efeen!e the paent ta%le pi$a7 &e7 !olu$n whi!h allows dupli!ates5
Boeign &e7 alwa7s atta!hed to the !hild ta%le5
6e !an add this !onstaint in ta%le and alte levels onl75
'/0
61
T()L' L'9'L
SQL- !eate ta%le e$p(e$pno nu$%e(1)# ena$e va!ha(23)# deptno nu$%e(1)#
pi$a7 &e7(e$pno)# foeign &e7(deptno) efeen!es dept(deptno)).
SQL- !eate ta%le e$p(e$pno nu$%e(1)# ena$e va!ha(23)# deptno nu$%e(1)#
!onstaint p& pi$a7 &e7(e$pno)# !onstaint f& foeign &e7(deptno) efeen!es
dept(deptno)).
(LT'R L'9'L
SQL- alte ta%le e$p add foeign &e7(deptno) efeen!es dept(deptno).
SQL- alte ta%le e$p add !onstaint f& foeign &e7(deptno) efeen!es dept(deptno).
On!e the pi$a7 &e7 and foeign &e7 elationship has %een !eated then 7ou !an not e$ove an7 paent e!od if the
dependent !hilds e/ists5
USIN8 ON D'LT' C(SC(D'
)7 using this !lause 7ou !an e$ove the paent e!od even it !hilds e/ists5
)e!ause when eve 7ou e$ove paent e!od oa!le auto$ati!all7 e$oves all its dependent e!ods fo$ !hild ta%le# if
this !lause is pesent while !eating foeign &e7 !onstaint5
'/0
T()L' L'9'L
SQL- !eate ta%le e$p(e$pno nu$%e(1)# ena$e va!ha(23)# deptno nu$%e(1)#
pi$a7 &e7(e$pno)# foeign &e7(deptno) efeen!es dept(deptno) on delete !as!ade).
SQL- !eate ta%le e$p(e$pno nu$%e(1)# ena$e va!ha(23)# deptno nu$%e(1)#
!onstaint p& pi$a7 &e7(e$pno)# !onstaint f& foeign &e7(deptno) efeen!es
dept(deptno) on delete !as!ade).
(LT'R L'9'L
SQL- alte ta%le e$p add foeign &e7(deptno) efeen!es dept(deptno) on delete !as!ade.
SQL- alte ta%le e$p add !onstaint f& foeign &e7(deptno) efeen!es dept(deptno) on
delete !as!ade.
COMAOSIT' F'*S
62
( !o$posite &e7 !an %e defined on a !o$%ination of !olu$ns5
6e !an define !o$posite &e7s on entit7 integit7 and efeential integit7 !onstaints5
Co$posite &e7 !an %e defined in ta%le and alte levels onl75
'/0
UNIQU' (T()L' L'9'L)
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
uniVue(no#na$e)).
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)# !onstaint
un uniVue(no#na$e)).
UNIQU' ((LT'R L'9'L)
SQL- alte ta%le student add uniVue(no#na$e).
SQL- alte ta%le student add !onstaint un uniVue(no#na$e).
ARIM(R* F'* (T()L' L'9'L)
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
pi$a7 &e7(no#na$e)).
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)# !onstaint
p& pi$a7 &e7(no#na$e)).
ARIM(R* F'* ((LT'R L'9'L)
SQL- alte ta%le student add pi$a7 &e7(no#an$e).
SQL- alte ta%le student add !onstaint p& pi$a7 &e7(no#na$e).
BOR'I8N F'* (T()L' L'9'L)
SQL- !eate ta%le e$p(e$pno nu$%e(1)# ena$e va!ha(23)# deptno nu$%e(1)# dna$e
va!ha(23)# pi$a7 &e7(e$pno)# foeign &e7(deptno#dna$e) efeen!es
dept(deptno#dna$e)).
SQL- !eate ta%le e$p(e$pno nu$%e(1)# ena$e va!ha(23)# deptno nu$%e(1)# dna$e
va!ha(23)# !onstaint p& pi$a7 &e7(e$pno)# !onstaint f& foeign
&e7(deptno#dna$e) efeen!es dept(deptno#dna$e)).
BOR'I8N F'* ((LT'R L'9'L)
63
SQL- alte ta%le e$p add foeign &e7(deptno#dna$e) efeen!es dept(deptno#dna$e).
SQL- alte ta%le e$p add !onstaint f& foeign &e7(deptno#dna$e) efeen!es
dept(deptno#dna$e).
D'B'RR()L' CONSTR(INTS
'a!h !onstaint has two additional atti%utes to suppot defeed !he!&ing of !onstaints5
Defeed initiall7 i$$ediate5
Defeed initiall7 defeed5
Defeed initiall7 i$$ediate !he!&s fo !onstaint violation at the ti$e of inset5
Defeed initiall7 defeed !he!&s fo !onstaint violation at the ti$e of !o$$it5
'/0
SQL- !eate ta%le student(no nu$%e(1)# na$e va!ha(23)# $a&s nu$%e(4)# !onstaint
un uniVue(no) defeed initiall7 i$$ediate).
SQL- !eate ta%le student(no nu$%e(1)# na$e va!ha(23)# $a&s nu$%e(4)# !onstaint
un uniVue(no) defeed initiall7 defeed).
SQL- alte ta%le student add !onstaint un uniVue(no) defea%le initiall7 defeed.
SQL- set !onstaints all i$$ediate.
This will ena%le all the !onstaints violations at the ti$e of inseting5
SQL- set !onstaints all defeed.
This will ena%le all the !onstaints violations at the ti$e of !o$$it5
OA'R(TIONS 6IT: CONSTR(INTS
Aossi%le opeations with !onstaints as follows5
'na%le
Disa%le
'nfo!e
Dop
64
'N()L'
This will ena%le the !onstaint5 )efoe ena%le# the !onstaint will !he!& the e/isting data5
'/0
SQL- alte ta%le student ena%le !onstaint un.
DIS()L'
This will disa%le the !onstaint5
'/0
SQL- alte ta%le student ena%le !onstaint un.
'NBORC'
This will enfo!e the !onstaint athe than ena%le fo futue insets o updates5
This will not !he!& fo e/isting data while enfo!ing data5
'/0
SQL- alte ta%le student enfo!e !onstaint un.
DROA
This will e$ove the !onstaint5
'/0
SQL- alte ta%le student dop !onstaint un.
On!e the ta%le is dopped# !onstaints auto$ati!all7 will dop5
C(S' (ND D'B(ULT
C(S'
Case is si$ila to de!ode %ut easie to undestand while going though !oding
'/0
SQL- Sele!t sal#
Case sal
6hen C33 then Llow;
6hen C333 then Lhigh;
'lse L$ediu$;
'nd !ase
65
Bo$ e$p.
S(L C(S'
""""" """"""""
C33 low
1C33 $ediu$
1333 $ediu$
4C33 $ediu$
4333 $ediu$
C333 high
@333 $ediu$
C333 high
2G33 $ediu$
2133 $ediu$
1333 $ediu$
1W33 $ediu$
1133 $ediu$
4133 $ediu$
2@ ows sele!ted5
D'B(ULT
,e%ault !an %e !onsideed as a su%stitute %ehavio of not null !onstaint when applied to new ows %eing enteed into the
ta%le5
6hen 7ou define a !olu$n with the de%ault &e7wod followed %7 a value# 7ou ae a!tuall7 telling the data%ase that# on
inset if a ow was not assigned a value fo this !olu$n# use the default value that 7ou have spe!ified5
Default is applied onl7 duing insetion of new ows5
'/0
SQL- !eate ta%le student(no nu$%e(1) default 22#na$e va!ha(1)).
SQL- inset into student values(2#=a=).
SQL- inset into student(na$e) values(=%=).
SQL- sele!t E fo$ student.
NO N(M'
66
"""""" """""""""
2 a
22 %
SQL- inset into student values(null# L!;).
SQL- sele!t E fo$ student.
NO N(M'
"""""" """""""""
2 a
22 %
C
"" Default !an not oveide nulls5
()STR(CT D(T( T*A'S
So$e ti$es 7ou $a7 want t7pe whi!h holds all t7pes of data in!luding nu$%es# !has and spe!ial !haa!tes so$ething
li&e this5 *ou !an not a!hieve this using pe"defined t7pes5
*ou !an define !usto$ t7pes whi!h holds 7ou desied data5
'/0
Suppose in a ta%le we have addess !olu$n whi!h holds hno and !it7 info$ation5
6e will define a !usto$ t7pe whi!h holds %oth nu$ei! as well as !ha data5
CR'(TIN8 (DT
SQL- !eate t7pe add as o%Ke!t(hno nu$%e(4)#!it7 va!ha(23)). ?
CR'(TIN8 T()L' )(S'D ON (DT
SQL- !eate ta%le student(no nu$%e(1)#na$e va!ha(1)#addess add).
INS'RTIN8 D(T( INTO (DT T()L'S
67
SQL- inset into student values(2#=a=#add(222#=h7d=)).
SQL- inset into student values(1#=%=#add(111#=%ang=)).
SQL- inset into student values(4#=!=#add(444#=delhi=)).
S'L'CTIN8 D(T( BROM (DT T()L'S
SQL- sele!t E fo$ student.
NO N(M' (DDR'SS(:NO# CIT*)
""" """"""" """""""""""""""""""""""""
2 a (DDR(222# =h7d=)
1 % (DDR(111# =%ang=)
4 ! (DDR(444# =delhi=)
SQL- sele!t no#na$e#s5addess5hno#s5addess5!it7 fo$ student s.
NO N(M' (DDR'SS5:NO (DDR'SS5CIT*
"""" """"""" """"""""""""""""" """"""""""""""""
2 a 222 h7d
1 % 111 %ang
4 ! 444 delhi
UAD(T' 6IT: (DT T()L'S
SQL- update student s set s5addess5!it7 I =%o$%a7= whee s5addess5hno I 444.
SQL- sele!t no#na$e#s5addess5hno#s5addess5!it7 fo$ student s.
NO N(M' (DDR'SS5:NO (DDR'SS5CIT*
"""" """"""" """"""""""""""""" """"""""""""""""
2 a 222 h7d
1 % 111 %ang
4 ! 444 %o$%a7
D'L'T' 6IT: (DT T()L'S
SQL- delete student s whee s5addess5hno I 222.
SQL- sele!t no#na$e#s5addess5hno#s5addess5!it7 fo$ student s.
68
NO N(M' (DDR'SS5:NO (DDR'SS5CIT*
"""" """"""" """"""""""""""""" """"""""""""""""
1 % 111 %ang
4 ! 444 %o$%a7
DROAAIN8 (DT
SQL- dop t7pe add.
O)>'CT 9I'6S (ND M'T:ODS
O)>'CT 9I'6S
If 7ou want to i$ple$ent o%Ke!ts with the e/isting ta%le# o%Ke!t views !o$e into pi!tue5
*ou define the o%Ke!t and !eate a view whi!h elates this o%Ke!t to the e/isting ta%le nothing %ut ob!ect view5
O%Ke!t views ae used to elate the use defined o%Ke!ts to the e/isting ta%le5
'/0
2) (ssu$e that the ta%le student has alead7 %een !eated with the following !olu$ns
SQL- !eate ta%le student(no nu$%e(1)#na$e va!ha(23)#hno nu$%e(4)#!it7
va!ha(23)).
1) Ceate the following t7pes
SQL- !eate t7pe add as o%Ke!t(hno nu$%e(1)#!it7 va!ha(23)).?
SQL- !eate t7pe stud as o%Ke!t(na$e va!ha(23)#addess add).?
4) Relate the o%Ke!ts to the student ta%le %7 !eating the o%Ke!t view
SQL- !eate view studentNov(no#studNinfo) as sele!t no#stud(na$e#add(hno#!it7)) fo$
student.
@) Now 7ou !an inset data into student ta%le in two wa7s
a) )7 egula inset
SQL- Inset into student values(2#;sudha;#222#;h7d;).
%) )7 using o%Ke!t view
69
SQL- Inset into studentNov values(2#stud(Lsudha;#add(222#;h7d;))).
M'T:ODS
*ou !an define $ethods whi!h ae nothing %ut fun!tions in t7pes and appl7 in the ta%les whi!h holds the t7pes.
'/0
2) Defining $ethods in t7pes
SQL- Ceate t7pe stud as o%Ke!t(na$e va!ha(23)#$a&s nu$%e(4)#
Me$%e fun!tion $a&sNf($a&s in nu$%e) etun nu$%e#
Aag$a esti!tNefeen!es($a&sNf#wnds#nds#wnps#fnps)).?
1) Defining t7pe %od7
SQL- Ceate t7pe %od7 stud as
Me$%e fun!tion $a&sNf($a&s in nu$%e) etun nu$%e is
)egin
Retun ($a&sH233).
'nd $a&sNf.
'nd.?
4) Ceate a ta%le using stud t7pe
SQL- Ceate ta%le student(no nu$%e(1)#info stud).
@) Inset so$e data into student ta%le
SQL- Inset into student values(2#stud(Lsudha;#233)).
C) Using $ethod in sele!t
SQL- Sele!t s5info5$a&sNf(s5info5$a&s) fo$ student s.
"" :ee we ae using the pag$a esti!tNefeen!es to avoid the wites to the data%ase5
70
9(RR(*S (ND N'ST'D T()L'S
9(RR(*S
( va7ing aa7 allows 7ou to stoe epeating atti%utes of a e!od in a single ow %ut with li$it5
'/0
2) 6e !an !eate vaa7s using oa!le t7pes as well as use defined t7pes5
a) 9aa7 using pe"defined t7pes
SQL- Ceate t7pe va as vaa7(C) of va!ha(23).?
%) 9aa7s using use defined t7pes
SQL- Ceate t7pe add as o%Ke!t(hno nu$%e(4)#!it7 va!ha(23)).?
SQL- Ceate t7pe va as vaa7(C) of add.?
1) Using vaa7 in ta%le
SQL- Ceate ta%le student(no nu$%e(1)#na$e va!ha(23)#addess va).
4) Inseting values into vaa7 ta%le
SQL- Inset into student values(2#;sudha;#va(add(222#;h7d;))).
SQL- Inset into student values(1#;Kagan;#va(add(222#;h7d;)#add(111#;%ang;))).
@) Sele!ting data fo$ vaa7 ta%le
SQL- Sele!t E fo$ student.
"" This will displa7 vaa7 !olu$n data along with vaa7 and adt.
SQL- Sele!t no#na$e# s5E fo$ student s2# ta%le(s25addess) s.
"" This will displa7 in geneal fo$at
C) Instead of s5E 7ou !an spe!if7 the !olu$ns in vaa7
SQL- Sele!t no#na$e# s5hno#s5!it7 fo$ student s2#ta%le(s25addess) s.
"" Update and delete not possi%le in vaa7s5
"" :ee we used ta%le fun!tion whi!h will ta&e the vaa7 !olu$n as input fo podu!ing
output e/!luding vaa7 and t7pes5
71
N'ST'D T()L'S
( nested ta%le is# as its na$e i$plies# a ta%le within a ta%le5 In this !ase it is a ta%le that is epesented as a !olu$n within
anothe ta%le5
Nested ta%le has the sa$e effe!t of vaa7s %ut has no li$it5
'/0
2) 6e !an !eate nested ta%les using oa!le t7pes and use defined t7pes whi!h has no li$it
a) Nested ta%les using pe"defined t7pes
SQL- Ceate t7pe nt as ta%le of va!ha(23).?
%) Nested ta%les using use defined t7pes
SQL- Ceate t7pe add as o%Ke!t(hno nu$%e(4)#!it7 va!ha(23)).?
SQL- Ceate t7pe nt as ta%le of add.?
1) Using nested ta%le in ta%le
SQL- Ceate ta%le student(no nu$%e(1)#na$e va!ha(23)#addess nt) nested ta%le
addess stoe as studentNte$p.
4) Inseting values into ta%le whi!h has nested ta%le
SQL- Inset into student values (2#;sudha;#nt(add(222#;h7d;))).
SQL- Inset into student values (1#;Kagan;#nt(add(222#;h7d;)#add(111#;%ang;))).
@) Sele!ting data fo$ ta%le whi!h has nested ta%le
SQL- Sele!t E fo$ student.
"" This will displa7 nested ta%le !olu$n data along with nested ta%le and adt.
SQL- Sele!t no#na$e# s5E fo$ student s2# ta%le(s25addess) s.
"" This will displa7 in geneal fo$at
C) Instead of s5E 7ou !an spe!if7 the !olu$ns in nested ta%le
SQL- Sele!t no#na$e# s5hno#s5!it7 fo$ student s2#ta%le(s25addess) s.
D) Inseting nested ta%le data to the e/isting ow
SQL- Inset into ta%le(sele!t addess fo$ student whee noI2)
values(add(CCC#;!hennai;)).
W) Update in nested ta%les
SQL- Update ta%le(sele!t addess fo$ student whee noI1) s set s5!it7I;%o$%a7; whee
s5hno I 111.
G) Delete in nested ta%le
72
SQL- Delete ta%le(sele!t addess fo$ student whee noI4) s whee s5hnoI444.
BL(S:)(CF QU'R*
Used to etieve the data whi!h has %een alead7 !o$$itted with out going fo e!ove75
Blash%a!&s ae of two t7pes
Ti$e %ase flash%a!&
SCN %ased flash%a!& (SCN stands fo S7ste$ Change Nu$%e)
'/0
2) Using ti$e %ased flash%a!&
a) SQL- Sele!t Efo$ student.
"" This will displa7 all the ows
%) SQL- Delete student.
!) SQL- Co$$it. "" this will !o$$it the wo&5
d) SQL- Sele!t Efo$ student.
"" :ee it will displa7 nothing
e) Then e/e!ute the following po!edues
SQL- '/e! d%$sNflash%a!&5ena%leNatNti$e(s7sdate"1?2@@3)
f) SQL- Sele!t Efo$ student.
"" :ee it will displa7 the lost data
"" The lost data will !o$e %ut the !uent s7ste$ ti$e was used
g) SQL- '/e! d%$sNflash%a!&5disa%le
"" :ee we have to disa%le the flash%a!& to ena%le it again
1) Using SCN %ased flash%a!&
a) De!lae a vaia%le to stoe SCN
SQL- 9aia%le s nu$%e
%) 8et the SCN
SQL- '/e! 0s 0I e/e! d%$sNflash%a!&5getNs7ste$N!hangeNnu$%e
!) To see the SCN
SQL- Aint s
d) Then e/e!ute the following po!edues
SQL- '/e! d%$sNflash%a!&5ena%leNatNs7ste$N!hangeNnu$%e(0s)
SQL- '/e! d%$sNflash%a!&5disa%le
73
'+T'RN(L T()L'S
*ou !an use e/tenal ta%le featue to a!!ess e/tenal files as if the7 ae ta%les inside the data%ase5
6hen 7ou !eate an e/tenal ta%le# 7ou define its stu!tue and lo!ation with in oa!le5
6hen 7ou Vue7 the ta%le# oa!le eads the e/tenal ta%le and etuns the esults Kust as if the data had %een stoed with
in the data%ase5
(CC'SSIN8 '+T'RN(L T()L' D(T(
To a!!ess e/tenal files fo$ within oa!le# 7ou $ust fist use the !eate die!to7 !o$$and to define a die!to7 o%Ke!t
pointing to the e/tenal file lo!ation
Uses who will a!!ess the e/tenal files $ust have the ead and wite pivilege on the die!to75
'/0
CR'(TIN8 DIR'CTOR* (ND OS L'9'L BIL'
SQL- SVlplus s7ste$?$anage
SQL- Ceate die!to7 saðNdi as L?9isd%?visd%?T5153?e/tenal;.
SQL- 8ant all on die!to7 saðNdi to sað.
SQL- Conn sað?sað
SQL- Spool dept5lst
SQL- Sele!t deptno XX L#; XX dna$e XX L#; XX lo! fo$ dept.
SQL- Spool off
CR'(TIN8 '+T'RN(L T()L'
SQL- Ceate ta%le deptNe/t
(deptno nu$%e(1)#
Dna$e va!ha(2@)#
Lo! va!ha(24))
OganiUation e/tenal ( t7pe oa!leNloade
Default die!to7 saðNdi
(!!ess paa$etes
( e!ods deli$ited %7 newline
Bields te$inated %7 Q#R
( deptno nu$%e(1)#
74
Dna$e va!ha(2@)#
Lo! va!ha(24)))
Lo!ation (L?9isd%?visd%?T5153?dept5lst;)).
S'L'CTIN8 D(T( BROM '+T'RN(L T()L'
SQL- sele!t E fo$ deptNe/t.
This will ead fo$ dept5lst whi!h is a opeating s7ste$ level file5
LIMIT(TIONS ON '+T'RN(L T()L'S
a) *ou !an not pefo$ inset# update# and delete opeations
a) Inde/ing not possi%le
b) Constaints not possi%le
)'N'BITS OB '+T'RN(L T()L'S
a) Queies of e/tenal ta%les !o$plete ve7 Vui!&l7 even though a full ta%le s!an id eVuied with ea!h a!!ess
%) *ou !an Koin e/tenal ta%les to ea!h othe o to standad ta%les
R'B D'R'B 9(LU'
R'B
The ef fun!tion allows efeen!ing of e/isting ow o%Ke!ts5
75
'a!h of the ow o%Ke!ts has an o%Ke!t id value assigned to it5
The o%Ke!t id assigned !an %e seen %7 using ef fun!tion5
D'R'B
The deef fun!tion pefo$s opposite a!tion5
It ta&es a efeen!e value of o%Ke!t id and etuns the value of the ow o%Ke!ts5
9(LU'
'ven though the pi$a7 ta%le is o%Ke!t ta%le# still it displa7s the ows in geneal fo$at5
To displa7 the entie stu!tue of the o%Ke!t# this will %e used5
'/0
2) !eate vendotNadt t7pe
SQL- Ceate t7pe vendoNadt as o%Ke!t (vendoN!ode nu$%e(1)# vendoNna$e
va!ha(1)# vendoNaddess va!ha(23)).?
1) !eate o%Ke!t ta%les vendos and vendos2
SQL- Ceate ta%le vendos of vendoNadt.
SQL- Ceate ta%le vendos2 of vendoNadt.
4) inset the data into o%Ke!t ta%les
SQL- inset into vendos values(2# La;# Lh7d;).
SQL- inset into vendos values(1# L%;# L%ang;).
SQL- inset into vendos2 values(4# L!;# Ldelhi;).
SQL- inset into vendos2 values(@# Ld;# L!hennai;).
@) !eate anothe ta%le odes whi!h holds the vendoNadt t7pe also5
SQL- Ceate ta%le odes (odeNno nu$%e(1)# vendoNinfo ef vendoNadt).
O
SQL- Ceate ta%le odes (odeNno nu$%e(1)# vendoNinfo ef vendoNadt with owid).
C) inset the data into odes ta%le
The vendoNinfo !olu$n in the following s7nta/es will stoe o%Ke!t id of an7 ta%le whi!h
is efeen!ed %7 vendoNadt o%Ke!t ( %oth vendos and vendos2)5
SQL- inset into odes values(22#(sele!t ef(v) fo$ vendos v whee vendoN!ode I 2)).
SQL- inset into odes values(21#(sele!t ef(v) fo$ vendos v whee vendoN!ode I 1)).
SQL- inset into odes values(24#(sele!t ef(v2) fo$ vendos2 v2 whee vendoN!ode I
76
2)).
SQL- inset into odes values(2@#(sele!t ef(v2) fo$ vendos2 v2 whee vendoN!ode I
2)).
D) To see the o%Ke!t ids of vendo ta%le
SQL- Sele!t ef(9) fo$ vendos v.
W) If 7ou see the vendoNinfo of odes it will show onl7 the o%Ke!t ids not the values# to see
the values
SQL- Sele!t deef(o5vendoNinfo) fo$ odes o.
G) 'ven though the vendos ta%le is o%Ke!t ta%le it will not show the adt along with data# to
see the data along with the adt
SQL-Sele!t E fo$ vendos.
This will give the data without adt5
SQL-Sele!t value(v) fo$ vendos v.
This will give the !olu$ns data along wih the t7pe5
R'B CONSTR(INTS
Ref !an also a!ts as !onstaint5
'ven though vendos2 also holding vendoNadt# the odes ta%le will stoe the o%Ke!t ids of vendos onl7 %e!ause it is
!onstained to that ta%le onl75
The vendoNinfo !olu$n in the following s7nta/es will stoe o%Ke!t ids of vendos onl75
SQL- Ceate ta%le odes (odeNno nu$%e(1)# vendoNinfo ef vendoNadt s!ope is
vendos).
O
SQL- Ceate ta%le odes (odeNno nu$%e(1)# vendoNinfo ef vendoNadt !onstaint f&
efeen!es vendos).
O)>'CT 9I'6S 6IT: R'B'R'NC'S
To i$ple$ent the o%Ke!ts and the ef !onstaints to the e/isting ta%les# what we !an do[ Si$pl7 dop the %oth ta%les and
e!eate with o%Ke!ts and ef !onstains5
)ut 7ou !an a!hieve this with out dopping the ta%les and without losing the data %7 !eating o%Ke!t views with efeen!es5
'/0
a) Ceate the following ta%les
77
SQL- Ceate ta%le student2(no nu$%e(1) pi$a7 &e7#na$e va!ha(1)#$a&s
nu$%e(4)).
SQL- Ceate ta%le student1(no nu$%e(1) pi$a7 &e7#hno nu$%e(4)#!it7 va!ha(23)#id
nu$%e(1)#foeign Fe7(id) efeen!es student2(no)).
%) Inset the e!ods into %oth ta%les
SQL- inset into student2(2#;a;#233).
SQL- inset into student2(1#;%;#133).
SQL- inset into student1(22#222#;h7d;#2).
SQL- inset into student1(21#111#;%ang;#1).
SQL- inset into student1(24#444#;%o$%a7;#2).
!) Ceate the t7pe
SQL- !eate o epla!e t7pe stud as o%Ke!t(no nu$%e(1)#na$e va!ha(1)#$a&s
nu$%e(4)).?
d) 8eneating OIDs
SQL- Ceate o epla!e view student2Nov of stud with o%Ke!t identifie(o id) (no) as
Sele!t E fo$ Student2.
e) 8eneating efeen!es
SQL- Ceate o epla!e view student1Nov as sele!t no#hno#!it7#$a&eNef(student2Nov#id)
id fo$ Student1.
d) Que7 the following
SQL- sele!t Efo$ student2Nov.
SQL- sele!t ef(s) fo$ student2Nov s.
SQL- sele!t values(s) fo$ student2Nov.
SQ- sele!t Efo$ student1Nov.
SQL- sele!t deef(s5id) fo$ student1Nov s.
A(RTITIONS
( single logi!al ta%le !an %e split into a nu$%e of ph7si!all7 sepaate pie!es %ased on anges of &e7 values5 'a!h of the
pats of the ta%le is !alled a patition5
( non"patitioned ta%le !an not %e patitioned late5
T*A'S
Range patitions
List patitions
:ash patitions
Su% patitions
78
(D9(NT(8'S
Redu!ing downti$e fo s!heduled $aintenan!e# whi!h allows $aintenan!e opeations to %e !aied out on
sele!ted patitions while othe patitions ae availa%le to uses5
Redu!ing downti$e due to data failue# failue of a pati!ula patition will no wa7 affe!t othe patitions5
Aatition independen!e allows fo !on!uent use of the vaious patitions fo vaious puposes5
(D9(NT(8'S OB A(RTITIONS )* STORIN8 T:'M IN DIBB'R'NT T()L'SA(C'S
Redu!es the possi%ilit7 of data !ouption in $ultiple patitions5
)a!& up and e!ove7 of ea!h patition !an %e done independentl75
DIS(D9(NT(8'S
Aatitioned ta%les !annot !ontain an7 !olu$ns with long o long aw datat7pes# LO) t7pes o o%Ke!t t7pes5
R(N8' A(RTITIONS
a) Ceating ange patitioned ta%le
SQL- Ceate ta%le student(no nu$%e(1)#na$e va!ha(1)) patition %7 ange(no) (patition
p2 values less than(23)# patition p1 values less than(13)# patition p4 values less
than(43)#patition p@ values less than($a/value)).
EE if 7ou ae using $a/value fo the last patition# 7ou !an not add a patition5
%) Inseting e!ods into ange patitioned ta%le
SQL- Inset into student values(2#;a;). "" this will go to p2
SQL- Inset into student values(22#;%;). "" this will go to p1
SQL- Inset into student values(12#;!;). "" this will go to p4
SQL- Inset into student values(42#;d;). "" this will go to p@
!) Retieving e!ods fo$ ange patitioned ta%le
SQL- Sele!t Efo$ student.
SQL- Sele!t Efo$ student patition(p2).
d) Aossi%le opeations with ange patitions
79
(dd
Dop
Tun!ate
Rena$e
Split
Move
'/!hange
e) (dding a patition
SQL- (lte ta%le student add patition pC values less than(@3).
f) Dopping a patition
SQL- (lte ta%le student dop patition p@.
g) Rena$ing a patition
SQL- (lte ta%le student ena$e patition p4 to pD.
h) Tun!ate a patition
SQL- (lte ta%le student tun!ate patition pD.
i) Splitting a patition
SQL- (lte ta%le student split patition p1 at(2C) into (patition p12#patition p11).
K) '/!hanging a patition
SQL- (lte ta%le student e/!hange patition p2 with ta%le student1.
&) Moving a patition
SQL- (lte ta%le student $ove patition p12 ta%lespa!e saðNts.
LIST A(RTITIONS
a) Ceating list patitioned ta%le
SQL- Ceate ta%le student(no nu$%e(1)#na$e va!ha(1)) patition %7 list(no) (patition p2
values(2#1#4#@#C)# patition p1 values(D#W#G#T#23)#patition p4 values(22#21#24#2@#2C)#
patition p@ values(2D#2W#2G#2T#13)).
%) Inseting e!ods into list patitioned ta%le
SQL- Inset into student values(2#;a;). "" this will go to p2
SQL- Inset into student values(D#;%;). "" this will go to p1
SQL- Inset into student values(22#;!;). "" this will go to p4
SQL- Inset into student values(2D#;d;). "" this will go to p@
!) Retieving e!ods fo$ list patitioned ta%le
SQL- Sele!t Efo$ student.
SQL- Sele!t Efo$ student patition(p2).
80
d) Aossi%le opeations with list patitions
(dd
Dop
Tun!ate
Rena$e
Move
'/!hange
e) (dding a patition
SQL- (lte ta%le student add patition pC values(12#11#14#1@#1C).
f) Dopping a patition
SQL- (lte ta%le student dop patition p@.
g) Rena$ing a patition
SQL- (lte ta%le student ena$e patition p4 to pD.
h) Tun!ate a patition
SQL- (lte ta%le student tun!ate patition pD.
i) '/!hanging a patition
SQL- (lte ta%le student e/!hange patition p2 with ta%le student1.
K) Moving a patition
SQL- (lte ta%le student $ove patition p1 ta%lespa!e saðNts.
:(S: A(RTITIONS
a) Ceating hash patitioned ta%le
SQL- Ceate ta%le student(no nu$%e(1)#na$e va!ha(1)) patition %7 hash(no) patitions
C.
:ee oa!le auto$ati!all7 gives patition na$es li&e
S*SNA2
S*SNA1
S*SNA4
S*SNA@
S*SNAC
%) Inseting e!ods into hash patitioned ta%le
it will inset the e!ods %ased on hash fun!tion !al!ulated %7 ta&ing the patition &e7
SQL- Inset into student values(2#;a;).
SQL- Inset into student values(D#;%;).
SQL- Inset into student values(22#;!;).
81
SQL- Inset into student values(2D#;d;).
!) Retieving e!ods fo$ hash patitioned ta%le
SQL- Sele!t Efo$ student.
SQL- Sele!t Efo$ student patition(s7sNp2).
d) Aossi%le opeations with hash patitions
(dd
Tun!ate
Rena$e
Move
'/!hange
e) (dding a patition
SQL- (lte ta%le student add patition pD .
f) Rena$ing a patition
SQL- (lte ta%le student ena$e patition pD to pW.
g) Tun!ate a patition
SQL- (lte ta%le student tun!ate patition pW.
h) '/!hanging a patition
SQL- (lte ta%le student e/!hange patition s7sNp2 with ta%le student1.
i) Moving a patition
SQL- (lte ta%le student $ove patition s7sNp1 ta%lespa!e saðNts.
SU)"A(RTITIONS 6IT: R(N8' (ND :(S:
Su%patitions !lause is used %7 hash onl75 6e !an not !eate su%patitions with list and hash patitions5
a) Ceating su%patitioned ta%le
SQL- Ceate ta%le student(no nu$%e(1)#na$e va!ha(1)#$a&s nu$%e(4))
Aatition %7 ange(no) su%patition %7 hash(na$e) su%patitions 4
(Aatition p2 values less than(23)#patition p1 values less than(13)).
This will !eate two patitions p2 and p1 with thee su%patitions fo ea!h patition
A2 \ S*SNSU)A2
S*SNSU)A1
S*SNSU)A4
A1 \ S*SNSU)A@
S*SNSU)AC
S*SNSU)AD
82
EE if 7ou ae using $a/value fo the last patition# 7ou !an not add a patition5
%) Inseting e!ods into su%patitioned ta%le
SQL- Inset into student values(2#;a;). "" this will go to p2
SQL- Inset into student values(22#;%;). "" this will go to p1
!) Retieving e!ods fo$ su%patitioned ta%le
SQL- Sele!t Efo$ student.
SQL- Sele!t Efo$ student patition(p2).
SQL- Sele!t Efo$ student su%patition(s7sNsu%p2).
d) Aossi%le opeations with su%patitions
(dd
Dop
Tun!ate
Rena$e
Split
e) (dding a patition
SQL- (lte ta%le student add patition p4 values less than(43).
f) Dopping a patition
SQL- (lte ta%le student dop patition p4.
g) Rena$ing a patition
SQL- (lte ta%le student ena$e patition p1 to p4.
h) Tun!ate a patition
SQL- (lte ta%le student tun!ate patition p2.
i) Splitting a patition
SQL- (lte ta%le student split patition p4 at(2C) into (patition p42#patition p41).
83
8ROUA )* (ND :(9IN8
8ROUA )*
Using goup %7# we !an !eate goups of elated info$ation5
Colu$ns used in sele!t $ust %e used with goup %7# othewise it was not a goup %7 e/pession5
'/0
SQL- sele!t deptno# su$(sal) fo$ e$p goup %7 deptno.
D'ATNO SUM(S(L)
"""""""""" """"""""""
23 GWC3
13 23GWC
43 T@33
SQL- sele!t deptno#Ko%#su$(sal) fo$ e$p goup %7 deptno#Ko%.
D'ATNO >O) SUM(S(L)
"""""""""" """"""""" """"""""""
23 CL'RF 2433
23 M(N(8'R 1@C3
23 AR'SID'NT C333
13 (N(L*ST D333
13 CL'RF 2T33
13 M(N(8'R 1TWC
43 CL'RF TC3
84
43 M(N(8'R 1GC3
43 S(L'SM(N CD33
T ows sele!ted5
:(9IN8
This will wo& as whee !lause whi!h !an %e used onl7 with goup %7 %e!ause of a%sen!e of whee !lause in goup %75
'/0
SQL- sele!t deptno#Ko%#su$(sal) tsal fo$ e$p goup %7 deptno#Ko% having su$(sal) - 4333.
D'ATNO >O) TS(L
"""""""""" """"""""" """"""""""
23 AR'SID'NT C333
13 (N(L*ST D333
43 S(L'SM(N CD33
SQL- sele!t deptno#Ko%#su$(sal) tsal fo$ e$p goup %7 deptno#Ko% having su$(sal) - 4333
ode %7 Ko%.
D'ATNO >O) TS(L
"""""""""" """"""""" """"""""""
13 (N(L*ST D333
23 AR'SID'NT C333
43 S(L'SM(N CD33
ORD'R OB '+'CUTION
8oup the ows togethe %ased on goup %7 !lause5
Cal!ulate the goup fun!tions fo ea!h goup5
Choose and eli$inate the goups %ased on the having !lause5
Ode the goups %ased on the spe!ified !olu$n5
85
ROLLUA 8ROUAIN8 CU)'
These ae the enhan!e$ents to the goup %7 featue5
USIN8 ROLLUA
This will give the salaies in ea!h depat$ent in ea!h Ko% !atego7 along wih the total sala7
fot individual depat$ents and the total sala7 of all the depat$ents5
SQL- Sele!t deptno#Ko%#su$(sal) fo$ e$p goup %7 ollup(deptno#Ko%).
D'ATNO >O) SUM(S(L)
"""""""""" """"""""" """"""""""
23 CL'RF 2433
23 M(N(8'R 1@C3
23 AR'SID'NT C333
23 GWC3
13 (N(L*ST D333
13 CL'RF 2T33
13 M(N(8'R 1TWC
13 23GWC
43 CL'RF TC3
43 M(N(8'R 1GC3
43 S(L'SM(N CD33
43 T@33
1T31C
24 ows sele!ted5
USIN8 8ROUAIN8
86
In the a%ove Vue7 it will give the total sala7 of the individual depat$ents %ut with a
%lan& in the Ko% !olu$n and gives the total sala7 of all the depat$ents with %lan&s in
deptno and Ko% !olu$ns5
To epla!e these %lan&s with 7ou desied sting gouping will %e used
SQL- sele!t de!ode(gouping(deptno)#2#=(ll Depts=#deptno)#de!ode(gouping(Ko%)#2#=(ll
Ko%s=#Ko%)#su$(sal) fo$ e$p goup %7 ollup(deptno#Ko%).
D'COD'(8ROUAIN8(D'ATNO)#2#=(LLD'ATS=#D'A D'COD'(8R SUM(S(L)
""""""""""""""""""""""""""""""""""" """""""""""""""""""""""""""""""""" """"""""""""""
23 CL'RF 2433
23 M(N(8'R 1@C3
23 AR'SID'NT C333
23 (ll Ko%s GWC3
13 (N(L*ST D333
13 CL'RF 2T33
13 M(N(8'R 1TWC
13 (ll Ko%s 23GWC
43 CL'RF TC3
43 M(N(8'R 1GC3
43 S(L'SM(N CD33
43 (ll Ko%s T@33
(ll Depts (ll Ko%s 1T31C
24 ows sele!ted5
8ouping will etun 2 if the !olu$n whi!h is spe!ified in the gouping fun!tion has %een
used in ollup5
8ouping will %e used in asso!iation with de!ode5
USIN8 CU)'
This will give the salaies in ea!h depat$ent in ea!h Ko% !atego7# the total sala7 fo individual depat$ents# the total
sala7 of all the depat$ents and the salaies in ea!h Ko% !atego75
SQL- sele!t de!ode(gouping(deptno)#2#;(ll Depts;#deptno)#de!ode(gouping(Ko%)#2#;(ll
>o%s;#Ko%)#su$(sal) fo$ e$p goup %7 !u%e(deptno#Ko%).
87
D'COD'(8ROUAIN8(D'ATNO)#2#=(LLD'ATS=#D'A D'COD'(8R SUM(S(L)
""""""""""""""""""""""""""""""""""" """""""""""""""""""""""""""""""""""" """"""""""""
23 CL'RF 2433
23 M(N(8'R 1@C3
23 AR'SID'NT C333
23 (ll >o%s GWC3
13 (N(L*ST D333
13 CL'RF 2T33
13 M(N(8'R 1TWC
13 (ll >o%s 23GWC
43 CL'RF TC3
43 M(N(8'R 1GC3
43 S(L'SM(N CD33
43 (ll >o%s T@33
(ll Depts (N(L*ST D333
(ll Depts CL'RF @2C3
(ll Depts M(N(8'R G1WC
(ll Depts AR'SID'NT C333
(ll Depts S(L'SM(N CD33
(ll Depts (ll >o%s 1T31C
2G ows sele!ted5
S'T OA'R(TORS
88
T*A'S
Union
Union all
Intese!t
Minus
UNION
This will !o$%ine the e!ods of $ultiple ta%les having the sa$e stu!tue5
'/0
SQL- sele!t E fo$ student2 union sele!t E fo$ student1.
UNION (LL
This will !o$%ine the e!ods of $ultiple ta%les having the sa$e stu!tue %ut in!luding dupli!ates5
'/0
SQL- sele!t E fo$ student2 union all sele!t E fo$ student1.
INT'RS'CT
This will give the !o$$on e!ods of $ultiple ta%les having the sa$e stu!tue5
'/0
SQL- sele!t E fo$ student2 intese!t sele!t E fo$ student1.
MINUS
This will give the e!ods of a ta%le whose e!ods ae not in othe ta%les having the sa$e stu!tue5
'/0
SQL- sele!t E fo$ student2 $inus sele!t E fo$ student1.
9I'6S
( view is a data%ase o%Ke!t that is a logi!al epesentation of a ta%le5 It is deliveed fo$ a ta%le %ut has no stoage of its
own and often $a7 %e used in the sa$e $anne as a ta%le5
89
( view ta&es the output of the Vue7 and teats it as a ta%le# theefoe a view !an %e thought of as a stoed Vue7 o a
vitual ta%le5
T*A'S
Si$ple view
Co$ple/ view
Si$ple view !an %e !eated fo$ one ta%le whee as !o$ple/ view !an %e !eated fo$ $ultiple ta%les5
6:* 9I'6S[
Aovides additional level of se!uit7 %7 esti!ting a!!ess to a pedete$ined set of ows and?o !olu$ns of a
ta%le5
:ide the data !o$ple/it75
Si$plif7 !o$$ands fo the use5
9I'6S 6IT:OUT DML
Read onl7 view
9iew with goup %7
9iew with aggegate fun!tions
9iew with ownu$
Aatition view
9iew with distin!t
'/0
SQL- Ceate view deptNv as sele!t Efo$ dept with ead onl7.
SQL- Ceate view deptNv as sele!t deptno# su$(sal) tNsal fo$ e$p goup %7 deptno.
SQL- Ceate view stud as sele!t ownu$ no# na$e# $a&s fo$ student.
SQL- Ceate view student as sele!t Efo$ student2 union sele!t Efo$ student1.
SQL- Ceate view stud as sele!t distin!t no#na$e fo$ student.
9I'6S 6IT: DML
9iew with not null !olu$n "" inset with out not null !olu$n not possi%le
"" update not null !olu$n to null is not possi%le
"" delete possi%le
9iew with out not null !olu$n whi!h was in %ase ta%le "" inset not possi%le
90
"" update# delete possi%le
9iew with e/pession "" inset # update not possi%le
"" delete possi%le
9iew with fun!tions (e/!ept aggegate) "" inset# update not possi%le
"" delete possi%le
9iew was !eated %ut the undel7ing ta%le was dopped then we will get the $essage li&e Q view has eos R5
9iew was !eated %ut the %ase ta%le has %een alteed %ut still the view was with the initial definition# we
have to epla!e the view to affe!t the !hanges5
Co$ple/ view (view with $oe than one ta%le) "" inset not possi%le
"" update# delete possi%le (not alwa7s)
CR'(TIN8 9I'6 6IT:OUT :(9IN8 T:' )(S' T()L'
SQL- Ceate fo!e view stud as sele!t EBo$ student.
"" On!e the %ase ta%le was !eated then the view is validated5
9I'6 6IT: C:'CF OATION CONSTR(INT
SQL- Ceate view stud as sele!t Efo$ student whee $a&s I C33 with !he!& option !onstaint
C&.
" Inset possi%le with $a&s value as C33
" Update possi%le e/!luding $a&s !olu$n
" Delete possi%le
DROAAIN8 9I'6S
SQL- dop view deptNv.
S*NON*M (ND S'QU'NC'
S*NON*M
( s7non7$ is a data%ase o%Ke!t# whi!h is used as an alias fo a ta%le# view o seVuen!e5
T*A'S
Aivate
Au%li!
91
Aivate s7non7$ is availa%le to the pati!ula use who !eates5
Au%li! s7non7$ is !eated %7 D)( whi!h is availa%le to all the uses5
(D9(NT(8'S
:ide the na$e and owne of the o%Ke!t5
Aovides lo!ation tanspaen!7 fo e$ote o%Ke!ts of a disti%uted data%ase5
CR'(T' (ND DROA
SQL- !eate s7non7$ s2 fo e$p.
SQL- !eate pu%li! s7non7$ s1 fo e$p.
SQL- dop s7non7$ s2.
S'QU'NC'
( seVuen!e is a data%ase o%Ke!t# whi!h !an geneate uniVue# seVuential intege values5
It !an %e used to auto$ati!all7 geneate pi$a7 &e7 o uniVue &e7 values5
( seVuen!e !an %e eithe in an as!ending o des!ending ode5
S7nta/0
Ceate seVuen!e ,se-_name- Oin!e$ent %t Ceate ta%le vendos of vendoNadt.7 nP Ostat with nP O$a/value nP O$invalue
nP O!7!le?no!7!leP O!a!he?no!a!heP.
)7 defalult the seVuen!e stats with 2# in!e$ents %7 2 with $invalue of 2 and with no!7!le# no!a!he5
Ca!he option pe"alloo!ates a set of seVuen!e nu$%es and etains the$ in $e$o7 fo faste a!!ess5
'/0
SQL- !eate seVuen!e s.
SQL- !eate seVuen!e s in!e$ent %7 23 stat with 233 $invalue 233 $a/value 133 !7!le
!a!he 13.
USIN8 S'QU'NC'
SQL- !eate ta%le student(no nu$%e(1)#na$e va!ha(23)).
SQL- inset into student values(s5ne/tval# Lsað).
Initiall7 !uval is not defined and ne/tval is stating value5
(fte that ne/tval and !uval ae alwa7s eVual5
92
CR'(TIN8 (LA:("NUM'RIC S'QU'NC'
SQL- !eate seVuen!e s stat with 22214@.
SQL- Inset into student values (s5ne/tval XX tanslate
(s5ne/tval#;214@CDWGT3;#;a%!defghiK;)).
(LT'RIN8 S'QU'NC'
6e !an alte the seVuen!e to pefo$ the following5
Set o eli$inate $inalue of $a/value5
Change the in!e$ent value5
Change the nu$%e of !a!hed seVuen!e nu$%es5
'/0
SQL- alte seVuen!e s $invalue C.
SQL- alte seVuen!e s in!e$ent %7 1.
SQL- alte seVuen!e s !a!he 23.
DROAAIN8 S'QU'NC'
SQL- dop seVuen!e s.
>OINS
The pupose of a Koin is to !o$%ine the data a!oss ta%les5
( Koin is a!tuall7 pefo$ed %7 the whee !lause whi!h !o$%ines the spe!ified ows of ta%les5
If a Koin involves in $oe than two ta%les then oa!le Koins fist two ta%les %ased on the Koins !ondition and then !o$paes
the esult with the ne/t ta%le and so on5
T*A'S
'Vui Koin
Non"eVui Koin
Self Koin
Natual Koin
Coss Koin
Oute Koin
Left oute
93
Right oute
Bull oute
Inne Koin
Using !lause
On !lause
(ssu$e that we have the following ta%les5
SQL- sele!t E fo$ dept.
D'ATNO DN(M' LOC
"""""" """""""""" """"""""""
23 $&t h7d
13 fin %ang
43 h %o$%a7
SQL- sele!t E fo$ e$p.
'MANO 'N(M' >O) M8R D'ATNO
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sað anal7st @@@ 23
111 sudha !le& 444 13
444 Kagan $anage 222 23
@@@ $adhu enginee 111 @3
'QUI >OIN
( Koin whi!h !ontains an LI; opeato in the Koins !ondition5
'/0
SQL- sele!t e$pno#ena$e#Ko%#dna$e#lo! fo$ e$p e#dept d whee e5deptnoId5deptno.
'MANO 'N(M' >O) DN(M' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sað anal7st $&t h7d
444 Kagan $anage $&t h7d
111 sudha !le& fin %ang
USIN8 CL(US'
94
SQL- sele!t e$pno#ena$e#Ko% #dna$e#lo! fo$ e$p e Koin dept d using(deptno).
'MANO 'N(M' >O) DN(M' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sað anal7st $&t h7d
444 Kagan $anage $&t h7d
111 sudha !le& fin %ang
ON CL(US'
SQL- sele!t e$pno#ena$e#Ko%#dna$e#lo! fo$ e$p e Koin dept d on(e5deptnoId5deptno).
'MANO 'N(M' >O) DN(M' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sað anal7st $&t h7d
444 Kagan $anage $&t h7d
111 sudha !le& fin %ang
NON"'QUI >OIN
( Koin whi!h !ontains an opeato othe than LI; in the Koins !ondition5
'/0
SQL- sele!t e$pno#ena$e#Ko%#dna$e#lo! fo$ e$p e#dept d whee e5deptno - d5deptno.
'MANO 'N(M' >O) DN(M' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
111 sudha !le& $&t h7d
@@@ $adhu enginee $&t h7d
@@@ $adhu enginee fin %ang
@@@ $adhu enginee h %o$%a7
S'LB >OIN
>oining the ta%le itself is !alled self Koin5
'/0
SQL- sele!t e25e$pno#e15ena$e#e25Ko%#e15deptno fo$ e$p e2#e$p e1 whee
e25e$pnoIe15$g.
95
'MANO 'N(M' >O) D'ATNO
"""""""""" """""""""" """""""""" """"""""""
222 Kagan anal7st 23
111 $adhu !le& @3
444 sudha $anage 13
@@@ sað enginee 23
N(TUR(L >OIN
Natual Koin !o$paes all the !o$$on !olu$ns5
'/0
SQL- sele!t e$pno#ena$e#Ko%#dna$e#lo! fo$ e$p natual Koin dept.
'MANO 'N(M' >O) DN(M' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sað anal7st $&t h7d
444 Kagan $anage $&t h7d
111 sudha !le& fin %ang
CROSS >OIN
This will gives the !oss podu!t5
'/0
SQL- sele!t e$pno#ena$e#Ko%#dna$e#lo! fo$ e$p !oss Koin dept.
'MANO 'N(M' >O) DN(M' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sað anal7st $&t h7d
111 sudha !le& $&t h7d
444 Kagan $anage $&t h7d
@@@ $adhu enginee $&t h7d
222 sað anal7st fin %ang
111 sudha !le& fin %ang
444 Kagan $anage fin %ang
@@@ $adhu enginee fin %ang
222 sað anal7st h %o$%a7
111 sudha !le& h %o$%a7
96
444 Kagan $anage h %o$%a7
@@@ $adhu enginee h %o$%a7
21 ows sele!ted5
OUT'R >OIN
Oute Koin gives the non"$at!hing e!ods along with $at!hing e!ods5
L'BT OUT'R >OIN
This will displa7 the all $at!hing e!ods and the e!ods whi!h ae in left hand side ta%le those that ae not in ight hand
side ta%le5
'/0
SQL- sele!t e$pno#ena$e#Ko%#dna$e#lo! fo$ e$p e left oute Koin dept d
on(e5deptnoId5deptno).
O
SQL- sele!t e$pno#ena$e#Ko%#dna$e#lo! fo$ e$p e#dept d whee e5deptnoId5deptno(H).
'MANO 'N(M' >O) DN(M' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sað anal7st $&t h7d
444 Kagan $anage $&t h7d
111 sudha !le& fin %ang
@@@ $adhu enginee
RI8:T OUT'R >OIN
This will displa7 the all $at!hing e!ods and the e!ods whi!h ae in ight hand side ta%le those that ae not in left hand
side ta%le5
'/0
SQL- sele!t e$pno#ena$e#Ko%#dna$e#lo! fo$ e$p e ight oute Koin dept d
on(e5deptnoId5deptno).
O
SQL- sele!t e$pno#ena$e#Ko%#dna$e#lo! fo$ e$p e#dept d whee e5deptno(H) I d5deptno.
97
'MANO 'N(M' >O) DN(M' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sað anal7st $&t h7d
444 Kagan $anage $&t h7d
111 sudha !le& fin %ang
h %o$%a7
BULL OUT'R >OIN
This will displa7 the all $at!hing e!ods and the non"$at!hing e!ods fo$ %oth ta%les5
'/0
SQL- sele!t e$pno#ena$e#Ko%#dna$e#lo! fo$ e$p e full oute Koin dept d
on(e5deptnoId5deptno).
'MANO 'N(M' >O) DN(M' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
444 Kagan $anage $&t h7d
222 sað anal7st $&t h7d
111 sudha !le& fin %ang
@@@ $adhu enginee
h %o$%a7
INN'R >OIN
This will displa7 all the e!ods that have $at!hed5
'/0
SQL- sele!t e$pno#ena$e#Ko%#dna$e#lo! fo$ e$p inne Koin dept using(deptno).
'MANO 'N(M' >O) DN(M' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sað anal7st $&t h7d
444 Kagan $anage $&t h7d
111 sudha !le& fin %ang
98
SU)QU'RI'S (ND '+ISTS
SU)QU'RI'S
Nesting of Vueies# one within the othe is te$ed as a su%Vue75
( state$ent !ontaining a su%Vue7 is !alled a paent Vue75
Su%Vueies ae used to etieve data fo$ ta%les that depend on the values in the ta%le itself5
T*A'S
Single ow su%Vueies
Multi ow su%Vueies
Multiple su%Vueies
Coelated su%Vueies
SIN8L' RO6 SU)QU'RI'S
In single ow su%Vue7# it will etun one value5
'/0
SQL- sele!t E fo$ e$p whee sal - (sele!t sal fo$ e$p whee e$pno I WCDD).
'MANO 'N(M' >O) M8R :IR'D(T' S(L COMM D'ATNO
"""""""""" """""""""" """"""""" """""""""" """""""""""" """"""" """""""""" """"""""""
WWGG SCOTT (N(L*ST WCDD 2T"(AR"GW 4333 13
WG4T FIN8 AR'SID'NT 2W"NO9"G2 C333 23
WT31 BORD (N(L*ST WCDD 34"D'C"G2 4333 13
99
MULTI RO6 SU)QU'RI'S
In $ulti ow su%Vue7# it will etun $oe than one value5 In su!h !ases we should in!lude opeatos li&e an7# all# in o not
in %etween the !o$paision opeato and the su%Vue75
'/0
SQL- sele!t E fo$ e$p whee sal - an7 (sele!t sal fo$ e$p whee sal %etween 1C33 and
@333).
'MANO 'N(M' >O) M8R :IR'D(T' S(L COMM D'ATNO
"""""""""" """""""""" """"""""" """""""""" """"""""""" """""""" """""""""" """"""""""
WCDD >ON'S M(N(8'R WG4T 31"(AR"G2 1TWC 13
WWGG SCOTT (N(L*ST WCDD 2T"(AR"GW 4333 13
WG4T FIN8 AR'SID'NT 2W"NO9"G2 C333 23
WT31 BORD (N(L*ST WCDD 34"D'C"G2 4333 13
SQL- sele!t E fo$ e$p whee sal - all (sele!t sal fo$ e$p whee sal %etween 1C33 and
@333).
'MANO 'N(M' >O) M8R :IR'D(T' S(L COMM D'ATNO
"""""""""" """""""""" """"""""" """""""""" """"""""""""" """""" """""""""" """"""""""
WG4T FIN8 AR'SID'NT 2W"NO9"G2 C333 23
MULTIAL' SU)QU'RI'S
Thee is no li$it on the nu$%e of su%Vueies in!luded in a whee !lause5 It allows nesting of a Vue7 within a su%Vue75
'/0
SQL- sele!t E fo$ e$p whee sal I (sele!t $a/(sal) fo$ e$p whee sal , (sele!t
$a/(sal) fo$ e$p)).
'MANO 'N(M' >O) M8R :IR'D(T' S(L COMM D'ATNO
"""""""""" """""""""" """"""""" """""""""" """""""""""" """"""" """""""""" """"""""""
WWGG SCOTT (N(L*ST WCDD 2T"(AR"GW 4333 13
WT31 BORD (N(L*ST WCDD 34"D'C"G2 4333 13
CORR'L(T'D SU)QU'RI'S
100
( su%Vue7 is evaluated on!e fo the entie paent state$ent whee as a !oelated su%Vue7 is evaluated on!e fo eve7
ow po!essed %7 the paent state$ent5
'/0
SQL- sele!t distin!t deptno fo$ e$p e whee C ,I (sele!t !ount(ena$e) fo$ e$p whee
e5deptno I deptno).
D'ATNO
""""""""""
13
43
'+ISTS
'/ists fun!tion is a test fo e/isten!e5 This is a logi!al test fo the etun of ows fo$ a Vue75
'/0
Suppose we want to displa7 the depat$ent nu$%es whi!h has $oe than @ e$plo7ees5
SQL- sele!t deptno#!ount(E) fo$ e$p goup %7 deptno having !ount(E) - @.
D'ATNO COUNT(E)
""""""""" """"""""""
13 C
43 D
Bo$ the a%ove Vue7 !an 7ou want to displa7 the na$es of e$plo7ees[
SQL- sele!t deptno#ena$e# !ount(E) fo$ e$p goup %7 deptno#ena$e having !ount(E) - @.
no ows sele!ted
The a%ove Vue7 etuns nothing %e!ause !o$%ination of deptno and ena$e neve etun
$oe than one !ount5
The solution is to use e/ists whi!h follows5
SQL- sele!t deptno#ena$e fo$ e$p e2 whee e/ists (sele!t E fo$ e$p e1
whee e25deptnoIe15deptno goup %7 e15deptno having !ount(e15ena$e) - @) ode %7
deptno#ena$e.
D'ATNO 'N(M'
"""""""""" """"""""""
13 (D(MS
101
13 BORD
13 >ON'S
13 SCOTT
13 SMIT:
43 (LL'N
43 )L(F'
43 >(M'S
43 M(RTIN
43 TURN'R
43 6(RD
22 ows sele!ted5
NOT '+ISTS
SQL- sele!t deptno#ena$e fo$ e$p e2 whee not e/ists (sele!t E fo$ e$p e1
whee e25deptnoIe15deptno goup %7 e15deptno having !ount(e15ena$e) - @) ode %7
deptno#ena$e.
D'ATNO 'N(M'
""""""""" """"""""""
23 CL(RF
23 FIN8
23 MILL'R
102
6(LFUA TR''S (ND INLIN' 9I'6
6(LFUA TR''S
Using hiea!hi!al Vueies# 7ou !an etieve data %ased on a natual hiea!hi!al elationship %etween ows in a ta%le5
:oweve# whee a hiea!hi!al elationship e/ists %etween the ows of a ta%le# a po!ess !alled tee wal&ing ena%les the
hiea!h7 to %e !onstu!ted5
'/0
SQL- sele!t ena$e XX =II-= XX pio ena$e# level fo$ e$p stat with ena$e I =FIN8=
!onne!t %7 pio e$pnoI$g.
'N(M'XX=II-=XXARIOR'N(M L'9'L
"""""""""""""""""""""""""""""""""""" """"""""
FIN8II- 2
>ON'SII-FIN8 1
SCOTTII->ON'S 4
(D(MSII-SCOTT @
BORDII->ON'S 4
SMIT:II-BORD @
)L(F'II-FIN8 1
(LL'NII-)L(F' 4
6(RDII-)L(F' 4
M(RTINII-)L(F' 4
TURN'RII-)L(F' 4
>(M'SII-)L(F' 4
CL(RFII-FIN8 1
MILL'RII-CL(RF 4
2@ ows sele!ted5
In the a%ove
Stat with !lause spe!ifies the oot ow of the ta%le5
Level pseudo !olu$n gives the 2 fo oot # 1 fo !hild and so on5
Conne!t %7 pio !lause spe!ifies the !olu$ns whi!h has paent"!hild elationship5
INLIN' 9I'6 OR TOA"N (N(L*SIS
103
In the sele!t state$ent instead of ta%le na$e# epla!ing the sele!t state$ent is &nown as inline view5
'/0
SQL- Sele!t ena$e# sal# ownu$ an& fo$ (sele!t Efo$ e$p ode %7 sal).
'N(M' S(L R(NF
"""""""""" """""""""" """"""""""
SMIT: G33 2
>(M'S TC3 1
(D(MS 2233 4
6(RD 21C3 @
M(RTIN 21C3 C
MILL'R 2433 D
TURN'R 2C33 W
(LL'N 2D33 G
CL(RF 1@C3 T
)L(F' 1GC3 23
>ON'S 1TWC 22
SCOTT 4333 21
BORD 4333 24
FIN8 C333 2@
2@ ows sele!ted5
LOCFS
104
Lo!&s ae the $e!hanis$s used to pevent destu!tive intea!tion %etween uses a!!essing sa$e esou!e
si$ultaneousl75 Lo!&s povides high degee of data !on!uen!75
T*A'S
Row level lo!&s
Ta%le level lo!&s
RO6 L'9'L LOCFS
In the ow level lo!& a ow is lo!&ed e/!lusivel7 so that othe !annot $odif7 the ow until the tansa!tion holding the lo!& is
!o$$itted o olled %a!&5 This !an %e done %7 using sele!t55o update !lause5
'/0
SQL- sele!t E fo$ e$p whee sal - 4333 fo update of !o$$5.
T()L' L'9'L LOCFS
( ta%le level lo!& will pote!t ta%le data thee%7 guaanteeing data integit7 when data is %eing a!!essed !on!uentl7 %7
$ultiple uses5 ( ta%le lo!& !an %e held in seveal $odes5
Shae lo!&
Shae update lo!&
'/!lusive lo!&
S:(R' LOCF
( shae lo!& lo!&s the ta%le allowing othe uses to onl7 Vue7 %ut not inset# update o delete ows in a ta%le5 Multiple
uses !an pla!e shae lo!&s on the sa$e esou!e at the sa$e ti$e5
'/0
SQL- lo!& ta%le e$p in shae $ode.
S:(R' UAD(T' LOCF
It lo!&s ows that ae to %e updated in a ta%le5 It pe$its othe uses to !on!uentl7 Vue7# inset # update o even lo!&
othe ows in the sa$e ta%le5 It pevents the othe uses fo$ updating the ow that has %een lo!&ed5
'/0
SQL- lo!& ta%le e$p in shae update $ode.
105
'+CLUSI9' LOCF
'/!lusive lo!& is the $ost esti!tive of ta%les lo!&s5 6hen issued %7 an7 use# it allows the othe use to onl7 Vue75 It is
si$ila to shae lo!& %ut onl7 one use !an pla!e e/!lusive lo!& on a ta%le at a ti$e5
'/0
SQL- lo!& ta%le e$p in shae e/!lusive $ode.
NO6(IT
If one use lo!&ed the ta%le without nowait then anothe use t7ing to lo!& the sa$e ta%le then he has to wait until the
use who has initiall7 lo!&ed the ta%le issues a !o$$it o oll%a!& state$ent5 This dela7 !ould %e avoided %7 appending a
nowait !lause in the lo!& ta%le !o$$and5
'/0
SQL- lo!& ta%le e$p in e/!lusive $ode nowait5
D'(DLOCF
( deadlo!& o!!us when two uses have a lo!& ea!h on sepaate o%Ke!t# and the7 want to a!Vuie a lo!& on the ea!h
othe;s o%Ke!t5 6hen this happens# the fist use has to wait fo the se!ond use to elease the lo!&# %ut the se!ond use
will not elease it until the lo!& on the fist use;s o%Ke!t is feed5 In su!h a !ase# oa!le dete!ts the deadlo!& auto$ati!all7
and solves the po%le$ %7 a%oting one of the two tansa!tions5
IND'+'S
Inde/ is t7pi!all7 a listing of &e7wods a!!o$panied %7 the lo!ation of info$ation on a su%Ke!t5 6e !an !eate inde/es
e/pli!itl7 to speed up SQL state$ent e/e!ution on a ta%le5 The inde/ points die!tl7 to the lo!ation of the ows !ontaining
the value5
6:* IND'+'S[
Inde/es ae $ost useful on lage ta%les# on !olu$ns that ae li&el7 to appea in whee !lauses as si$ple eVualit75
106
T*A'S
UniVue inde/
Non"uniVue inde/
)tee inde/
)it$ap inde/
Co$posite inde/
Revese &e7 inde/
Bun!tion"%ased inde/
Do$ain inde/
O%Ke!t inde/
Cluste inde/
Te/t inde/
Inde/ oganiUed ta%le
Aatition inde/
Lo!al inde/
Lo!al pefi/ed
Lo!al non"pefi/ed
8lo%al inde/
8lo%al pefi/ed
8lo%al non"pefi/ed
UNIQU' IND'+
UniVue inde/es guaantee that no two ows of a ta%le have dupli!ate values in the !olu$ns that define the inde/5 UniVue
inde/ is auto$ati!all7 !eated when pi$a7 &e7 o uniVue !onstaint is !eated5
'/0
SQL- !eate uniVue inde/ studNind on student(sno).
NON"UNIQU' IND'+
Non"UniVue inde/es do not i$pose the a%ove esti!tion on the !olu$n values5
'/0
SQL- !eate inde/ studNind on student(sno).
107
)TR'' IND'+
The default t7pe of inde/ used in an oa!le data%ase is the %tee inde/5 ( %tee inde/ is designed to povide %oth apid
a!!ess to individual ows and Vui!& a!!ess to goups of ows within a ange5 The %tee inde/ does this %7 pefo$ing a
su!!ession of value !o$paisons5 'a!h !o$paison eli$inates $an7 of the ows5
'/0
SQL- !eate inde/ studNind on student(sno).
)ITM(A IND'+
This !an %e used fo low !adinalit7 !olu$ns0 that is !olu$ns in whi!h the nu$%e of distin!t values is snall when
!o$paed to the nu$%e of the ows in the ta%le5
'/0
SQL- !eate %it$ap inde/ studNind on student(se/).
COMAOSIT' IND'+
( !o$posite inde/ also !alled a !on!atenated inde/ is an inde/ !eated on $ultiple !olu$ns of a ta%le5 Colu$ns in a
!o$posite inde/ !an appea in an7 ode and need not %e adKa!ent !olu$ns of the ta%le5
'/0
SQL- !eate %it$ap inde/ studNind on student(sno# sna$e).
R'9'RS' F'* IND'+
( evese &e7 inde/ when !o$paed to standad inde/# eveses ea!h %7te of the !olu$n %eing inde/ed while &eeping the
!olu$n ode5 Su!h an aange$ent !an help avoid pefo$an!e degadations in inde/es5
'/0
SQL- !eate inde/ studNind on student(sno# evese).
6e !an e%uild a evese &e7 inde/ into no$al inde/ using the noevese &e7wod5
'/0
SQL- alte inde/ studNind e%uild noevese.
BUNCTION )(S'D IND'+
This will use esult of the fun!tion as &e7 instead of using !olu$n as the value fo the &e75
'/0
108
SQL- !eate inde/ studNind on student(uppe(sna$e)).
IND'+"OR8(NIZ'D T()L'
(n inde/"oganiUed ta%le &eeps its data soted a!!oding to the pi$a7 &e7 !olu$n values fo the ta%le5 Inde/"oganiUed
ta%les stoe thei data as if the entie ta%le was stoed in an inde/5
(n inde/"oganiUed ta%le allows 7ou to stoe the entie ta%le;s data in an inde/5
'/0
SQL- !eate ta%le student (sno nu$%e(1)#sna$e va!ha(23)#s$a&s nu$%e(4) !onstaint
p& pi$a7 &e7(sno) oganiUation inde/.
109
IMA QU'RI'S
2) To find the nth ow of a ta%le
SQL- Sele!t Efo$ e$p whee owid I (sele!t $a/(owid) fo$ e$p whee ownu$ ,I @).
O
SQL- Sele!t Efo$ e$p whee ownu$ ,I @ $inus sele!t Efo$ e$p whee ownu$ ,I 4.
1) To find dupli!ate ows
SQL- Sele!t Efo$ e$p whee owid in (sele!t $a/(owid) fo$ e$p goup %7 e$pno#
ena$e# $g# Ko%# hiedate# !o$$# deptno# sal).
O
SQL- Sele!t e$pno#ena$e#sal#Ko%#hiedate#!o$$ # !ount(E) fo$ e$p goup %7
e$pno#ena$e#sal#Ko%#hiedate#!o$$ having !ount(E) -I2.
4) To delete dupli!ate ows
SQL- Delete e$p whee owid in (sele!t $a/(owid) fo$ e$p goup %7
e$pno#ena$e#$g#Ko%#hiedate#sal#!o$$#deptno).
@) To find the !ount of dupli!ate ows
SQL- Sele!t ena$e# !ount(E) fo$ e$p goup %7 ena$e having !ount(E) -I 2.
C) :ow to displa7 altenative ows in a ta%le[
SQL- sele!t Efo$ e$p whee (owid#3) in (sele!t owid#$od(ownu$#1) fo$ e$p).
110
D) 8etting e$plo7ee details of ea!h depat$ent who is dawing $a/i$u$ sal[
SQL- sele!t Efo$ e$p whee (deptno#sal) in
( sele!t deptno#$a/(sal) fo$ e$p goup %7 deptno).
W) :ow to get nu$%e of e$plo7ees in ea!h depat$ent # in whi!h depat$ent is having $oe than 1C33 e$plo7ees[
SQL- Sele!t deptno#!ount(E) fo$ e$p goup %7 deptno having !ount(E) -1C33.
T) To eset the ti$e to the %eginning of the da7
SQL- Sele!t toN!ha(tun!(s7sdate)#;dd"$on"7777 hh0$i0ss a$;) fo$ dual.
23) To find nth $a/i$u$ sal
SQL- Sele!t Efo$ e$p whee sal in (sele!t $a/(sal) fo$ (sele!t Efo$ e$p ode %7 sal)
whee owu$ ,I C).
111
INTRODUCTION
C:(R(CT'RSTICS
:ighl7 stu!tued# eada%le and a!!essi%le language5
Standad and Aota%le language5
'$%edded language5
I$poved e/e!ution authoit75
23g B'(TUR'S
Opti$iUed !o$pile5
To !hange the opti$iUe settings fo the entie data%ase# set the data%ase paa$ete
ALSQLNOATIMIZ'NL'9'L5 9alid settings ae as follows
3 " No opti$iUation
2 " Modeate opti$iUation
1 " (ggessive opti$iUation
These settings ae also $odifia%le fo the !uent session5
SQL- alte session set plsVlNopti$UeNlevelI1.
Oa!le etains opti$iUe settings on a $odule"%7"$odule %asis5 6hen 7ou e!o$pile a pati!ula $odule with
nondefault settings# the settings will sti!& allowing 7ou to e!o$pile late on using R'US' S'TTIN8S5
SQL- (lte po!edue po! !o$pile plsVlNopti$iUeNlevelI2.
SQL- (lte po!edue po! !o$pile euse settings.
Co$pile"ti$e wanings5
112
Stating with oa!le data%ase 23g elease 2 7ou !an ena%le additional !o$pile"ti$e wanings to help $a&e 7ou
poga$s $oe o%ust5 The !o$pile !an dete!t potential unti$e po%le$s with 7ou !ode# su!h as identif7ing
lines of !ode that will neve %e un5 This po!ess# also &nown as lint c'eckin5
To ena%le these wanings fo the entie data%ase# set the data%ase paa$ete ALSQLN6(RNIN8S5
These settings ae also $odifia%le fo the !uent session5
SQL- alte session set plsVlNwanings I Lena%le0all;.
The a%ove !an %e a!hieved using the %uilt"in pa!&age D)MSN6(RNIN85
Conditional !o$pilation5
Conditional !o$pilation allows the !o$pile to allow to !o$pile sele!ted pats of a poga$ %ased on !onditions
7ou povide with the ]IB die!tive5
Suppot fo non"seVuential !olle!tions in BOR(LL5
I$poved datat7pe suppot5
)a!&ta!e an e/!eption to its line nu$%e5
6hen handling an eo# how !an 7ou find the line nu$%e on whi!h the eo was oiginall7 aised[
In ealie elease# the onl7 wa7 to do this was allow 7ou e/!eption to go unhandled and then view the full eo
ta!e sta!&5
Now 7ou !an !all D)MSNUTILIT*5BORM(TN'RRORN)(CFTR(C' fun!tion to o%tain that sta!& and $anipulate it
poga$$ati!all7 within 7ou poga$5
Set opeatos fo nested ta%les5
Suppot fo egula e/pessions5
Oa!le data%ase 23g suppots the use of egula e/pessions inside AL?SQL !ode via fou new %uilt"in fun!tions5
R'8'+ANLIF'
R'8'+ANINSTR
R'8'+ANSU)STR
R'8'+ANR'AL(C'
Aoga$$e"defined Vuoting $e!hanis$5
Stating with oa!le data%ase 23g elease 2# 7ou !an define 7ou own Vuoting $e!hanis$ fo sting liteals in %oth
SQL and AL?SQL5
113
Use the !haa!tes V;(V followed %7 a single Vuote) to note the poga$$e"defined delie$ete fo 7ou sting
liteal5
'/0
D'CL(R'
v va!ha(23) 0I =!o$pute=.
)'8IN
d%$sNoutput5putNline(V=Ev I E= XX v).
d%$sNoutput5putNline(V=]v I ]= XX v).
'ND.
Output0
v I !o$pute
v I !o$pute
Man7 new %uilt"in pa!&ages5
D)MSNSC:'DUL'R
Repesents a $aKo update to D)MSN>O)5 D)MSNSC:'DUL'R povides $u!h i$poved fun!tionalit7 fo s!heduling
and e/e!uting Ko%s defined via stoed po!edues5
D)MSNCR*ATO
Offes the a%ilit7 to en!7pt and de!7pt !o$$on oa!le datat7pe# in!luding R(6s# )LO)s# and CLO)s5 It also
povides glo%aliUation suppot fo en!7pting data a!oss diffeent !haa!tesets5
D)MSNMONITOR
Aovides an (AI to !ontol additional ta!ing and statisti!s gatheing of sessions5
D)MSN6(RNIN8
Aovides an (AI into the AL?SQL !o$pile wanings $odule# allowing 7ou to ead and !hange settings that !ontol
whi!h wanings ae suppessed# displa7ed# o teated as eos5
ST(ND(RD A(CF(8'
Oa!le has defined in this spe!ial pa!&age5 Oa!le defines Vuite a few identifies in this pa!&age# in!luding %uilt"in
e/!eptions# fun!tions and su%t7pes5
*ou !an efeen!e the %uilt"in fo$ %7 pefi/ing it with ST(ND(RD5
The %asi! unit in an7 AL?SQL poga$ is %lo!&5 (ll AL?SQL poga$s ae !o$posed of %lo!&s whi!h !an o!!u seVuentiall7
o nested5
114
)LOCF STRUCTUR'
De!lae
"" de!laative se!tion
)egin
"" e/e!uta%le se!tion
'/!eption
"" e/!eption se!tion
'nd.
In the a%ove de!laative and e/!eptiona se!tions ae optional5
)LOCF T*A'S
(non7$ous %lo!&s
Na$ed %lo!&s
La%eled %lo!&s
Su%poga$s
Tigges
(NON*MOUS )LOCFS
(non7$ous %lo!&s i$plies %asi! %lo!& stu!tue5
'/0
)'8IN
D%$sNoutput5putNline(LM7 fist poga$;)0
'ND.
L()'L'D )LOCFS
La%eled %lo!&s ae anon7$ous %lo!&s with a la%el whi!h gives a na$e to the %lo!&5
'/0
,,$7N%loo!&--
)'8IN
D%$sNoutput5putNline(LM7 fist poga$;)0
'ND.
115
SU)ARO8R(MS
Su%poga$s ae po!edues and fun!tions5 The7 !an %e stoed in the data%ase as stand"alone o%Ke!ts# as pat of
pa!&age o as $ethods of an o%Ke!t t7pe5
TRI88'RS
Tigges !onsists of a AL?SQL %lo!& that is asso!iated with an event that o!!u in the data%ase5
N'ST'D )LOCFS
( %lo!& !an %e nested within the e/e!uta%le o e/!eption se!tion of an oute %lo!&5
ID'NTIBI'RS
Identifies ae used to na$e AL?SQL o%Ke!ts# su!h as vaia%les# !usos# t7pes and su%poga$s5 Identifies !onsists of a
lette# optionall7 followed %7 an7 seVuen!e of !haa!tes# in!luding lettes# nu$%es# dolla signs# undes!oes# and pound
signs onl75 The $a/i$u$ length fo an identifie is 43 !haa!tes5
QUOT'D ID'NTIBI'RS
If 7ou want to $a&e an identifie !ase sensitive# in!lude !haa!tes su!h as spa!es o use a eseved wod# 7ou !an
en!lose the identifie in dou%le Vuotation $a&s5
'/0
D'CL(R'
^a^ nu$%e 0I C.
^(^ nu$%e 0I D.
)'8IN
d%$sNoutput5putNline(=a I = XX a).
d%$sNoutput5putNline(=( I = XX ().
'ND.
Output0
a I D
( I D
COMM'NTS
116
Co$$ents i$pove eada%ilit7 and $a&e 7ou poga$ $oe undestanda%le5 The7 ae ignoed %7 the AL?SQL !o$pile5
Thee ae two t7pes of !o$$ents availa%le5
Single line !o$$ents
Multiline !o$$ents
SIN8L' LIN' COMM'NTS
( single"line !o$$ent !an stat an7 point on a line with two dashes and !ontinues until the end of the line5
'/0
)'8IN
D%$sNoutput5putNline(Lhello;). "" sa$ple poga$
'ND.
MULTILIN' COMM'NTS
Multiline !o$$ents stat with the ?E deli$ite and ends with E? deli$ite5
'/0
)'8IN
D%$sNoutput5putNline(Lhello;). ?E sa$ple poga$ E?
'ND.
9(RI()L' D'CL'R(TIONS
9aia%les !an %e de!laed in de!laative se!tion of the %lo!&.
'/0
D'CL(R'
a nu$%e.
% nu$%e 0I C.
! nu$%e default D.
CONST(NT D'CL'R(TIONS
To de!lae a !onstant# 7ou in!lude the CONST(NT &e7wod# and 7ou $ust suppl7 a default value5
'/0
D'CL(R'
% !onstant nu$%e 0I C.
! !onstant nu$%e default D.
117
NOT NULL CL(US'
*ou !an also spe!if7 that the vaia%le $ust %e not null5
'/0
D'CL(R'
% !onstant nu$%e not null0I C.
! nu$%e not null default D.
(NC:OR'D D'CL'R(TIONS
AL?SQL offes two &inds of a!hoing5
S!ala an!hoing
Re!od an!hoing
SC(L(R (NC:ORIN8
Use the MT*A' atti%ute to define 7ou vaia%le %ased on ta%le;s !olu$n of so$e othe AL?SQL s!ala vaia%le5
'/0
D'CL(R'
dno dept5deptnoMt7pe.
Su%t7pe tNnu$%e is nu$%e.
a tNnu$%e.
Su%t7pe tNsno is student5snoMt7pe.
9Nsno tNsno.
R'CORD (NC:ORIN8
Use the MRO6T*A' atti%ute to define 7ou e!od stu!tue %ased on a ta%le5
'/0
D'CL(R'
9Ndept deptMowt7pe.
)'N'BITS OB (NC:OR'D D'CL(R(TIONS
S7n!honiUation with data%ase !olu$ns5
No$aliUation of lo!al vaia%les5
ARO8R(MM'R"D'BIN'D T*A'S
118
6ith the SU)T*A' state$ent# AL?SQL allows 7ou to define 7ou own su%t7pes o aliases of pedefined datat7pes#
so$eti$es efeed to as a%sta!t datat7pes5
Thee ae two &inds of su%t7pes5
Constained
Un!onstained
CONSTR(IN'D SU)T*A'
( su%t7pe that esti!ts o !onstains the values no$all7 allowd %7 the datat7pe itself5
'/0
Su%t7pe positive is %ina7Nintege ange 25512@W@G4D@W.
In the a%ove de!laation a vaia%le that is de!laed as positive !an stoe onl7 ingege geate than Ueo even though
%ina7Nintege anges fo$ "12@W@G4D@W55H12@W@G4D@W5
UNCONSTR(IN'D SU)T*A'
( su%t7pe that does not esti!t the values of the oiginal datat7pe in vaia%les de!laed with the su%t7pe5
'/0
Su%t7pe float is nu$%e.
Su%t7pe st is va!ha(233).
st2 st.
D(T(T*A' CON9'RSIONS
AL?SQL !an handle !onvesions %etween diffeent fa$ilies a$ong the datat7pes5
Convesion !an %e done in two wa7s5
'/pli!it !onvesion
I$pli!it !onvesion
'+ALICIT CON9'RSION
This !an %e done using the %uilt"in fun!tions availa%le5
IMALICIT CON9'RSION
AL?SQL will auto$ati!all7 !onvet %etween datat7pe fa$ilies when possi%le5
'/0
119
D'CL(R'
a va!ha(23).
)'8IN
sele!t deptno into a fo$ dept whee dna$eI=(CCOUNTIN8=.
'ND.
In the a%ove vaia%le a is !ha t7pe and deptno is nu$%e t7pe even though# oa!le will auto$ati!all7 !onvets the
nu$ei! data into !ha t7pe assigns to the vaia%le5
AL?SQL !an auto$ati!all7 !onvet %etween
Chaa!tes and nu$%es
Chaa!tes and dates
9(RI()L' SCOA' (ND 9ISI)ILIT*
The s!ope of a vaia%le is the potion of the poga$ in whi!h the vaia%le !an %e a!!essed5 Bo AL?SQL vaia%les# this is
fo$ the vaia%le de!laation until the end of the %lo!&5 6hen a vaia%le goes out of s!ope# the AL?SQL engine will fee the
$e$o7 used to stoe the vaia%le5
The visi%ilit7 of a vaia%le is the potion of the poga$ whee the vaia%le !an %e a!!essed without having to Vualif7 the
efeen!e5 The visi%ilit7 is alwa7s within the s!ope5 If it is out of s!ope# it is not visi%le5
'/20
D'CL(R'
a nu$%e. "" s!ope of a
)'8IN
""""""""
D'CL(R'
% nu$%e. "" s!ope of %
)'8IN
"""""
'ND.
""""""
'ND.
'/10
D'CL(R'
a nu$%e.
% nu$%e.
)'8IN
"" a # % availa%le hee
120
D'CL(R'
% !ha(23).
)'8IN
"" a and !ha t7pe % is availa%le hee
'ND.
"""""
'ND.
'/40
,,$7N%lo!&--
D'CL(R'
a nu$%e.
% nu$%e.
)'8IN
"" a # % availa%le hee
D'CL(R'
% !ha(23).
)'8IN
"" a and !ha t7pe % is availa%le hee
"" nu$%e t7pe % is availa%le using ,,$7N%lo!&--5%
'ND.
""""""
'ND.
AL?SQL CONTROL STRUCTUR'S
AL?SQL has a vaiet7 of !ontol stu!tues that allow 7ou to !ontol the %ehaviou of the %lo!& as it uns5 These stu!tues
in!lude !onditional state$ents and loops5
If"then"else
Case
Case with no else
La%eled !ase
Sea!hed !ase
Si$ple loop
6hile loop
Bo loop
8oto and La%els
121
IB"T:'N"'LS'
S7nta/0
If ,condition1- then
"e-uence o% statements.
'lsif ,condition1- then
"e-uence o% statements.
SS
'lse
"e-uence o% statements.
'nd if.
'/0
D'CL(R'
dno nu$%e(1).
)'8IN
sele!t deptno into dno fo$ dept whee dna$e I =(CCOUNTIN8=.
if dno I 23 then
d%$sNoutput5putNline(=Lo!ation is N'6 *ORF=).
elsif dno I 13 then
d%$sNoutput5putNline(=Lo!ation is D(LL(S=).
elsif dno I 43 then
d%$sNoutput5putNline(=Lo!ation is C:IC(8O=).
else
d%$sNoutput5putNline(=Lo!ation is )OSTON=).
end if.
'ND.
Output0
Lo!ation is N'6 *ORF
C(S'
S7nta/0
Case test/variable
6hen value1 then se-uence o% statements.
6hen value2 then se-uence o% statements.
SS
122
6hen valuen then se-uence o% statements.
'lse se-uence o% statements.
'nd !ase.
'/0
D'CL(R'
dno nu$%e(1).
)'8IN
sele!t deptno into dno fo$ dept whee dna$e I =(CCOUNTIN8=.
!ase dno
when 23 then
d%$sNoutput5putNline(=Lo!ation is N'6 *ORF=).
when 13 then
d%$sNoutput5putNline(=Lo!ation is D(LL(S=).
when 43 then
d%$sNoutput5putNline(=Lo!ation is C:IC(8O=).
else
d%$sNoutput5putNline(=Lo!ation is )OSTON=).
end !ase.
'ND.
Output0
Lo!ation is N'6 *ORF
C(S' 6IT:OUT 'LS'
S7nta/0
Case test/variable
6hen value1 then se-uence o% statements.
6hen value2 then se-uence o% statements.
SS
6hen valuen then se-uence o% statements.
'nd !ase.
'/0
D'CL(R'
dno nu$%e(1).
)'8IN
sele!t deptno into dno fo$ dept whee dna$e I =(CCOUNTIN8=.
!ase dno
when 23 then
123
d%$sNoutput5putNline(=Lo!ation is N'6 *ORF=).
when 13 then
d%$sNoutput5putNline(=Lo!ation is D(LL(S=).
when 43 then
d%$sNoutput5putNline(=Lo!ation is C:IC(8O=).
when @3 then
d%$sNoutput5putNline(=Lo!ation is )OSTON=).
end !ase.
'ND.
Output0
Lo!ation is N'6 *ORF
L()'L'D C(S'
S7nta/0
,,label--
Case test/variable
6hen value1 then se-uence o% statements.
6hen value2 then se-uence o% statements.
SS
6hen valuen then se-uence o% statements.
'nd !ase.
'/0
D'CL(R'
dno nu$%e(1).
)'8IN
sele!t deptno into dno fo$ dept whee dna$e I =(CCOUNTIN8=.
,,$7N!ase--
!ase dno
when 23 then
d%$sNoutput5putNline(=Lo!ation is N'6 *ORF=).
when 13 then
d%$sNoutput5putNline(=Lo!ation is D(LL(S=).
when 43 then
d%$sNoutput5putNline(=Lo!ation is C:IC(8O=).
when @3 then
d%$sNoutput5putNline(=Lo!ation is )OSTON=).
124
end !ase $7N!ase.
'ND.
Output0
Lo!ation is N'6 *ORF
S'(RC:'D C(S'
S7nta/0
Case
6hen ,condition1- then se-uence o% statements.
6hen ,condition2- then se-uence o% statements.
SS
6hen ,conditionn- then se-uence o% statements.
'nd !ase.
'/0
D'CL(R'
dno nu$%e(1).
)'8IN
sele!t deptno into dno fo$ dept whee dna$e I =(CCOUNTIN8=.
!ase dno
when dno I 23 then
d%$sNoutput5putNline(=Lo!ation is N'6 *ORF=).
when dno I 13 then
d%$sNoutput5putNline(=Lo!ation is D(LL(S=).
when dno I 43 then
d%$sNoutput5putNline(=Lo!ation is C:IC(8O=).
when dno I @3 then
d%$sNoutput5putNline(=Lo!ation is )OSTON=).
end !ase.
'ND.
Output0
Lo!ation is N'6 *ORF
SIMAL' LOOA
S7nta/0
125
Loop
"e-uence o% statements.
'/it when ,condition-.
'nd loop.
In the s7nta/ e/it when ,condition- is eVuivalent to
If ,condition- then
'/it.
'nd if.
'/0
D'CL(R'
i nu$%e 0I 2.
)'8IN
loop
d%$sNoutput5putNline(=i I = XX i).
i 0I i H 2.
e/it when i - C.
end loop.
'ND.
Output0
i I 2
i I 1
i I 4
i I @
i I C
6:IL' LOOA
S7nta/0
6hile ,!ondition- loop
"e-uence o% statements.
'nd loop.
'/0
D'CL(R'
i nu$%e 0I 2.
)'8IN
6hile i ,I C loop
126
d%$sNoutput5putNline(=i I = XX i).
i 0I i H 2.
end loop.
'ND.
Output0
i I 2
i I 1
i I 4
i I @
i I C
BOR LOOA
S7nta/0
Bo ,loop_counter_variable- in low_bound55'i'_bound loop
"e-uence o% statements.
'nd loop.
'/20
)'8IN
Bo i in 255C loop
d%$sNoutput5putNline(=i I = XX i).
end loop.
'ND.
Output0
i I 2
i I 1
i I 4
i I @
i I C
'/10
)'8IN
Bo i in evese 255C loop
d%$sNoutput5putNline(=i I = XX i).
end loop.
'ND.
127
Output0
i I C
i I @
i I 4
i I 1
i I 2
NULL ST(T'M'NT
Usuall7 when 7ou wite a state$ent in a poga$# 7ou want it to do so$ething5 Thee ae !ases# howeve# when 7ou want
to tell AL?SQL to do a%solutel7 nothing# and that is whee the NULL !o$es5
The NULL state$ent deos nothing e/!ept pass !ontol to the ne/t e/e!uta%le state$ent5
*ou !an use NULL state$ent in the following situations5
I$poving poga$ eada%ilit75
So$eti$es # it is helpful to avoid an7 a$%iguit7 inheent in an IB state$ent that doesn;t !ove all possi%le !ases5 Bo
e/a$ple# when 7ou wite an IB state$ent# 7ou do not have to in!lude an 'LS' !lause5
Nullif7ing a aised e/!eption5
6hen 7ou don;t want to wite an7 spe!ial !ode to handle an e/!eption# 7ou !an use the NULL state$ent to $a&e sue
that a aised e/!eption halts e/e!ution of the !uent AL?SQL %lo!& %ut does not popagate an7 e/!eptions to
en!losing %lo!&s5
Using null afte a la%el5
In so$e !ases# 7ou !an pai NULL with 8OTO to avoid having to e/e!ute additional state$ents5 Bo e/a$ple# I use a
8OTO state$ent to Vui!&l7 $ove to the end of $7 poga$ if the state of $7 data indi!ates that no futhe po!essing
is eVuied5 )e!ause I do not have to do an7thing at the te$ination of the poga$# I pla!e a NULL state$ent afte
the la%el %e!ause at least one e/e!uta%le state$ent is eVuied thee5 'ven though NULL deos nothing# it is still an
e/e!uta%le state$ent5
8OTO (ND L()'LS
S7nta/0
8oto label.
6hee label is a la%el defined in the AL?SQL %lo!&5 La%els ae en!losed in dou%le angle %a!&ets5 6hen a goto state$ent
is evaluated# !ontol i$$ediatel7 passes to the state$ent identified %7 the la%el5
128
'/0
)'8IN
Bo i in 255C loop
d%$sNoutput5putNline(=i I = XX i).
if i I @ then
goto e/itNloop.
end if.
end loop.
,,e/itNloop--
Null.
'ND.
Output0
i I 2
i I 1
i I 4
i I @
R'STRICTIONS ON 8OTO
It is illegal to %an!h into an inne %lo!&# loop5
(t least one e/e!uta%le state$ent $ust follow5
It is illegal to %an!h into an if state$ent5
It is illegal to %an!h fo$ one if state$ent to anothe if state$ent5
It is illegal to %an!h fo$ e/!eption %lo!& to the !uent %lo!&5
AR(8M(S
Aag$as ae !o$pile die!tives5 The7 seve as instu!tions to the AL?SQL !o$pile5 The !o$pile will a!t on the pag$a
duing the !o$pilation of the %lo!&5
S7nta/0
AR8(M( instu!tionNtoN!o$pile5
AL?SQL offes seveal pag$as0
(UTONOMOUSNTR(NS(CTION
'+C'ATIONNINIT
129
R'STRICTNR'B'R'NC'S
S'RI(LL*NR'US()L'
SU)ARO8R(MS
In paa$ete will a!t as pl?sVl !onstant5
130
Out paa$ete will a!t as unintialiUed vaia%le5
In out paa$ete will a!t as initialiUed vaia%le5
In paa$etes %7 default !all %7 efeen!e whee as out and in out !all %7 value5
6e !an !o$%ine positional and na$e notation %ut positional notation !an not %e followed %7 the na$e notation5
6e !an not use L!all; without paa$etes5
6e !an not use !all with out and in out paa$etes5
Out and In Out paa$etes !an not have default values5
6e !an not use Le/e!; with out o in out paa$etes5
Default Aaa$etes will not allow in the %eginning and $iddle5
'/0
po!edue p(a in nu$%e default C# % in nu$%e default D# ! in nu$%e default W) \
valild
po!edue p(a in nu$%e# % in nu$%e default D# ! in nu$%e default W) \ valild
po!edue p(a in nu$%e# % in nu$%e# ! in nu$%e default W) \ valild
po!edue p(a in nu$%e# % in nu$%e default D# ! in nu$%e) \ invalild
po!edue p(a in nu$%e default C# % in nu$%e default D# ! in nu$%e) \ invalild
po!edue p(a in nu$%e default C# % in nu$%e# ! in nu$%e) \ invalild
If the su%poga$ !ontains a vaia%le with the sa$e na$e as the !olu$n na$e of the ta%le then use the dot
$ethod to diffeentiate (p5sal)5
If an7 su%poga$ was !alled# on!e the !all was !o$pleted then the values of fo$al
paa$etes ae !opied to the a!tual paa$etes5
The !op7ing fo$ fo$al to a!tual !an %e esti!ted %7 issuing no!op7 Vualifie5
Su%poga$s !an %e lo!al5
6hen a stoed su%poga$ is !eated# it is stoed in the data di!tiona75
The su%poga$ is stoed in !o$pile fo$ whi!h is &nown as p"!ode in addition to the sou!e te/t5
The p"!ode has all of the efeen!es in the su%poga$ evaluated# and the sou!e !ode is tanslated into a fo$
that is easil7 eada%le %7 pl?sVl engine5
6hen the su%poga$ is !alled# the p"!ode is ead fo$ the dis&# if ne!essa7# and e/e!uted5
On!e it eads fo$ the dis&# the p"!ode is stoed in the shaed pool potion of the s7ste$ glo%al aea (S8()#
whee it !an %e a!!essed %7 $ultiple uses as needed5
Li&e all of the !ontents of the shaed pool# p"!ode is aged out of the shaed pool a!!oding to a least e!entl7
used (LRU) algoith$5
Lo!al su%poga$s $ust %e de!laed in the de!laative se!tion of pl?sVl %lo!& and !alled fo$ the e/e!uta%le
se!tion5
Su%poga$s !an not have the de!laative se!tion sepaatel75
Stoed su%poga$s !an have lo!al su%poga$s.
131
(n7 lo!al su%poga$ $ust %e de!laed at the end of the de!laative se!tion5
'/0
CR'(T' OR R'AL(C' AROC'DUR' S(MAL'(a in nu$%e#% out nu$%e#! in out nu$%e) is
)'8IN
d%$sNoutput5putNline(=(fte !all=).
d%$sNoutput5putNline(=a I = XX a XX= % I = XX % XX = ! I = XX !).
% 0I 23.
! 0I 13.
d%$sNoutput5putNline(=(fte assign$ent=).
d%$sNoutput5putNline(=a I = XX a XX= % I = XX % XX = ! I = XX !).
'ND S(MAL'.
D'CL(R'
v2 nu$%e 0I @.
v1 nu$%e 0I C.
v4 nu$%e 0I D.
)'8IN
d%$sNoutput5putNline(=)efoe !all=).
d%$sNoutput5putNline(=v2 I = XX v2 XX = v1 I = XX v1 XX = v4 I = XX v4).
sa$ple(v2#v1#v4).
d%$sNoutput5putNline(=(fte !o$pletion of !all=).
d%$sNoutput5putNline(=v2 I = XX v2 XX = v1 I = XX v1 XX = v4 I = XX v4).
'ND.
Output0
)efoe !all
v2 I @ v1 I C v4 I D
(fte !all
a I @ % I ! I D
(fte assign$ent
a I @ % I 23 ! I 13
(fte !o$pletion of !all
v2 I @ v1 I 23 v4 I 13
C(LL )* R'B'R'NC' (ND C(LL )* 9(LU'
6hen paa$ete passed %7 efeen!e# a pointe to the a!tual paa$ete is passed to the
132
!oesponding fo$al paa$ete5
6hen paa$ete passed %7 value it !opies the value of the a!tual paa$ete to the fo$al paa$ete5
Call %7 efeen!e is faste than the !all %7 value %e!ause it avoids the !op7ing5
R'STRICTIONS ON BORM(L A(R(M'T'RS
)7 de!laing with spe!ified siUe in a!tual paa$etes5
)7 de!laing fo$al paa$etes with Mt7pe spe!ifie5
SU)ARO8R(MS O9'RLO(DIN8
Aossi%le with diffeent nu$%e of paa$etes5
Aossi%le with diffeent t7pes of data5
Aossi%le with sa$e t7pe with o%Ke!ts5
Can not %e possi%le with diffeent t7pes of $odes5
6e !an oveload lo!al su%poga$s also5
'/0
SQL- !eate o epla!e t7pe t2 as o%Ke!t(a nu$%e).?
SQL- !eate o epla!e t7pe t2 as o%Ke!t(a nu$%e).?
D'CL(R'
i t2 0I t2(C).
K t1 0I t1(C).
AROC'DUR' A($ t2) IS
)'8IN
d%$sNoutput5putNline(=a I = XX $5a).
'ND A.
AROC'DUR' A(n t1) IS
)'8IN
d%$sNoutput5putNline(=% I = XX n5%).
'ND A.
AROC'DUR' ARODUCT(a nu$%e#% nu$%e) IS
)'8IN
d%$sNoutput5putNline(=Aodu!t of a#% I = XX a E %).
'ND ARODUCT.
AROC'DUR' ARODUCT(a nu$%e#% nu$%e#! nu$%e) IS
)'8IN
133
d%$sNoutput5putNline(=Aodu!t of a#% I = XX a E % E !).
'ND ARODUCT.
)'8IN
p(i).
p(K).
podu!t(@#C).
podu!t(@#C#D).
'ND.
Output0
a I C
% I C
Aodu!t of a#% I 13
Aodu!t of a#% I 213
AROC'DUR'S 9'RS'S BUNCTIONS
Ao!edues $a7 etun though out and in out paa$etes whee as fun!tion $ust etun5
Ao!edues !an not have etun !lause whee as fun!tions $ust5
6e !an use !all state$ent die!tl7 fo e/e!uting po!edue whee as we need to de!lae a vaia%le in !ase of
fun!tions5
Bun!tions !an use in sele!t state$ents whee as po!edues !an not5
Bun!tions !an !all fo$ epots envion$ent whee as po!edues !an not5
6e !an use e/e! fo e/e!uting po!edues whee as fun!tions !an not5
Bun!tion !an %e used in d%$sNoutput whee as po!edue !an not5
STOR' 9'RS'S LOC(L SU)ARO8R(MS
The stoed su%poga$ is stoed in !o$piled p"!ode in the data%ase# when the po!edue is !alled it does not
have to %e !o$piled5
The lo!al su%poga$ is !o$piled as pat of its !ontaining %lo!&5 If the !ontaining %lo!&
is anon7$ous and is un $ultiple ti$es# the su%poga$ has to %e !o$piled ea!h ti$e5
Stoed su%poga$s !an %e !alled fo$ an7 %lo!& su%$itted %7 a use who has e/e!ute pivileges on the
su%poga$5
Lo!al su%poga$s !an %e !alled onl7 fo$ the %lo!& !ontaining the su%poga$5
)7 &eeping the stoed su%poga$ !ode sepaate fo$ the !alling %lo!&# the !alling %lo!& is shote and easie to
undestand5
134
The lo!al su%poga$ and the !alling %lo!& ae one and the sa$e# whi!h !an lead to pat
!onfusion5 If a !hange to the !alling %lo!& is $ade# the su%poga$ will %e e!o$piled as
of the e!o$pilation of the !ontaining %lo!&5
The !o$piled p"!ode !an %e pinned in the shaed pool using the D)MSNS:(R'DNAOOL
Aa!&age5 This !an i$pove pefo$an!e5
Lo!al su%poga$s !annot %e pinned in the shaed pool %7 the$selves5
Stand alone stoed su%poga$s !an not %e oveloaded# %ut pa!&aged su%poga$s !an
%e oveloaded within the sa$e pa!&age5
Lo!al su%poga$s !an %e oveloaded within the sa$e %lo!&5
'/20
CR'(T' OR R'AL(C' AROC'DUR' A IS
)'8IN
d%$sNoutput5putNline(=Stoed su%poga$=).
'ND.
Output0
SQL- e/e! p
Stoed su%poga$
'/10
D'CL(R'
AROC'DUR' A IS
)'8IN
d%$sNoutput5putNline(=Stoed su%poga$=).
'ND.
)'8IN
p.
'ND.
Output0
Stoed su%poga$
COMAILIN8 SU)ARO8R(MS
SQL- (lte po!edue A2 !o$pile.
SQL- (lte fun!tion B2 !o$pile.
SU)ARO8R(MS D'A'ND'CI'S
( stoed su%poga$ is $a&ed as invalid in the data di!tiona7 if it has !o$pile eos5
135
( stoed su%poga$ !an also %e!o$e invalid if a DDL opeation is pefo$ed on one of its dependent o%Ke!ts5
If a su%poga$ is invalidated# the AL?SQL engine will auto$ati!all7 atte$pt to e!o$pile in the ne/t ti$e it is
!alled5
If we have two po!edues li&e A2 and A1 in whi!h A2 depends on A15 If we !o$pile A1 then A2 is invalidated5
SU)ARO8R(MS D'A'ND'NCI'S IN R'MOT' D(T()(S'S
6e will !all e$ote su%poga$ using !onne!t sting li&e A2_9IS.
If we have two po!edues li&e A2 and A1 in whi!h A2 depends on A1 %ut A1 was in e$ote data%ase5 If we
!o$pile A1 it will not invalidate A2 i$$ediatel7 %e!ause the data di!tiona7 does not ta!& e$ote dependen!ies5
Instead the validit7 of e$ote o%Ke!ts is !he!&ed at unti$e5 6hen A2 is !alled# the e$ote data di!tiona7 is
Vueied to dete$ine the status of A15
A2 and A1 ae !o$paed to see it A2 needs to %e e!o$piled# thee ae two diffeent $ethods of !o$paision
Ti$esta$p Model
Signatue Model
TIM'ST(MA MOD'L
This is the default $odel used %7 oa!le5
6ith this $odel# the ti$esta$ps of the last $odifi!ations of the two o%Ke!ts ae
!o$paed5
The last_ddl_time field of user_ob!ects !ontains the ti$esta$p5
If the %ase o%Ke!t has a newe ti$esta$p than the dependent o%Ke!t# the dependent
o%Ke!t will %e e!o$piled5
ISSU'S 6IT: T:IS MOD'L
If the o%Ke!ts ae in diffeent ti$e Uones# the !o$paison is invalid5
6hen A2 is in a !lient side AL?SQL engine su!h as oa!le fo$s# in this !ase it $a7 not possi%le to e!o$pile A2#
%e!ause the sou!e fo it $a7 not %e in!luded with the fo$s5
SI8N(TUR' MOD'L
6hen a po!edue is !eated# a signatue is stoed in the data di!tiona7 in addition to the p"!ode5
The signatue en!odes the t7pes and ode of the paa$etes5
136
6hen A2 is !o$piled the fist ti$e# the signatue of A1 is in!luded5 Thus# A2 onl7 needs to e!o$piled when the
signatue of A1 !hanges5
In ode to use the signatue $odel# the paa$ete R'MOT'ND'A'ND'NCI'SNMOD' $ust %e set to SI8N(TUR'5 This
is a paa$ete in the data%ase initialiUation file5
T:R'' 6(*S OB S'TTIN8 T:IS MOD'
(dd the line R'MOT'ND'A'ND'NCI'SNMOD'ISI8N(TUR' to the data%ase initialiUation file5 The ne/t ti$e the
data%ase is stated# the $ode will %e set to SI8N(TUR' fo all sessions5
(lte s7ste$ set e$oteNdependen!iesN$ode I signatue.
This will affe!t the entie data%ase (all sessions) fo$ the ti$e the state$ent is issued5
*ou $ust have the (LT'R S*ST'M pivilege to issue this !o$$and5
(lte session set e$oteNdependen!iesN$ode I signatue.
This will onl7 affe!t 7ou session
ISSU'S 6IT: T:IS MOD'L
Signatues don;t get $odified if the default values of fo$al paa$etes ae !hanged5
Suppose A1 has a default value fo one of its paa$etes# and A2 is using this default
value5 If the default in the spe!ifi!ation fo A1 is !hanged# A2 will not %e e!o$piled
%7 default5 The old value fo the default paa$ete will still %e used until A2 is $anuall7
e!o$piled5
If A2 is !alling a pa!&aged po!edue A1# and a new oveloaded vesion of A1 is added to
the e$ote pa!&age# the signatue is not !hanged5 A2 will still use the old vesion
(not the new oveloaded one) until A2 is e!o$piled $anuall75
BOR6(RD D'CL'R(TION
)efoe going to use the po!edue in an7 othe su%poga$ o othe %lo!& # 7ou $ust de!lae the potot7pe of the
po!edue in de!laative se!tion5
'/20
D'CL(R'
AROC'DUR' A2 IS
)'8IN
d%$sNoutput5putNline(=Bo$ po!edue p2=).
p1.
137
'ND A2.
AROC'DUR' A1 IS
)'8IN
d%$sNoutput5putNline(=Bo$ po!edue p1=).
p4.
'ND A1.
AROC'DUR' A4 IS
)'8IN
d%$sNoutput5putNline(=Bo$ po!edue p4=).
'ND A4.
)'8IN
p2.
'ND.
Output0
p1.
E
'RROR at line C0
OR("3DCC30 line C# !olu$n 20
ALS"334240 =A1= not de!laed in this s!ope
OR("3DCC30 line C# !olu$n 20
AL?SQL0 State$ent ignoed
OR("3DCC30 line 23# !olu$n 20
ALS"334240 =A4= not de!laed in this s!ope
OR("3DCC30 line 23# !olu$n 20
AL?SQL0 State$ent ignoed
'/10
D'CL(R'
AROC'DUR' A1. "" fowad de!laation
AROC'DUR' A4.
AROC'DUR' A2 IS
)'8IN
d%$sNoutput5putNline(=Bo$ po!edue p2=).
p1.
'ND A2.
AROC'DUR' A1 IS
)'8IN
d%$sNoutput5putNline(=Bo$ po!edue p1=).
p4.
'ND A1.
138
AROC'DUR' A4 IS
)'8IN
d%$sNoutput5putNline(=Bo$ po!edue p4=).
'ND A4.
)'8IN
p2.
'ND.
Output0
Bo$ po!edue p2
Bo$ po!edue p1
Bo$ po!edue p4
ARI9IL'8'S (ND STOR'D SU)ARO8R(MS
'+'CUT' AR'9IL'8'
Bo stoed su%poga$s and pa!&ages the elevant pivilege is '+'CUT'5
If use ( had the po!edue !alled e$pNpo! then use ( gants e/e!ute pivilege on po!edue to use ) with
the following !o$$and5
8ant e/e!ute on e$pNpo! to use )5
Then use ) !an un the po!edue %7 issuing
'/e! use (5e$pNpo!
use( !eated the following po!edue
CR'(T' OR R'AL(C' AROC'DUR' A IS
!uso is sele!t Efo$ student2.
)'8IN
fo v in ! loop
inset into student1 values(v5no#v5na$e#v5$a&s).
end loop.
'ND A.
use( ganted e/e!ute pivilege to use) using
SQL- gant e/e!ute on p to use)
Then use) e/e!uted the po!edue
SQL- '/e! use(5p
If suppose use) also having student1 ta%le then whi!h ta%le will populate whethe use(;s o use);s5
139
The answe is use(;s student1 ta%le onl7 %e!ause %7 default the po!edue will e/e!ute unde the pivlige set of its
owne5
The a%ove po!edue is &nown as define;s po!edue5
:O6 TO AOAUL(T' US'R );s T()L'
Oa!le intodu!es Invo&e;s and Define;s ights5
)7 default it will use the define;s ights5
(n invo&e;s ights outine !an %e !eated %7 using (UT:ID !lause to populate the use);s ta%le5
It is valid fo stand"alone su%poga$s# pa!&age spe!ifi!ations# and o%Ke!t t7pe spe!ifi!ations onl75
use( !eated the following po!edue
CR'(T' OR R'AL(C' AROC'DUR' A
(UT:ID CURR'NTNUS'R IS
!uso is sele!t Efo$ student2.
)'8IN
fo v in ! loop
inset into student1 values(v5no#v5na$e#v5$a&s).
end loop.
'ND A.
Then gant e/e!ute pivilege on p to use)5
'/e!uting the po!edue %7 use)# whi!h populates use);s ta%le5
The a%ove po!edue is !alled invo&e;s po!edue5
Instead of !uentNuse of authid !lause# if 7ou use define then it will %e !alled define; po!edue5
STOR'D SU)ARO8R(MS (ND ROL'S
we have two uses sað and sudha in whi!h sað has student ta%le and sudha does not5
Sudha is going to !eate a po!edue %ased on student ta%le owned %7 sað5 )efoe doing this sað $ust gant the
pe$issions on this ta%le to sudha5
SQL- !onn sað?sað
SQL- gant all on student to sudha.
then sudha !an !eate po!edue
SQL- !onn sudha?sudha
CR'(T' OR R'AL(C' AROC'DUR' A IS
!uso ! is sele!t Efo$ sað5student.
140
)'8IN
fo v in ! loop
d%$sNoutput5putNline(LNo I L XX v5no).
end loop.
'ND A.
hee po!edue will %e !eated5
If the sa$e pivilege was ganted though a ole it wont !eate the po!edue5
'/a$ine the following !ode
SQL- !onn sað?sað
SQL- !eate ole saðNole.
SQL- gant all on student to saðNole.
SQL- gant saðNole to sudha.
then !onn sudha?sudha
CR'(T' OR R'AL(C' AROC'DUR' A IS
!uso ! is sele!t Efo$ sað5student.
)'8IN
fo v in ! loop
d%$sNoutput5putNline(LNo I L XX v5no).
end loop.
'ND A.
The a%ove !ode will aise eo instead of !eating po!edue 5
This is %e!ause of eal7 %inding whi!h AL?SQL uses %7 default in whi!h efeen!es ae evaluated in !o$pile ti$e %ut
when 7ou ae using a ole this will affe!t i$$ediatel75
ISSU'S 6IT: IN9OF'R;S RI8:TS
In an invo&e;s ights outine# e/tenal efeen!es in SQL state$ents will %e esolved using the !alle;s pivilege
set5
)ut efeen!es in AL?SQL state$ents ae still esolved unde the owne;s pivilege set5
TRI88'RS# 9I'6S (ND IN9OF'R;S RI8:TS
141
( data%ase tigge will alwa7s %e e/e!uted with define;s ights and will e/e!ute unde the pivilege set of the
s!he$a that owns the tiggeing ta%le5
This is also tue fo AL?SQL fun!tion that is !alled fo$ a view5 In this !ase# the fun!tion will e/e!ute unde the
pivilege set of the view;s owne5
9iew
( view is a vitual ta%le that !onsists of !olu$ns fo$ one o $oe ta%les5 Though it is si$ila to a
ta%le# it is stoed in the data%ase5 It is a Vue7 stoed as an o%Ke!t5 :en!e# a view is an o%Ke!t that
deives its data fo$ one o $oe ta%les5
'/a$ple
CR'(T' 9I'6 vwCusto$e
(S
S'L'CT Custo$eId# Co$pan7 Na$e# Ahone
BROM Custo$es
142
A(CF(8'S
The fist ti$e a pa!&aged su%poga$ is !alled o an7 efeen!e to a pa!&aged vaia%le o t7pe is $ade# the
pa!&age is instantiated5
'a!h session will have its own !op7 of pa!&aged vaia%les# ensuing that two sessions e/e!uting su%poga$s in
the sa$e pa!&age use diffeent $e$o7 lo!ations5
In $an7 !ases initialiUation needs to %e un the fist ti$e the pa!&age is instantiated within a session5 This !an %e
done %7 adding initialiUation se!tion to the pa!&age %od7 afte all the o%Ke!ts5
Aa!&ages ae stoed in the data di!tiona7 and !an not %e lo!al5
Aa!&aged su%poga$s has an advantage ove stand alone su%poga$5
6hen eve an7 efeen!e to pa!&age# the whole pa!&age p"!ode was stoed in shaed pool of S8(5
Aa!&age $a7 have lo!al su%poga$s5
COMAILIN8 A(CF(8'S
(lte pa!&age AF8 !o$pile.
(lte pa!&age AF8 !o$pile spe!ifi!ation.
(lte pa!&age AF8 !o$pile %od7.
A(CF(8' D'A'ND'NCI'S
The pa!&age %od7 depends on the so$e o%Ke!ts and the pa!&age heade5
The pa!&age heade does not depend on the pa!&age %od7# whi!h is an advantage of pa!&ages5
6e !an !hange the pa!&age %od7 with out !hanging the heade5
A(CF(8' RUNTIM' ST(T'
Aa!&age unti$e state is diffe fo the following pa!&ages5
Seiall7 eusa%le pa!&ages
Non seiall7 eusa%le pa!&ages
S'RI(LL* R'US()L' A(CF(8'S
143
To fo!e the oa!le to use seiall7 eusa%le vesion then in!lude AR(8M( S'RI(LL*NR'US()L' in %oth pa!&age spe!
and %od75
'/a$ine the following pa!&age
!eate o epla!e pa!&age p&g is
pag$a seiall7Neusa%le.
po!edue e$pNpo!.
end p&g.
!eate o epla!e pa!&age %od7 p&g is
pag$a seiall7Neusa%le.
!uso ! is sele!t ena$e fo$ e$p.
po!edue e$pNpo! is
vNena$e e$p5ena$eMt7pe.
vNflag %oolean 0I tue.
vNnu$ows nu$%e 0I 3.
%egin
if not !Misopen then
open !.
end if.
while vNflag loop
fet!h ! into vNena$e.
vNnu$ows 0I vNnu$ows H 2.
if vNnu$ows I C then
vNflag 0I false.
end if.
d%$sNoutput5putNline(='na$e I = XX vNena$e).
end loop.
end e$pNpo!.
end p&g.
SQL- e/e! p&g5e$pNpo!
'na$e I SMIT:
'na$e I (LL'N
'na$e I 6(RD
'na$e I >ON'S
144
'na$e I M(RTIN
SQL- e/e! p&g5e$pNpo!
'na$e I SMIT:
'na$e I (LL'N
'na$e I 6(RD
'na$e I >ON'S
'na$e I M(RTIN
The a%ove pa!&age displa7s the sa$e output fo ea!h e/e!ution even though the !uso is not !losed5
)e!ause the seiall7 eusa%le vesion esets the state of the !uso ea!h ti$e it was !alled5
NON S'RI(LL * R'US()L' A(CF(8'S
This is the default vesion used %7 the oa!le5
'/a$ine the following pa!&age
!eate o epla!e pa!&age p&g is
po!edue e$pNpo!.
end p&g.
!eate o epla!e pa!&age %od7 p&g is
!uso ! is sele!t ena$e fo$ e$p.
po!edue e$pNpo! is
vNena$e e$p5ena$eMt7pe.
vNflag %oolean 0I tue.
vNnu$ows nu$%e 0I 3.
%egin
if not !Misopen then
open !.
end if.
while vNflag loop
fet!h ! into vNena$e.
vNnu$ows 0I vNnu$ows H 2.
if vNnu$ows I C then
vNflag 0I false.
145
end if.
d%$sNoutput5putNline(='na$e I = XX vNena$e).
end loop.
end e$pNpo!.
end p&g.
SQL- e/e! p&g5e$pNpo!
'na$e I SMIT:
'na$e I (LL'N
'na$e I 6(RD
'na$e I >ON'S
'na$e I M(RTIN
SQL- e/e! p&g5e$pNpo!
'na$e I )L(F'
'na$e I CL(RF
'na$e I SCOTT
'na$e I FIN8
'na$e I TURN'R
The a%ove pa!&age displa7s the diffeent output fo ea!h e/e!ution even though the !uso is not !losed5
)e!ause the non"seiall7 eusa%le vesion e$ains the state of the !uso ove data%ase !alls5
D'A'ND'NCI'S OB A(CF(8' RUNTIM' ST(T'
Dependen!ies !an e/ists %etween pa!&age state and anon7$ous %lo!&s5
'/a$ine the following poga$
Ceate this pa!&age in fist session
!eate o epla!e pa!&age p&g is
v nu$%e 0I C.
po!edue p.
end p&g.
!eate o epla!e pa!&age %od7 p&g is
po!edue p is
146
%egin
d%$sNoutput5putNline(=v I = XX v).
v 0I 23.
d%$sNoutput5putNline(=v I = XX v).
end p.
end p&g.
Conne!t to se!ond session
Run the following !ode
%egin
p&g5p.
end.
The a%ove !ode wil wo&5
8o %a!& to fist session and e!eate the pa!&age using !eate5
Then !onne!t to se!ond session and un the following !ode again5
%egin
p&g5p.
end.
This a%ove !ode will not wo& %e!ause of the following5
The anon7$ous %lo!& depends on p&g5 This is !o$pile ti$e dependen!75
Thee is also a unti$e dependen!7 on the pa!&aged vaia%les# sin!e ea!h session has its own !op7 of pa!&aged
vaia%les5
Thus when p&g is e!o$piled the unti$e dependen!7 is followed# whi!h invalidates the %lo!& and aises the
oa!le eo5
Runti$e dependen!ies e/ist onl7 on pa!&age state5 This in!ludes vaia%les and !usos de!laed in a pa!&age5
If the pa!&age had no glo%al vaia%les# the se!ond e/e!ution of the anon7$ous %lo!& would have su!!eeded5
D(T( MOD'L BOR SU)ARO8R(MS (ND A(CF(8'S
UseNo%Ke!ts
UseNsou!e
UseNeos
147
CURSORS
Cursor is a pointe to $e$o7 lo!ation whi!h is !alled as conte&t area whi!h !ontains the info$ation ne!essa7 fo
po!essing# in!luding the nu$%e of ows po!essed %7 the state$ent# a pointe to the pased epesentation of the
state$ent# and the active set whi!h is the set of ows etuned %7 the Vue75
Cuso !ontains two pats
:eade
)od7
:eade in!ludes !uso na$e# an7 paa$etes and the t7pe of data %eing loaded5
)od7 in!ludes the sele!t state$ent5
'/0
Cuso !(dno in nu$%e) etun deptMowt7pe is sele!t Efo$ dept.
148
In the a%ove
:eade \ !uso !(dno in nu$%e) etun deptMowt7pe
)od7 \ sele!t Efo$ dept
CURSOR T*A'S
I$pli!it (SQL)
'/pli!it
Aaa$eteiUed !usos
R'B !usos
CURSOR ST(8'S
Open
Bet!h
Close
CURSOR (TTRI)UT'S
Mfound
Mnotfound
Mow!ount
Misopen
M%ul&Now!ount
M%ul&Ne/!eptions
CURSOR D'CL'R(TION
S7nta/0
Cuso ,cursor_name- is select statement.
'/0
Cuso ! is sele!t Efo$ dept.
CURSOR LOOAS
Si$ple loop
6hile loop
Bo loop
SIMAL' LOOA
149
S7nta/0
Loop
Bet!h ,cursor_name- into ,record_variable-.
'/it when ,cursor_name- M notfound.
,statements-.
'nd loop.
'/0
D'CL(R'
!uso ! is sele!t E fo$ student.
vNstud studentMowt7pe.
)'8IN
open !.
loop
fet!h ! into vNstud.
e/it when !Mnotfound.
d%$sNoutput5putNline(=Na$e I = XX vNstud5na$e).
end loop.
!lose !.
'ND.
Output0
Na$e I sað
Na$e I sinu
Na$e I satish
Na$e I sudha
6:IL' LOOA
S7nta/0
6hile ,cursor_name- M found loop
Bet!h ,cursor_name- into ,record_variable-.
,statements-.
'nd loop.
'/0
D'CL(R'
!uso ! is sele!t E fo$ student.
vNstud studentMowt7pe.
)'8IN
150
open !.
fet!h ! into vNstud.
while !Mfound loop
fet!h ! into vNstud.
d%$sNoutput5putNline(=Na$e I = XX vNstud5na$e).
end loop.
!lose !.
'ND.
Output0
Na$e I sað
Na$e I sinu
Na$e I satish
Na$e I sudha
BOR LOOA
S7nta/0
fo ,record_variable- in ,cursor_name- loop
,statements-.
'nd loop.
'/0
D'CL(R'
!uso ! is sele!t E fo$ student.
)'8IN
fo vNstud in ! loop
d%$sNoutput5putNline(=Na$e I = XX vNstud5na$e).
end loop.
'ND.
Output0
Na$e I sað
Na$e I sinu
Na$e I satish
Na$e I sudha
A(R(M'T(RIZ'D CURSORS
151
This was used when 7ou ae going to use the !uso in $oe than one pla!e with diffeent values fo the sa$e
whee !lause5
Cuso paa$etes $ust %e in $ode5
Cuso paa$etes $a7 have default values5
The s!ope of !uso paa$ete is within the sele!t state$ent5
'/0
D'CL(R'
!uso !(dno in nu$%e) is sele!t E fo$ dept whee deptno I dno.
vNdept deptMowt7pe.
)'8IN
open !(13).
loop
fet!h ! into vNdept.
e/it when !Mnotfound.
d%$sNoutput5putNline(=Dna$e I = XX vNdept5dna$e XX = Lo! I = XX vNdept5lo!).
end loop.
!lose !.
'ND.
Output0
Dna$e I R'S'(RC: Lo! I D(LL(S
A(CF(8'D CURSORS 6IT: :'(D'R IN SA'C (ND )OD* IN A(CF(8' )OD*
!usos de!laed in pa!&ages will not !lose auto$ati!all75
In pa!&aged !usos 7ou !an $odif7 the sele!t state$ent without $a&ing an7 !hanges to the !uso heade in the
pa!&age spe!ifi!ation5
*ou !an use dot $ethod fo pa!&aged !usos li&e p&g5!
Aa!&aged !usos with $ust %e defined in the pa!&age %od7 itself# and then use it as glo%al fo the pa!&age5
*ou !an not define the pa!&aged !uso in an7 su%poga$s5
Cuso de!laation in pa!&age with out %od7 needs the etun !lause5
R'B CURSORS (ND CURSOR 9(RI()L'S
This is un!onstained !uso whi!h will etun diffeent t7pes depends upon the use input5
Ref !usos !an not %e !losed i$pli!itl75
152
Ref !uso with etun t7pe is !alled stron cursor5
Ref !uso with out etun t7pe is !alled weak cursor5
Cuso vaia%les !an %e assigned fo$ one to anothe5
*ou !an de!lae a !uso vaia%le in one s!ope and assign anothe !uso vaia%le with diffeent s!ope# then 7ou
!an use the !uso vaia%le even though the assigned !uso vaia%le goes out of s!ope5
Cuso vaia%les !an %e passed as a paa$etes to the su%poga$s5
Cuso vaia%les $odes ae in o out o in out5
Cuso vaia%les !an not %e de!laed in pa!&age spe! and pa!&age %od7 (e/!luding su%poga$s)5
*ou !an de!lae ef !uso t7pe in pa!&age spe! as well as %od75
*ou !an de!lae ef !uso t7pes in lo!al su%poga$s o anon7$ous %lo!&s5
*ou !an not use e$ote po!edue !alls to pass !uso vaia%les fo$ one seve to anothe5
Cuso vaia%les !an not use fo update !lause5
*ou !an not assign nulls to !uso vaia%les5
*ou !an not !o$pae !uso vaia%les fo eVualit7# ineVualit7 and nullit75
'/0
CR'(T' OR R'AL(C' AROC'DUR' R'BNCURSOR(T()L'NN(M' IN 9(RC:(R) IS
t7pe t is ef !uso.
! t.
vNdept deptMowt7pe.
t7pe is e!od(ena$e e$p5ena$eMt7pe#Ko% e$p5Ko%Mt7pe#sal e$p5salMt7pe).
vNe$p .
vNstud student5na$eMt7pe.
)'8IN
if ta%leNna$e I =D'AT= then
open ! fo sele!t E fo$ dept.
elsif ta%leNna$e I ='MA= then
open ! fo sele!t ena$e#Ko%#sal fo$ e$p.
elsif ta%leNna$e I =STUD'NT= then
open ! fo sele!t na$e fo$ student.
end if.
loop
if ta%leNna$e I =D'AT= then
fet!h ! into vNdept.
e/it when !Mnotfound.
d%$sNoutput5putNline(=Deptno I = XX vNdept5deptno XX = Dna$e I = XX
vNdept5dna$e XX = Lo! I = XX vNdept5lo!).
153
elsif ta%leNna$e I ='MA= then
fet!h ! into vNe$p.
e/it when !Mnotfound.
d%$sNoutput5putNline(='na$e I = XX vNe$p5ena$e XX = >o% I = XX vNe$p5Ko% XX = Sal
I = XX vNe$p5sal).
elsif ta%leNna$e I =STUD'NT= then
fet!h ! into vNstud.
e/it when !Mnotfound.
d%$sNoutput5putNline(=Na$e I = XX vNstud).
end if.
end loop.
!lose !.
'ND.
Output0
SQL- e/e! efN!uso(=D'AT=)
Deptno I 23 Dna$e I (CCOUNTIN8 Lo! I N'6 *ORF
Deptno I 13 Dna$e I R'S'(RC: Lo! I D(LL(S
Deptno I 43 Dna$e I S(L'S Lo! I C:IC(8O
Deptno I @3 Dna$e I OA'R(TIONS Lo! I )OSTON
SQL- e/e! efN!uso(='MA=)
'na$e I SMIT: >o% I CL'RF Sal I G33
'na$e I (LL'N >o% I S(L'SM(N Sal I 2D33
'na$e I 6(RD >o% I S(L'SM(N Sal I 21C3
'na$e I >ON'S >o% I M(N(8'R Sal I 1TWC
'na$e I M(RTIN >o% I S(L'SM(N Sal I 21C3
'na$e I )L(F' >o% I M(N(8'R Sal I 1GC3
'na$e I CL(RF >o% I M(N(8'R Sal I 1@C3
'na$e I SCOTT >o% I (N(L*ST Sal I 4333
'na$e I FIN8 >o% I AR'SID'NT Sal I C333
'na$e I TURN'R >o% I S(L'SM(N Sal I 2C33
'na$e I (D(MS >o% I CL'RF Sal I 2233
'na$e I >(M'S >o% I CL'RF Sal I TC3
'na$e I BORD >o% I (N(L*ST Sal I 4333
'na$e I MILL'R >o% I CL'RF Sal I 2433
154
SQL- e/e! efN!uso(=STUD'NT=)
Na$e I sað
Na$e I sinu
Na$e I satish
Na$e I sudha
CURSOR '+AR'SSIONS
*ou !an use !uso e/pessions in e/pli!it !usos5
*ou !an use !uso e/pessions in d7na$i! SQL5
*ou !an use !uso e/pessions in R'B !uso de!laations and vaia%les5
*ou !an not use !uso e/pessions in i$pli!it !usos5
Oa!le opens the nested !uso defined %7 a !uso e/pession i$pli!itl7 as soon as it fet!hes the data !ontaining
the !uso e/pession fo$ the paent o oute !uso5
Nested !uso !loses if 7ou !lose e/pli!itl75
Nested !uso !loses wheneve the oute o paent !uso is e/e!uted again o !losed o !an!eled5
Nested !uso !loses wheneve an e/!eption is aised while fet!hing data fo$ a paent !uso5
Cuso e/pessions !an not %e used when de!laing a view5
Cuso e/pessions !an %e used as an agu$ent to ta%le fun!tion5
*ou !an not pefo$ %ind and e/e!ute opeations on !uso e/pessions when using the !uso e/pessions in
d7na$i! SQL5
USIN8 N'ST'D CURSORS OR CURSOR '+AR'SSIONS
'/0
D'CL(R'
!uso ! is sele!t ena$e#!uso(sele!t dna$e fo$ dept d whee e5e$pno I d5deptno) fo$ e$p e.
t7pe t is ef !uso.
!2 t.
!1 t.
v2 e$p5ena$eMt7pe.
v1 dept5dna$eMt7pe.
)'8IN
open !.
loop
155
fet!h !2 into v2.
e/it when !2Mnotfound.
fet!h !1 into v1.
e/it when !1Mnotfound.
d%$sNoutput5putNline(='na$e I = XX v2 XX = Dna$e I = XX v1).
end loop.
end loop.
!lose !.
'ND.
CURSOR CL(US'S
Retun
Bo update
6hee !uent of
)ul& !olle!t
R'TURN
Cuso ! etun deptMowt7pe is sele!t Efo$ dept.
O
Cuso !2 is sele!t Efo$ dept.
Cuso ! etun !2Mowt7pe is sele!t Efo$ dept.
O
T7pe t is e!od(deptno dept5deptnoMt7pe# dna$e dept5dna$eMt7pe).
Cuso ! etun t is sele!t deptno# dna$e fo$ dept.
BOR UAD(T' (ND 6:'R' CURR'NT OB
Cuso ! is sele!t Efo$ dept fo update.
O
Cuso ! is sele!t Efo$ dept fo update of dna$e.
Update dept set dna$e I Lfin; whee deptno I 13 whee !uent of !.
Co$$it.
)ULF COLL'CT
156
This is used fo aa7 fet!hes
6ith this 7ou !an etieve $ultiple ows of data with a single oundtip5
This edu!es the nu$%e of !onte/t swit!hes %etween the pl?sVl and sVl engines5
Redu!es the ovehead of etieving data5
*ou !an use %ul& !olle!t in %oth d7na$i! and stati! sVl5
*ou !an use %ul& !olle!t in sele!t# fet!h into and etuning into !lauses5
SQL engine auto$ati!all7 initialiUes and e/tends the !olle!tions 7ou efeen!e in the %ul& !olle!t !lause5
)ul& !olle!t opeation e$pties the !olle!tion efeen!ed in the into !lause %efoe e/e!uting the Vue75
*ou !an use the li$it !lause of %ul& !olle!t to esti!t the no of ows etieved5
*ou !an fet!h into $ulti%le !olle!tions with one !olu$n ea!h5
Using the etuning !lause we !an etun data to the anothe !olle!tion5
)ULF COLL'CT IN B'TC:
de!lae
T7pe t is ta%le of deptMowt7pe.
nt t.
Cuso ! is sele!t Efo$ dept.
%egin
Open !.
Bet!h ! %ul& !olle!t into nt.
Close !.
'nd.
)ULF COLL'CT IN S'L'CT
de!lae
T7pe t is ta%le of deptMowt7pe.
Nt t.
%egin
Sele!t E into nt fo$ dept.
'nd.
LIMIT IN )ULF COLL'CT
de!lae
T7pe t is ta%le of e$pMowt7pe.
157
nt t.
Cuso ! is sele!t Efo$ e$p.
%egin
Open !.
Bet!h ! %ul& !olle!t into nt li$it C.
Close !.
'nd.
MULTIAL' B'TC:'S IN INTO CL(US'
de!lae
t7pe t is ta%le of dept5deptnoMt7pe.
nt t.
t7pe t2 is ta%le of dept5dna$eMt7pe.
nt2 t2.
!uso ! is sele!t deptno#dna$e fo$ dept.
%egin
open !.
fet!h ! %ul& !olle!t into nt#nt2.
!lose !.
end.
R'TURNIN8 CL(US' IN )ULF COLL'CT
de!lae
t7pe t is ta%le of nu$%e(1).
nt t 0I t(2#1#4#@).
t7pe t2 is ta%le of va!ha(1).
nt2 t2.
t7pe t1 is ta%le of studentMowt7pe.
nt1 t1.
%egin
sele!t na$e %ul& !olle!t into nt2 fo$ student.
foall v in nt25fist55nt25last
update student set no I nt(v) whee na$e I nt2(v) etuning no#na$e#$a&s %ul& !olle!t into nt1.
fo v in nt15fist55nt15last loop
d%$sNoutput5putNline(=Ma&s I = XX nt1(v)).
158
end loop.
end.
AOINTS TO R'M'M)'R
Cuso na$e !an %e up to 43 !haa!tes in length5
Cusos de!laed in anon7$ous %lo!&s o su%poga$s !loses auto$ati!all7 when that %lo!& te$inates
e/e!ution5
M%ul&Now!ount and M%ul&Ne/!eptions !an %e used onl7 with foall !onstu!t5
Cuso de!laations $a7 have e/pessions with !olu$n aliases5
These e/pessions ae !alled vitual !olu$ns o !al!ulated !olu$ns5
SQL IN AL?SQL
The onl7 state$ents allowed die!tl7 in pl?sVl ae DML and TCL5
)INDIN8
159
)inding a vaia%le is the po!ess of identif7ing the stoage lo!ation asso!iated with an identifie in the poga$5
T7pes of %inding
'al7 %inding
Late %inding
)inding duing the !o$piled phase is eal7 %inding5
)inding duing the unti$e phase is late %inding5
In eal7 %inding !o$pile phase will ta&e longe %e!ause of %inding wo& %ut the e/e!ution
is faste5
In late %inding it will shoten the !o$pile phase %ut lengthens the e/e!ution ti$e5
Al?sVl %7 default uses eal7 %inding5
)inding also involves !he!&ing the data%ase fo pe$issions to a!!ess the o%Ke!t
Refeen!ed5
D*N(MIC SQL
If 7ou use DDL in pl?sVl it validates the pe$issions and e/isten!e if eVuies duing !o$pile ti$e whi!h $a&es invalid5
6e !an avoid this %7 using D7na$i! SQL5
D7na$i! SQL allows 7ou to !eate a SQL state$ent d7na$i!all7 at unti$e5
Two te!hniVues ae availa%le fo D7na$i! SQL5
Native D7na$i! SQL
D)MSNSQL pa!&age
USIN8 N(TI9' D*N(MIC SQL
Using e/e!ute i$$ediate
)egin
'/e!ute i$$ediate L!eate ta%le student(no nu$%e(1)#na$e va!ha(23));.
o
'/e!ute i$$ediate (L!eate ta%le student(no nu$%e(1)#na$e va!ha(23));).
'nd.
Using e/e!ute i$$ediate with pl?sVl vaia%les
de!lae
v va!ha(233).
%egin
v 0I =!eate ta%le student(no nu$%e(1)#na$e va!ha(23))=.
e/e!ute i$$ediate v.
end.
Using e/e!ute i$$ediate with %ind vaia%les and using !lause
160
de!lae
v va!ha(233).
%egin
v 0I =inset into student values(0v2#0v1#0v4)=.
e/e!ute i$$ediate v using D#=f=#D33.
end.
'/e!uting Vueies with open fo and using !lause
!eate o epla!e po!edue p(s$a&s in nu$%e) is
s va!ha(233) 0I =sele!t Efo$ student whee $a&s - 0$=.
t7pe t is ef !uso.
! t.
v studentMowt7pe.
%egin
open ! fo s using s$a&s.
loop
fet!h ! into v.
e/it when !Mnotfound.
d%$sNoutput5putNline(=Student Ma&s I = XX v5$a&s).
end loop.
!lose !.
end.
Queies with e/e!ute i$$ediate
de!lae
dNna$e dept5dna$eMt7pe.
l! dept5lo!Mt7pe.
v va!ha(233).
%egin
v 0I =sele!t dna$e fo$ dept whee deptno I 23=.
e/e!ute i$$ediate v into dNna$e.
d%$sNoutput5putNline(=Dna$e I =XX dNna$e).
v 0I =sele!t lo! fo$ dept whee dna$e I 0dn=.
e/e!ute i$$ediate v into l! using dNna$e.
d%$sNoutput5putNline(=Lo! I = XX l!).
end.
161
)ind vaia%les
De!lae
9 nu$%e 0I C33.
)egin
Update student set $a&s I v whee. "" hee v is %ind vaia%le
'nd.
9aia%le Na$es
De!lae
Ma&s nu$%e(4) 0I 233.
)egin
Delete student whee $a&s I $a&s. "" this will delete all the ows in the student ta%le
'nd.
This !an %e avoided %7 using the la%eled %lo!&s5
,,$7N%lo!&--
De!lae
Ma&s nu$%e(4) 0I 233.
)egin
Delete student whee $a&s I $7N%lo!&5$a&s. "" delete ows whi!h has a $a&s of 233
'nd.
8etting data into pl?sVl vaia%les
De!lae
92 nu$%e.
91 va!ha(1).
)egin
Sele!t no#na$e into v2#v1 fo$ student whee $a&s I 233.
'nd.
DML and Re!ods
!eate o epla!e po!edue p(sow in studentMowt7pe) is
%egin
inset into student values sow.
end p.
de!lae
162
s studentMowt7pe.
%egin
s5no 0I 22.
s5na$e 0I =aa=.
s5$a&s 0I 233.
p(s).
end.
Re!od %ased insets
de!lae
sow studentMowt7pe.
%egin
sow5no 0I W.
sow5na$e 0I =!!=.
sow5$a&s 0I C33.
inset into student values sow.
end.
Re!od %ased updates
de!lae
sow studentMowt7pe.
%egin
sow5no 0I D.
sow5na$e 0I =!!=.
sow5$a&s 0I C33.
update student set owIsow whee no I sow5no.
end.
Using e!ods with etuning !lause
de!lae
sow studentMowt7pe.
setun studentMowt7pe.
%egin
sow5no 0I G.
sow5na$e 0I =dd=.
sow5$a&s 0I C33.
inset into student values sow etuning no#na$e#$a&s into setun.
163
d%$sNoutput5putNline(=No I = XX setun5no).
d%$sNoutput5putNline(=No I = XX setun5na$e).
d%$sNoutput5putNline(=No I = XX setun5$a&s).
end.
Boall with non"seVuential aa7s
de!lae
t7pe t is ta%le of student5noMt7pe inde/ %7 %ina7Nintege.
i%t t.
%egin
i%t(2) 0I 2.
i%t(23) 0I 1.
foall i in i%t5fist55i%t5last
update student set $a&s I T33 whee no I i%t(i).
end.
The a%ove poga$ will give eo li&e Lele$ent at inde/ O1P does not e/ists5
Usage of indi!es of to avoid the a%ove eo
de!lae
t7pe t is ta%le of student5noMt7pe inde/ %7 %ina7Nintege.
i%t t.
t7pe t2 is ta%le of %oolean inde/ %7 %ina7Nintege.
i%t2 t2.
%egin
i%t(2) 0I 2.
i%t(23) 0I 1.
i%t(233) 0I 4.
i%t2(2) 0I tue.
i%t(23) 0I tue.
i%t2(233) 0I tue.
foall i in indi!es of i%t2
update student set $a&s I T33 whee no I i%t(i).
end.
)ul& )inds
Aassing the entie pl?sVl ta%le to the SQL engine in one step is &nown as %ul& %ind5
164
)ul& %inds ae done using the foall state$ent5
If thee is an eo po!essing one of the ows in %ul& DML opeation# onl7 that ow is olled %a!&5
Retuning !lause
This will %e used onl7 with DML state$ents to etun data into pl?sVl vaia%les5
This will %e useful in situations li&e # when pefo$ing inset o update o delete if 7ou want to &now the data of the
ta%le whi!h has %een effe!ted %7 the DML5
6ith out going fo anothe S'L'CT using R'TURNIN8 !lause we will get the data whi!h will avoid a !all to
RD)MS &enel5
COLL'CTIONS
Colle!tions ae also !o$posite t7pes# in that the7 allow 7ou to teat seveal vaia%les as a unit5 ( !olle!tion !o$%ines
vaia%les of the sa$e t7pe5
T*A'S
9aa7s
Nested ta%les
Inde/ " %7 ta%les
9(RR(*S
( vaa7 is datat7pe ve7 si$ila to an aa75 ( vaa7 has a fi/ed li$it on its siUe# spe!ified as pat of the de!laation5
'le$ents ae inseted into vaa7 stating at inde/ 2# up to $a/i$u$ lenth de!laed in the vaa7 t7pe5 The $a/i$u$ siUe
of the vaa7 is 1 giga %7tes5
S7nta/0
T7pe ,type_name$ is vaa7 X va7ing aa7 (,limit$) of ,element_type$.
'/20
165
D'CL(R'
t7pe t is vaa7(23) of va!ha(1).
va t 0I t(=a=#=%=#=!=#=d=).
flag %oolean.
)'8IN
d%$sNoutput5putNline(=Li$it I = XX va5li$it).
d%$sNoutput5putNline(=Count I = XX va5!ount).
d%$sNoutput5putNline(=Bist Inde/ I = XX va5fist).
d%$sNoutput5putNline(=Last Inde/ I = XX va5last).
d%$sNoutput5putNline(=Ne/t Inde/ I = XX va5ne/t(1)).
d%$sNoutput5putNline(=Aevious Inde/ I = XX va5pio(4)).
d%$sNoutput5putNline(=9(RR(* 'L'M'NTS=).
fo i in va5fist55va5last loop
d%$sNoutput5putNline(=vaO= XX i XX =P I = XX va(i)).
end loop.
flag 0I va5e/ists(4).
if flag I tue then
d%$sNoutput5putNline(=Inde/ 4 e/ists with an ele$ent = XX va(4)).
else
d%$sNoutput5putNline(=Inde/ 4 does not e/ists=).
end if.
va5e/tend.
d%$sNoutput5putNline(=(fte e/tend of one inde/# Count I = XX va5!ount).
flag 0I va5e/ists(C).
if flag I tue then
d%$sNoutput5putNline(=Inde/ C e/ists with an ele$ent = XX va(C)).
else
d%$sNoutput5putNline(=Inde/ C does not e/ists=).
end if.
flag 0I va5e/ists(D).
if flag I tue then
d%$sNoutput5putNline(=Inde/ D e/ists with an ele$ent = XX va(D)).
else
d%$sNoutput5putNline(=Inde/ D does not e/ists=).
end if.
va5e/tend(1).
d%$sNoutput5putNline(=(fte e/tend of two inde/es# Count I = XX va5!ount).
d%$sNoutput5putNline(=9(RR(* 'L'M'NTS=).
166
fo i in va5fist55va5last loop
d%$sNoutput5putNline(=vaO= XX i XX =P I = XX va(i)).
end loop.
va(C) 0I =e=.
va(D) 0I =f=.
va(W) 0I =g=.
d%$sNoutput5putNline(=(BT'R (SSIN8NIN8 9(LU'S TO '+T'ND'D 'L'M'NTS#
9(RR(* 'L'M'NTS=).
fo i in va5fist55va5last loop
d%$sNoutput5putNline(=vaO= XX i XX =P I = XX va(i)).
end loop.
va5e/tend(4#1).
d%$sNoutput5putNline(=(fte e/tend of thee inde/es# Count I = XX va5!ount).
d%$sNoutput5putNline(=9(RR(* 'L'M'NTS=).
fo i in va5fist55va5last loop
d%$sNoutput5putNline(=vaO= XX i XX =P I = XX va(i)).
end loop.
va5ti$.
d%$sNoutput5putNline(=(fte ti$ of one inde/# Count I = XX va5!ount).
va5ti$(4).
d%$sNoutput5putNline(=(fte ti$ of thee inde/s# Count I = XX va5!ount).
d%$sNoutput5putNline(=(BT'R TRIM# 9(RR(* 'L'M'NTS=).
fo i in va5fist55va5last loop
d%$sNoutput5putNline(=vaO= XX i XX =P I = XX va(i)).
end loop.
va5delete.
d%$sNoutput5putNline(=(fte delete of entie vaa7# Count I = XX va5!ount).
'ND.
Output0
Li$it I 23
Count I @
Bist Inde/ I 2
Last Inde/ I @
Ne/t Inde/ I 4
Aevious Inde/ I 1
9(RR(* 'L'M'NTS
vaO2P I a
167
vaO1P I %
vaO4P I !
vaO@P I d
Inde/ 4 e/ists with an ele$ent !
(fte e/tend of one inde/# Count I C
Inde/ C e/ists with an ele$ent
Inde/ D does not e/ists
(fte e/tend of two inde/es# Count I W
9(RR(* 'L'M'NTS
vaO2P I a
vaO1P I %
vaO4P I !
vaO@P I d
vaOCP I
vaODP I
vaOWP I
(BT'R (SSIN8NIN8 9(LU'S TO '+T'ND'D 'L'M'NTS# 9(RR(* 'L'M'NTS
vaO2P I a
vaO1P I %
vaO4P I !
vaO@P I d
vaOCP I e
vaODP I f
vaOWP I g
(fte e/tend of thee inde/es# Count I 23
9(RR(* 'L'M'NTS
vaO2P I a
vaO1P I %
vaO4P I !
vaO@P I d
vaOCP I e
vaODP I f
vaOWP I g
vaOGP I %
vaOTP I %
vaO23P I %
(fte ti$ of one inde/# Count I T
168
(fte ti$ of thee inde/s# Count I D
(BT'R TRIM# 9(RR(* 'L'M'NTS
vaO2P I a
vaO1P I %
vaO4P I !
vaO@P I d
vaOCP I e
vaODP I f
(fte delete of entie vaa7# Count I 3
'/10
D'CL(R'
t7pe t is vaa7(@) of studentMowt7pe.
va t 0I t(null#null#null#null).
)'8IN
fo i in 255va5!ount loop
sele!t E into va(i) fo$ student whee sno I i.
d%$sNoutput5putNline(=Sno I = XX va(i)5sno XX = Sna$e I = XX va(i)5sna$e).
end loop.
'ND.
Output0
Sno I 2 Sna$e I sað
Sno I 1 Sna$e I sinu
Sno I 4 Sna$e I div7a
Sno I @ Sna$e I $anogni
'/40
D'CL(R'
t7pe t is vaa7(@) of student5s$a&sMt7pe.
va t 0I t(null#null#null#null).
)'8IN
fo i in 255va5!ount loop
sele!t s$a&s into va(i) fo$ student whee sno I i.
d%$sNoutput5putNline(=S$a&s I = XX va(i)).
end loop.
'ND.
Output0
S$a&s I 233
169
S$a&s I 133
S$a&s I 433
S$a&s I @33
'/@0
D'CL(R'
t7pe is e!od(!2 student5sna$eMt7pe#!1 student5s$a&sMt7pe).
t7pe t is vaa7(@) of .
va t 0I t(null#null#null#null).
)'8IN
fo i in 255va5!ount loop
sele!t sna$e#s$a&s into va(i) fo$ student whee sno I i.
d%$sNoutput5putNline(=Sna$e I = XX va(i)5!2 XX = S$a&s I = XX va(i)5!1).
end loop.
'ND.
Output0
Sna$e I sað S$a&s I 233
Sna$e I sinu S$a&s I 133
Sna$e I div7a S$a&s I 433
Sna$e I $anogni S$a&s I @33
'/C0
D'CL(R'
t7pe t is vaa7(2) of add.
va t 0I t(null).
!uso ! is sele!t E fo$ e$plo7.
i nu$%e 0I 2.
)'8IN
fo v in ! loop
sele!t addess into va(i) fo$ e$plo7 whee ena$e I v5ena$e.
d%$sNoutput5putNline(=:no I = XX va(i)5hno XX = Cit7 I = XX va(i)5!it7).
end loop.
'ND.
Output0
:no I 22 Cit7 I h7d
:no I 11 Cit7 I %ang
:no I 44 Cit7 I &o!hi
170
'/D0
D'CL(R'
t7pe t is vaa7(C) of va!ha(1).
va2 t.
va1 t 0I t().
)'8IN
if va2 is null then
d%$sNoutput5putNline(=va2 is null=).
else
d%$sNoutput5putNline(=va2 is not null=).
end if.
if va1 is null then
d%$sNoutput5putNline(=va1 is null=).
else
d%$sNoutput5putNline(=va1 is not null=).
end if.
'ND.
Output0
va2 is null
va1 is not null
N'ST'D T()L'S
( nested ta%le is thought of a data%ase ta%le whi!h has no li$it on its siUe5 'le$ents ae inseted into nested ta%le
stating at inde/ 25 The $a/i$u$ siUe of the vaa7 is 1 giga %7tes5
S7nta/0
T7pe ,type_name$ is ta%le of ,table_type$.
'/20
D'CL(R'
t7pe t is ta%le of va!ha(1).
nt t 0I t(=a=#=%=#=!=#=d=).
171
flag %oolean.
)'8IN
if nt5li$it is null then
d%$sNoutput5putNline(=No li$it to Nested Ta%les=).
else
d%$sNoutput5putNline(=Li$it I = XX nt5li$it).
end if.
d%$sNoutput5putNline(=Count I = XX nt5!ount).
d%$sNoutput5putNline(=Bist Inde/ I = XX nt5fist).
d%$sNoutput5putNline(=Last Inde/ I = XX nt5last).
d%$sNoutput5putNline(=Ne/t Inde/ I = XX nt5ne/t(1)).
d%$sNoutput5putNline(=Aevious Inde/ I = XX nt5pio(4)).
d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=).
fo i in 255nt5!ount loop
d%$sNoutput5putNline(=ntO= XX i XX =P I = XX nt(i)).
end loop.
flag 0I nt5e/ists(4).
if flag I tue then
d%$sNoutput5putNline(=Inde/ 4 e/ists with an ele$ent = XX nt(4)).
else
d%$sNoutput5putNline(=Inde/ 4 does not e/ists=).
end if.
nt5e/tend.
d%$sNoutput5putNline(=(fte e/tend of one inde/# Count I = XX nt5!ount).
flag 0I nt5e/ists(C).
if flag I tue then
d%$sNoutput5putNline(=Inde/ C e/ists with an ele$ent = XX nt(C)).
else
d%$sNoutput5putNline(=Inde/ C does not e/ists=).
end if.
flag 0I nt5e/ists(D).
if flag I tue then
d%$sNoutput5putNline(=Inde/ D e/ists with an ele$ent = XX nt(D)).
else
d%$sNoutput5putNline(=Inde/ D does not e/ists=).
end if.
nt5e/tend(1).
d%$sNoutput5putNline(=(fte e/tend of two inde/es# Count I = XX nt5!ount).
172
d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=).
fo i in 255nt5!ount loop
d%$sNoutput5putNline(=ntO= XX i XX =P I = XX nt(i)).
end loop.
nt(C) 0I =e=.
nt(D) 0I =f=.
nt(W) 0I =g=.
d%$sNoutput5putNline(=(BT'R (SSIN8NIN8 9(LU'S TO '+T'ND'D 'L'M'NTS# N'ST'D T()L'
'L'M'NTS=).
fo i in 255nt5!ount loop
d%$sNoutput5putNline(=ntO= XX i XX =P I = XX nt(i)).
end loop.
nt5e/tend(C#1).
d%$sNoutput5putNline(=(fte e/tend of five inde/es# Count I = XX nt5!ount).
d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=).
fo i in 255nt5!ount loop
d%$sNoutput5putNline(=ntO= XX i XX =P I = XX nt(i)).
end loop.
nt5ti$.
d%$sNoutput5putNline(=(fte ti$ of one inde/# Count I = XX nt5!ount).
nt5ti$(4).
d%$sNoutput5putNline(=(fte ti$ of thee inde/s# Count I = XX nt5!ount).
d%$sNoutput5putNline(=(BT'R TRIM# N'ST'D T()L' 'L'M'NTS=).
fo i in 255nt5!ount loop
d%$sNoutput5putNline(=ntO= XX i XX =P I = XX nt(i)).
end loop.
nt5delete(2).
d%$sNoutput5putNline(=(fte delete of fist inde/# Count I = XX nt5!ount).
d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=).
fo i in 155nt5!ountH2 loop
d%$sNoutput5putNline(=ntO= XX i XX =P I = XX nt(i)).
end loop.
nt5delete(@).
d%$sNoutput5putNline(=(fte delete of fouth inde/# Count I = XX nt5!ount).
d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=).
fo i in 1554 loop
d%$sNoutput5putNline(=ntO= XX i XX =P I = XX nt(i)).
end loop.
173
fo i in C55nt5!ountH1 loop
d%$sNoutput5putNline(=ntO= XX i XX =P I = XX nt(i)).
end loop.
nt5delete.
d%$sNoutput5putNline(=(fte delete of entie nested ta%le# Count I = XX nt5!ount).
'ND.
Output0
No li$it to Nested Ta%les
Count I @
Bist Inde/ I 2
Last Inde/ I @
Ne/t Inde/ I 4
Aevious Inde/ I 1
N'ST'D T()L' 'L'M'NTS
ntO2P I a
ntO1P I %
ntO4P I !
ntO@P I d
Inde/ 4 e/ists with an ele$ent !
(fte e/tend of one inde/# Count I C
Inde/ C e/ists with an ele$ent
Inde/ D does not e/ists
(fte e/tend of two inde/es# Count I W
N'ST'D T()L' 'L'M'NTS
ntO2P I a
ntO1P I %
ntO4P I !
ntO@P I d
ntOCP I
ntODP I
ntOWP I
(BT'R (SSIN8NIN8 9(LU'S TO '+T'ND'D 'L'M'NTS# N'ST'D T()L' 'L'M'NTS
ntO2P I a
ntO1P I %
ntO4P I !
ntO@P I d
ntOCP I e
174
ntODP I f
ntOWP I g
(fte e/tend of five inde/es# Count I 21
N'ST'D T()L' 'L'M'NTS
ntO2P I a
ntO1P I %
ntO4P I !
ntO@P I d
ntOCP I e
ntODP I f
ntOWP I g
ntOGP I %
ntOTP I %
ntO23P I %
ntO22P I %
ntO21P I %
(fte ti$ of one inde/# Count I 22
(fte ti$ of thee inde/s# Count I G
(BT'R TRIM# N'ST'D T()L' 'L'M'NTS
ntO2P I a
ntO1P I %
ntO4P I !
ntO@P I d
ntOCP I e
ntODP I f
ntOWP I g
ntOGP I %
(fte delete of fist inde/# Count I W
N'ST'D T()L' 'L'M'NTS
ntO1P I %
ntO4P I !
ntO@P I d
ntOCP I e
ntODP I f
ntOWP I g
ntOGP I %
(fte delete of fouth inde/# Count I D
175
N'ST'D T()L' 'L'M'NTS
ntO1P I %
ntO4P I !
ntOCP I e
ntODP I f
ntOWP I g
ntOGP I %
(fte delete of entie nested ta%le# Count I 3
'/10
D'CL(R'
t7pe t is ta%le of studentMowt7pe.
nt t 0I t(null#null#null#null).
)'8IN
fo i in 255nt5!ount loop
sele!t E into nt(i) fo$ student whee sno I i.
d%$sNoutput5putNline(=Sno I = XX nt(i)5sno XX = Sna$e I = XX nt(i)5sna$e).
end loop.
'ND.
Output0
Sno I 2 Sna$e I sað
Sno I 1 Sna$e I sinu
Sno I 4 Sna$e I div7a
Sno I @ Sna$e I $anogni
'/40
D'CL(R'
t7pe t is ta%le of student5s$a&sMt7pe.
nt t 0I t(null#null#null#null).
)'8IN
fo i in 255nt5!ount loop
sele!t s$a&s into nt(i) fo$ student whee sno I i.
d%$sNoutput5putNline(=S$a&s I = XX nt(i)).
end loop.
'ND.
Output0
S$a&s I 233
176
S$a&s I 133
S$a&s I 433
S$a&s I @33
'/@0
D'CL(R'
t7pe is e!od(!2 student5sna$eMt7pe#!1 student5s$a&sMt7pe).
t7pe t is ta%le of .
nt t 0I t(null#null#null#null).
)'8IN
fo i in 255nt5!ount loop
sele!t sna$e#s$a&s into nt(i) fo$ student whee sno I i.
d%$sNoutput5putNline(=Sna$e I = XX nt(i)5!2 XX = S$a&s I = XX nt(i)5!1).
end loop.
'ND.
Output0
Sna$e I sað S$a&s I 233
Sna$e I sinu S$a&s I 133
Sna$e I div7a S$a&s I 433
Sna$e I $anogni S$a&s I @33
'/C0
D'CL(R'
t7pe t is ta%le of add.
nt t 0I t(null).
!uso ! is sele!t E fo$ e$plo7.
i nu$%e 0I 2.
)'8IN
fo v in ! loop
sele!t addess into nt(i) fo$ e$plo7 whee ena$e I v5ena$e.
d%$sNoutput5putNline(=:no I = XX nt(i)5hno XX = Cit7 I = XX nt(i)5!it7).
end loop.
'ND.
Output0
:no I 22 Cit7 I h7d
:no I 11 Cit7 I %ang
:no I 44 Cit7 I &o!hi
177
'/D0
D'CL(R'
t7pe t is vaa7(C) of va!ha(1).
nt2 t.
nt1 t 0I t().
)'8IN
if nt2 is null then
d%$sNoutput5putNline(=nt2 is null=).
else
d%$sNoutput5putNline(=nt2 is not null=).
end if.
if nt1 is null then
d%$sNoutput5putNline(=nt1 is null=).
else
d%$sNoutput5putNline(=nt1 is not null=).
end if.
'ND.
Output0
nt2 is null
nt1 is not null
IND'+")* T()L'S
(n inde/"%7 ta%le has no li$it on its siUe5 'le$ents ae inseted into inde/"%7 ta%le whose inde/ $a7 stat non"
seVuentiall7 in!luding negative integes5
S7nta/0
T7pe ,type_name$ is ta%le of ,table_type$ inde/ %7 %ina7Nintege.
'/0
D'CL(R'
t7pe t is ta%le of va!ha(1) inde/ %7 %ina7Nintege.
i%t t.
flag %oolean.
)'8IN
178
i%t(2) 0I =a=.
i%t("13) 0I =%=.
i%t(43) 0I =!=.
i%t(233) 0I =d=.
if i%t5li$it is null then
d%$sNoutput5putNline(=No li$it to Inde/ %7 Ta%les=).
else
d%$sNoutput5putNline(=Li$it I = XX i%t5li$it).
end if.
d%$sNoutput5putNline(=Count I = XX i%t5!ount).
d%$sNoutput5putNline(=Bist Inde/ I = XX i%t5fist).
d%$sNoutput5putNline(=Last Inde/ I = XX i%t5last).
d%$sNoutput5putNline(=Ne/t Inde/ I = XX i%t5ne/t(1)).
d%$sNoutput5putNline(=Aevious Inde/ I = XX i%t5pio(4)).
d%$sNoutput5putNline(=IND'+ )* T()L' 'L'M'NTS=).
d%$sNoutput5putNline(=i%tO"13P I = XX i%t("13)).
d%$sNoutput5putNline(=i%tO2P I = XX i%t(2)).
d%$sNoutput5putNline(=i%tO43P I = XX i%t(43)).
d%$sNoutput5putNline(=i%tO233P I = XX i%t(233)).
flag 0I i%t5e/ists(43).
if flag I tue then
d%$sNoutput5putNline(=Inde/ 43 e/ists with an ele$ent = XX i%t(43)).
else
d%$sNoutput5putNline(=Inde/ 43 does not e/ists=).
end if.
flag 0I i%t5e/ists(C3).
if flag I tue then
d%$sNoutput5putNline(=Inde/ C3 e/ists with an ele$ent = XX i%t(43)).
else
d%$sNoutput5putNline(=Inde/ C3 does not e/ists=).
end if.
i%t5delete(2).
d%$sNoutput5putNline(=(fte delete of fist inde/# Count I = XX i%t5!ount).
i%t5delete(43).
d%$sNoutput5putNline(=(fte delete of inde/ thit7# Count I = XX i%t5!ount).
d%$sNoutput5putNline(=IND'+ )* T()L' 'L'M'NTS=).
d%$sNoutput5putNline(=i%tO"13P I = XX i%t("13)).
179
d%$sNoutput5putNline(=i%tO233P I = XX i%t(233)).
i%t5delete.
d%$sNoutput5putNline(=(fte delete of entie inde/"%7 ta%le# Count I = XX i%t5!ount).
'ND.
Output0
No li$it to Inde/ %7 Ta%les
Count I @
Bist Inde/ I "13
Last Inde/ I 233
Ne/t Inde/ I 43
Aevious Inde/ I 2
IND'+ )* T()L' 'L'M'NTS
i%tO"13P I %
i%tO2P I a
i%tO43P I !
i%tO233P I d
Inde/ 43 e/ists with an ele$ent !
Inde/ C3 does not e/ists
(fte delete of fist inde/# Count I 4
(fte delete of inde/ thit7# Count I 1
IND'+ )* T()L' 'L'M'NTS
i%tO"13P I %
i%tO233P I d
(fte delete of entie inde/"%7 ta%le# Count I 3
DIBB'R'NC'S (MON8 COLL'CTIONS
9aa7s has li$it# nested ta%les and inde/"%7 ta%les has no li$it5
9aa7s and nested ta%les $ust %e initialiUed %efoe assign$ent of ele$ents# in inde/"%7 ta%les we !an die!tl7
assign ele$ents5
9aa7s and nested ta%les stoed in data%ase# %ut inde/"%7 ta%les !an not5
Nested ta%les and inde/"%7 ta%les ae AL?SQL ta%les# %ut vaa7s !an not5
Fe7s $ust %e positive in !ase of nested ta%les and vaa7s# in !ase of inde/"%7 ta%les &e7s !an %e positive o
negative5
180
Refeen!ing none/istent ele$ents aises SU)SCRIATN)'*ONDNCOUNT in %oth nested ta%les and vaa7s# %ut in
!ase of inde/"%7 ta%les NOND(T(NBOUND aises5
Fe7s ae seVuential in %oth nested ta%les and vaa7s# non"seVuential in inde/"%7 ta%les5
Individual inde/es !an %e deleted in %oth nested ta%les and inde/"%7 ta%les# %ut in vaa7s !an not5
Individual inde/es !an %e ti$$ed in %oth nested ta%les and vaa7s# %ut in inde/"%7 ta%les !an not5
Individual inde/es !an %e e/tended in %oth nested ta%les and vaa7s# %ut in inde/"%7 ta%les !an not5
MULTIL'9'L COLL'CTIONS
Colle!tions of $oe than one di$ension whi!h is a !olle!tion of !olle!tions# &nown as $ultilevel !olle!tions5
S7nta/0
T7pe ,type_name1$ is ta%le of ,table_type$ inde/ %7 %ina7Nintege.
T7pe ,type_name2$ is vaa7(,limit-) X ta%le X of ,type_name1$ 0 inde/ %7
%ina7Nintege.
'/20
D'CL(R'
t7pe t2 is ta%le of va!ha(1) inde/ %7 %ina7Nintege.
t7pe t1 is vaa7(C) of t2.
va t1 0I t1().
! nu$%e 0I TW.
flag %oolean.
)'8IN
va5e/tend(@).
d%$sNoutput5putNline(=Count I = XX va5!ount).
d%$sNoutput5putNline(=Li$it I = XX va5li$it).
fo i in 255va5!ount loop
fo K in 255va5!ount loop
va(i)(K) 0I !h(!).
! 0I ! H 2.
end loop.
end loop.
d%$sNoutput5putNline(=9(RR(* 'L'M'NTS=).
fo i in 255va5!ount loop
fo K in 255va5!ount loop
d%$sNoutput5putNline(=vaO= XX i XX =PO= XX K XX =P I = XX va(i)(K)).
181
end loop.
end loop.
d%$sNoutput5putNline(=Bist inde/ I = XX va5fist).
d%$sNoutput5putNline(=Last inde/ I = XX va5last).
d%$sNoutput5putNline(=Ne/t inde/ I = XX va5ne/t(1)).
d%$sNoutput5putNline(=Aevious inde/ I = XX va5pio(4)).
flag 0I va5e/ists(1).
if flag I tue then
d%$sNoutput5putNline(=Inde/ 1 e/ists=).
else
d%$sNoutput5putNline(=Inde/ 1 e/ists=).
end if.
va5e/tend.
va(2)(C) 0I =V=.
va(1)(C) 0I ==.
va(4)(C) 0I =s=.
va(@)(C) 0I =t=.
va(C)(2) 0I =u=.
va(C)(1) 0I =v=.
va(C)(4) 0I =w=.
va(C)(@) 0I =/=.
va(C)(C) 0I =7=.
d%$sNoutput5putNline(=(fte e/tend of one inde/# Count I = XX va5!ount).
d%$sNoutput5putNline(=9(RR(* 'L'M'NTS=).
fo i in 255va5!ount loop
fo K in 255va5!ount loop
d%$sNoutput5putNline(=vaO= XX i XX =PO= XX K XX =P I = XX va(i)(K)).
end loop.
end loop.
va5ti$.
d%$sNoutput5putNline(=(fte ti$ of one inde/# Count I = XX va5!ount).
va5ti$(1).
d%$sNoutput5putNline(=(fte ti$ of two inde/es# Count I = XX va5!ount).
d%$sNoutput5putNline(=9(RR(* 'L'M'NTS=).
fo i in 255va5!ount loop
fo K in 255va5!ount loop
d%$sNoutput5putNline(=vaO= XX i XX =PO= XX K XX =P I = XX va(i)(K)).
182
end loop.
end loop.
va5delete.
d%$sNoutput5putNline(=(fte delete of entie vaa7# Count I = XX va5!ount).
'ND.
Output0
Count I @
Li$it I C
9(RR(* 'L'M'NTS
vaO2PO2P I a
vaO2PO1P I %
vaO2PO4P I !
vaO2PO@P I d
vaO1PO2P I e
vaO1PO1P I f
vaO1PO4P I g
vaO1PO@P I h
vaO4PO2P I i
vaO4PO1P I K
vaO4PO4P I &
vaO4PO@P I l
vaO@PO2P I $
vaO@PO1P I n
vaO@PO4P I o
vaO@PO@P I p
Bist inde/ I 2
Last inde/ I @
Ne/t inde/ I 4
Aevious inde/ I 1
Inde/ 1 e/ists
(fte e/tend of one inde/# Count I C
9(RR(* 'L'M'NTS
vaO2PO2P I a
vaO2PO1P I %
vaO2PO4P I !
vaO2PO@P I d
vaO2POCP I V
183
vaO1PO2P I e
vaO1PO1P I f
vaO1PO4P I g
vaO1PO@P I h
vaO1POCP I
vaO4PO2P I i
vaO4PO1P I K
vaO4PO4P I &
vaO4PO@P I l
vaO4POCP I s
vaO@PO2P I $
vaO@PO1P I n
vaO@PO4P I o
vaO@PO@P I p
vaO@POCP I t
vaOCPO2P I u
vaOCPO1P I v
vaOCPO4P I w
vaOCPO@P I /
vaOCPOCP I 7
(fte ti$ of one inde/# Count I @
(fte ti$ of two inde/es# Count I 1
9(RR(* 'L'M'NTS
vaO2PO2P I a
vaO2PO1P I %
vaO1PO2P I e
vaO1PO1P I f
(fte delete of entie vaa7# Count I 3
'/10
D'CL(R'
t7pe t2 is ta%le of va!ha(1) inde/ %7 %ina7Nintege.
t7pe t1 is ta%le of t2.
nt t1 0I t1().
! nu$%e 0I DC.
v nu$%e 0I 2.
flag %oolean.
)'8IN
184
nt5e/tend(@).
d%$sNoutput5putNline(=Count I = XX nt5!ount).
if nt5li$it is null then
d%$sNoutput5putNline(=No li$it to Nested Ta%les=).
else
d%$sNoutput5putNline(=Li$it I = XX nt5li$it).
end if.
fo i in 255nt5!ount loop
fo K in 255nt5!ount loop
nt(i)(K) 0I !h(!).
! 0I ! H 2.
if ! I T2 then
! 0I TW.
end if.
end loop.
end loop.
d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=).
fo i in 255nt5!ount loop
fo K in 255nt5!ount loop
d%$sNoutput5putNline(=ntO= XX i XX =PO= XX K XX =P I = XX nt(i)(K)).
end loop.
end loop.
d%$sNoutput5putNline(=Bist inde/ I = XX nt5fist).
d%$sNoutput5putNline(=Last inde/ I = XX nt5last).
d%$sNoutput5putNline(=Ne/t inde/ I = XX nt5ne/t(1)).
d%$sNoutput5putNline(=Aevious inde/ I = XX nt5pio(4)).
flag 0I nt5e/ists(1).
if flag I tue then
d%$sNoutput5putNline(=Inde/ 1 e/ists=).
else
d%$sNoutput5putNline(=Inde/ 1 e/ists=).
end if.
nt5e/tend(1).
nt(2)(C) 0I =Q=.
nt(2)(D) 0I =R=.
nt(1)(C) 0I =S=.
nt(1)(D) 0I =T=.
185
nt(4)(C) 0I =U=.
nt(4)(D) 0I =9=.
nt(@)(C) 0I =6=.
nt(@)(D) 0I =+=.
nt(C)(2) 0I =*=.
nt(C)(1) 0I =Z=.
nt(C)(4) 0I =a=.
nt(C)(@) 0I =%=.
nt(C)(C) 0I =!=.
nt(C)(D) 0I =d=.
nt(D)(2) 0I =e=.
nt(D)(1) 0I =f=.
nt(D)(4) 0I =g=.
nt(D)(@) 0I =h=.
nt(D)(C) 0I =i=.
nt(D)(D) 0I =K=.
d%$sNoutput5putNline(=(fte e/tend of one inde/# Count I = XX nt5!ount).
d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=).
fo i in 255nt5!ount loop
fo K in 255nt5!ount loop
d%$sNoutput5putNline(=ntO= XX i XX =PO= XX K XX =P I = XX nt(i)(K)).
end loop.
end loop.
nt5ti$.
d%$sNoutput5putNline(=(fte ti$ of one inde/e# Count I = XX nt5!ount).
nt5ti$(1).
d%$sNoutput5putNline(=(fte ti$ of two inde/es# Count I = XX nt5!ount).
d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=).
fo i in 255nt5!ount loop
fo K in 255nt5!ount loop
d%$sNoutput5putNline(=ntO= XX i XX =PO= XX K XX =P I = XX nt(i)(K)).
end loop.
end loop.
nt5delete(1).
d%$sNoutput5putNline(=(fte delete of se!ond inde/# Count I = XX nt5!ount).
d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=).
loop
186
e/it when v I @.
fo K in 255nt5!ountH2 loop
d%$sNoutput5putNline(=ntO= XX v XX =PO= XX K XX =P I = XX nt(v)(K)).
end loop.
v 0I v H 2.
if vI 1 then
v 0I 4.
end if.
end loop.
nt5delete.
d%$sNoutput5putNline(=(fte delete of entie nested ta%le# Count I = XX nt5!ount).
'ND.
Output0
Count I @
No li$it to Nested Ta%les
N'ST'D T()L' 'L'M'NTS
ntO2PO2P I (
ntO2PO1P I )
ntO2PO4P I C
ntO2PO@P I D
ntO1PO2P I '
ntO1PO1P I B
ntO1PO4P I 8
ntO1PO@P I :
ntO4PO2P I I
ntO4PO1P I >
ntO4PO4P I F
ntO4PO@P I L
ntO@PO2P I M
ntO@PO1P I N
ntO@PO4P I O
ntO@PO@P I A
Bist inde/ I 2
Last inde/ I @
Ne/t inde/ I 4
Aevious inde/ I 1
Inde/ 1 e/ists
187
(fte e/tend of one inde/# Count I D
N'ST'D T()L' 'L'M'NTS
ntO2PO2P I (
ntO2PO1P I )
ntO2PO4P I C
ntO2PO@P I D
ntO2POCP I Q
ntO2PODP I R
ntO1PO2P I '
ntO1PO1P I B
ntO1PO4P I 8
ntO1PO@P I :
ntO1POCP I S
ntO1PODP I T
ntO4PO2P I I
ntO4PO1P I >
ntO4PO4P I F
ntO4PO@P I L
ntO4POCP I U
ntO4PODP I 9
ntO@PO2P I M
ntO@PO1P I N
ntO@PO4P I O
ntO@PO@P I A
ntO@POCP I 6
ntO@PODP I +
ntOCPO2P I *
ntOCPO1P I Z
ntOCPO4P I a
ntOCPO@P I %
ntOCPOCP I !
ntOCPODP I d
ntODPO2P I e
ntODPO1P I f
ntODPO4P I g
ntODPO@P I h
ntODPOCP I i
188
ntODPODP I K
(fte ti$ of one inde/e# Count I C
(fte ti$ of two inde/es# Count I 4
N'ST'D T()L' 'L'M'NTS
ntO2PO2P I (
ntO2PO1P I )
ntO2PO4P I C
ntO1PO2P I '
ntO1PO1P I B
ntO1PO4P I 8
ntO4PO2P I I
ntO4PO1P I >
ntO4PO4P I F
(fte delete of se!ond inde/# Count I 1
N'ST'D T()L' 'L'M'NTS
ntO2PO2P I (
ntO2PO1P I )
ntO2PO4P I C
ntO4PO2P I I
ntO4PO1P I >
ntO4PO4P I F
(fte delete of entie nested ta%le# Count I 3
'/40
D'CL(R'
t7pe t2 is ta%le of va!ha(1) inde/ %7 %ina7Nintege.
t7pe t1 is ta%le of t2 inde/ %7 %ina7Nintege.
i%t t1.
flag %oolean.
)'8IN
d%$sNoutput5putNline(=Count I = XX i%t5!ount).
if i%t5li$it is null then
d%$sNoutput5putNline(=No li$it to Inde/"%7 Ta%les=).
else
d%$sNoutput5putNline(=Li$it I = XX i%t5li$it).
end if.
i%t(2)(2) 0I =a=.
i%t(@)(C) 0I =%=.
189
i%t(C)(2) 0I =!=.
i%t(D)(1) 0I =d=.
i%t(G)(4) 0I =e=.
i%t(4)(@) 0I =f=.
d%$sNoutput5putNline(=IND'+")* T()L' 'L'M'NTS=).
d%$sNoutput5putNline(=i%t(O2PO2P I = XX i%t(2)(2)).
d%$sNoutput5putNline(=i%t(O@POCP I = XX i%t(@)(C)).
d%$sNoutput5putNline(=i%t(OCPO2P I = XX i%t(C)(2)).
d%$sNoutput5putNline(=i%t(ODPO1P I = XX i%t(D)(1)).
d%$sNoutput5putNline(=i%t(OGPO4P I = XX i%t(G)(4)).
d%$sNoutput5putNline(=i%t(O4PO@P I = XX i%t(4)(@)).
d%$sNoutput5putNline(=Bist Inde/ I = XX i%t5fist).
d%$sNoutput5putNline(=Last Inde/ I = XX i%t5last).
d%$sNoutput5putNline(=Ne/t Inde/ I = XX i%t5ne/t(4)).
d%$sNoutput5putNline(=Aio Inde/ I = XX i%t5pio(G)).
i%t(2)(1) 0I =g=.
i%t(2)(4) 0I =h=.
i%t(2)(@) 0I =i=.
i%t(2)(C) 0I =&=.
i%t(2)(D) 0I =l=.
i%t(2)(W) 0I =$=.
i%t(2)(G) 0I =n=.
d%$sNoutput5putNline(=Count I = XX i%t5!ount).
d%$sNoutput5putNline(=IND'+")* T()L' 'L'M'NTS=).
fo i in 255G loop
d%$sNoutput5putNline(=i%tO2PO= XX i XX =P I = XX i%t(2)(i)).
end loop.
d%$sNoutput5putNline(=i%t(O@POCP I = XX i%t(@)(C)).
d%$sNoutput5putNline(=i%t(OCPO2P I = XX i%t(C)(2)).
d%$sNoutput5putNline(=i%t(ODPO1P I = XX i%t(D)(1)).
d%$sNoutput5putNline(=i%t(OGPO4P I = XX i%t(G)(4)).
d%$sNoutput5putNline(=i%t(O4PO@P I = XX i%t(4)(@)).
flag 0I i%t5e/ists(4).
if flag I tue then
d%$sNoutput5putNline(=Inde/ 4 e/ists=).
else
d%$sNoutput5putNline(=Inde/ 4 e/ists=).
190
end if.
i%t5delete(2).
d%$sNoutput5putNline(=(fte delete of fist inde/# Count I = XX i%t5!ount).
i%t5delete(@).
d%$sNoutput5putNline(=(fte delete of fouth inde/# Count I = XX i%t5!ount).
d%$sNoutput5putNline(=IND'+")* T()L' 'L'M'NTS=).
d%$sNoutput5putNline(=i%t(OCPO2P I = XX i%t(C)(2)).
d%$sNoutput5putNline(=i%t(ODPO1P I = XX i%t(D)(1)).
d%$sNoutput5putNline(=i%t(OGPO4P I = XX i%t(G)(4)).
d%$sNoutput5putNline(=i%t(O4PO@P I = XX i%t(4)(@)).
i%t5delete.
d%$sNoutput5putNline(=(fte delete of entie inde/"%7 ta%le# Count I = XX i%t5!ount).
'ND.
Output0
Count I 3
No li$it to Inde/"%7 Ta%les
IND'+")* T()L' 'L'M'NTS
i%t(O2PO2P I a
i%t(O@POCP I %
i%t(OCPO2P I !
i%t(ODPO1P I d
i%t(OGPO4P I e
i%t(O4PO@P I f
Bist Inde/ I 2
Last Inde/ I G
Ne/t Inde/ I @
Aio Inde/ I D
Count I D
IND'+")* T()L' 'L'M'NTS
i%tO2PO2P I a
i%tO2PO1P I g
i%tO2PO4P I h
i%tO2PO@P I i
i%tO2POCP I &
i%tO2PODP I l
i%tO2POWP I $
i%tO2POGP I n
191
i%t(O@POCP I %
i%t(OCPO2P I !
i%t(ODPO1P I d
i%t(OGPO4P I e
i%t(O4PO@P I f
Inde/ 4 e/ists
(fte delete of fist inde/# Count I C
(fte delete of fouth inde/# Count I @
IND'+")* T()L' 'L'M'NTS
i%t(OCPO2P I !
i%t(ODPO1P I d
i%t(OGPO4P I e
i%t(O4PO@P I f
(fte delete of entie inde/"%7 ta%le# Count I 3
'/40
D'CL(R'
t7pe t2 is ta%le of va!ha(1) inde/ %7 %ina7Nintege.
t7pe t1 is ta%le of t2 inde/ %7 %ina7Nintege.
t7pe t4 is ta%le of t1.
nt t4 0I t4().
! nu$%e 0I DC.
)'8IN
nt5e/tend(1).
d%$sNoutput5putNline(=Count I = XX nt5!ount).
fo i in 255nt5!ount loop
fo K in 255nt5!ount loop
fo & in 255nt5!ount loop
nt(i)(K)(&) 0I !h(!).
! 0I ! H 2.
end loop.
end loop.
end loop.
d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=).
fo i in 255nt5!ount loop
fo K in 255nt5!ount loop
fo & in 255nt5!ount loop
d%$sNoutput5putNline(=ntO= XX i XX =PO= XX K XX =PO= XX & XX =P I = XX
192
nt(i)(K)(&)).
end loop.
end loop.
end loop.
'ND.
Output0
Count I 1
N'ST'D T()L' 'L'M'NTS
ntO2PO2PO2P I (
ntO2PO2PO1P I )
ntO2PO1PO2P I C
ntO2PO1PO1P I D
ntO1PO2PO2P I '
ntO1PO2PO1P I B
ntO1PO1PO2P I 8
ntO1PO1PO1P I :
O)>'CTS US'D IN T:' '+(MAL'S
SQL- sele!t E fo$ student.
SNO SN(M' SM(RFS
"""""""""" """""""""""""" """"""""""
2 sað 233
1 sinu 133
4 div7a 433
@ $anogni @33
SQL- !eate o epla!e t7pe add as o%Ke!t(hno nu$%e(1)#!it7 va!ha(23)).?
SQL- sele!t E fo$ e$plo7.
'N(M' >O) (DDR'SS(:NO# CIT*)
"""""""""" """""""""" """""""""""""""""""""""""""""
RanKit !le& (DDR(22# =h7d=)
Satish $anage (DDR(11# =%ang=)
193
Sinu enginee (DDR(44# =&o!hi=)
'RROR :(NDLIN8
AL?SQL i$ple$ents eo handling with e/!eptions and e/!eption handles5 '/!eptions !an %e asso!iated with oa!le
eos o with 7ou own use"defined eos5 )7 using e/!eptions and e/!eption handles# 7ou !an $a&e 7ou AL?SQL
poga$s o%ust and a%le to deal with %oth une/pe!ted and e/pe!ted eos duing e/e!ution5
'RROR T*A'S
Co$pile"ti$e eos
Runti$e eos
194
'os that o!!u duing the !o$pilation phase ae dete!ted %7 the AL?SQL engine and epoted %a!& to the use# we have
to !oe!t the$5
Runti$e eos ae dete!ted %7 the AL?SQL unti$e engine whi!h !an poga$$ati!all7 aise and !aught %7 e/!eption
handles5
'/!eptions ae designed fo un"ti$e eo handling# athe than !o$pile"ti$e eo handling5
:(NDLIN8 '+C'ATIONS
6hen e/!eption is aised# !ontol passes to the e/!eption se!tion of the %lo!&5 The e/!eption se!tion !onsists of handles
fo so$e o all of the e/!eptions5 (n e/!eption handle !ontains the !ode that is e/e!uted when the eo asso!iated with
the e/!eption o!!us# and the e/!eption is aised5
S7nta/0
'+C'ATION
6hen e/!eptionNna$e then
SeVuen!eNofNstate$ents.
6hen e/!eptionNna$e then
SeVuen!eNofNstate$ents.
6hen othes then
SeVuen!eNofNstate$ents.
'ND.
'+C'ATION T*A'S
Aedefined e/!eptions
Use"defined e/!eptions
AR'D'BIN'D '+C'ATIONS
Oa!le has pedefined seveal e/!eptions that !oesponds to the $ost !o$$on oa!le eos5 Li&e the pedefined t7pes#
the identifies of these e/!eptions ae defined in the ST(ND(RD pa!&age5 )e!ause of this# the7 ae alead7 availa%le to the
poga$# it is not ne!essa7 to de!lae the$ in the de!laative se!ion5
'/20
D'CL(R'
a nu$%e.
% va!ha(1).
195
vN$a&s nu$%e.
!uso ! is sele!t E fo$ student.
t7pe t is vaa7(4) of va!ha(1).
va t 0I t(=a=#=%=).
va2 t.
)'8IN
"" NOND(T(NBOUND
)'8IN
sele!t s$a&s into vN$a&s fo$ student whee sno I C3.
'+C'ATION
when noNdataNfound then
d%$sNoutput5putNline(=Invalid student nu$%e=).
'ND.
"" CURSORN(LR'(D*NOA'N
)'8IN
open !.
open !.
'+C'ATION
when !usoNalead7Nopen then
d%$sNoutput5putNline(=Cuso is alead7 opened=).
'ND.
"" IN9(LIDNCURSOR
)'8IN
!lose !.
open !.
!lose !.
!lose !.
'+C'ATION
when invalidN!uso then
d%$sNoutput5putNline(=Cuso is alead7 !losed=).
'ND.
"" TOONM(N*NRO6S
)'8IN
sele!t s$a&s into vN$a&s fo$ student whee sno - 2.
'+C'ATION
when tooN$an7Nows then
d%$sNoutput5putNline(=Too $an7 values ae !o$ing to $a&s vaia%le=).
'ND.
"" Z'RONDI9ID'
)'8IN
196
a 0I C?3.
'+C'ATION
when UeoNdivide then
d%$sNoutput5putNline(=Divided %7 Ueo " invalid opeation=).
'ND.
"" 9(LU'N'RROR
)'8IN
% 0I =sað=.
'+C'ATION
when valueNeo then
d%$sNoutput5putNline(=Invalid sting length=).
'ND.
"" IN9(LIDNNUM)'R
)'8IN
inset into student values(=a=#=sinu=#233).
'+C'ATION
when invalidNnu$%e then
d%$sNoutput5putNline(=Invalid nu$%e=).
'ND.
"" SU)SCRIATNOUTSID'NLIMIT
)'8IN
va(@) 0I =!=.
'+C'ATION
when su%s!iptNoutsideNli$it then
d%$sNoutput5putNline(=Inde/ is geate than the li$it=).
'ND.
"" SU)SCRIATN)'*ONDNCOUNT
)'8IN
va(4) 0I =!=.
'+C'ATION
when su%s!iptN%e7ondN!ount then
d%$sNoutput5putNline(=Inde/ is geate than the !ount=).
'ND.
"" COLL'CTIONNISNNULL
)'8IN
va2(2) 0I =a=.
'+C'ATION
when !olle!tionNisNnull then
d%$sNoutput5putNline(=Colle!tion is e$pt7=).
'ND.
""
'ND.
197
Output0
Invalid student nu$%e
Cuso is alead7 opened
Cuso is alead7 !losed
Too $an7 values ae !o$ing to $a&s vaia%le
Divided %7 Ueo " invalid opeation
Invalid sting length
Invalid nu$%e
Inde/ is geate than the li$it
Inde/ is geate than the !ount
Colle!tion is e$pt7
'/10
D'CL(R'
! nu$%e.
)'8IN
! 0I C?3.
'+C'ATION
when UeoNdivide then
d%$sNoutput5putNline(=Invalid Opeation=).
when othes then
d%$sNoutput5putNline(=Bo$ OT:'RS handle0 Invalid Opeation=).
'ND.
Output0
Invalid Opeation
US'R"D'BIN'D '+C'ATIONS
( use"defined e/!eption is an eo that is defined %7 the poga$$e5 Use"defined e/!eptions ae de!laed in the
de!laative se!ion of a AL?SQL %lo!&5 >ust li&e vaia%les# e/eptions have a t7pe '+C'ATION and s!ope5
R(ISIN8 '+C'ATIONS
Use"defined e/!eptions ae aised e/pli!itl7 via the R(IS' state$ent5
'/0
D'CL(R'
e e/!eption.
198
)'8IN
aise e.
'+C'ATION
when e then
d%$sNoutput5putNline(=e is aised=).
'ND.
Output0
e is aised
SQLCOD' (ND SQL'RRM
SQLCOD' etuns the !uent eo !ode# and SQL'RRM etuns the !uent eo $essage te/t.
Bo use"defined e/!eption SQLCOD' etuns 2 and SQL'RRM etuns Quse"deifned e/!eptionR5
SQL'RRM wiil ta&e onl7 negative value e/!ept 2335 If an7 positive value othe than 233 etuns non"oa!le e/!eption5
'/20
D'CL(R'
e e/!eption.
vNdna$e va!ha(23).
)'8IN
"" US'R"D'BIN'D '+C'ATION
)'8IN
aise e.
'+C'ATION
when e then
d%$sNoutput5putNline(SQLCOD' XX = = XX SQL'RRM).
'ND.
"" AR'D'BIN'D '+C'ATION
)'8IN
sele!t dna$e into vNdna$e fo$ dept whee deptno I C3.
'+C'ATION
when noNdataNfound then
d%$sNoutput5putNline(SQLCOD' XX = = XX SQL'RRM).
'ND.
'ND.
Output0
2 Use"Defined '/!eption
233 OR("32@340 no data found
'/10
199
)'8IN
d%$sNoutput5putNline(SQL'RRM(233)).
d%$sNoutput5putNline(SQL'RRM(3)).
d%$sNoutput5putNline(SQL'RRM(2)).
d%$sNoutput5putNline(SQL'RRM("233)).
d%$sNoutput5putNline(SQL'RRM("C33)).
d%$sNoutput5putNline(SQL'RRM(133)).
d%$sNoutput5putNline(SQL'RRM("T33)).
'ND.
Output0
OR("32@340 no data found
OR("33330 no$al# su!!essful !o$pletion
Use"Defined '/!eption
OR("332330 no data found
OR("33C330 Message C33 not found. podu!tIRD)MS. fa!ilit7IOR(
"1330 non"OR(CL' e/!eption
OR("33T330 invalid SQL state$ent
'+C'ATIONNINIT AR(8M(
Using this 7ou !an asso!iate a na$ed e/!eption with a pati!ula oa!le eo5 This gives 7ou the a%ilit7 to tap this eo
spe!ifi!all7# athe than via an OT:'RS handle5
S7nta/0
AR(8M( '+C'ATIONNINIT(e&ception_name# oracle_error_number).
'/0
D'CL(R'
e e/!eption.
pag$a e/!eptionNinit(e#"2@WD).
! nu$%e.
)'8IN
! 0I C?3.
'+C'ATION
when e then
d%$sNoutput5putNline(=Invalid Opeation=).
'ND.
Output0
Invalid Opeation
200
R(IS'N(AALIC(TIONN'RROR
*ou !an use this %uilt"in fun!tion to !eate 7ou own eo $essages# whi!h !an %e $oe des!iptive than na$ed
e/!eptions5
S7nta/0
R(IS'N(AALIC(TIONN'RROR(error_number# error_messae## Okeep_errors_%laP).
The )oolean paa$ete keep_errors_%la is optional5 If it is TRU'# the new eo is added to the list of eos alead7
aised5 If it is B(LS'# whi!h is default# the new eo will epla!e the !uent list of eos5
'/0
D'CL(R'
! nu$%e.
)'8IN
! 0I C?3.
'+C'ATION
when UeoNdivide then
aiseNappli!ationNeo("13111#=Invalid Opeation=).
'ND.
Output0
D'CL(R'
E
'RROR at line 20
OR("131110 Invalid Opeation
OR("3DC210 at line W
'+C'ATION AROA(8(TION
'/!eptions !an o!!u in the de!laative# the e/e!uta%le# o the e/!eption se!tion of a AL?SQL %lo!&5
'+C'ATION R(IS'D IN T:' '+'CU(T()L' S'CTION
'/!eptions aised in e/e!uata%le se!tion !an %e handled in !uent %lo!& o oute %lo!&5
'/20
D'CL(R'
e e/!eption.
)'8IN
)'8IN
aise e.
'ND.
'+C'ATION
201
when e then
d%$sNoutput5putNline(=e is aised=).
'ND.
Output0
e is aised
'/10
D'CL(R'
e e/!eption.
)'8IN
)'8IN
aise e.
'ND.
'ND.
Output0
'RROR at line 20
OR("3DC230 AL?SQL0 unhandled use"defined e/!eption
OR("3DC210 at line C
'+C'ATION R(IS'D IN T:' D'CL(R(TI9' S'CTION
'/!eptions aised in the de!laative se!ion $ust %e handled in the oute %lo!&5
'/20
D'CL(R'
! nu$%e(4) 0I =a%!d=.
)'8IN
d%$sNoutput5putNline(=:ello=).
'+C'ATION
when othes then
d%$sNoutput5putNline(=Invalid sting length=).
'ND.
Output0
'RROR at line 20
OR("3DC310 AL?SQL0 nu$ei! o value eo0 !haa!te to nu$%e !onvesion eo
OR("3DC210 at line 1
'/10
)'8IN
D'CL(R'
202
! nu$%e(4) 0I =a%!d=.
)'8IN
d%$sNoutput5putNline(=:ello=).
'+C'ATION
when othes then
d%$sNoutput5putNline(=Invalid sting length=).
'ND.
'+C'ATION
when othes then
d%$sNoutput5putNline(=Bo$ oute %lo!&0 Invalid sting length=).
'ND.
Output0
Bo$ oute %lo!&0 Invalid sting length
'+C'ATION R(IS'D IN T:' '+C'ATION S'CTION
'/!eptions aised in the de!laative se!ion $ust %e handled in the oute %lo!&5
'/20
D'CL(R'
e2 e/!eption.
e1 e/!eption.
)'8IN
aise e2.
'+C'ATION
when e2 then
d%$sNoutput5putNline(=e2 is aised=).
aise e1.
when e1 then
d%$sNoutput5putNline(=e1 is aised=).
'ND.
Output0
e2 is aised
D'CL(R'
E
'RROR at line 20
OR("3DC230 AL?SQL0 unhandled use"defined e/!eption
OR("3DC210 at line T
203
OR("3DC230 AL?SQL0 unhandled use"defined e/!eption
'/10
D'CL(R'
e2 e/!eption.
e1 e/!eption.
)'8IN
)'8IN
aise e2.
'+C'ATION
when e2 then
d%$sNoutput5putNline(=e2 is aised=).
aise e1.
when e1 then
d%$sNoutput5putNline(=e1 is aised=).
'ND.
'+C'ATION
when e1 then
d%$sNoutput5putNline(=Bo$ oute %lo!&0 e1 is aised=).
'ND.
Output0
e2 is aised
Bo$ oute %lo!&0 e1 is aised
'/40
D'CL(R'
e e/!eption.
)'8IN
aise e.
'+C'ATION
when e then
d%$sNoutput5putNline(=e is aised=).
aise e.
'ND.
Output0
e is aised
D'CL(R'
E
204
'RROR at line 20
OR("3DC230 AL?SQL0 unhandled use"defined e/!eption
OR("3DC210 at line G
OR("3DC230 AL?SQL0 unhandled use"defined e/!eption
205