Sie sind auf Seite 1von 285

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)).
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).
SQL- inset into student values (1# ;sað# 133).

Copy rights are reserved.
1
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)
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)
!) 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).
'/0
SQL- inset into student (no# na$e) values (4# ;Ra$esh;).
SQL- inset into student (no# na$e) values (@# ;Madhu;).
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 .&coln);
Copy rights are reserved.
2
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=)
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=)
S'L'CTIN8 D(T(
S7nta/0
Sele!t E fo$ ,table_name-. "" hee E indi!ates all !olu$ns
o
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&eth 133
2 >agan 433
1 Naen @33
4 Ra$esh
@ Madhu
C 9isu
D Rattu
SQL- sele!t no# na$e# $a&s fo$ student.
NO N(M' M(RFS
Copy rights are reserved.
3
""" """""" """"""""
2 Sudha 233
1 Sa&eth 133
2 >agan 433
1 Naen @33
4 Ra$esh
@ Madhu
C 9isu
D Rattu
SQL- sele!t no# na$e fo$ student.
NO N(M'
""" """""""
2 Sudha
1 Sa&eth
2 >agan
1 Naen
4 Ra$esh
@ Madhu
C 9isu
D Rattu
CONDITION(L S'L'CTIONS (ND OA'R(TORS
6e have two !lauses used in this
6hee
Ode %7
USIN8 6:'R'
S7nta/0
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
Copy rights are reserved.
4
(ith$eti! opeatos "" highest pe!eden!e
G# "# E# ?
Co$paison opeatos
H# IH# -# ,# -H# ,H# ,-
%etween# not %etween
in# not in
null# not null
li&e
Logi!al opeatos
(nd
O "" lowest pe!eden!e
not
a) USIN8 H# -# ,# -H# ,H# IH# ,-

'/0
SQL- sele!t E fo$ student whee no H 1.
NO N(M' M(RFS
""" """"""" """""""""
1 Sa&eth 133
1 Naen @33

SQL- sele!t E fo$ student whee no , 1.
NO N(M' M(RFS
""" """"""" """"""""""
2 Sudha 233
2 >agan 433
SQL- sele!t E fo$ student whee no - 1.
NO N(M' M(RFS
""" """"""" """"""""""
4 Ra$esh
@ Madhu
C 9isu
D Rattu
Copy rights are reserved.
5
SQL- sele!t E fo$ student whee no ,H 1.
NO N(M' M(RFS
""" """"""" """"""""""
2 Sudha 233
1 Sa&eth 133
2 >agan 433
1 Naen @33

SQL- sele!t E fo$ student whee no -H 1.
NO N(M' M(RFS
""" """"""" """""""""
1 Sa&eth 133
1 Naen @33
4 Ra$esh
@ Madhu
C 9isu
D Rattu
SQL- sele!t E fo$ student whee no IH 1.
NO N(M' M(RFS
""" """"""" """"""""""
2 Sudha 233
2 >agan 433
4 Ra$esh
@ Madhu
C 9isu
D Rattu
SQL- sele!t E fo$ student whee no ,- 1.
NO N(M' M(RFS
""" """"""" """"""""""
2 Sudha 233
2 >agan 433
4 Ra$esh
Copy rights are reserved.
6
@ Madhu
C 9isu
D Rattu
%) 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 H 1 and $a&s -H 133.
NO N(M' M(RFS
""" """"""" """"""""
1 Sa&eth 133
1 Naen @33
!) 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 H 1 o $a&s -H 133.
NO N(M' M(RFS
""" """"""" """""""""
1 Sa&eth 133
2 >agan 433
1 Naen @33
d) USIN8 )'T6''N
This will gives the output %ased on the !olu$n and its lowe %ound# uppe%ound5
Copy rights are reserved.
7
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&eth 133
2 >agan 433
1 Naen @33
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.
NO N(M' M(RFS
""" """"""" """""""""
2 Sudha 233
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
Copy rights are reserved.
8
""" """"""" """""""""
2 Sudha 233
1 Sa&eth 133
2 >agan 433
1 Naen @33
4 Ra$esh
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
D Rattu
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
Copy rights are reserved.
9
D Rattu
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&eth 133
2 >agan 433
1 Naen @33
J) 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-.

'/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
ii) This will give the ows whose na$e stat with KS;5
SQL- sele!t E fo$ student whee na$e li&e =SL=.
NO N(M' M(RFS
Copy rights are reserved.
10
""" """"""" """""""""
2 Sudha 233
1 Sa&eth 133
iii) This will give the ows whose na$e ends with Kh;5
SQL- sele!t E fo$ student whee na$e li&e =Lh=.

NO N(M' M(RFS
""" """"""" """""""""
1 Sa&eth 133
4 Ra$esh
i9) This will give the ows whose na$e;s se!ond lette stat with Ka;5
SQL- sele!t E fo$ student whee na$e li&e =MaL=.
NO N(M' M(RFS
""" """"""" """"""""
1 Sa&eth 133
2 >agan 433
1 Naen @33
4 Ra$esh
@ Madhu
D Rattu

9) This will give the ows whose na$e;s thid lette stat with Kd;5
SQL- sele!t E fo$ student whee na$e li&e =MMdL=.
NO N(M' M(RFS
""" """"""" """""""""
2 Sudha 233
@ Madhu
9i) This will give the ows whose na$e;s se!ond lette stat with Kt; fo$ ending5
SQL- sele!t E fo$ student whee na$e li&e =LMtL=.
Copy rights are reserved.
11
NO N(M' M(RFS
""" """"""" """""""""
1 Sa&eth 133
D Rattu

9ii) This will give the ows whose na$e;s thid lette stat with Ke; fo$ ending5
SQL- sele!t E fo$ student whee na$e li&e =LeMML=.
NO N(M' M(RFS
""" """"""" """""""""
1 Sa&eth 133
4 Ra$esh
9iii) This will give the ows whose na$e !otains 1 a;s5
SQL- sele!t E fo$ student whee na$e li&e =LaL a L=.
NO N(M' M(RFS
""" """"""" """"""""""
2 >agan 433
E *ou have to spe!if7 the pattens in lie using undes!oe ( M )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 de!c &e7wod afte the !olu$n5
'/0
SQL- sele!t E fo$ student ode %7 no.
NO N(M' M(RFS
Copy rights are reserved.
12
""" """"""" """""""""
2 Sudha 233
2 >agan 433
1 Sa&eth 133
1 Naen @33
4 Ra$esh
@ Madhu
C 9isu
D Rattu
SQL- sele!t E fo$ student ode %7 no des!.
NO N(M' M(RFS
""" """"""" """""""""
D Rattu
C 9isu
@ Madhu
4 Ra$esh
1 Sa&eth 133
1 Naen @33
2 Sudha 233
2 >agan 433
USIN8 DML
USIN8 UAD(T'
This !an %e used to $odif7 the ta%le data5
S7nta/0
Update ,table_name- set ,col1- H value2# ,col2- H value1 whee ,condition-.
'/0
SQL- update student set $a&s H C33.
If 7ou ae not spe!if7ing an7 !ondition this will update entie ta%le5
SQL- update student set $a&s H C33 whee no H 1.
SQL- update student set $a&s H C33# na$e H =9enu= whee no H 2.
USIN8 D'L'T'
Copy rights are reserved.
13
This !an %e used to delete the ta%le data te$poail75
S7nta/0
Delete ,table_name- whee ,condition-.
'/0
SQL- delete student.
If 7ou ae not spe!if7ing an7 !ondition this will delete entie ta%le5
SQL- delete student whee no H 1.
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%.
Copy rights are reserved.
14
!) 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.

'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
Copy rights are reserved.
15
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%Je!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%Je!t.
S7nta/0
ena$e ,oldMtable_name- to ,new_table_name-.
'/0
SQL- ena$e student to stud.
Copy rights are reserved.
16
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&.
Copy rights are reserved.
17
E 6hile po!ess is going on# if suddenl7 powe goes then oa!le will oll%a!& the
tansa!tion5
USIN8 S(9'AOINT
*ou !an use savepoints to oll%a!& potions of 7ou !uent set of tansa!tions5
S7nta/0
Savepoint ,!avepoint_name-.
'/0
SQL- savepoint s2.
SQL- inset into student values(2# Ka;# 233).
SQL- savepoint s1.
SQL- inset into student values(1# K%;# 133).
SQL- savepoint s4.
SQL- inset into student values(4# K!;# 433).
SQL- savepoint s@.
SQL- inset into student values(@# Kd;# @33).

)efoe oll%a!&
SQL- sele!t E fo$ student.
NO N(M' M(RFS
""" """"""" """"""""""
2 a 233
1 % 133
4 ! 433
@ d @33

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
""" """"""" """"""""""
Copy rights are reserved.
18
2 a 233
1 % 133
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
Copy rights are reserved.
19
8ant ,privile"e!- on ,ob#ect_name- to ,u!er_name- Nwith gant optionO.
'/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%Je!t5
SQL- sele!t E fo$ sa&eth5student.
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 ,privile"e!- on ,ob#ect_name- fo$ ,u!er_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
CR'(T' 6IT: S'L'CT
6e !an !eate a ta%le using e/isting ta%le Nalong with dataO5
S7nta/0
Ceate ta%le ,new_table_name- Ncol1, col2, col3 ... colnO 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
Copy rights are reserved.
20
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 H 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 %or"inal_col& %alia!_name& 'rom %tableMna$e-.
'/0
SQL- sele!t no sno fo$ student.
o
SQL- sele!t no PsnoQ fo$ student.
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 ,alia!_name-5,col1-# ,alia!_name-5,col2- R ,alia!_name-5,coln- fo$
Copy rights are reserved.
21
,table_name- ,alia!_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
Using (sele!t EBo$ student1) s1
On(s25noHs15no)
6hen $at!hed then
Update set $a&s H 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#
Copy rights are reserved.
22
na$e# hno# !it75
SQL- Mege into student2 s2
Using (sele!t EBo$ student1) s1
On(s25noHs15no)
6hen $at!hed then
Update set $a&s H 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
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 deptnoH23.
"" This insets 4 ows
Copy rights are reserved.
23
!) 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 deptnoH23.
"" 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 S 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 H KS(L'S; then
Into student1 values(1#;%;#133)
6hen lo! H KN'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
Copy rights are reserved.
24
Into student2 values(2#;a;#233)
6hen dna$e H KS; then
Into student1 values(1#;%;#133)
6hen lo! H KN'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 H 13 then
Into student2 values(2#;a;#233)
6hen dna$e H KR'S'(RC:; then
Into student1 values(1#;%;#133)
6hen lo! H KN'6 *ORF; then
Into student4 values(4#;!;#433)
Sele!t Efo$ dept whee deptnoH13.

"" 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 H 43 then
Into student2 values(2#;a;#233)
6hen dna$e H KR; then
Into student1 values(1#;%;#133)
6hen lo! H KN'6 *ORF; then
Into student4 values(4#;!;#433)
'lse
Into student values(@#;d;#@33)
Sele!t Efo$ dept whee deptnoH13.
"" This insets 2 e!od %e!ause the else !lause satisfied on!e
Copy rights are reserved.
25
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 deptnoH23.
"" 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
Copy rights are reserved.
26
Bun!tions !an %e !ategoiTed as follows5
Single ow fun!tions
8oup fun!tions
SIN8L' RO6 BUNCTIONS
Single ow fun!tions !an %e !ategoiTed 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
Convesion fun!tions
NUM'RIC BUNCTIONS
(%s
Sign
SUt
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
Copy rights are reserved.
27
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.
SI8N(C) SI8N("C) SI8N(3) SI8N(NULL)
"""""""""" """""""""" """""""""" """"""""""""""
2 "2 3
!) SQRT
This will give the sUuae oot of the given value5
S7nta/0 sUt (value) "" hee value $ust %e positive5
'/0
SQL- sele!t sUt(@)# sUt(3)# sUt(null)# sUt(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, divi!or)
Copy rights are reserved.
28
'/0
SQL- sele!t $od(V#@)# $od(2#C)# $od(null#null)# $od(3#3)# $od("V#@) fo$ dual.
MOD(V#@) MOD(2#C) MOD(NULL#NULL) MOD(3#3) MOD("V#@)
"""""""""""" """""""""" """"""""""""""""""""" """"""""""" """""""""""""
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 !
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.

Copy rights are reserved.
29
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.
'+A(2) '+A(1) '+A(3) '+A(NULL) '+A("1)
"""""""" """"""""" """""""" """"""""""""" """"""""""
15V2W1W2W4 V54WS3CD2 2 524C44C1W4
h) LN
This is %ased on natual o %ase e logaith$5
S7nta/0 ln (value) "" hee value $ust %e geate than Teo whi!h is positive onl75
'/0
SQL- sele!t ln(2)# ln(1)# ln(null) fo$ dual.
Copy rights are reserved.
30
LN(2) LN(1) LN(NULL)
""""""" """"""" """"""""""""
3 5DS42@V2W2
Ln and '/p ae e!ipo!al to ea!h othe5
'+A (4) H 1353WCC4DS
LN (1353WCC4DS) H 4
i) LO8
This is %ased on 23 %ased logaith$5
S7nta/0 log (23# value)"" hee value $ust %e geate than Teo 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 543231SSSD 3
LN (value) H LO8 ('+A(2)# value)
SQL- sele!t ln(4)# log(e/p(2)#4) fo$ dual.
LN(4) LO8('+A(2)#4)
""""""" """""""""""""""""
253SWD211S 253SWD211S
J) C'IL
This will podu!e a whole nu$%e that is geate than o eUual 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)
Copy rights are reserved.
31
""""""""" """"""""""" """""""""" """""""""""" """""""" """"""""""""""
C D "C "C 3
&) BLOOR
This will podu!e a whole nu$%e that is less than o eUual 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, preci!ion)
'/0
SQL- sele!t ound(214514@C)# ound(214514@C#1)# ound(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.
ROUND(214#3) ROUND(214#2) ROUND(214#1)
""""""""""""""""" """"""""""""""""" """"""""""""""""
214 214 214
Copy rights are reserved.
32
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, preci!ion)
'/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.
TRUNC(214514@C#"2) TRUNC(214514@C#"1) TRUNC(214514@C#"4) TRUNC(214514@C#"@)
Copy rights are reserved.
33
"""""""""""""""""""""""" """""""""""""""""""""""" """"""""""""""""""""""" """"""""""""""""""""""""
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
Copy rights are reserved.
34
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 Teos then it will displa7 Teo5
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 Teos then it will displa7 Teo5
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'
Copy rights are reserved.
35
This will etun fist non"null value5
S7nta/0 !oales!e (value1, value2, value3 valuen)
'/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 ( K XX K Con!atenation opeato)
(s!ii
Ch
Su%st
Inst
De!ode
8eatest
Least
Coales!e
a) INITC(A
This will !apitaliTe the initial lette of the sting5
S7nta/0 init!ap (!trin")
Copy rights are reserved.
36
'/0
SQL- sele!t init!ap(=!o$pute=) fo$ dual.
INITC(A
"""""""""""
Co$pute
%) UAA'R
This will !onvet the sting into uppe!ase5
S7nta/0 uppe (!trin")
'/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 (!trin")
'/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 (!trin")
Copy rights are reserved.
37
'/0
SQL- sele!t length(=!o$pute=) fo$ dual.
L'N8T:
"""""""""""
W
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 (!trin", len"t) *, paddin"_c)ar+)
'/0
SQL- sele!t pad(=!o$pute=#2C#=E=)# pad(=!o$pute=#2C#=EY=) fo$ dual.
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 (!trin", len"t) *, 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
Copy rights are reserved.
38
This will ti$ off unwanted !haa!tes fo$ the left end of sting5
S7nta/0 lti$ (!trin" *,unwanted_c)ar!+)
'/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
S7nta/0 ti$ (!trin" *, unwanted_c)ar!+)
'/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
Copy rights are reserved.
39
This will ti$ off unwanted !haa!tes fo$ the %oth sides of sting5
S7nta/0 ti$ (unwanted_c)ar! fo$ !trin")
'/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
J) TR(NSL(T'
This will epla!e the set of !haa!tes# !haa!te %7 !haa!te5
S7nta/0 tanslate (!trin", old_c)ar!, new_c)ar!)
'/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
Copy rights are reserved.
40
S7nta/0 epla!e (!trin", old_c)ar! *, new_c)ar!+)
'/0
SQL- sele!t epla!e(=india=#=in=#=/7=)# epla!e(Kindia;#;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/ (!trin")
'/0
SQL- sele!t E fo$ e$p whee sounde/(ena$e) H sounde/(=SMIT=).
'MANO 'N(M' >O) M8R :IR'D(T' S(L D'ATNO
"""""""" """""""" """"" """"" """""""""""" """"""""" """"""""""
V4DS SMIT: CL'RF VS31 2V"D'C"W3 C33 13
$) CONC(T
This will %e used to !o$%ine two stings onl75
S7nta/0 !on!at (!trin"1, !trin"2)
'/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
Copy rights are reserved.
41
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 (!trin")
'/0
SQL- sele!t as!ii(=a=)# as!ii(=apple=) fo$ dual.
(SCII(=(=) (SCII(=(AAL'=)
"""""""""""" """"""""""""""""""
SV SV
o) C:R
This will etun the !haa!te having the %ina7 eUuivalent 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(SV) fo$ dual.
C:R
"""""
a
p) SU)STR
This will %e used to e/ta!t su%stings5
S7nta/0 su%st (!trin", !tart_c)r_count *, no_o'_c)ar!+)
Copy rights are reserved.
42
'/0
SQL- sele!t su%st(=!o$pute=#1)# su%st(=!o$pute=#1#C)# su%st(=!o$pute=#4#V)
fo$ dual.
SU)STR( SU)ST SU)STR
"""""""""" """"""" """"""""
o$pute o$put $pute
If no_o'_c)ar! paa$ete is negative then it will displa7 nothing5
If %oth paa$etes e/!ept !trin" ae null o Teos then it will displa7 nothing5
If no_o'_c)ar! paa$ete is geate than the length of the sting then it
ignoes and !al!ulates %ased on the oginal sting length5
If !tart_c)r_count is negative then it will e/ta!t the su%sting fo$ ight end5
2 1 4 @ C D V W
C O M A U T ' R
"W "V "D "C "@ "4 "1 "2
U) INSTR
This will allows 7ou fo sea!hing though a sting fo set of !haa!tes5
S7nta/0 inst (!trin", !earc)_!tr *, !tart_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 !tart_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 !tart_c)r_count and occurrence ae null# it will displa7
nothing5
) D'COD'
Copy rights are reserved.
43
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, . el!e).
'/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$
4C33 Mediu$
4333 Mediu$
C333 :igh
@333 Mediu$
C333 :igh
2W33 Mediu$
2133 Mediu$
1333 Mediu$
1V33 Mediu$
1133 Mediu$
4133 Mediu$
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 Teos then de!ode will displa7 Teo5
s) 8R'(T'ST
This will give the geatest sting5
Copy rights are reserved.
44
S7nta/0 geatest (!trn"1, !trin"2, !trin"3 !trin"n)
'/0
SQL- sele!t geatest(=a=# =%=# =!=)# geatest(=satish=#=sinu=#=sa&eth=) 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
t) L'(ST
This will give the least sting5
S7nta/0 geatest (!trn"1, !trin"2, !trin"3 !trin"n)
'/0
SQL- sele!t least(=a=# =%=# =!=)# least(=satish=#=sinu=#=sa&eth=) fo$ dual.
L'(ST L'(ST
""""""" """""""
a sa&eth
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 (!trn"1, !trin"2, !trin"3 !trin"n)
'/0
SQL- sele!t !oales!e(=a=#=%=#=!=)# !oales!e(null#=a=#null#=%=) fo$ dual.
CO(L'SC' CO(L'SC'
Copy rights are reserved.
45
""""""""""" """""""""""
a a
D(T' BUNCTIONS
S7sdate
CuentMdate
CuentMti$esta$p
S7sti$esta$p
Lo!alti$esta$p
D%ti$eTone
Sessionti$eTone
ToM!ha
ToMdate
(ddM$onths
MonthsM%etween
Ne/tMda7
LastMda7
'/ta!t
8eatest
Least
Round
Tun!
NewMti$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 nlsMdateMfo$at H KDD"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'
"""""""""""
Copy rights are reserved.
46
1@"D'C"3D
%) CURR'NTMD(T'
This will etuns the !uent date in the session;s ti$eTone5
'/0
SQL- sele!t !uentMdate fo$ dual.
CURR'NTMD(T'
""""""""""""""""""
1@"D'C"3D
!) CURR'NTMTIM'ST(MA
This will etuns the !uent ti$esta$p with the a!tive ti$e Tone info$ation5
'/0
SQL- sele!t !uentMti$esta$p fo$ dual.
CURR'NTMTIM'ST(MA
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1@"D'C"3D 345@15@254W44DS (M G3C043
d) S*STIM'ST(MA
This will etuns the s7ste$ date# in!luding fa!tional se!onds and ti$e Tone of the
data%ase5
'/0
SQL- sele!t s7sti$esta$p fo$ dual.
S*STIM'ST(MA
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1@"D'C"3D 345@S5425W433SS (M G3C043
e) LOC(LTIM'ST(MA
This will etuns lo!al ti$esta$p in the a!tive ti$e Tone info$ation# with no ti$e
Tone info$ation shown5
Copy rights are reserved.
47
'/0
SQL- sele!t lo!alti$esta$p fo$ dual.
LOC(LTIM'ST(MA
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1@"D'C"3D 345@@52W5C31WV@ (M
f) D)TIM'ZON'
This will etuns the !uent data%ase ti$e Tone in UTC fo$at5 (Coodinated Univesal
Ti$e)
'/0
SQL- sele!t d%ti$eTone fo$ dual.
D)TIM'ZON'
"""""""""""""""
"3V033
g) S'SSIONTIM'ZON'
This will etuns the value of the !uent session;s ti$e Tone5
'/0
SQL- sele!t sessionti$eTone fo$ dual.
S'SSIONTIM'ZON'
""""""""""""""""""""""""""""""""""""
G3C043

h) TOMC:(R
This will %e used to e/ta!t vaious date fo$ats5
The availa%le date fo$ats as follows5
S7nta/0 toM!ha (date# 'ormat)

D(T' BORM(TS

D "" No of da7s in wee&
DD "" No of da7s in $onth
Copy rights are reserved.
48
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 Uuates
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
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 toM!ha(s7sdate#=dd $onth 7777 hh0$i0ss a$ d7=) fo$ dual.
Copy rights are reserved.
49
TOMC:(R(S*SD(T'#=DD MONT: ****::0MI
""""""""""""""""""""""""""""""""""""""""""""""""""""
1@ de!e$%e 133D 31034014 p$ sun
SQL- sele!t toM!ha(s7sdate#=dd $onth 7ea=) fo$ dual.
TOMC:(R(S*SD(T'#=DDMONT:*'(R=)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""
1@ de!e$%e two thousand si/
SQL- sele!t toM!ha(s7sdate#=dd f$$onth 7ea=) fo$ dual.
TOMC:(R(S*SD(T'#=DD BMMONT: *'(R=)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""
1@ de!e$%e two thousand si/
SQL- sele!t toM!ha(s7sdate#=ddth DDT:=) fo$ dual.
TOMC:(R(S
""""""""""""
1@th 1@
T:
SQL- sele!t toM!ha(s7sdate#=ddspth DDSAT:=) fo$ dual.
TOMC:(R(S*SD(T'#=DDSAT:DDSAT:
""""""""""""""""""""""""""""""""""""""""""
twent7"fouth T6'NT*"BOURT:
SQL- sele!t toM!ha(s7sdate#=ddsp Ddsp DDSA =) fo$ dual.
TOMC:(R(S*SD(T'#=DDSADDSADDSA=)
""""""""""""""""""""""""""""""""""""""""""""""""
twent7"fou Twent7"Bou T6'NT*"BOUR
i) TOMD(T'
This will %e used to !onvet the sting into data fo$at5
Copy rights are reserved.
50
S7nta/0 toMdate (date)
'/0
SQL- sele!t toM!ha(toMdate(=1@?de!?133D=#=dd?$on?7777=)# =dd E $onth E da7=)
fo$ dual.
TOMC:(R(TOMD(T'(=1@?D'C?13
""""""""""""""""""""""""""
1@ E de!e$%e E Sunda7
"" If 7ou ae not using toM!ha oa!le will displa7 output in default date fo$at5
J) (DDMMONT:S
This will add the spe!ified $onths to the given date5
S7nta/0 addM$onths (date, no_o'_mont)!)
'/0
SQL- sele!t addM$onths(toMdate(=22"Jan"2SS3=#=dd"$on"7777=)# C) fo$ dual.
(DDMMONT:S
""""""""""""""""
22">UN"S3
SQL- sele!t addM$onths(toMdate(=22"Jan"2SS3=#=dd"$on"7777=)# "C) fo$ dual.
(DDMMONT:
"""""""""""""""
22"(U8"WS
If no_o'_mont)! is Teo then it will displa7 the sa$e date5
If no_o'_mont)! is null then it will displa7 nothing5
&) MONT:SM)'T6''N
This will give diffeen!e of $onths %etween two dates5
S7nta/0 $onthsM%etween (date1, date2)
Copy rights are reserved.
51
'/0
SQL- sele!t $onthsM%etween(toMdate(=22"aug"2SS3=#=dd"$on"7777=)# toMdate(=22"
Jan"2SS3=#=dd"$on"7777=)) fo$ dual.

MONT:SM)'T6''N(TOMD(T'(=22"(U8"2SS3=#=DD"MON"****=)#TOMD(T'(=22">(N"
2SS3=#=DD"MON"****=))
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
V
SQL- sele!t $onthsM%etween(toMdate(=22"Jan"2SS3=#=dd"$on"7777=)# toMdate(=22"
aug"2SS3=#=dd"$on"7777=)) fo$ dual.

MONT:SM)'T6''N(TOMD(T'(=22">(N"2SS3=#=DD"MON"****=)#TOMD(T'(=22"(U8"
2SS3=#=DD"MON"****=))
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"V
l) N'+TMD(*
This will podu!e ne/t da7 of the given da7 fo$ the spe!ified date5
S7nta/0 ne/tMda7 (date, day)
'/0
SQL- sele!t ne/tMda7(toMdate(=1@"de!"133D=#=dd"$on"7777=)#=sun=) fo$ dual.
N'+TMD(*(
"""""""""""""
42"D'C"3D
"" If the da7 paa$ete is null then it will displa7 nothing5
$) L(STMD(*
This will podu!e last da7 of the given date5
S7nta/0 lastMda7 (date)
Copy rights are reserved.
52
'/0
SQL- sele!t lastMda7(toMdate(=1@"de!"133D=#=dd"$on"7777=)#=sun=) fo$ dual.
L(STMD(*(
"""""""""""""
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(toMdate(=22"Jan"S3=#=dd"$on"77=)#toMdate(=22"$a"S3=#=dd"
$on"77=)#toMdate(=22"ap"S3=#=dd"$on"77=)) fo$ dual.

8R'(T'ST(
"""""""""""""
22"(AR"S3
p) L'(ST
This will give the least date5
S7nta/0 least (date1, date2, date3 daten)
Copy rights are reserved.
53
'/0
SQL- sele!t least(toMdate(=22"Jan"S3=#=dd"$on"77=)#toMdate(=22"$a"S3=#=dd"$on"
77=)#toMdate(=22"ap"S3=#=dd"$on"77=)) fo$ dual.
L'(ST(
"""""""""""""
22">(N"S3
U) ROUND
Round will ounds the date to whi!h it was eUual 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
Copy rights are reserved.
54
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 %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(toMdate(=1@"de!"3@=#=dd"$on"77=)#=7ea=)# ound(toMdate(=22"$a"
3D=#=dd"$on"77=)#=7ea=) fo$ dual.
ROUND(TOM ROUND(TOM
"""""""""""" """""""""""""""
32">(N"3C 32">(N"3D
SQL- sele!t ound(toMdate(=22"Jan"3@=#=dd"$on"77=)#=$onth=)# ound(toMdate(=2W"
Jan"3@=#=dd"$on"77=)#=$onth=) fo$ dual.

ROUND(TOM ROUND(TOM
""""""""""""" """""""""""""""
32">(N"3@ 32"B')"3@
SQL- sele!t ound(toMdate(=1D"de!"3D=#=dd"$on"77=)#=da7=)# ound(toMdate(=1S"de!"
3D=#=dd"$on"77=)#=da7=) fo$ dual.

ROUND(TOM ROUND(TOM
"""""""""""""" """"""""""""""
1@"D'C"3D 42"D'C"3D
SQL- sele!t toM!ha(ound(toMdate(=1@"de!"3D=#=dd"$on"77=))# =dd $on 7777
hh0$i0ss a$=) fo$ dual.
TOMC:(R(ROUND(TOMD(T'(=
"""""""""""""""""""""""""""""""""
1@ de! 133D 21033033 a$
) TRUNC
Tun! will !hops off the date to whi!h it was eUual to o less than the given date5
S7nta/0 tun! (date, (da7 X $onth X 7ea))
Copy rights are reserved.
55
If the se!ond paa$ete was year then it alwa7s etuns the fist da7 of the !uent
7ea5
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!(toMdate(=1@"de!"3@=#=dd"$on"77=)#=7ea=)# tun!(toMdate(=22"$a"
3D=#=dd"$on"77=)#=7ea=) fo$ dual.
TRUNC(TOM TRUNC(TOM
""""""""""""" """"""""""""""
32">(N"3@ 32">(N"3D
SQL- sele!t tun!(toMdate(=22"Jan"3@=#=dd"$on"77=)#=$onth=)# tun!(toMdate(=2W"Jan"
3@=#=dd"$on"77=)#=$onth=) fo$ dual.
TRUNC(TOM TRUNC(TOM
""""""""""""" """""""""""""
32">(N"3@ 32">(N"3@

SQL- sele!t tun!(toMdate(=1D"de!"3D=#=dd"$on"77=)#=da7=)# tun!(toMdate(=1S"de!"
3D=#=dd"$on"77=)#=da7=) fo$ dual.
TRUNC(TOM TRUNC(TOM
""""""""""""" """"""""""""""
1@"D'C"3D 1@"D'C"3D

SQL- sele!t toM!ha(tun!(toMdate(=1@"de!"3D=#=dd"$on"77=))# =dd $on 7777 hh0$i0ss
a$=) fo$ dual.
TOMC:(R(TRUNC(TOMD(T'(=
"""""""""""""""""""""""""""""""""
1@ de! 133D 21033033 a$
s) N'6MTIM'
This will give the desied ti$eTone;s date and ti$e5
Copy rights are reserved.
56
S7nta/0 newMti$e (date, current_time,one, de!ired_time,one)
(vaila%le ti$eTones 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 toM!ha(newMti$e(s7sdate#=g$t=#=7st=)#=dd $on 7777 hh0$i0ss a$=) fo$
dual.
TOMC:(R(N'6MTIM'(S*SD(T
"""""""""""""""""""""""""""""""""""
1@ de! 133D 310C2013 p$
SQL- sele!t toM!ha(newMti$e(s7sdate#=g$t=#=est=)#=dd $on 7777 hh0$i0ss a$=) fo$
dual.
TOMC:(R(N'6MTIM'(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
Copy rights are reserved.
57
SQL- sele!t !oales!e(=21"Jan"S3=#=24"Jan"SS=)# !oales!e(null#=21"Jan"S3=#=14"$a"
SW=#null) fo$ dual.
CO(L'SC'( CO(L'SC'(
""""""""""""" """"""""""""
21"Jan"S3 21"Jan"S3
MISC'LL(N'OUS BUNCTIONS
Uid
Use
9siTe
Ran&
DenseMan&
a) UID
This will etuns the intege value !oesponding to the use !uentl7 logged in5
'/0
SQL- sele!t uid fo$ dual.
UID
""""""""""
42S
%) 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
Copy rights are reserved.
58
'/0
SQL- sele!t vsiTe(214)# vsiTe(=!o$pute=)# vsiTe(=21"Jan"S3=) fo$ dual.
9SIZ'(214) 9SIZ'(=COMAUT'R=) 9SIZ'(=21">(N"S3=)
""""""""""""" """"""""""""""""""""""" """"""""""""""""""""""
4 W S
d) R(NF
This will give the non"seUuential an&ing5
'/0
SQL- sele!t ownu$#sal fo$ (sele!t sal fo$ e$p ode %7 sal des!).
RO6NUM S(L
"""""""""" """"""""""
2 C333
1 4333
4 4333
@ 1SVC
C 1WC3
D 1@C3
V 2D33
W 2C33
S 2433
23 21C3
22 21C3
21 2233
24 2333
2@ SC3
2C W33
SQL- sele!t an&(1SVC) within goup(ode %7 sal des!) fo$ e$p.
R(NF(1SVC)6IT:IN8ROUA(ORD'R)*S(LD'SC)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
@
d) D'NS'MR(NF
Copy rights are reserved.
59
This will give the seUuential an&ing5

'/0
SQL- sele!t denseMan&(1SVC) within goup(ode %7 sal des!) fo$ e$p.
D'NS'MR(NF(1SVC)6IT:IN8ROUA(ORD'R)*S(LD'SC)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
4
CON9'RSION BUNCTIONS
)inMtoMnu$
Chatoowid
Rowidto!ha
ToMnu$%e
ToM!ha
ToMdate
a) )INMTOMNUM
This will !onvet the %ina7 value to its nu$ei!al eUuivalent5
S7nta/0 %inMtoMnu$( binary_bit!)
'/0
SQL- sele!t %inMtoMnu$(2#2#3) fo$ dual.
)INMTOMNUM(2#2#3)
""""""""""""""""""""""""
D
If all the %its ae Teo then it podu!es Teo5
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
Copy rights are reserved.
60
This will !onvet an intenal oa!le ow identifie o owid to !haa!te sting5
d) TOMNUM)'R
This will !onvet a !ha o va!ha to nu$%e5
e) TOMC:(R
This will !onvet a nu$%e o date to !haa!te sting5
f) TOMD(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)
""""""""""
4WD33
%) (98
Copy rights are reserved.
61
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)
"""""""""""""""
1VCV52@1WD
!) M(+
This will give the $a/i$u$ of the values of the spe!ified !olu$n5
S7nta/0 $a/ (column)
'/0
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
Copy rights are reserved.
62
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@
CONSTR(INTS
Constaints ae !ategoiTed as follows5
Copy rights are reserved.
63
Do$ain integit7 !onstaints
Not null
Che!&
'ntit7 integit7 !onstaints
UniUue
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
This is used to inset the values %ased on spe!ified !ondition5
6e !an add this !onstaint in all thee levels5
Copy rights are reserved.
64
'/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) uniUue# na$e va!ha(23)# $a&s
nu$%e(4)).
SQL- !eate ta%le student(no nu$%e(1) !onstaint un uniUue# 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)#
uniUue(no)).
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
Copy rights are reserved.
65
!onstaint un uniUue(no)).
(LT'R L'9'L
SQL- alte ta%le student add uniUue(no).
SQL- alte ta%le student add !onstaint un uniUue(no).
ARIM(R* F'*
This is used to avoid dupli!ates and nulls5 This will wo& as !o$%ination of uniUue
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
Copy rights are reserved.
66
6e !an add this !onstaint in ta%le and alte levels onl75
'/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)).
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.
Copy rights are reserved.
67
SQL- alte ta%le e$p add !onstaint f& foeign &e7(deptno) efeen!es dept(deptno) on
delete !as!ade.
COMAOSIT' F'*S
( !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)#
uniUue(no#na$e)).
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
!onstaint un uniUue(no#na$e)).

UNIQU' ((LT'R L'9'L)
SQL- alte ta%le student add uniUue(no#na$e).
SQL- alte ta%le student add !onstaint un uniUue(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)).
Copy rights are reserved.
68

BOR'I8N F'* ((LT'R L'9'L)

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$$ediate
Defeed initiall7 defeed
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 uniUue(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 uniUue(no) defeed initiall7 defeed).
SQL- alte ta%le student add !onstaint un uniUue(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
'N()L'
This will ena%le the !onstaint5 )efoe ena%le# the !onstaint will !he!& the e/isting data5
Copy rights are reserved.
69
'/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
Copy rights are reserved.
70
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 Klow;
6hen C333 then Khigh;
'lse K$ediu$;
'nd !ase
Bo$ e$p.
S(L C(S'
""""" """"""""
C33 low
1C33 $ediu$
1333 $ediu$
4C33 $ediu$
4333 $ediu$
C333 high
@333 $ediu$
C333 high
2W33 $ediu$
2133 $ediu$
1333 $ediu$
1V33 $ediu$
1133 $ediu$
4133 $ediu$
D'B(ULT
Copy rights are reserved.
71
-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'
"""""" """""""""
2 a
22 %
SQL- inset into student values(null# K!;).
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
Copy rights are reserved.
72
'/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%Je!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
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 H =%o$%a7= whee s5addess5hno H 444.
SQL- sele!t no#na$e#s5addess5hno#s5addess5!it7 fo$ student s.
Copy rights are reserved.
73
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 H 222.
SQL- sele!t no#na$e#s5addess5hno#s5addess5!it7 fo$ student s.
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%Je!ts with the e/isting ta%le# o%Je!t views !o$e into pi!tue5
*ou define the o%Je!t and !eate a view whi!h elates this o%Je!t to the e/isting ta%le
nothing %ut ob#ect view5
O%Je!t views ae used to elate the use defined o%Je!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
Copy rights are reserved.
74
SQL- !eate t7pe add as o%Je!t(hno nu$%e(1)#!it7 va!ha(23)).?
SQL- !eate t7pe stud as o%Je!t(na$e va!ha(23)#addess add).?
4) Relate the o%Je!ts to the student ta%le %7 !eating the o%Je!t view
SQL- !eate view studentMov(no#studMinfo) 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%Je!t view
SQL- Inset into studentMov values(2#stud(Ksudha;#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%Je!t(na$e va!ha(23)#$a&s nu$%e(4)#
Me$%e fun!tion $a&sMf($a&s in nu$%e) etun nu$%e#
Aag$a esti!tMefeen!es($a&sMf#wnds#nds#wnps#fnps)).?
1) Defining t7pe %od7
SQL- Ceate t7pe %od7 stud as
Me$%e fun!tion $a&sMf($a&s in nu$%e) etun nu$%e is
)egin
Retun ($a&sG233).
'nd $a&sMf.
'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(Ksudha;#233)).
C) Using $ethod in sele!t
SQL- Sele!t s5info5$a&sMf(s5info5$a&s) fo$ student s.
"" :ee we ae using the pag$a esti!tMefeen!es to avoid the wites to the
Data%ase5
Copy rights are reserved.
75
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%Je!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#;Jagan;#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.
Copy rights are reserved.
76
"" 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


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$it5
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%Je!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 studentMte$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#;Jagan;#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
Copy rights are reserved.
77
SQL- Inset into ta%le(sele!t addess fo$ student whee noH2)
values(add(CCC#;!hennai;)).
V) Update in nested ta%les
SQL- Update ta%le(sele!t addess fo$ student whee noH1) s set s5!it7H;%o$%a7;
whee s5hno H 111.
W) Delete in nested ta%le
SQL- Delete ta%le(sele!t addess fo$ student whee noH4) s whee s5hnoH444.
D(T( MOD'L
(LLMCOLLMT*A'S
(LLMT*A'S
D)(MCOLLMT*A'S
D)(MT*A'S
US'RMCOLLMT*A'S
US'RMT*A'S
Copy rights are reserved.
78
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%$sMflash%a!&5ena%leMatMti$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%$sMflash%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 0H e/e! d%$sMflash%a!&5getMs7ste$M!hangeMnu$%e
!) To see the SCN
SQL- Aint s
d) Then e/e!ute the following po!edues
SQL- '/e! d%$sMflash%a!&5ena%leMatMs7ste$M!hangeMnu$%e(0s)
SQL- '/e! d%$sMflash%a!&5disa%le
Copy rights are reserved.
79
'+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 Uue7 the ta%le# oa!le eads the e/tenal ta%le and etuns the esults Just 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%Je!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- SUlplus s7ste$?$anage
SQL- Ceate die!to7 sa&ethMdi as K?9isd%?visd%?S5153?e/tenal;.
SQL- 8ant all on die!to7 sa&ethMdi to sa&eth.
SQL- Conn sa&eth?sa&eth
SQL- Spool dept5lst
SQL- Sele!t deptno XX K#; XX dna$e XX K#; XX lo! fo$ dept.
SQL- Spool off
CR'(TIN8 '+T'RN(L T()L'
SQL- Ceate ta%le deptMe/t
(deptno nu$%e(1)#
Dna$e va!ha(2@)#
Lo! va!ha(24))
OganiTation e/tenal ( t7pe oa!leMloade
Default die!to7 sa&ethMdi
(!!ess paa$etes
( e!ods deli$ited %7 newline
Bields te$inated %7 P#Q
( deptno nu$%e(1)#
Dna$e va!ha(2@)#
Copy rights are reserved.
80
Lo! va!ha(24)))
Lo!ation (K?9isd%?visd%?S5153?dept5lst;)).
S'L'CTIN8 D(T( BROM '+T'RN(L T()L'
SQL- sele!t E fo$ deptMe/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
%) Constaints not possi%le
)'N'BITS OB '+T'RN(L T()L'S
a) Queies of e/tenal ta%les !o$plete ve7 Uui!&l7 even though a full ta%le s!an id
eUuied with ea!h a!!ess
%) *ou !an Join 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%Je!ts5
'a!h of the ow o%Je!ts has an o%Je!t id value assigned to it5
The o%Je!t id assigned !an %e seen %7 using ef fun!tion5
Copy rights are reserved.
81
D'R'B
The deef fun!tion pefo$s opposite a!tion5
It ta&es a efeen!e value of o%Je!t id and etuns the value of the ow o%Je!ts5
9(LU'
'ven though the pi$a7 ta%le is o%Je!t ta%le# still it displa7s the ows in geneal
fo$at5
To displa7 the entie stu!tue of the o%Je!t# this will %e used5
'/0
2) !eate vendotMadt t7pe
SQL- Ceate t7pe vendoMadt as o%Je!t (vendoM!ode nu$%e(1)# vendoMna$e
va!ha(1)# vendoMaddess va!ha(23)).?
1) !eate o%Je!t ta%les vendos and vendos2
SQL- Ceate ta%le vendos of vendoMadt.
SQL- Ceate ta%le vendos2 of vendoMadt.
4) inset the data into o%Je!t ta%les
SQL- inset into vendos values(2# Ka;# Kh7d;).
SQL- inset into vendos values(1# K%;# K%ang;).
SQL- inset into vendos2 values(4# K!;# Kdelhi;).
SQL- inset into vendos2 values(@# Kd;# K!hennai;).
@) !eate anothe ta%le odes whi!h holds the vendoMadt t7pe also5
SQL- Ceate ta%le odes (odeMno nu$%e(1)# vendoMinfo ef vendoMadt).
O
SQL- Ceate ta%le odes (odeMno nu$%e(1)# vendoMinfo ef vendoMadt with
owid).
C) inset the data into odes ta%le
The vendoMinfo !olu$n in the following s7nta/es will stoe o%Je!t id of an7 ta%le
whi!h is efeen!ed %7 vendoMadt o%Je!t ( %oth vendos and vendos2)5
SQL- inset into odes values(22#(sele!t ef(v) fo$ vendos v whee vendoM!ode
H 2)).
SQL- inset into odes values(21#(sele!t ef(v) fo$ vendos v whee vendoM!ode
H 1)).
SQL- inset into odes values(24#(sele!t ef(v2) fo$ vendos2 v2 whee
vendoM!ode H 2)).
Copy rights are reserved.
82
SQL- inset into odes values(2@#(sele!t ef(v2) fo$ vendos2 v2 whee
vendoM!ode H 2)).
D) To see the o%Je!t ids of vendo ta%le
SQL- Sele!t ef(9) fo$ vendos v.
V) If 7ou see the vendoMinfo of odes it will show onl7 the o%Je!t ids not the values#
to see the values
SQL- Sele!t deef(o5vendoMinfo) fo$ odes o.
W) 'ven though the vendos ta%le is o%Je!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 vendoMadt# the odes ta%le will stoe the
o%Je!t ids of vendos onl7 %e!ause it is !onstained to that ta%le onl75
The vendoMinfo !olu$n in the following s7nta/es will stoe o%Je!t ids of vendos
onl75
SQL- Ceate ta%le odes (odeMno nu$%e(1)# vendoMinfo ef vendoMadt s!ope is
vendos).
O
SQL- Ceate ta%le odes (odeMno nu$%e(1)# vendoMinfo ef vendoMadt !onstaint f&
efeen!es vendos).
Copy rights are reserved.
83
O)>'CT 9I'6S 6IT: R'B'R'NC'S
To i$ple$ent the o%Je!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%Je!ts and ef !onstains5
)ut 7ou !an a!hieve this with out dopping the ta%les and without losing the data %7
!eating o%Je!t views with efeen!es5
'/0
a) Ceate the following ta%les
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).
Copy rights are reserved.
84
SQL- inset into student1(24#444#;%o$%a7;#2).
!) Ceate the t7pe
SQL- !eate o epla!e t7pe stud as o%Je!t(no nu$%e(1)#na$e va!ha(1)#$a&s
nu$%e(4)).?
d) 8eneating OIDs
SQL- Ceate o epla!e view student2Mov of stud with o%Je!t identifie(o id) (no) as
Sele!t E fo$ Student2.
e) 8eneating efeen!es
SQL- Ceate o epla!e view student1Mov as sele!t no#hno#!it7#
$a&eMef(student2Mov#id) id fo$ Student1.
d) Que7 the following
SQL- sele!t Efo$ student2Mov.
SQL- sele!t ef(s) fo$ student2Mov s.
SQL- sele!t values(s) fo$ student2Mov.
SQ- sele!t Efo$ student1Mov.
SQL- sele!t deef(s5id) fo$ student1Mov 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
(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
Copy rights are reserved.
85
(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%Je!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
(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
Copy rights are reserved.
86
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).
J) '/!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&ethMts.
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#V#W#S#23)#patition p4
values(22#21#24#2@#2C)# patition p@ values(2D#2V#2W#2S#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).
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
Copy rights are reserved.
87
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.
J) Moving a patition
SQL- (lte ta%le student $ove patition p1 ta%lespa!e sa&ethMts.
:(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*SMA2
S*SMA1
S*SMA4
S*SMA@
S*SMAC
%) 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#;!;).
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(s7sMp2).
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 pV.
Copy rights are reserved.
88
g) Tun!ate a patition
SQL- (lte ta%le student tun!ate patition pV.
h) '/!hanging a patition
SQL- (lte ta%le student e/!hange patition s7sMp2 with ta%le student1.
i) Moving a patition
SQL- (lte ta%le student $ove patition s7sMp1 ta%lespa!e sa&ethMts.
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*SMSU)A2
S*SMSU)A1
S*SMSU)A4
A1 \ S*SMSU)A@
S*SMSU)AC
S*SMSU)AD
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(s7sMsu%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).
Copy rights are reserved.
89
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).
D(T( MOD'L
(LLMINDMA(RTITIONS
(LLMINDMSU)A(RTITIONS
(LLMT()MA(RTITIONS
(LLMT()MSU)A(RTITIONS
D)(MINDMA(RTITIONS
D)(MINDMSU)A(RTITIONS
D)(MT()MA(RTITIONS
D)(MT()MSU)A(RTITIONS
US'RMINDMA(RTITIONS
US'RMINDMSU)A(RTITIONS
US'RMT()MA(RTITIONS
US'RMT()MSU)A(RTITIONS
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
Copy rights are reserved.
90
'/0
SQL- sele!t deptno# su$(sal) fo$ e$p goup %7 deptno.
D'ATNO SUM(S(L)
"""""""""" """"""""""
23 WVC3
13 23WVC
43 S@33
SQL- sele!t deptno#Jo%#su$(sal) fo$ e$p goup %7 deptno#Jo%.
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 2S33
13 M(N(8'R 1SVC
43 CL'RF SC3
43 M(N(8'R 1WC3
43 S(L'SM(N CD33
:(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#Jo%#su$(sal) tsal fo$ e$p goup %7 deptno#Jo% 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#Jo%#su$(sal) tsal fo$ e$p goup %7 deptno#Jo% having su$(sal) -
Copy rights are reserved.
91
4333 ode %7 Jo%.

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
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 Jo% !atego7 along wih the total sala7
fo individual depat$ents and the total sala7 of all the depat$ents5
SQL- Sele!t deptno#Jo%#su$(sal) fo$ e$p goup %7 ollup(deptno#Jo%).
D'ATNO >O) SUM(S(L)
"""""""""" """"""""" """"""""""
23 CL'RF 2433
Copy rights are reserved.
92
23 M(N(8'R 1@C3
23 AR'SID'NT C333
23 WVC3
13 (N(L*ST D333
13 CL'RF 2S33
13 M(N(8'R 1SVC
13 23WVC
43 CL'RF SC3
43 M(N(8'R 1WC3
43 S(L'SM(N CD33
43 S@33
1S31C
USIN8 8ROUAIN8
In the a%ove Uue7 it will give the total sala7 of the individual depat$ents %ut with a
%lan& in the Jo% !olu$n and gives the total sala7 of all the depat$ents with %lan&s in
deptno and Jo% !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(Jo%)#2#=(ll
Jo%s=#Jo%)#su$(sal) fo$ e$p goup %7 ollup(deptno#Jo%).
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 Jo%s WVC3
13 (N(L*ST D333
13 CL'RF 2S33
13 M(N(8'R 1SVC
13 (ll Jo%s 23WVC
43 CL'RF SC3
43 M(N(8'R 1WC3
43 S(L'SM(N CD33
43 (ll Jo%s S@33
(ll Depts (ll Jo%s 1S31C
Copy rights are reserved.
93
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 Jo% !atego7# the total sala7 fo
individual depat$ents# the total sala7 of all the depat$ents and the salaies in ea!h Jo%
!atego75

SQL- sele!t de!ode(gouping(deptno)#2#;(ll Depts;#deptno)#de!ode(gouping(Jo%)#2#;(ll
>o%s;#Jo%)#su$(sal) fo$ e$p goup %7 !u%e(deptno#Jo%).
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 WVC3
13 (N(L*ST D333
13 CL'RF 2S33
13 M(N(8'R 1SVC
13 (ll >o%s 23WVC
43 CL'RF SC3
43 M(N(8'R 1WC3
43 S(L'SM(N CD33
43 (ll >o%s S@33
(ll Depts (N(L*ST D333
(ll Depts CL'RF @2C3
(ll Depts M(N(8'R W1VC
(ll Depts AR'SID'NT C333
(ll Depts S(L'SM(N CD33
(ll Depts (ll >o%s 1S31C
Copy rights are reserved.
94
S'T OA'R(TORS
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
Copy rights are reserved.
95
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.
Copy rights are reserved.
96
9I'6S
( view is a data%ase o%Je!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
( view ta&es the output of the Uue7 and teats it as a ta%le# theefoe a view !an %e
thought of as a stoed Uue7 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 deptMv as sele!t Efo$ dept with ead onl7.
SQL- Ceate view deptMv as sele!t deptno# su$(sal) tMsal fo$ e$p goup %7 deptno.
Copy rights are reserved.
97
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
"" 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 P view has eos Q5
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 H 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 deptMv.
Copy rights are reserved.
98
D(T( MOD'L
(LLM9I'6
D)(M9I'6
US'RM9I'6S
S*NON*M (ND S'QU'NC'
S*NON*M
( s7non7$ is a data%ase o%Je!t# whi!h is used as an alias fo a ta%le# view o seUuen!e5
T*A'S
Aivate
Copy rights are reserved.
99
Au%li!
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%Je!t5
Aovides lo!ation tanspaen!7 fo e$ote o%Je!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'
( seUuen!e is a data%ase o%Je!t# whi!h !an geneate uniUue# seUuential intege values5
It !an %e used to auto$ati!all7 geneate pi$a7 &e7 o uniUue &e7 values5
( seUuen!e !an %e eithe in an as!ending o des!ending ode5
S7nta/0
Ceate seUuen!e ,!e._name- Nin!e$ent %t7 nO Nstat with nO N$a/value nO
N$invalue nO N!7!le?no!7!leO N!a!he?no!a!heO.
)7 defalult the seUuen!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 seUuen!e nu$%es and etains the$ in $e$o7 fo
faste a!!ess5
'/0
SQL- !eate seUuen!e s.
SQL- !eate seUuen!e s in!e$ent %7 23 stat with 233 $invalue C $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# Ksa&eth;).
Copy rights are reserved.
100
Initiall7 !uval is not defined and ne/tval is stating value5
(fte that ne/tval and !uval ae alwa7s eUual5
CR'(TIN8 (LA:("NUM'RIC S'QU'NC'

SQL- !eate seUuen!e s stat with 22214@.
SQL- Inset into student values (s5ne/tval XX tanslate
(s5ne/tval#;214@CDVWS3;#;a%!defghiJ;)).
(LT'RIN8 S'QU'NC'
6e !an alte the seUuen!e to pefo$ the following5
Set o eli$inate $invalue o $a/value5
Change the in!e$ent value5
Change the nu$%e of !a!hed seUuen!e nu$%es5
'/0
SQL- alte seUuen!e s $invalue C.
SQL- alte seUuen!e s in!e$ent %7 1.
SQL- alte seUuen!e s !a!he 23.
DROAAIN8 S'QU'NC'
SQL- dop seUuen!e s.
>OINS
The pupose of a Join is to !o$%ine the data a!oss ta%les5
( Join is a!tuall7 pefo$ed %7 the whee !lause whi!h !o$%ines the spe!ified ows
of ta%les5
If a Join involves in $oe than two ta%les then oa!le Joins fist two ta%les %ased on
the Joins !ondition and then !o$paes the esult with the ne/t ta%le and so on5
T*A'S
'Uui Join
Non"eUui Join
Self Join
Natual Join
Coss Join
Oute Join
Copy rights are reserved.
101
Left oute
Right oute
Bull oute
Inne Join
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&eth anal7st @@@ 23
111 sudha !le& 444 13
444 Jagan $anage 222 23
@@@ $adhu enginee 111 @3
'QUI >OIN
( Join whi!h !ontains an KH; opeato in the Joins !ondition5
'/0
SQL- sele!t e$pno#ena$e#Jo%#dna$e#lo! fo$ e$p e#dept d whee e5deptnoHd5deptno.
'MANO 'N(M' >O) DN(M' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sa&eth anal7st $&t h7d
Copy rights are reserved.
102
444 Jagan $anage $&t h7d
111 sudha !le& fin %ang
USIN8 CL(US'
SQL- sele!t e$pno#ena$e#Jo% #dna$e#lo! fo$ e$p e Join dept d using(deptno).
'MANO 'N(M' >O) DN(M' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sa&eth anal7st $&t h7d
444 Jagan $anage $&t h7d
111 sudha !le& fin %ang
ON CL(US'
SQL- sele!t e$pno#ena$e#Jo%#dna$e#lo! fo$ e$p e Join dept d on(e5deptnoHd5deptno).
'MANO 'N(M' >O) DN(M' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sa&eth anal7st $&t h7d
444 Jagan $anage $&t h7d
111 sudha !le& fin %ang
NON"'QUI >OIN
( Join whi!h !ontains an opeato othe than KH; in the Joins !ondition5
'/0
SQL- sele!t e$pno#ena$e#Jo%#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
Copy rights are reserved.
103
>oining the ta%le itself is !alled self Join5
'/0
SQL- sele!t e25e$pno#e15ena$e#e25Jo%#e15deptno fo$ e$p e2#e$p e1 whee
e25e$pnoHe15$g.
'MANO 'N(M' >O) D'ATNO
"""""""""" """""""""" """""""""" """"""""""
222 Jagan anal7st 23
111 $adhu !le& @3
444 sudha $anage 13
@@@ sa&eth enginee 23
N(TUR(L >OIN
Natual Join !o$paes all the !o$$on !olu$ns5
'/0
SQL- sele!t e$pno#ena$e#Jo%#dna$e#lo! fo$ e$p natual Join dept.
'MANO 'N(M' >O) DN(M' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sa&eth anal7st $&t h7d
444 Jagan $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#Jo%#dna$e#lo! fo$ e$p !oss Join dept.
'MANO 'N(M' >O) DN(M' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sa&eth anal7st $&t h7d
111 sudha !le& $&t h7d
444 Jagan $anage $&t h7d
@@@ $adhu enginee $&t h7d
222 sa&eth anal7st fin %ang
Copy rights are reserved.
104
111 sudha !le& fin %ang
444 Jagan $anage fin %ang
@@@ $adhu enginee fin %ang
222 sa&eth anal7st h %o$%a7
111 sudha !le& h %o$%a7
444 Jagan $anage h %o$%a7
@@@ $adhu enginee h %o$%a7
OUT'R >OIN
Oute Join 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#Jo%#dna$e#lo! fo$ e$p e left oute Join dept d
on(e5deptnoHd5deptno).
O
SQL- sele!t e$pno#ena$e#Jo%#dna$e#lo! fo$ e$p e#dept d whee
e5deptnoHd5deptno(G).
'MANO 'N(M' >O) DN(M' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sa&eth anal7st $&t h7d
444 Jagan $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#Jo%#dna$e#lo! fo$ e$p e ight oute Join dept d
on(e5deptnoHd5deptno).
O
Copy rights are reserved.
105
SQL- sele!t e$pno#ena$e#Jo%#dna$e#lo! fo$ e$p e#dept d whee e5deptno(G) H
d5deptno.
'MANO 'N(M' >O) DN(M' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sa&eth anal7st $&t h7d
444 Jagan $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#Jo%#dna$e#lo! fo$ e$p e full oute Join dept d
on(e5deptnoHd5deptno).
'MANO 'N(M' >O) DN(M' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
444 Jagan $anage $&t h7d
222 sa&eth 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#Jo%#dna$e#lo! fo$ e$p inne Join dept using(deptno).
'MANO 'N(M' >O) DN(M' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sa&eth anal7st $&t h7d
444 Jagan $anage $&t h7d
111 sudha !le& fin %ang
Copy rights are reserved.
106
SU)QU'RI'S (ND '+ISTS
SU)QU'RI'S
Nesting of Uueies# one within the othe is te$ed as a su%Uue75
( state$ent !ontaining a su%Uue7 is !alled a paent Uue75
Su%Uueies ae used to etieve data fo$ ta%les that depend on the values in the
ta%le itself5
T*A'S
Single ow su%Uueies
Multi ow su%Uueies
Multiple su%Uueies
Coelated su%Uueies
SIN8L' RO6 SU)QU'RI'S
In single ow su%Uue7# it will etun one value5
'/0
SQL- sele!t E fo$ e$p whee sal - (sele!t sal fo$ e$p whee e$pno H VCDD).
'MANO 'N(M' >O) M8R :IR'D(T' S(L COMM D'ATNO
"""""""""" """""""""" """"""""" """""""""" """""""""""" """"""" """""""""" """"""""""
VVWW SCOTT (N(L*ST VCDD 2S"(AR"WV 4333 13
VW4S FIN8 AR'SID'NT 2V"NO9"W2 C333 23
VS31 BORD (N(L*ST VCDD 34"D'C"W2 4333 13
MULTI RO6 SU)QU'RI'S
In $ulti ow su%Uue7# 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%Uue75
'/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
Copy rights are reserved.
107
"""""""""" """""""""" """"""""" """""""""" """"""""""" """""""" """""""""" """"""""""
VCDD >ON'S M(N(8'R VW4S 31"(AR"W2 1SVC 13
VVWW SCOTT (N(L*ST VCDD 2S"(AR"WV 4333 13
VW4S FIN8 AR'SID'NT 2V"NO9"W2 C333 23
VS31 BORD (N(L*ST VCDD 34"D'C"W2 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
"""""""""" """""""""" """"""""" """""""""" """"""""""""" """""" """""""""" """"""""""
VW4S FIN8 AR'SID'NT 2V"NO9"W2 C333 23
MULTIAL' SU)QU'RI'S
Thee is no li$it on the nu$%e of su%Uueies in!luded in a whee !lause5 It allows nesting
of a Uue7 within a su%Uue75
'/0
SQL- sele!t E fo$ e$p whee sal H (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
"""""""""" """""""""" """"""""" """""""""" """""""""""" """"""" """""""""" """"""""""
VVWW SCOTT (N(L*ST VCDD 2S"(AR"WV 4333 13
VS31 BORD (N(L*ST VCDD 34"D'C"W2 4333 13
CORR'L(T'D SU)QU'RI'S
( su%Uue7 is evaluated on!e fo the entie paent state$ent whee as a !oelated
su%Uue7 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 ,H (sele!t !ount(ena$e) fo$ e$p
whee e5deptno H deptno).
D'ATNO
""""""""""
13
43
Copy rights are reserved.
108
'+ISTS
'/ists fun!tion is a test fo e/isten!e5 This is a logi!al test fo the etun of ows fo$ a
Uue75
'/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 Uue7 !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 Uue7 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 e25deptnoHe15deptno goup %7 e15deptno having !ount(e15ena$e) - @)
ode %7 deptno#ena$e.
D'ATNO 'N(M'
"""""""""" """"""""""
13 (D(MS
13 BORD
13 >ON'S
13 SCOTT
13 SMIT:
43 (LL'N
43 )L(F'
Copy rights are reserved.
109
43 >(M'S
43 M(RTIN
43 TURN'R
43 6(RD
NOT '+ISTS
SQL- sele!t deptno#ena$e fo$ e$p e2 whee not e/ists (sele!t E fo$ e$p e1
whee e25deptnoHe15deptno goup %7 e15deptno having !ount(e15ena$e) - @) ode
%7 deptno#ena$e.
D'ATNO 'N(M'
""""""""" """"""""""
23 CL(RF
23 FIN8
23 MILL'R
6(LFUA TR''S (ND INLIN' 9I'6
6(LFUA TR''S
Using hiea!hi!al Uueies# 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 =HH-= XX pio ena$e# level fo$ e$p stat with ena$e H =FIN8=
!onne!t %7 pio e$pnoH$g.

'N(M'XX=HH-=XXARIOR'N(M L'9'L
"""""""""""""""""""""""""""""""""""" """"""""
FIN8HH- 2
>ON'SHH-FIN8 1
Copy rights are reserved.
110
SCOTTHH->ON'S 4
(D(MSHH-SCOTT @
BORDHH->ON'S 4
SMIT:HH-BORD @
)L(F'HH-FIN8 1
(LL'NHH-)L(F' 4
6(RDHH-)L(F' 4
M(RTINHH-)L(F' 4
TURN'RHH-)L(F' 4
>(M'SHH-)L(F' 4
CL(RFHH-FIN8 1
MILL'RHH-CL(RF 4
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
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: W33 2
>(M'S SC3 1
(D(MS 2233 4
6(RD 21C3 @
M(RTIN 21C3 C
MILL'R 2433 D
TURN'R 2C33 V
(LL'N 2D33 W
CL(RF 1@C3 S
)L(F' 1WC3 23
>ON'S 1SVC 22
Copy rights are reserved.
111
SCOTT 4333 21
BORD 4333 24
FIN8 C333 2@
LOCFS
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!t55fo 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!&
Copy rights are reserved.
112
Shae update lo!&
'/!lusive lo!&
S:(R' LOCF
( shae lo!& lo!&s the ta%le allowing othe uses to onl7 Uue7 %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 Uue7#
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.
'+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 Uue75 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
Copy rights are reserved.
113
( deadlo!& o!!us when tow uses have a lo!& ea!h on sepaate o%Je!t# and the7 want to
a!Uuie a lo!& on the ea!h othe;s o%Je!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%Je!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%Je!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 eUualit75
T*A'S
UniUue inde/
Non"uniUue inde/
)tee inde/
)it$ap inde/
Co$posite inde/
Revese &e7 inde/
Bun!tion"%ased inde/
Des!ending inde/
Do$ain inde/
O%Je!t inde/
Cluste inde/
Te/t inde/
Inde/ oganiTed ta%le
Aatition inde/
Lo!al inde/
Lo!al pefi/ed
Lo!al non"pefi/ed
8lo%al inde/
Copy rights are reserved.
114
8lo%al pefi/ed
8lo%al non"pefi/ed
UNIQU' IND'+
UniUue inde/es guaantee that no two ows of a ta%le have dupli!ate values in the !olu$ns
that define the inde/5 UniUue inde/ is auto$ati!all7 !eated when pi$a7 &e7 o uniUue
!onstaint is !eated5
'/0
SQL- !eate uniUue inde/ studMind on student(sno).
NON"UNIQU' IND'+
Non"UniUue inde/es do not i$pose the a%ove esti!tion on the !olu$n values5
'/0
SQL- !eate inde/ studMind on student(sno).
)TR'' IND'+ o (SC'NDIN8 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 Uui!& 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/ studMind 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/ studMind on student(se/).
COMAOSIT' IND'+
Copy rights are reserved.
115
( !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 adJa!ent
!olu$ns of the ta%le5
'/0
SQL- !eate %it$ap inde/ studMind 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 6hen the !olu$n is inde/ed in evese $ode
then the !olu$n values will %e stoed in an inde/ in diffeent %lo!&s as the stating value
diffes5 Su!h an aange$ent !an help avoid pefo$an!e degadations in inde/es whee
$odifi!ations to the inde/ ae !on!entated on a s$all set of %lo!&s5
'/0
SQL- !eate inde/ studMind on student(sno# evese).
6e !an e%uild a evese &e7 inde/ into no$al inde/ using the noevese &e7wod5
'/0
SQL- alte inde/ studMind 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
SQL- !eate inde/ studMind on student(uppe(sna$e)).
D'SC'NDIN8 IND'+
The ode used %7 )"tee inde/es has %een as!ending ode5 *ou !an !ategoiTe data in )"
tee inde/ in des!ending ode as well5 This featue !an %e useful in appli!ations whee
soting opeations ae eUuied5
Copy rights are reserved.
116
'/0
SQL- !eate inde/ studMind on student(sno des!).
T'+T IND'+
Que7ing te/t is diffeent fo$ Uue7ing data %e!ause wods have shades of $eaning#
elationships to othe wods# and opposites5 *ou $a7 want to sea!h fo wods that ae
nea ea!h othe# o wods that ae elated to thes5 These Uueies would %e e/te$el7
diffi!ult if all 7ou had availa%le was the standad elational opeatos5 )7 e/tending SQL to
in!lude te/t inde/es# oa!le te/t pe$its 7ou to as& ve7 !o$ple/ Uuestions a%out the te/t5
To use oa!le te/t# 7ou need to !eate a te(t inde( on the !olu$n in whi!h the te/t is stoed5
Te/t inde/ is a !olle!tion of ta%les and inde/es that stoe info$ation a%out the te/t stoed
in the !olu$n5
T*A'S
Thee ae seveal diffeent t7pes of inde/es availa%le in oa!le Si5 The fist# CONT'+T is
suppoted in oa!le Wi as well as oa!le Si5 (s of oa!le Si# 7ou !an use the CT+C(T te/t inde/
fo futhe enhan!e 7ou te/t inde/ $anage$ent and Uue7 !apa%ilities5
CONT'+T
CT+C(T
CT+RUL'
The CT+C(T inde/ t7pe suppots the tansa!tional s7n!honiTation of data %etween the %ase
ta%le and its te/t inde/5 6ith CONT'+T inde/es# 7ou need to $anuall7 tell oa!le to update
the values in the te/t inde/ afte data !hanges in %ase ta%le5 CT+C(T inde/ t7pes do not
geneate s!oe values duing the te/t Uueies5
:O6 TO CR'(T' T'+T IND'+[
*ou !an !eate a te/t inde/ via a spe!ial vesion of the !eate inde/ !o$$an5 Bo !onte/t
inde/# spe!if7 the !t/s7s5!onte/t inde/ t7pe and fo !t/!at inde/# spe!if7 the !t/s7s5!t/!at
inde/ t7pe5
'/0
Suppose 7ou have a ta%le !alled )OOFS with the following !olu$ns
Title# (utho# Info5
Copy rights are reserved.
117
SQL- !eate inde/ %oo&Minde/ on %oo&s(info) inde/t7pe is !t/s7s5!onte/t.
SQL- !eate inde/ %oo&Minde/ on %oo&s(info) inde/t7pe is !t/s7s5!t/!at.
T'+T QU'RI'S
On!e a te/t inde/ is !eated on the info !olu$n of )OOFS ta%le# te/t"sea!hing !apa%ilities
in!ease d7na$i!all75
CONT(INS < C(TS'(RC:
CONT(INS fun!tion ta&es two paa$etes \ the !olu$n na$e and the sea!h sting5
S7nta/0
Contains(inde(ed_column, !earc)_!tr).
If 7ou !eate a CT+C(T inde/# use the C(TS'(RC: fun!tion in pla!e of CONT(INS5 C(TS'(RC: ta&es
thee paa$etes \ the !olu$n na$e# the sea!h sting and the inde/ set5
S7nta/0
Contains(inde(ed_column, !earc)_!tr, inde(_!et).
:O6 ( T'+T Q'UR* 6ORFS[
6hen a fun!tion su!h as CONT(INS o C(TS'(RC: is used in Uue7# the te/t potion of the Uue7
is po!essed %7 oa!le te/t5 The e$ainde of the Uue7 is po!essed Just li&e a egula
Uue7 within the data%ase5 The esult of the te/t Uue7 po!essing and the egula Uue7
po!essing ae $eged to etun a single set of e!ods to the use5
S'(RC:IN8 BOR (N '+(CT M(TC: OB ( 6ORD
The following Uueies will sea!h fo a wod !alled Kppet7; whose s!oe is geate than
Teo5
SQL- sele!t E fo$ %oo&s whee !ontains(info# Kpopet7;) - 3.
SQL- sele!t E fo$ %oo&s whee !atsea!h(info# Kpopet7;# null) - 3.
Suppose if 7ou want to &now the s!oe of the Kpopet7; in ea!h %oo&# if s!oe values fo
individual sea!hes ange fo$ 3 to 23 fo ea!h o!!uen!e of the sting within the te/t then
use the s!oe fun!tion5
SQL- sele!t title# s!oe(23) fo$ %oo&s whee !ontains(info# Kpopet7;# 23) - 3.
Copy rights are reserved.
118
S'(RC:IN8 BOR (N '+(CT M(TC: OB MULTIAL' 6ORDS
The following Uueies will sea!h fo two wods5
SQL- sele!t E fo$ %oo&s whee !ontains(info# Kpopet7 (ND havests;) - 3.
SQL- sele!t E fo$ %oo&s whee !atsea!h(info# Kpopet7 (ND havests;# null) - 3.
Instead of using (ND 7ou !ould hae used an a$pesand(<)5 )efoe using this $ethod# set
define off so the < !haa!te will not %e seen as pat of a vaia%le na$e5
SQL- set define off
SQL- sele!t E fo$ %oo&s whee !ontains(info# Kpopet7 < havests;) - 3.
SQL- sele!t E fo$ %oo&s whee !atsea!h(info# Kpopet7 havests;# null) - 3.
The following Uueies will sea!h fo $oe than two wods5
SQL- sele!t E fo$ %oo&s whee !ontains(info# Kpopet7 (ND havests (ND wo&es;) - 3.
SQL- sele!t E fo$ %oo&s whee !atsea!h(info# Kpopet7 havests wo&es;# null) - 3.
The following Uueies will sea!h fo eithe of the two wods5
SQL- sele!t E fo$ %oo&s whee !ontains(info# Kpopet7 OR havests;) - 3.
Instead of OR 7ou !an use a veti!al line (X)5
SQL- sele!t E fo$ %oo&s whee !ontains(info# Kpopet7 X havests;) - 3.
SQL- sele!t E fo$ %oo&s whee !atsea!h(info# Kpopet7 X havests;# null) - 3.
In the following Uueies the (CCUM(a!!u$ulate) opeato adds togethe the s!oes of the
individual sea!hes and !o$paes the a!!u$ulated s!oe to the theshold value5
SQL- sele!t E fo$ %oo&s whee !ontains(info# Kpopet7 (CCUM havests;) - 3.
SQL- sele!t E fo$ %oo&s whee !atsea!h(info# Kpopet7 (CCUM havests;# null) - 3.
Instead of OR 7ou !an use a !o$$a(#)5
SQL- sele!t E fo$ %oo&s whee !ontains(info# Kpopet7 # havests;) - 3.
SQL- sele!t E fo$ %oo&s whee !atsea!h(info# Kpopet7 # havests;# null) - 3.
In the following Uueies the MINUS opeato su%ta!ts the s!oe of the se!ond te$;s sea!h
fo$ the s!oe of the fist te$;s sea!h5
Copy rights are reserved.
119
SQL- sele!t E fo$ %oo&s whee !ontains(info# Kpopet7 MINUS havests;) - 3.
SQL- sele!t E fo$ %oo&s whee !atsea!h(info# Kpopet7 NOT havests;# null) - 3.
Instead of MINUS 7ou !an use \ and instead of NOT 7ou !an use ]5
SQL- sele!t E fo$ %oo&s whee !ontains(info# Kpopet7 " havests;) - 3.
SQL- sele!t E fo$ %oo&s whee !atsea!h(info# Kpopet7 ] havests;# null) - 3.
S'(RC:IN8 BOR (N '+(CT M(TC: OB ( A:R(S'
The following Uueies will sea!h fo the phase5 If the sea!h phase in!ludes a eseved
wod within oa!le te/t# the 7ou $ust use !ul7 %a!es (^_) to en!lose te/t5
SQL- sele!t E fo$ %oo&s whee !ontains(info# Ktansa!tions ^and_ finan!es;) - 3.
SQL- sele!t E fo$ %oo&s whee !atsea!h(info# Ktansa!tions ^and_ finan!es;# null) - 3.
*ou !an en!lose the entie phase within !ul7 %a!es# in whi!h !ase an7 eseved wods
within the phase will %e teated as pat of the sea!h !iteia5
SQL- sele!t E fo$ %oo&s whee !ontains(info# K^tansa!tions and finan!es_;) - 3.
SQL- sele!t E fo$ %oo&s whee !atsea!h(info# K^tansa!tions and finan!es_;# null) - 3.
S'(RC:IN8 BOR 6ORDS T:(T (R' N'(R '(C: OT:'R
The following Uueies will sea!h fo the wods that ae in %etween the sea!h te$s5
SQL- sele!t E fo$ %oo&s whee !ontains(info# Kwo&es N'(R havests;) - 3.
Instead of N'(R 7ou !an use .5
SQL- sele!t E fo$ %oo&s whee !ontains(info# Kwo&es . havests;) - 3.
In CONT'+T inde/ Uueies# 7ou !an spe!if7 the $a/i$u$ nu$%e of wods %etween the
sea!h te$s5
SQL- sele!t E fo$ %oo&s whee !ontains(info# KN'(R((wo&es# havests)#23); - 3.
USIN8 6ILDC(RDS DURIN8 S'(RC:'S
Copy rights are reserved.
120
*ou !an use wild!ads to e/pand the list of valid sea!h te$s used duing 7ou Uue75 >ust
as in egula te/t"sting wild!ad po!essing# two wild!ads ae availa%le5
L " pe!ent sign. $ultiple"!haa!te wild!ad
M " undes!oe. single"!haa!te wild!ad
SQL- sele!t E fo$ %oo&s whee !ontains(info# Kwo&eL;) - 3.
SQL- sele!t E fo$ %oo&s whee !ontains(info# Kwo&MMM;) - 3.
S'(RC:IN8 BOR 6ORDS T:(T S:(R' T:' S(M' ST'M
Rathe than using wild!ads# 7ou !an use ste$"e/pansion !apa%ilities to e/pand the list of
te/t stings5 8iven the Kste$; of a wod# oa!le will e/pand the list of wods to sea!h fo to
in!lude all wods having the sa$e ste$5 Sa$ple e/pansions ae show hee5
Ala7 " pla7s pla7ing pla7ed pla7ful
SQL- sele!t E fo$ %oo&s whee !ontains(info# K`$anage;) - 3.
S'(RC:IN8 BOR BUZZ* M(TC:'S
( fuTT7 $at!h e/pands the spe!ified sea!h te$ to in!lude wods that ae spelled si$ilal7
%ut that do not ne!essail7 have the sa$e wod ste$5 BuTT7 $at!hes ae $ost helpful when
the te/t !ontains $isspellings5 The $isspellings !an %e eithe in the sea!hed te/t o in the
sea!h sting spe!ified %7 the use duing the Uue75
The following Uueies will not etun an7thing %e!ause its sea!h does not !ontain the wod
Khadest;5
SQL- sele!t E fo$ %oo&s whee !ontains(info# Khadest;) - 3.
It does# howeve# !ontains the wod Khavest;5 ( fuTT7 $at!h will etun the %oo&s
!ontaining the wod Khavest; even though Khavest; has a diffeent wod ste$ thant the
wod used as the sea!h te$5
To use a fuTT7 $at!h# pe!ede the sea!h te$ with a Uuestion $a&# with no spa!e
%etween the Uuestion $a& and the %eginning of the sea!h te$5
SQL- sele!t E fo$ %oo&s whee !ontains(info# K[hadest;) - 3.
S'(RC:IN8 BOR 6ORDS T:(T SOUND LIF' OT:'R 6ORDS
Copy rights are reserved.
121
SOUND'+# e/pands sea!h te$s %ased on how the wod sounds5 The SOUND'+ e/pansion
$ethod uses the sa$e te/t"$at!hing logi! availa%le via the SOUND'+ fun!tion in SQL5
To use the SOUND'+ option# 7ou $ust pe!ede the sea!h te$ with an e/!la$ation
$a&(I)5
SQL- sele!t E fo$ %oo&s whee !ontains(info# KIgate;) - 3.
IND'+ S*NC:RONIZ(TION
6hen using CONT'+T inde/es# 7ou need to $anage the te/t inde/ !ontents. the te/t inde/es
ae not updated when the %ase ta%le is updated5 6hen the ta%le was updated# its te/t inde/
is out of s7n! with the %ase ta%le5 To s7n! of the inde/# e/e!ute the S*NCMIND'+ po!edue of
the CT+MDDL pa!&age5
SQL- e/e! CT+MDDL5S*NCMIND'+(K%oo&Minde/;).
IND'+ S'TS
:istoi!all7# po%le$s with Uueies of te/t inde/es have o!!ued when othe !iteia ae
used alongside te/t sea!hes as pat of the whee !lause5 To i$pove the $i/ed Uue7
!apa%ilit7# oa!le featues inde/ sets5 The inde/es within the inde/ set $a7 %e stu!tued
elational !olu$ns o on te/t !olu$ns5
To !eate an inde/ set# use the CT+MDDL pa!&age to !eate the inde/ set and add inde/es to
it5 6hen 7ou !eate a te/t inde/# 7ou !an then spe!if7 the inde/ set it %elongs to5
SQL- e/e! CT+MDDL5CR'(T'MIND'+MS'T(K%oo&sMinde/Mset;).
The add non"te/t inde/es5
SQL- e/e! CT+MDDL5(DDMIND'+(K%oo&sMinde/Mset;# KtitleMinde/;).
Now !eate a CT+C(T te/t inde/5 Spe!if7 !t/s7s5!t/!at as the inde/ t7pe# and list the inde/
set in the paa$etes !lause5
SQL- !eate inde/ %oo&Minde/ on %oo&s(info) inde/t7pe is !t/s7s5!t/!at
paa$etes(Kinde/ set %oo&sMinde/Mset;).
IND'+"OR8(NIZ'D T()L'
Copy rights are reserved.
122
(n inde/"oganiTed ta%le &eeps its data soted a!!oding to the pi$a7 &e7 !olu$n values
fo the ta%le5 Inde/"oganiTed ta%les stoe thei data as if the entie ta%le was stoed in an
inde/5
(n inde/"oganiTed 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) oganiTation inde/.
A(RTITION IND'+
Si$ila to patitioning ta%les# oa!le allows 7ou to patition inde/es too5 Li&e ta%le
patitions# inde/ patitions !ould %e in diffeent ta%lespa!es5
LOC(L IND'+'S
Lo!al &e7wod tells oa!le to !eate a sepate inde/ fo ea!h patition5
In the lo!al pefi/ed inde/ the patition &e7 is spe!ified on the left pefi/5 6hen the
undel7ing ta%le is patitioned %aes on# sa7 two !olu$ns then the inde/ !an %e
pefi/ed on the fist !olu$n spe!ified5
Lo!al pefi/ed inde/es !an %e uniUue o non uniUue5
Lo!al inde/es $a7 %e easie to $anage than glo%al inde/es5
'/0
SQL- !eate inde/ studMinde/ on student(sno) lo!al.
8LO)(L IND'+'S
( glo%al inde/ $a7 !ontain values fo$ $ultiple patitions5
(n inde/ is glo%al pefi/ed if it is patitioned on the left pefi/ of the inde/ !olu$ns5
The glo%al !lause allows 7ou to !eate a non"patitioned inde/5
8lo%al inde/es $a7 pefo$ uniUueness !he!&s faste than lo!al (patitioned)
inde/es5
*ou !annot !eate glo%al inde/es fo hash patitions o su%patitions5
'/0
SQL- !eate inde/ studMinde/ on student(sno) glo%al.
Copy rights are reserved.
123
Si$ila to ta%le patitions# it is possi%le to $ove the$ fo$ one devi!e to anothe5 )ut
unli&e ta%le patitions# $ove$ent of inde/ patitions eUuies individual e!onstu!tion of
the inde/ o ea!h patition (onl7 in the !ase of glo%al inde/)5
'/0
SQL- alte inde/ studMind e%uild patition p1
Inde/ patitions !annot %e dopped $anuall75
The7 ae dopped i$pli!itl7 when the data the7 efe to is dopped fo$ the
patitioned ta%le5
MONITORIN8 US' OB IND'+'S
On!e 7ou tuned on the $onitoing the use of inde/es# then we !an !he!& whethe the ta%le
is hitting the inde/ o not5
To $onito the use of inde/ use the follwing s7nta/5
S7nta/0
alte inde/ inde(_name $onitoing usage.
then !he!& fo the details in 9`O)>'CTMUS(8' view5
If 7ou want to stop $onitoing use the following5
S7nta/0
alte inde/ inde(_name no$onitoing usage.
D(T( MOD'L
(LLMIND'+'S
D)(MIND'+'S
US'RMIND'+'S
(LLMIND"COLUMNS
D)("INDMCOLUMNS
US'RMINDMCOLUMNS
(LLMA(RTMIND'+'S
D)(MA(RTMIND'+'S
Copy rights are reserved.
124
US'RMA(RTMIND'+'S
9`O)>'CTMUS(8'
SQLEALUS COMMN(NDS
These !o$$ands does not eUuie state$ent te$inato and appli!a%le to the sessions #
those will %e auto$ati!all7 !leaed when session was !losed5
)R'(F
Copy rights are reserved.
125
This will %e used to %ea&up the data depending on the gouping5
S7nta/0
)ea& o %e Non ,!olu$n_name- on epotO

COMAUT'
This will %e used to pefo$ goup fun!tions on the data5
S7nta/0
Co$pute o !o$p NgoupMfun!tion of column_name on breain"_column_name o
epotO
TTITL'
This will give the top title fo 7ou epot5 *ou !an on o off the ttitle5
S7nta/0
Ttitle o ttit Nleft X !ente X ightO title_name s&ip n ot)er_c)aracter!
Ttitle o ttit Non o offO
)TITL'
This will give the %otto$ title fo 7ou epot5 *ou !an on o off the %title5
S7nta/0
)title o %tit Nleft X !ente X ightO title_name s&ip n ot)er_c)aracter!
)title o %tit Non o offO
'/0
SQL- %e on deptno s&ip 2 on epot
SQL- !o$p su$ of sal on deptno
SQL- !o$p su$ of sal on epot
SQL- ttitle !ente ='MALO*'' D'T(ILS= s&ip2 !ente =""""""""""""""""=
SQL- %title !ente =EE T:(NFQ EE=
SQL- sele!t E fo$ e$p ode %7 deptno.
Output0
'MALO*'' D'T(ILS
Copy rights are reserved.
126
"""""""""""""""""""""""
'MANO 'N(M' >O) M8R :IR'D(T' S(L COMM D'ATNO
"""""""""" """""""""" """"""""" """"""" """""""""""""" """""""" """""""""" """"""""""
VVW1 CL(RF M(N(8'R VW4S 3S">UN"W2 1@C3 23
VW4S FIN8 AR'SID'NT 2V"NO9"W2 C333
VS4@ MILL'R CL'RF VVW1 14">(N"W1 2433
"""""""""" EEEEEEEEEE
WVC3 su$
V4DS SMIT: CL'RF VS31 2V"D'C"W3 W33 13
VWVD (D(MS CL'RF VVWW 14"M(*"WV 2233
VS31 BORD (N(L*ST VCDD 34"D'C"W2 4333
VVWW SCOTT (N(L*ST VCDD 2S"(AR"WV 4333
VCDD >ON'S M(N(8'R VW4S 31"(AR"W2 1SVC
"""""""""" EEEEEEEEEE
23WVC su$
V@SS (LL'N S(L'SM(N VDSW 13"B')"W2 2D33 433 43
VDSW )L(F' M(N(8'R VW4S 32"M(*"W2 1WC3
VDC@ M(RTIN S(L'SM(N VDSW 1W"S'A"W2 21C3 2@33
VS33 >(M'S CL'RF VDSW 34"D'C"W2 SC3
VW@@ TURN'R S(L'SM(N VDSW 3W"S'A"W2 2C33 3
VC12 6(RD S(L'SM(N VDSW 11"B')"W2 21C3 C33
"""""""""" EEEEEEEEEE
S@33 su$
""""""""""
su$ 1S31C
EE T:(NFQ EE
CL'(R
This will !lea the e/isting %uffes o %ea& o !o$putations o !olu$ns fo$atting5
S7nta/0
Clea o !le %uffe X %e X !o$p X !ol.
'/0
Copy rights are reserved.
127
SQL- !lea %uffe
)uffe !leaed
SQL- !lea %e
)ea&s !leaed
SQL- !lea !o$p
Co$putes !leaed
SQL- !lea !ol
Colu$ns !leaed
C:(N8'
This will %e used to epla!e an7 stings in SQL state$ents5
S7nta/0
Change o !?old_!trin"?new_!trin"
If the old_!trin" epeats $an7 ti$es then new_!trin" epla!es the fist sting onl75
'/0
SQL- sele!t E fo$ det.
sele!t E fo$ det
E
'RROR at line 20
OR("33S@10 ta%le o view does not e/ist
SQL- !?det?dept
2E sele!t E fo$ dept
SQL- ?
D'ATNO DN(M' LOC
"""""""""" """""""""""""""" """""""""""
23 (CCOUNTIN8 N'6 *ORF
13 R'S'(RC: (LL(S
43 S(L'S C:IC(8O
@3 OA'R(TIONS )OSTON
COLUMN
This will %e used to in!ease o de!ease the width of the ta%le !olu$ns5
Copy rights are reserved.
128
S7nta/0
Colu$n o !ol ,column_name- fo$at ,num_'ormatXte(t_'ormat-
'/0
SQL- !ol deptno fo$at SSS
SQL- !ol dna$e fo$at a23
S(9'
This will %e used to save 7ou !uent SQL state$ent as SQL S!ipt file5
S7nta/0
Save o sav ,'ile_name-5Ne/tensionO epla!e o ep
If 7ou want to save the filena$e with e/isting filena$e the 7ou have to use epla!e option5
)7 default it will ta&e !.l as the e/tension5
'/0
SQL- save ss
Ceated file ss5sUl
SQL- save ss epla!e
6ote file ss5sUl
'+'CUT'
This will %e used to e/e!ute stoed su%poga$s o pa!&aged su%poga$s5
S7nta/0
'/e!ute o e/e! ,!ubpro"ram_name-
'/0
SQL- e/e! sa$pleMpo!
SAOOL
This will e!od the data when 7ou spool on# upto when 7ou sa7 spool off5 )7 default it will
give l!t as e/tension5
S7nta/0
Copy rights are reserved.
129
Spool on X off X out X ,'ile_name-5N'/tensionO
'/0
SQL- spool on
SQL- sele!t E fo$ dept.
D'ATNO DN(M' LOC
""""""""" """""""""""""" """"""""""
23 (CCOUNTIN8 N'6 *ORF
13 R'S'(RC: D(LL(S
43 S(L'S C:IC(8O
@3 OA'R(TIONS )OSTON
SQL- spool off
SQL- ed on5lst
SQL- sele!t E fo$ dept.
D'ATNO DN(M' LOC
""""""""" """""""""""""" """"""""""
23 (CCOUNTIN8 N'6 *ORF
13 R'S'(RC: D(LL(S
43 S(L'S C:IC(8O
@3 OA'R(TIONS )OSTON
SQL- spool off
LIST
This will give the !uent SQL state$ent5
S7nta/0
List o li N!tart_line_numberO Nend_line_numberO
'/0
SQL- sele!t
1 E
4 fo$
@ dept.
SQL- list
Copy rights are reserved.
130
2 sele!t
1 E
4 fo$
@E dept
SQL- list 2
2E sele!t
SQL- list 4
4E fo$
SQL- list 2 4
2 sele!t
1 E
4E fo$
INAUT
This will inset the new line to the !uent SQL state$ent5
S7nta/0
Input o in ,!trin"-
'/0
SQL- sele!t E
SQL- list
2E sele!t E
SQL- input fo$ dept
SQL- list
2 sele!t E
1E fo$ dept
(AA'ND
This will adds a new sting to the e/isting sting in the SQL state$ent without an7 spa!e5
S7nta/0
(ppend o app ,!trin"-
'/0
SQL- sele!t E
SQL- list
Copy rights are reserved.
131
2E sele!t E
SQL- append fo$ dept
2E sele!t E fo$ dept
SQL- list
2E sele!t E fo$ dept
D'L'T'
This will delete the !uent SQL state$ent lines5
S7nta/0
Delete o del ,!tart_line_number- N,end_line_number-O
'/0
SQL- sele!t
1 E
4 fo$
@ dept
C whee
D deptno
V -23.
SQL- list
2 sele!t
1 E
4 fo$
@ dept
C whee
D deptno
VE -23
SQL- del 2
SQL- list
2 E
1 fo$
4 dept
@ whee
C deptno
DE -23
SQL- del 1
SQL- list
Copy rights are reserved.
132
2 E
1 dept
4 whee
@ deptno
CE -23
SQL- del 1 @
SQL- list
2 E
1E -23
SQL- del
SQL- list
2 E
9(RI()L'
This will %e used to de!lae a vaia%le5
S7nta/0
9aia%le o va ,variable_name- ,variable_type-
'/0
SQL- va deptMna$e va!ha(2C)
SQL- sele!t dna$e into deptMna$e fo$ dept whee deptno H 23.
ARINT
This will %e used to pint the output of the vaia%les that will %e de!laed at SQL level5
S7nta/0
Aint ,variable_name-
'/0
SQL- pint deptMna$e
D'ATMN(M'
""""""""""""""
(CCOUNTIN8
ST(RT
Copy rights are reserved.
133
This will %e used to e/e!ute SQL s!ipts5
S7nta/0
stat ,filena$e_name-5sUl
'/0
SQL- stat ss5sUl
SQL- ass5sUl "" this will e/e!ute sUl s!ipt files onl75
:OST
This will %e used to intea!t with the OS level fo$ SQL5
S7nta/0
:ost NoperationO
'/0
SQL- host
SQL- host di
S:O6
Using this# 7ou !an see seveal !o$$ands that use the set !o$$and and status5
S7nta/0
Show all X ,!et_command-
'/0
SQL- show all
appinfo is OBB and set to bSQLEAlusb
aa7siTe 2C
auto!o$$it OBB
autopint OBB
autoe!ove7 OBB
autota!e OBB
%lo!&te$inato b5b (he/ 1e)
%title OBB and is the fist few !haa!tes of the ne/t S'L'CT state$ent
!$dsep OBB
!olsep b b
!o$pati%ilit7 vesion N(TI9'
Copy rights are reserved.
134
!on!at b5b (he/ 1e)
!op7!o$$it 3
COA*T*A'C:'CF is ON
define b<b (he/ 1D)
des!i%e D'AT: 2 LIN'NUM OBB IND'NT ON
e!ho OBB
editfile bafiedt5%ufb
e$%edded OBB
es!ape OBB
B''D)(CF ON fo D o $oe ows
flagge OBB
flush ON
SQL- sho veif7
veif7 OBB
RUN
This will uns the !o$$and in the %uffe5
S7nta/0
Run X ?
'/0
SQL- un
SQL- ?
STOR'
This will save all the set !o$$and statuses in a file5
S7nta/0
Stoe set ,filena$e-5Ne/tensionO N!eateO X Nepla!eO X NappendO
'/0
SQL- stoe set $7Msettings5s!$d
Ceated file $7Msettings5s!$d
SQL- stoe set $7Msettings5!$d epla!e
6ote file $7Msettings5!$d
SQL- stoe set $7Msettings5!$d append
(ppended file to $7Msettings5!$d
Copy rights are reserved.
135
BOLDM(BT'R
This will fold the !olu$ns one afte the othe5
S7nta/0
Colu$n ,column_name- foldMafte Nno_o'_line!O
'/0
SQL- !ol deptno foldMafte 2
SQL- !ol dna$e foldMafte 2
SQL- !ol lo! foldMafte 2
SQL- set heading off
SQL- sele!t E fo$ dept.
23
(CCOUNTIN8
N'6 *ORF
13
R'S'(RC:
D(LL(S
43
S(L'S
C:IC(8O
@3
OA'R(TIONS
)OSTON
BOLDM)'BOR'
This will fold the !olu$ns one %efoe the othe5
S7nta/0
Colu$n ,column_name- foldM%efoe Nno_o'_line!O
D'BIN'
This will give the list of all the vaia%les !uentl7 defined5
Copy rights are reserved.
136
S7nta/0
Define Nvaia%leMna$eO
'/0
SQL- define
D'BIN' MD(T' H b2D"M(*"3Vb (C:(R)
D'BIN' MCONN'CTMID'NTIBI'R H boa!leb (C:(R)
D'BIN' MUS'R H bSCOTTb (C:(R)
D'BIN' MARI9IL'8' H bb (C:(R)
D'BIN' MSQLALUSMR'L'(S' H b2332333133b (C:(R)
D'BIN' M'DITOR H bNotepadb (C:(R)
D'BIN' MOM9'RSION H bOa!le Data%ase 23g 'ntepise 'dition Release
2352535153 \ Aodu!tion 6ith the Aatitioning# OL(A and
Data Mining optionsb (C:(R)
D'BIN' MOMR'L'(S' H b2332333133b (C:(R)
S'T COMM(NDS
These !o$$ands does not eUuie state$ent te$inato and appli!a%le to the sessions #
those will %e auto$ati!all7 !leaed when session was !losed5
LIN'SIZ'
This will %e used to set the linesiTe5 Default linesiTe is W35
S7nta/0
Set linesiTe ,value-
'/0
SQL- set linesiTe 233
A(8'SIZ'
This will %e used to set the pagesiTe5 Default pagesiTe is 2@5
S7nta/0
Set pagesiTe ,value-
Copy rights are reserved.
137
'/0
SQL- set pagesiTe 43
D'SCRI)'
This will %e used to see the o%Je!t;s stu!tue5
S7nta/0
Des!i%e o des! ,ob#ect_name-
'/0
SQL- des! dept
Na$e Null[ T7pe
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" """""""""""""""""""""
D'ATNO NOT NULL NUM)'R(1)
DN(M' 9(RC:(R1(2@)
LOC 9(RC:(R1(24)
A(US'
6hen the displa7ed data !ontains hundeds o thousands of lines# when 7ou sele!t it then it
will auto$ati!all7 s!olls and displa7s the last page data5 To pevent this 7ou !an use this
pause option5 )7 using this it will displa7 the data !oespoinding to the pagesiTe with a
%ea& whi!h will !ontinue %7 hitting the etun &e75 )7 default this will %e off5
S7nta/0
Set pause on X off
'/0
SQL- set pause on
B''D)(CF
This will give the info$ation egading how$an7 ows 7ou sele!ted the o%Je!t5 )7 default
the feed%a!& $essage will %e displa7ed# onl7 when the o%Je!t !ontains $oe than C ows5
S7nta/0
Set feed%a!& ,value-
Copy rights are reserved.
138
'/0
SQL- set feed%a!& @
SQL- sele!t E fo$ dept.
D'ATNO DN(M' LOC
"""""""""" """""""""""""" """""""""""""
23 (CCOUNTIN8 N'6 *ORF
13 R'S'(RC: D(LL(S
43 S(L'S C:IC(8O
@3 OA'R(TIONS )OSTON
@ ows sele!ted5
:'(DIN8
If 7ou want to displa7 data without headings# then 7ou !an a!hieve with this5 )7 default
heading is on5
S7nta/0
Set heading on X off
'/0
SQL- set heading off
SQL- sele!t E fo$ dept.
23 (CCOUNTIN8 N'6 *ORF
13 R'S'(RC: D(LL(S
43 S(L'S C:IC(8O
@3 OA'R(TIONS )OSTON
S'R9'ROUTAUT
This will %e used to displa7 the output of the AL?SQL poga$s5 )7 default this will %e off5
S7nta/0
Set seveoutput on X off
'/0
Copy rights are reserved.
139
SQL- set seveoutput on
TIM'
This will %e used to displa7 the ti$e5 )7 default this will %e off5
S7nta/0
Set ti$e on X off
'/0
SQL- set ti$e on
2S0CD044 SQL-
TIMIN8
This will give the ti$e ta&en to e/e!ute the !uent SQL state$ent5 )7 default this will %e off5
S7nta/0
Set ti$ing on X off
'/0
SQL- set ti$ing on
SQL- sele!t E fo$ dept.
D'ATNO DN(M' LOC
"""""""""" """""""""""""" """""""""""""
23 (CCOUNTIN8 N'6 *ORF
13 R'S'(RC: D(LL(S
43 S(L'S C:IC(8O
@3 OA'R(TIONS )OSTON
'lapsed0 3303303353D
SQLAROMAT
This will %e used to !hange the SQL po$pt5
S7nta/0
Copy rights are reserved.
140
Set sUlpo$pt ,prompt-
'/0
SQL- set sUlpo$pt =OR(CL'-=
OR(CL'-
SQLC(S'
This will %e used to !hange the !ase of the SQL state$ents5 )7 default the !ase is $i/ed5
S7nta/0
Set sUl!ase uppe X $i/ed X lowe
'/0
SQL- set sUl!ase uppe
SQLT'RMIN(TOR
This will %e used to !hange the te$inato of the SQL state$ents5 )7 default the te$inato
is .5
S7nta/0
Set sUlte$inato ,termination_c)aracter-
'/0
SQL- set sUlte$inato 0
SQL- sele!t E fo$ dept0
D'BIN'
)7 default if the < !haa!te finds then it will teat as %ind vaia%le and as& fo the input5
Suppose 7ou want to teat it as a no$al !haa!te while inseting data# then 7ou !an
pevent this %7 using the define option5 )7 default this will %e on
S7nta/0
Set define on X off
'/0
SQL-inset into dept values(C3#=R<D=#=:*D=).
Copy rights are reserved.
141
'nte value fo d0
old 20 inset into dept values(C3#=R<D=#=:*D=)
new 20 INS'RT INTO D'AT 9(LU'S(C3#=R=#=:*D=)
SQL- set define off
SQL-inset into dept values(C3#=R<D=#=:*D=). "" hee it won;t as& fo value
N'6A(8'
This will shows how $an7 %lan& lines will %e left %efoe the epot5 )7 default it will leave
one %lan& line5
S7nta/0
Set newpage ,value-
'/0
SQL- set newpage 23
The Teo value fo newpage does not podu!e Teo %lan& lines instead it swit!hes to a
spe!ial popet7 whi!h podu!es a top"of"fo$ !haa!te (he/ 24) Just %efoe the date on
ea!h page5 Most $oden pintes espond to this %7 $oving i$$ediatel7 to the top of the
ne/t page# whee the piting of the epot will %egin5
:'(DS'A
This allow 7ou to indi!ate whee 7ou want to %ea& a page title o a !olu$n heading that
uns longe than one line5 The default heading sepaato is veti!al %a (X)5
S7nta/0
Set headsep ,!eparation_c)ar-
'/0
SQL- sele!t E fo$ dept.
D'ATNO DN(M' LOC
"""""""""" """""""""""""" """""""""""""
23 (CCOUNTIN8 N'6 *ORF
13 R'S'(RC: D(LL(S
43 S(L'S C:IC(8O
@3 OA'R(TIONS )OSTON
Copy rights are reserved.
142
SQL- set headsetp I
SQL- !ol dna$e heading =D'A(RTM'NT I N(M'=
SQL- ?
D'A(RTM'NT
D'ATNO N(M' LOC
"""""""""" """"""""""""""""" """"""""""
23 (CCOUNTIN8 N'6 *ORF
13 R'S'(RC: D(LL(S
43 S(L'S C:IC(8O
@3 OA'R(TIONS )OSTON
'C:O
6hen using a %ind vaia%le# the SQL state$ent is $aintained %7 e!ho5 )7 default this is off5
S7nta/0
Set e!ho on X off
9'RIB*
6hen using a %ind vaia%le# the old and new state$ents will %e $aintained %7 veif75 )7
default this is on5
S7nta/0
Set veif7 on X off
'/0
SQL- sele!t E fo$ dept whee deptno H <dno.
'nte value fo dno0 23
old 20 sele!t E fo$ dept whee deptno H <dno
new 20 sele!t E fo$ dept whee deptno H 23
D'ATNO DN(M' LOC
"""""""""" """""""""""""""" """""""""""
23 (CCOUNTIN8 N'6 *ORF
SQL- set veif7 off
Copy rights are reserved.
143
SQL- sele!t E fo$ dept whee deptno H <dno.
'nte value fo dno0 13
D'ATNO DN(M' LOC
"""""""""" """"""""""""" """""""""""
13 R'S'(RC: D(LL(S
ANO
This will give displa7s the page nu$%es5 )7 default the value would %e Teo5
'/0
SQL- !ol hiedate newMvalue /toda7 nopint fo$at a2 tun!
SQL- ttitle left /toda7 ight =page= sUl5pno
SQL- sele!t E fo$ e$p whee deptno H 23.
3S">UN"W2 page 2
'MANO 'N(M' >O) M8R S(L COMM D'ATNO
"""""""""" """""""""" """"""""""""""" """"""""" """"" """""""""" """"""""""
VVW1 CL(RF M(N(8'R VW4S 1@C3 23
VW4S FIN8 AR'SID'NT C333 23
VS4@ MILL'R CL'RF VVW1 2433 23
In the a%ove nopint tells SQLALUS not to displa7 this !olu$n when it pints the esults of the
SQL state$ent5 Dates that have %een efo$atted %7 TOMC:(R get a default width of a%out
233 !haa!tes5 )7 !hanging the fo$at to a2 tun!# 7ou $ini$iTe this effe!t5 N'6M9(LU'
insets !ontents of the !olu$n etieved %7 the SQL state$ent into a vaia%le !alled /toda75
Copy rights are reserved.
144
SA'CI(L BIL'S
LO8IN5sUl
If 7ou would li&e SQLALUS to define 7ou own envion$ental settings# put all the eUuied
!o$$ands in a file na$ed login5sUl5 This is a spe!ial filena$e that SQLALUS alwa7s loo&s fo
wheneve it stats up5 If it finds login5sUl# it e/e!utes an7 !o$$ands in it as if 7ou had
enteed then %7 hand5 *ou !an put an7 !o$$and in login5sUl that 7ou !an use in SQLALUS#
in!luding SQLALUS !o$$ands and SQL state$ents5 (ll ot the$ e/e!uted %efoe SQLALUS gives
7ou the SQL- po$pt5
8LO8IN5sUl
This is used in the sa$e wa7s as LO8IN5sUl %ut to esta%lish default SQLALUS settings fo all
uses of a data%ase5
Copy rights are reserved.
145
IMAORT(NT QU'RI'S
2) To find the nth ow of a ta%le
SQL- Sele!t Efo$ e$p whee owid H (sele!t $a/(owid) fo$ e$p whee ownu$
,H @).
O
SQL- Sele!t Efo$ e$p whee ownu$ ,H @ $inus sele!t Efo$ e$p whee ownu$
,H 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# Jo%# hiedate# !o$$# deptno# sal).
O
SQL- Sele!t e$pno#ena$e#sal#Jo%#hiedate#!o$$ # !ount(E) fo$ e$p goup %7
e$pno#ena$e#sal#Jo%#hiedate#!o$$ having !ount(E) -H2.

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#Jo%#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) -H 2.
Copy rights are reserved.
146
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).
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).
V) :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.
W) To eset the ti$e to the %eginning of the da7
SQL- Sele!t toM!ha(tun!(s7sdate)#;dd"$on"7777 hh0$i0ss a$;) fo$ dual.
S) 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 ownu$ ,H C).

Copy rights are reserved.
147
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
/ptimi,ed compiler
.
To !hange the opti$iTe settings fo the entie data%ase# set the data%ase paa$ete
ALSQLMOATIMIZ'ML'9'L5 9alid settings ae as follows
3 " No opti$iTation
2 " Modeate opti$iTation
1 " (ggessive opti$iTation
These settings ae also $odifia%le fo the !uent session5
SQL- alte session set plsUlMopti$TeMlevelH1.
Oa!le etains opti$iTe 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 plsUlMopti$iTeMlevelH2.
SQL- (lte po!edue po! !o$pile euse settings.
Compile0time warnin"!.
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)ecin"5
Copy rights are reserved.
148
To ena%le these wanings fo the entie data%ase# set the data%ase paa$ete
ALSQLM6(RNIN8S5 These settings ae also $odifia%le fo the !uent session5
SQL- alte session set plsUlMwanings H Kena%le0all;.
The a%ove !an %e a!hieved using the %uilt"in pa!&age D)MSM6(RNIN85
Conditional compilation.
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
$upport 'or non0!e.uential collection! in 1/2344.
5mproved datatype !upport.
6actrace an e(ception to it! line number.
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)MSMUTILIT*5BORM(TM'RRORM)(CFTR(C' fun!tion to o%tain that sta!&
and $anipulate it poga$$ati!all7 within 7ou poga$5
$et operator! 'or ne!ted table!.
$upport 'or re"ular e(pre!!ion!.
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'+AMLIF'
R'8'+AMINSTR
R'8'+AMSU)STR
R'8'+AMR'AL(C'
7ro"rammer0de'ined .uotin" mec)ani!m.
Stating with oa!le data%ase 23g elease 2# 7ou !an define 7ou own Uuoting
$e!hanis$ fo sting liteals in %oth SQL and AL?SQL5
Copy rights are reserved.
149
Use the !haa!tes U;(U followed %7 a single Uuote) to note the poga$$e"defined
delie$ete fo 7ou sting liteal5
'/0
D'CL(R'
v va!ha(23) 0H =!o$pute=.
)'8IN
d%$sMoutput5putMline(U=Ev H E= XX v).
d%$sMoutput5putMline(U=`v H `= XX v).
'ND.
Output0
v H !o$pute
v H !o$pute
8any new built0in paca"e!.
D)MSMSC:'DUL'R
Repesents a $aJo update to D)MSM>O)5 D)MSMSC:'DUL'R povides $u!h i$poved
fun!tionalit7 fo s!heduling and e/e!uting Jo%s defined via stoed po!edues5
D)MSMCR*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%aliTation suppot fo en!7pting data a!oss
diffeent !haa!tesets5
D)MSMMONITOR
Aovides an (AI to !ontol additional ta!ing and statisti!s gatheing of sessions5
D)MSM6(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'
Copy rights are reserved.
150
Oa!le has defined in this spe!ial pa!&age5 Oa!le defines Uuite 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 seUuentiall7 o nested5
)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%$sMoutput5putMline(KM7 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
Copy rights are reserved.
151
'/0
,,$7M%loo!&--
)'8IN
D%$sMoutput5putMline(KM7 fist poga$;)0
'ND.
SU)ARO8R(MS
Su%poga$s ae po!edues and fun!tions5 The7 !an %e stoed in the data%ase as stand"
alone o%Je!ts# as pat of pa!&age o as $ethods of an o%Je!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%Je!ts# su!h as vaia%les# !usos# t7pes and
su%poga$s5 Identifies !onsists of a lette# optionall7 followed %7 an7 seUuen!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 Uuotation $a&s5
'/0
D'CL(R'
bab nu$%e 0H C.
b(b nu$%e 0H D.
)'8IN
d%$sMoutput5putMline(=a H = XX a).
d%$sMoutput5putMline(=( H = XX ().
'ND.
Copy rights are reserved.
152
Output0
a H D
( H D
COMM'NTS
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%$sMoutput5putMline(Khello;). "" 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%$sMoutput5putMline(Khello;). ?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 0H C.
! nu$%e default D.
CONST(NT D'CL'R(TIONS
Copy rights are reserved.
153
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 0H C.
! !onstant nu$%e default D.
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 null0H 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 LT*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 dept5deptnoLt7pe.
Su%t7pe tMnu$%e is nu$%e.
a tMnu$%e.
Su%t7pe tMsno is student5snoLt7pe.
9Msno tMsno.
R'CORD (NC:ORIN8
Use the LRO6T*A' atti%ute to define 7ou e!od stu!tue %ased on a ta%le5
'/0
Copy rights are reserved.
154
cD'CL(R'
9Mdept deptLowt7pe.
)'N'BITS OB (NC:OR'D D'CL(R(TIONS
S7n!honiTation with data%ase !olu$ns5
No$aliTation of lo!al vaia%les5
ARO8R(MM'R"D'BIN'D T*A'S
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 %ina7Mintege ange 25512@V@W4D@V.
In the a%ove de!laation a vaia%le that is de!laed as positive !an stoe onl7 ingege
geate than Teo even though %ina7Mintege anges fo$ "12@V@W4D@V55G12@V@W4D@V5
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.
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
Copy rights are reserved.
155
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
D'CL(R'
a va!ha(23).
)'8IN
sele!t deptno into a fo$ dept whee dna$eH=(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 Uualif7 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 %
Copy rights are reserved.
156
)'8IN
"""""
'ND.
""""""
'ND.
'/10
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
'ND.
"""""
'ND.
'/40
,,$7M%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 ,,$7M%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
Copy rights are reserved.
157
Case with no else
La%eled !ase
Sea!hed !ase
Si$ple loop
6hile loop
Bo loop
8oto and La%els
IB"T:'N"'LS'
S7nta/0
If ,condition1- then
$e.uence o' !tatement!;
'lsif ,condition1- then
$e.uence o' !tatement!;
RR
'lse
$e.uence o' !tatement!;
'nd if.
'/0
D'CL(R'
dno nu$%e(1).
)'8IN
sele!t deptno into dno fo$ dept whee dna$e H =(CCOUNTIN8=.
if dno H 23 then
d%$sMoutput5putMline(=Lo!ation is N'6 *ORF=).
elsif dno H 13 then
d%$sMoutput5putMline(=Lo!ation is D(LL(S=).
elsif dno H 43 then
d%$sMoutput5putMline(=Lo!ation is C:IC(8O=).
else
d%$sMoutput5putMline(=Lo!ation is )OSTON=).
end if.
'ND.
Output0
Lo!ation is N'6 *ORF
Copy rights are reserved.
158
C(S'
S7nta/0
Case te!t0variable
6hen value1 then !e.uence o' !tatement!;
6hen value2 then !e.uence o' !tatement!;
RR
6hen valuen then !e.uence o' !tatement!;
'lse !e.uence o' !tatement!;
'nd !ase.
'/0
D'CL(R'
dno nu$%e(1).
)'8IN
sele!t deptno into dno fo$ dept whee dna$e H =(CCOUNTIN8=.
!ase dno
when 23 then
d%$sMoutput5putMline(=Lo!ation is N'6 *ORF=).
when 13 then
d%$sMoutput5putMline(=Lo!ation is D(LL(S=).
when 43 then
d%$sMoutput5putMline(=Lo!ation is C:IC(8O=).
else
d%$sMoutput5putMline(=Lo!ation is )OSTON=).
end !ase.
'ND.
Output0
Lo!ation is N'6 *ORF
C(S' 6IT:OUT 'LS'
S7nta/0
Case te!t0variable
6hen value1 then !e.uence o' !tatement!;
6hen value2 then !e.uence o' !tatement!;
RR
Copy rights are reserved.
159
6hen valuen then !e.uence o' !tatement!;
'nd !ase.
'/0
D'CL(R'
dno nu$%e(1).
)'8IN
sele!t deptno into dno fo$ dept whee dna$e H =(CCOUNTIN8=.
!ase dno
when 23 then
d%$sMoutput5putMline(=Lo!ation is N'6 *ORF=).
when 13 then
d%$sMoutput5putMline(=Lo!ation is D(LL(S=).
when 43 then
d%$sMoutput5putMline(=Lo!ation is C:IC(8O=).
when @3 then
d%$sMoutput5putMline(=Lo!ation is )OSTON=).
end !ase.
'ND.
Output0
Lo!ation is N'6 *ORF
L()'L'D C(S'
S7nta/0
,,label--
Case te!t0variable
6hen value1 then !e.uence o' !tatement!;
6hen value2 then !e.uence o' !tatement!;
RR
6hen valuen then !e.uence o' !tatement!;
'nd !ase.
'/0
D'CL(R'
dno nu$%e(1).
)'8IN
sele!t deptno into dno fo$ dept whee dna$e H =(CCOUNTIN8=.
,,$7M!ase--
Copy rights are reserved.
160
!ase dno
when 23 then
d%$sMoutput5putMline(=Lo!ation is N'6 *ORF=).
when 13 then
d%$sMoutput5putMline(=Lo!ation is D(LL(S=).
when 43 then
d%$sMoutput5putMline(=Lo!ation is C:IC(8O=).
when @3 then
d%$sMoutput5putMline(=Lo!ation is )OSTON=).
end !ase $7M!ase.
'ND.
Output0
Lo!ation is N'6 *ORF
S'(RC:'D C(S'
S7nta/0
Case
6hen ,condition1- then !e.uence o' !tatement!;
6hen ,condition2- then !e.uence o' !tatement!;
RR
6hen ,conditionn- then !e.uence o' !tatement!;
'nd !ase.
'/0
D'CL(R'
dno nu$%e(1).
)'8IN
sele!t deptno into dno fo$ dept whee dna$e H =(CCOUNTIN8=.
!ase dno
when dno H 23 then
d%$sMoutput5putMline(=Lo!ation is N'6 *ORF=).
when dno H 13 then
d%$sMoutput5putMline(=Lo!ation is D(LL(S=).
when dno H 43 then
d%$sMoutput5putMline(=Lo!ation is C:IC(8O=).
when dno H @3 then
d%$sMoutput5putMline(=Lo!ation is )OSTON=).
end !ase.
Copy rights are reserved.
161
'ND.
Output0
Lo!ation is N'6 *ORF
SIMAL' LOOA
S7nta/0
Loop
$e.uence o' !tatement!;
'/it when ,condition-.
'nd loop.
In the s7nta/ e/it when ,condition- is eUuivalent to
If ,condition- then
'/it.
'nd if.
'/0
D'CL(R'
i nu$%e 0H 2.
)'8IN
loop
d%$sMoutput5putMline(=i H = XX i).
i 0H i G 2.
e/it when i - C.
end loop.
'ND.
Output0
i H 2
i H 1
i H 4
i H @
i H C
6:IL' LOOA
S7nta/0
6hile ,!ondition- loop
Copy rights are reserved.
162
$e.uence o' !tatement!;
'nd loop.
'/0
D'CL(R'
i nu$%e 0H 2.
)'8IN
6hile i ,H C loop
d%$sMoutput5putMline(=i H = XX i).
i 0H i G 2.
end loop.
'ND.
Output0
i H 2
i H 1
i H 4
i H @
i H C
BOR LOOA
S7nta/0
Bo ,loop_counter_variable- in low_bound55)i")_bound loop
$e.uence o' !tatement!;
'nd loop.
'/20
)'8IN
Bo i in 255C loop
d%$sMoutput5putMline(=i H = XX i).
end loop.
'ND.
Output0
i H 2
i H 1
i H 4
i H @
i H C
Copy rights are reserved.
163
'/10
)'8IN
Bo i in evese 255C loop
d%$sMoutput5putMline(=i H = XX i).
end loop.
'ND.
Output0
i H C
i H @
i H 4
i H 1
i H 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 Uui!&l7 $ove to the end of $7
poga$ if the state of $7 data indi!ates that no futhe po!essing is eUuied5 )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 eUuied thee5 'ven though
NULL deos nothing# it is still an e/e!uta%le state$ent5
Copy rights are reserved.
164
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
'/0
)'8IN
Bo i in 255C loop
d%$sMoutput5putMline(=i H = XX i).
if i H @ then
goto e/itMloop.
end if.
end loop.
,,e/itMloop--
Null.
'ND.
Output0
i H 2
i H 1
i H 4
i H @
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
Copy rights are reserved.
165
AR8(M( in!truction_to_compiler5
AL?SQL offes seveal pag$as0
(UTONOMOUSMTR(NS(CTION
'+C'ATIONMINIT
R'STRICTMR'B'R'NC'S
S'RI(LL*MR'US()L'
Copy rights are reserved.
166
SU)ARO8R(MS
AROC'DUR'S
( po!edue is a $odule that pefo$s one o $oe a!tions5
S7nta/0
Ao!edue Ns!he$a5Oname N(parameter1 N#parameter2 RO)O
Nauthid define X !uentMuseO is
"" Nde!laationsO
)egin
"" e/e!uta%le state$ents
N'/!eption
"" e/!eption handlesO
'nd Nna$eO.
In the a%ove aut)id !lause defines whethe the po!edue will e/e!ute unde the authoit7
of the define of the po!edue o unde the authoit7 of the !uent use5
BUNCTIONS
( fun!tion is a $odule that etuns a value5
S7nta/0
Bun!tion Ns!he$a5Oname N(parameter1 N#parameter2 RO)O
Retun etunMdatat7pe
Nauthid define X !uentMuseO
Ndete$inisti!O
NpaallelMena%leO is
"" Nde!laationsO
)egin
"" e/e!uta%le state$ents
N'/!eption
"" e/!eption handlesO
'nd Nna$eO.
In the a%ove aut)id !lause defines whethe the po!edue will e/e!ute unde the authoit7
of the define of the po!edue o unde the authoit7 of the !uent use5
Copy rights are reserved.
167
-etermini!tic !lause defines# an opti$iTation hint that lets the s7ste$ use a saved !op7 of
the fun!tion;s etun esult# if availa%le5 The Uuet7 opti$iTe !an !hoose whethe to use the
saved !op7 o e"!all the fun!tion5
7arallel_enable !lause defines# an opti$iTation hint that ena%les the fun!tion to %e
e/e!uted in paallel when !alled fo$ within S'L'CT state$ent5
A(R(M'T'R MOD'S
In (Default)
Out
In out
IN
In paa$ete will a!t as pl9!.l con!tant5
OUT
Out paa$ete will a!t as unintiali,ed variable5
*ou !annot povide a default value to an out paa$ete5
(n7 assign$ents $ade to out paa$ete ae olled %a!& when an e/!eption is aised
in the poga$5
(n a!tual paa$ete !oesponding to an out fo$al paa$ete $ust %e a vaia%le5
IN OUT
In out paa$ete will a!t as initiali,ed variable5
(n a!tual paa$ete !oesponding to an in out fo$al paa$ete $ust %e a vaia%le5
D'B(ULT A(R(M'T'RS
Default Aaa$etes will not allow in the be"innin" and middle5
/ut and 5n /ut paa$etes !an not have default values5
'/0
po!edue p(a in nu$%e default C# % in nu$%e default D# ! in nu$%e default V) \ valid
po!edue p(a in nu$%e# % in nu$%e default D# ! in nu$%e default V) \ valild
Copy rights are reserved.
168
po!edue p(a in nu$%e# % in nu$%e# ! in nu$%e default V) \ 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
NOT(TIONS
Notations ae of two t7pes5
Aositional notation
Na$e notation
6e !an !o$%ine positional and na$e notation %ut positional notation !an not %e followed
%7 the na$e notation5
'/0
Suppose we have a po!edue po!(a nu$%e#% nu$%e#! nu$%e) and we have one
anon7$ous %lo!& whi!h !ontains v2#v1# and v4.
SQL- e/e! po! (v2#v1#v4) "" Aositional notation
SQL- e/e! po! (aH-v2#%H-v1#!H-v4) "" Na$ed notation
BORM(L (ND (CTU(L A(R(M'T'RS
Aaa$etes whi!h ae in !alling su%poga$ ae actual parameter!5
Aaa$etes whi!h ae in !alled su%poga$ ae 'ormal parameter!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
'/20
CR'(T' OR R'AL(C' AROC'DUR' S(MAL'(a in nu$%e#% out nu$%e#! in out
nu$%e) is
)'8IN
d%$sMoutput5putMline(=(fte !all=).
d%$sMoutput5putMline(=a H = XX a XX= % H = XX % XX = ! H = XX !).
% 0H 23.
! 0H 13.
d%$sMoutput5putMline(=(fte assign$ent=).
d%$sMoutput5putMline(=a H = XX a XX= % H = XX % XX = ! H = XX !).
'ND S(MAL'.
Copy rights are reserved.
169
D'CL(R'
v2 nu$%e 0H @.
v1 nu$%e 0H C.
v4 nu$%e 0H D.
)'8IN
d%$sMoutput5putMline(=)efoe !all=).
d%$sMoutput5putMline(=v2 H = XX v2 XX = v1 H = XX v1 XX = v4 H = XX v4).
sa$ple(v2#v1#v4).
d%$sMoutput5putMline(=(fte !o$pletion of !all=).
d%$sMoutput5putMline(=v2 H = XX v2 XX = v1 H = XX v1 XX = v4 H = XX v4).
'ND.
Output0
)efoe !all
v2 H @ v1 H C v4 H D
(fte !all
a H @ % H ! H D
(fte assign$ent
a H @ % H 23 ! H 13
(fte !o$pletion of !all
v2 H @ v1 H 23 v4 H 13
'/10
CR'(T' OR R'AL(C' BUN(a in nu$%e#% out nu$%e#! in out nu$%e) etun
nu$%e IS
)'8IN
d%$sMoutput5putMline(=(fte !all=).
d%$sMoutput5putMline(=a H = XX a XX = % H = XX % XX = ! H = XX !).
d%$sMoutput5putMline(=)efoe assigne$ent Result H = XX (aEnvl(%#2)E!)).
% 0H C.
! 0H V.
d%$sMoutput5putMline(=(fte assign$ent=).
d%$sMoutput5putMline(=a H = XX a XX = % H = XX % XX = ! H = XX !).
etun (aE%E!).
'ND BUN.
D'CL(R'
v2 nu$%e 0H 2.
v1 nu$%e 0H 1.
v4 nu$%e 0H 4.
v nu$%e.
Copy rights are reserved.
170
)'8IN
d%$sMoutput5putMline(=)efoe !all=).
d%$sMoutput5putMline(=v2 H = XX v2 XX = v1 H = XX v1 XX = v4 H = XX v4).
v 0H fun(v2#v1#v4).
d%$sMoutput5putMline(=(fte !all !o$pleted=).
d%$sMoutput5putMline(=v2 H = XX v2 XX = v1 H = XX v1 XX = v4 H = XX v4).
d%$sMoutput5putMline(=Result H = XX v).
'ND.
Output0
)efoe !all
v2 H 2 v1 H 1 v4 H 4
(fte !all
a H 2 % H ! H 4
)efoe assigne$ent Result H 4
(fte assign$ent
a H 2 % H C ! H V
(fte !all !o$pleted
v2 H 2 v1 H C v4 H V
Result H 4C
R'STRICTIONS ON BORM(L A(R(M'T'RS
)7 de!laing with spe!ified siTe in a!tual paa$etes5
)7 de!laing fo$al paa$etes with Lt7pe spe!ifie5
USIN8 NOCOA*
:ocopy is a hint# not a !o$$and5 This $eans that the !o$pile $ight silentl7 de!ide
that it !an;t fulfill 7ou eUuest fo a nocopy paa$ete5
The !op7ing fo$ fo$al to a!tual !an %e esti!ted %7 issuing nocopy Uualifie5
To pass the out and in out paa$etes %7 efeen!e use no!op7 Uualifie5
'/0
CR'(T' OR R'AL(C' AROC'DUR' AROC(a in out no!op7 nu$%e) IS
)'8IN
""""
'ND AROC.
C(LL (ND '+'C
Copy rights are reserved.
171
Call is a SQL state$ent# whi!h !an %e used to e/e!ute su%poga$s li&e e/e!5
S7nta/0
Call !ubpro"ram_name(Nar"ument_li!tO) Ninto )o!t_variableO.
The paantheses ae alwa7s eUuied# even if the su%poga$ ta&es no agu$ents5
6e !an not use !all with out and in out paa$etes5
Call is a SQL state$ent# it is not valid inside a AL?SQL %lo!&.
The INTO !lause is used fo the output vaia%les of fun!tions onl75
6e !an not use Ke/e!; with out o in out paa$etes5
'/e! is not valid inside a AL?SQL %lo!&.
'/20
CR'(T' OR R'AL(C' AROC IS
)'8IN
d%$sMoutput5putMline(=hello wold=).
'ND AROC.
Output0
SQL- !all po!().
hello wold
'/10
CR'(T' OR R'AL(C' AROC(a in nu$%e#% in nu$%e) IS
)'8IN
d%$sMoutput5putMline(=a H = XX a XX = % H = XX %).
'ND AROC.
Output0
SQL- !all po!(C#D).
a H C % H D
'/40
CR'(T' OR R'AL(C' BUNCTION BUN R'TURN 9(RC:(R IS
)'8IN
etun =hello wold=.
'ND BUN.
Output0
Copy rights are reserved.
172
SQL- vaia%le v va!ha(13)
SQL- !all fun() into 0v.
SQL- pint v
hello wold
C(LL )* R'B'R'NC' (ND C(LL )* 9(LU'
In paa$etes %7 default call by re'erence whee as out and in out call by value5
6hen paa$ete passed %7 efeen!e# a pointe to the a!tual paa$ete is passed to
the !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
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%Je!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%Je!t(a nu$%e).?
SQL- !eate o epla!e t7pe t2 as o%Je!t(a nu$%e).?
D'CL(R'
i t2 0H t2(C).
J t1 0H t1(C).
AROC'DUR' A($ t2) IS
)'8IN
d%$sMoutput5putMline(=a H = XX $5a).
'ND A.
AROC'DUR' A(n t1) IS
)'8IN
d%$sMoutput5putMline(=% H = XX n5%).
'ND A.
AROC'DUR' ARODUCT(a nu$%e#% nu$%e) IS
)'8IN
d%$sMoutput5putMline(=Aodu!t of a#% H = XX a E %).
'ND ARODUCT.
AROC'DUR' ARODUCT(a nu$%e#% nu$%e#! nu$%e) IS
Copy rights are reserved.
173
)'8IN
d%$sMoutput5putMline(=Aodu!t of a#% H = XX a E % E !).
'ND ARODUCT.
)'8IN
p(i).
p(J).
podu!t(@#C).
podu!t(@#C#D).
'ND.
Output0
a H C
% H C
Aodu!t of a#% H 13
Aodu!t of a#% H 213
)'N'BITS OB O9'RLO(DIN8
Suppoting $an7 data !o$%inations
Bitting the poga$ to the use5
R'STRICTIONS ON O9'RLO(DIN8
Oveloaded poga$s with paa$ete lists that diffe onl7 %7 na$e $ust %e !alled
using na$ed notation5
The paa$ete list of oveloaded poga$s $ust diffe %7 $oe than paa$ete $ode5
(ll of the oveloaded poga$s $ust %e defined within the sa$e AL?SQL s!ope o
%lo!&5
Oveloaded fun!tions $ust diffe %7 $oe than thei etun t7pe5
IMAORT(NT AOINTS ()OUT SU)ARO8R(MS
6hen a stoed su%poga$ is !eated# it is stoed in the data dictionary5
The su%poga$ is stoed in !o$pile fo$ whi!h is &nown as p0code 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 AL?SQL engine5
6hen the su%poga$ is !alled# the p"!ode is ead fo$ the dis&# if ne!essa7# and
e/e!uted5
Copy rights are reserved.
174
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
Su%poga$s !an %e local5
Lo!al su%poga$s $ust %e de!laed in the de!laative se!tion of AL?SQL %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.
Lo!al su%poga$s also !an have lo!al su%poga$s5
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 (!ubpro"ram_name5sal)5
Su%poga$s !an %e invalidated5
AROC'DUR'S 9 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%$sMoutput whee as po!edue !an not5
Ao!edue !all is a standalone e/e!uta%le state$ent whee as fun!tion !all is a pat of
an e/e!uta%le state$ent5
STOR'D 9 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
Copy rights are reserved.
175
)7 &eeping the stoed su%poga$ !ode sepaate fo$ the !alling %lo!&# the !alling
%lo!& is shote and easie to undestand5
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)MSMS:(R'DMAOOL 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%$sMoutput5putMline(=Stoed su%poga$=).
'ND.
Output0
SQL- e/e! p
Stoed su%poga$
'/10
D'CL(R'
AROC'DUR' A IS
)'8IN
d%$sMoutput5putMline(=Lo!al su%poga$=).
'ND.
)'8IN
p.
'ND.
Output0
Lo!al 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
Copy rights are reserved.
176
( stoed su%poga$ is $a&ed as invalid in the data di!tiona7 if it has !o$pile
eos5
( stoed su%poga$ !an also %e!o$e invalid if a DDL opeation is pefo$ed on one
of its dependent o%Je!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 A2aOR(CL'.
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%Je!ts is !he!&ed at unti$e5 6hen A2 is !alled# the
e$ote data di!tiona7 is Uueied 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%Je!ts ae
!o$paed5
The la!t_ddl_time field of u!er_ob#ect! !ontains the ti$esta$p5
If the %ase o%Je!t has a newe ti$esta$p than the dependent o%Je!t# the
dependent o%Je!t will %e e!o$piled5
ISSU'S 6IT: T:IS MOD'L
If the o%Je!ts ae in diffeent ti$e Tones# 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
Copy rights are reserved.
177
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
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'MD'A'ND'NCI'SMMOD' $ust
%e set to SI8N(TUR'5 This is a paa$ete in the data%ase initialiTation file5
T:R'' 6(*S OB S'TTIN8 T:IS MOD'
(dd the line R'MOT'MD'A'ND'NCI'SMMOD'HSI8N(TUR' to the data%ase initialiTation 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$oteMdependen!iesM$ode H 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$oteMdependen!iesM$ode H 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'
Copy rights are reserved.
178
AROC'DUR' A2 IS
)'8IN
d%$sMoutput5putMline(=Bo$ po!edue p2=).
p1.
'ND A2.
AROC'DUR' A1 IS
)'8IN
d%$sMoutput5putMline(=Bo$ po!edue p1=).
p4.
'ND A1.
AROC'DUR' A4 IS
)'8IN
d%$sMoutput5putMline(=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%$sMoutput5putMline(=Bo$ po!edue p2=).
p1.
'ND A2.
AROC'DUR' A1 IS
)'8IN
d%$sMoutput5putMline(=Bo$ po!edue p1=).
Copy rights are reserved.
179
p4.
'ND A1.
AROC'DUR' A4 IS
)'8IN
d%$sMoutput5putMline(=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$pMpo! then use ( gants e/e!ute pivilege on
po!edue to use ) with the following !o$$and5
SQL- 8ant e/e!ute on e$pMpo! to use )5
Then use ) !an un the po!edue %7 issuing
SQL- '/e! use (5e$pMpo!
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)
Copy rights are reserved.
180
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
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 5nvoer;! and -e'iner;! ri")t!.
)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%Je!t t7pe
spe!ifi!ations onl75
use( !eated the following po!edue
CR'(T' OR R'AL(C' AROC'DUR' A
(UT:ID CURR'NTMUS'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 !uentMuse 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&eth and sudha in whi!h sa&eth has student ta%le and sudha does not5
Copy rights are reserved.
181
Sudha is going to !eate a po!edue %ased on student ta%le owned %7 sa&eth5 )efoe doing
this sa&eth $ust gant the pe$issions on this ta%le to sudha5
SQL- !onn sa&eth?sa&eth
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&eth5student.
)'8IN
fo v in ! loop
d%$sMoutput5putMline(KNo H K 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&eth?sa&eth
SQL- !eate ole sa&ethMole.
SQL- gant all on student to sa&ethMole.
SQL- gant sa&ethMole to sudha.
then !onn sudha?sudha
CR'(T' OR R'AL(C' AROC'DUR' A IS
!uso ! is sele!t Efo$ sa&eth5student.
)'8IN
fo v in ! loop
d%$sMoutput5putMline(KNo H K 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
Copy rights are reserved.
182
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
( 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
Copy rights are reserved.
183
A(CF(8'S
( paca"e is a !ontaine fo elated o%Je!ts5 It has spe!ifi!ation and %od75 'a!h of the$
is stoed sepaatel7 in data di!tiona75
A(CF(8' S*NT(+
Ceate o epla!e pa!&age ,paca"e_name- is
"" pa!&age spe!ifi!ation in!ludes su%poga$s signatues# !usos and glo%al o
pu%li! vaia%les5
'nd ,paca"e_name-.
Ceate o epla!e pa!&age %od7 ,paca"e_name- is
"" pa!&age %od7 in!ludes %od7 fo all the su%poga$s de!laed in the spe!# pivate
9aia%les and !usos5
)egin
"" initialiTation se!tion
'/!eption
"" '/!eption handling se!iton
'nd ,paca"e_name-.
IMAORT(NT AOIN8S ()OUT 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 initialiTation needs to %e un the fist ti$e the pa!&age is instantiated
within a session5 This !an %e done %7 adding initialiTation se!tion to the pa!&age %od7
afte all the o%Je!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
*ou !an in!lude authid !lause inside the pa!&age spe! not in the %od75
The e/e!ution se!tion of a pa!&age is &now as initialiTation se!tion5
*ou !an have an e/!eption se!tion at the %otto$ of a pa!&age %od75
Aa!&ages su%poga$s ae not invalidated5
Copy rights are reserved.
184
COMAILIN8 A(CF(8'S
SQL- (lte pa!&age AF8 !o$pile.
SQL- (lte pa!&age AF8 !o$pile spe!ifi!ation.
SQL- (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%Je!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
To fo!e the oa!le to use seiall7 eusa%le vesion then in!lude AR(8M( S'RI(LL*MR'US()L' in
%oth pa!&age spe! and %od7# '/a$ine the following pa!&age5
CR'(T' OR R'AL(C' A(CF(8' AF8 IS
pag$a seiall7Meusa%le.
po!edue e$pMpo!.
'ND AF8.
CR'(T' OR R'AL(C' A(CF(8' )OD* AF8 IS
pag$a seiall7Meusa%le.
!uso ! is sele!t ena$e fo$ e$p.
AROC'DUR' 'MAMAROC IS
vMena$e e$p5ena$eLt7pe.
vMflag %oolean 0H tue.
vMnu$ows nu$%e 0H 3.
)'8IN
if not !Lisopen then
Copy rights are reserved.
185
open !.
end if.
while vMflag loop
fet!h ! into vMena$e.
vMnu$ows 0H vMnu$ows G 2.
if vMnu$ows H C then
vMflag 0H false.
end if.
d%$sMoutput5putMline(='na$e H = XX vMena$e).
end loop.
'ND 'MAMAROC.
'ND AF8.
SQL- e/e! p&g5e$pMpo!
'na$e H SMIT:
'na$e H (LL'N
'na$e H 6(RD
'na$e H >ON'S
'na$e H M(RTIN
SQL- e/e! p&g5e$pMpo!
'na$e H SMIT:
'na$e H (LL'N
'na$e H 6(RD
'na$e H >ON'S
'na$e H 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!le# e/a$ine the following pa!&age5
CR'(T' OR R'AL(C' A(CF(8' AF8 IS
po!edue e$pMpo!.
'ND AF8.
Copy rights are reserved.
186
CR'(T' OR R'AL(C' A(CF(8' )OD* AF8 IS
!uso ! is sele!t ena$e fo$ e$p.
AROC'DUR' 'MAMAROC IS
vMena$e e$p5ena$eLt7pe.
vMflag %oolean 0H tue.
vMnu$ows nu$%e 0H 3.
)'8IN
if not !Lisopen then
open !.
end if.
while vMflag loop
fet!h ! into vMena$e.
vMnu$ows 0H vMnu$ows G 2.
if vMnu$ows H C then
vMflag 0H false.
end if.
d%$sMoutput5putMline(='na$e H = XX vMena$e).
end loop.
'ND 'MAMAROC.
'ND AF8.
SQL- e/e! p&g5e$pMpo!
'na$e H SMIT:
'na$e H (LL'N
'na$e H 6(RD
'na$e H >ON'S
'na$e H M(RTIN
SQL- e/e! p&g5e$pMpo!
'na$e H )L(F'
'na$e H CL(RF
'na$e H SCOTT
'na$e H FIN8
'na$e H 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
Copy rights are reserved.
187
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
CR'(T' OR R'AL(C' A(CF(8' AF8 IS
v nu$%e 0H C.
po!edue p.
'ND AF8.
CR'(T' OR R'AL(C' A(CF(8' )OD* AF8 IS
AROC'DUR' A IS
)'8IN
d%$sMoutput5putMline(=v H = XX v).
v 0H 23.
d%$sMoutput5putMline(=v H = XX v).
'ND A.
'ND AF8.
Conne!t to se!ond session# un the following !ode5
)'8IN
p&g5p.
'ND.
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
)'8IN
p&g5p.
'ND.
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
Copy rights are reserved.
188
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
AURIT* L'9'LS
In geneal# !alls to su%poga$s ae po!edual# the7 !annot %e !alled fo$ SQL state$ents5
:oweve# if a stand"alone o pa!&aged fun!tion $eets !etain esti!tions# it !an %e !alled
duing e/e!ution of a SQL state$ent5
Use"defined fun!tions ae !alled the sa$e wa7 as %uilt"in fun!tions %ut it $ust $eet
diffeent esti!tions5 These esti!tions ae defined in te$s of puit7 levels5
Thee ae fou t7pes of puit7 levels5
6NDS "" 6ites No Data%ase State
RNDS "" Reads No Data%ase State
6NAS "" 6ites No Aa!&age State
RNAS "" Reads No Aa!&age State
In addition to the pe!eding esti!tions# a use"defined fun!tion $ust also $eet the
following eUuie$ents to %e !alled fo$ a SQL state$ent5
The fun!tion has to %e stoed in the data%ase# eithe stand"alone o as pat of a
pa!&age5
The fun!tion !an ta&e onl7 in paa$etes5
The fo$al paa$etes $ust use onl7 data%ase t7pes# not AL?SQL t7pes su!h as
%oolean o e!od5
The etun t7pe of the fun!tion $ust also %e a data%ase t7pe5
The fun!tion $ust not end the !uent tansa!tion with !o$$it o oll%a!&# o
oll%a!& to a savepoint pio to the fun!tion e/e!ution5
It also $ust not issue an7 alte session o alte s7ste$ !o$$ands5
R'STRICTMR'B'R'NC'S
Bo pa!&aged fun!tions# howeve# the R'STRICTMR'B'R'NC'S pag$a is eUuied to spe!if7 the
puit7 level of a given fun!tion5
Copy rights are reserved.
189
S7nta/0
AR(8M( R'STRICTMR'B'R'NC'S(!ubpro"ram_name or paca"e_name# 6NDS N#6NASO
N#RNDSO N#RNASO).
'/0
CR'(T' OR R'AL(C' A(CF(8' AF8 IS
fun!tion fun2 etun va!ha.
pag$a esti!tMefeen!es(fun2#wnds).
fun!tion fun1 etun va!ha.
pag$a esti!tMefeen!es(fun1#wnds).
'ND AF8.
CR'(T' OR R'AL(C' A(CF(8' )OD* AF8 IS
BUNCTION BUN2 etun va!ha IS
)'8IN
update dept set deptno H 22.
etun =hello=.
'ND BUN2.
BUNCTION BUN1 etun va!ha IS
)'8IN
update dept set dna$e H=aa=.
etun =hello=.
'ND BUN1.
'ND AF8.
The a%ove pa!&age %od7 will not !eated# it will give the following eos5
ALS"33@C10 Su%poga$ =BUN2= violates its asso!iated pag$a
ALS"33@C10 Su%poga$ =BUN1= violates its asso!iated pag$a
CR'(T' OR R'AL(C' A(CF(8' )OD* AF8 IS
BUNCTION BUN2 etun va!ha IS
)'8IN
etun =hello=.
'ND BUN2.
BUNCTION BUN1 etun va!ha IS
)'8IN
etun =hello=.
'ND BUN1.
'ND AF8.
Now the pa!&age %od7 will %e !eated5
D'B(ULT
Copy rights are reserved.
190
If thee is no R'STRICTMR'B'R'NC'S pag$a asso!iated with a given pa!&aged fun!tion# it will
not have an7 puit7 level asseted5 :oweve# 7ou !an !hange the default puit7 level fo a
pa!&age5 The D'B(ULT &e7wod is used instead of the su%poga$ na$e in the pag$a5
'/0
CR'(T' OR R'AL(C' A(CF(8' AF8 IS
pag$a esti!tMefeen!es(default#wnds).
fun!tion fun2 etun va!ha.
fun!tion fun1 etun va!ha.
'ND AF8.
CR'(T' OR R'AL(C' A(CF(8' )OD* AF8 IS
BUNCTION BUN2 etun va!ha IS
)'8IN
update dept set deptno H 22.
etun =hello=.
'ND BUN2.
BUNCTION BUN1 etun va!ha IS
)'8IN
update dept set dna$e H=aa=.
etun =hello=.
'ND BUN1.
'ND AF8.
The a%ove pa!&age %od7 will not !eated# it will give the following eos %e!ause the
pag$a will appl7 to all the fun!tions5
ALS"33@C10 Su%poga$ =BUN2= violates its asso!iated pag$a
ALS"33@C10 Su%poga$ =BUN1= violates its asso!iated pag$a
CR'(T' OR R'AL(C' A(CF(8' )OD* AF8 IS
BUNCTION BUN2 etun va!ha IS
)'8IN
etun =hello=.
'ND BUN2.
BUNCTION BUN1 etun va!ha IS
)'8IN
etun =hello=.
'ND BUN1.
'ND AF8.
Now the pa!&age %od7 will %e !eated5
Copy rights are reserved.
191
TRUST
If the TRUST &e7wod is pesent# the esti!tions listed in the pag$a ae not enfo!ed5
Rathe# the7 ae tusted to %e tue5
'/0
CR'(T' OR R'AL(C' A(CF(8' AF8 IS
fun!tion fun2 etun va!ha.
pag$a esti!tMefeen!es(fun2#wnds#tust).
fun!tion fun1 etun va!ha.
pag$a esti!tMefeen!es(fun1#wnds#tust).
'ND AF8.
CR'(T' OR R'AL(C' A(CF(8' )OD* AF8 IS
BUNCTION BUN2 etun va!ha IS
)'8IN
update dept set deptno H 22.
etun =hello=.
'ND BUN2.
BUNCTION BUN1 etun va!ha IS
)'8IN
update dept set dna$e H=aa=.
etun =hello=.
'ND BUN1.
'ND AF8.
The a%ove pa!&age will %e !eated su!!essfull75
IMAORT(NT AOINTS ()OUT R'STRICTMR'B'R'NC'S
This pag$a !an appea an7whee in the pa!&age spe!ifi!ation# afte the fun!tion
de!laation5
It !an appl7 to onl7 one fun!tion definition5
Bo oveload fun!tions# the pag$a applies to the neaest definition pio to the
Aag$a5
This pag$a is eUuied onl7 fo pa!&ages fun!tions not fo stand"alone fun!tions5
The Aag$a !an %e de!laed onl7 inside the pa!&age spe!ifi!ation5
The pag$a is !he!&ed at !o$pile ti$e# not unti$e5
Copy rights are reserved.
192
It is possi%le to spe!if7 without an7 puit7 levels when tust o !o$%ination of
default and tust &e7wods ae pesent5
AINNIN8 IN T:' S:(R'D AOOL
The !)ared pool is the potion of the S8S that !ontains# a$ong othe things# the p"!ode of
!o$piled su%poga$s as the7 ae un5 The fist ti$e a stoed a stoe su%poga$ is !alled#
the p"!ode is loaded fo$ dis& into the shaed pool5 On!e the o%Je!t is no longe efeen!ed#
it is fee to %e aged out5 O%Je!ts ae aged out of the shaed pool using an LRU(Least Re!entl7
Used) algoith$5
The D)MSMS:(R'DMAOOL pa!&age allows 7ou to pin o%Je!ts in the shaed pool5 6hen an o%Je!t
is pinned# it will neve %e aged out until 7ou eUuest it# no $atte how full the pool gets o
how often the o%Je!t is a!!essed5 This !an i$pove pefo$an!e# as it ta&es ti$e to eload a
pa!&age fo$ dis&5
D)MSMS:(R'DMAOOL has fou po!edues
F''A
UNF''A
SIZ'S
()ORT'DMR'QU'STMT:R'S:OLD
F''A
The D)MSMS:(R'DMAOOL5F''A po!edue is used to pin o%Je!ts in the pool5
S7nta/0
AROC'DUR' F''A(ob#ect_name va!ha1#'la" !ha default KA;).
:ee the flag epesents diffeent t7pes of flag values fo diffeent t7pes of o%Je!ts5
A "" Aa!&age# fun!tion o po!edue
Q "" SeUuen!e
R "" Tigge
C "" SQL Cuso
T "" O%Je!t t7pe
>S "" >ava sou!e
>C "" >ava !lass
>R "" >ava esou!e
Copy rights are reserved.
193
>D "" >ava shaed data
UNF''A
UNF''A is the onl7 wa7 to e$ove a &ept o%Je!t fo$ the shaed pool# without estating the
data%ase5 Fept o%Je!ts ae neve aged out auto$ati!all75
S7nta/0
AROC'DUR' UNF''A(ob#ect_name va!ha1# 'la" !ha default KA;).
SIZ'S
SIZ'S will e!ho the !ontents of the shaed pool to the s!een5
S7nta/0
AROC'DUR' SIZ'S(min!i,e nu$%e).
O%Je!ts with geate than the min!i,e will %e etuned5 SIZ'S uses D)MSMOUTAUT to etun the
data5
()ORT'DMR'QU'STMT:R'S:OLD
6hen the data%ase dete$ines that thee is not enough $e$o7 in the shaed pool to
satisf7 a given eUuest# it will %egin aging o%Je!ts out until thee is enough $e$o75 It
enough o%Je!ts ae aged out# this !an have a pefo$an!e i$pa!t on othe data%ase
sessions5 The ()ORT'DMR'QU'STMT:R'S:OLD !an %e used to e$ed7 this5
S7nta/0
AROC'DUR' ()ORT'DMR'QU'STMT:R'S:OLD(t)re!)old_!i,e nu$%e).
On!e this po!edue is !alled# oa!le will not stat aging o%Je!ts fo$ the pool unless at
least t)re!)old_!i,e %7tes is needed5
D(T( MOD'L BOR SU)ARO8R(MS (ND A(CF(8'S
US'RMO)>'CTS
US'RMSOURC'
US'RM'RRORS
D)(MO)>'CTS
D)(MSOURC'
D)(M'RRORS
(LLMO)>'CTS
Copy rights are reserved.
194
(LLMSOURC'
(LLM'RRORS
CURSORS
Cur!or 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 !et whi!h
is the set of ows etuned %7 the Uue75
Cuso !ontains two pats
:eade
)od7
Copy rights are reserved.
195
: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 deptLowt7pe is sele!t Efo$ dept.
In the a%ove
:eade \ !uso !(dno in nu$%e) etun deptLowt7pe
)od7 \ sele!t Efo$ dept
CURSOR T*A'S
I$pli!it (SQL)
'/pli!it
Aaa$eteiTed !usos
R'B !usos
CURSOR ST(8'S
Open
Bet!h
Close
CURSOR (TTRI)UT'S
Lfound
Lnotfound
Low!ount
Lisopen
L%ul&Mow!ount
L%ul&Me/!eptions
CURSOR D'CL'R(TION
S7nta/0
Cuso ,cur!or_name- is !elect !tatement.
'/0
Cuso ! is sele!t Efo$ dept.
Copy rights are reserved.
196
CURSOR LOOAS
Si$ple loop
6hile loop
Bo loop
SIMAL' LOOA
S7nta/0
Loop
Bet!h ,cur!or_name- into ,record_variable-.
'/it when ,cur!or_name- L notfound.
,!tatement!-.
'nd loop.
'/0
D'CL(R'
!uso ! is sele!t E fo$ student.
vMstud studentLowt7pe.
)'8IN
open !.
loop
fet!h ! into vMstud.
e/it when !Lnotfound.
d%$sMoutput5putMline(=Na$e H = XX vMstud5na$e).
end loop.
!lose !.
'ND.
Output0
Na$e H sa&eth
Na$e H sinu
Na$e H satish
Na$e H sudha
6:IL' LOOA
S7nta/0
6hile ,cur!or_name- L found loop
Bet!h ,cur!or_name- nto ,record_variable-.
Copy rights are reserved.
197
,!tatement!-.
'nd loop.
'/0
D'CL(R'
!uso ! is sele!t E fo$ student.
vMstud studentLowt7pe.
)'8IN
open !.
fet!h ! into vMstud.
while !Lfound loop
fet!h ! into vMstud.
d%$sMoutput5putMline(=Na$e H = XX vMstud5na$e).
end loop.
!lose !.
'ND.
Output0
Na$e H sa&eth
Na$e H sinu
Na$e H satish
Na$e H sudha
BOR LOOA
S7nta/0
fo ,record_variable- in ,cur!or_name- loop
,!tatement!-.
'nd loop.
'/0
D'CL(R'
!uso ! is sele!t E fo$ student.
)'8IN
fo vMstud in ! loop
d%$sMoutput5putMline(=Na$e H = XX vMstud5na$e).
end loop.
'ND.
Output0
Copy rights are reserved.
198
Na$e H sa&eth
Na$e H sinu
Na$e H satish
Na$e H sudha
A(R(M'T(RIZ'D CURSORS
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 H dno.
vMdept deptLowt7pe.
)'8IN
open !(13).
loop
fet!h ! into vMdept.
e/it when !Lnotfound.
d%$sMoutput5putMline(=Dna$e H = XX vMdept5dna$e XX = Lo! H = XX vMdept5lo!).
end loop.
!lose !.
'ND.
Output0
Dna$e H R'S'(RC: Lo! H 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
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
Copy rights are reserved.
199
'/20
CR'(T' OR R'AL(C' A(CF(8' AF8 IS
!uso ! etun deptLowt7pe is sele!t E fo$ dept.
po!edue po! is
'ND AF8.
CR'(T' OR R'AL(C' A(FC(8' )OD* AF8 IS
!uso ! etun deptLowt7pe is sele!t E fo$ dept.
AROC'DUR' AROC IS
)'8IN
fo v in ! loop
d%$sMoutput5putMline(=Deptno H = XX v5deptno XX = Dna$e H = XX
v5dna$e XX = Lo! H = XX v5lo!).
end loop.
'ND AROC.
'ND AF8.
Output0
SQL- e/e! p&g5po!
Deptno H 23 Dna$e H (CCOUNTIN8 Lo! H N'6 *ORF
Deptno H 13 Dna$e H R'S'(RC: Lo! H D(LL(S
Deptno H 43 Dna$e H S(L'S Lo! H C:IC(8O
Deptno H @3 Dna$e H OA'R(TIONS Lo! H )OSTON
'/10
CR'(T' OR R'AL(C' A(FC(8' )OD* AF8 IS
!uso ! etun deptLowt7pe is sele!t E fo$ dept whee deptno - 13.
AROC'DUR' AROC IS
)'8IN
fo v in ! loop
d%$sMoutput5putMline(=Deptno H = XX v5deptno XX = Dna$e H = XX
v5dna$e XX = Lo! H = XX v5lo!).
end loop.
'ND AROC.
'ND AF8.
Output0
SQL- e/e! p&g5po!
Deptno H 43 Dna$e H S(L'S Lo! H C:IC(8O
Deptno H @3 Dna$e H OA'R(TIONS Lo! H )OSTON
Copy rights are reserved.
200
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
Ref !uso with etun t7pe is !alled !tron" cur!or5
Ref !uso with out etun t7pe is !alled wea cur!or5
*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
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 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 eUualit7# ineUualit7 and nullit75
'/0
CR'(T' OR R'AL(C' AROC'DUR' R'BMCURSOR(T()L'MN(M' IN 9(RC:(R) IS
t7pe t is ef !uso.
! t.
vMdept deptLowt7pe.
t7pe is e!od(ena$e e$p5ena$eLt7pe#Jo% e$p5Jo%Lt7pe#sal e$p5salLt7pe).
vMe$p .
vMstud student5na$eLt7pe.
)'8IN
if ta%leMna$e H =D'AT= then
open ! fo sele!t E fo$ dept.
elsif ta%leMna$e H ='MA= then
open ! fo sele!t ena$e#Jo%#sal fo$ e$p.
Copy rights are reserved.
201
elsif ta%leMna$e H =STUD'NT= then
open ! fo sele!t na$e fo$ student.
end if.
loop
if ta%leMna$e H =D'AT= then
fet!h ! into vMdept.
e/it when !Lnotfound.
d%$sMoutput5putMline(=Deptno H = XX vMdept5deptno XX = Dna$e H = XX
vMdept5dna$e XX = Lo! H = XX vMdept5lo!).
elsif ta%leMna$e H ='MA= then
fet!h ! into vMe$p.
e/it when !Lnotfound.
d%$sMoutput5putMline(='na$e H = XX vMe$p5ena$e XX = >o% H = XX vMe$p5Jo%
XX = Sal H = XX vMe$p5sal).
elsif ta%leMna$e H =STUD'NT= then
fet!h ! into vMstud.
e/it when !Lnotfound.
d%$sMoutput5putMline(=Na$e H = XX vMstud).
end if.
end loop.
!lose !.
'ND.
Output0
SQL- e/e! efM!uso(=D'AT=)
Deptno H 23 Dna$e H (CCOUNTIN8 Lo! H N'6 *ORF
Deptno H 13 Dna$e H R'S'(RC: Lo! H D(LL(S
Deptno H 43 Dna$e H S(L'S Lo! H C:IC(8O
Deptno H @3 Dna$e H OA'R(TIONS Lo! H )OSTON
SQL- e/e! efM!uso(='MA=)
'na$e H SMIT: >o% H CL'RF Sal H W33
'na$e H (LL'N >o% H S(L'SM(N Sal H 2D33
'na$e H 6(RD >o% H S(L'SM(N Sal H 21C3
'na$e H >ON'S >o% H M(N(8'R Sal H 1SVC
'na$e H M(RTIN >o% H S(L'SM(N Sal H 21C3
'na$e H )L(F' >o% H M(N(8'R Sal H 1WC3
'na$e H CL(RF >o% H M(N(8'R Sal H 1@C3
Copy rights are reserved.
202
'na$e H SCOTT >o% H (N(L*ST Sal H 4333
'na$e H FIN8 >o% H AR'SID'NT Sal H C333
'na$e H TURN'R >o% H S(L'SM(N Sal H 2C33
'na$e H (D(MS >o% H CL'RF Sal H 2233
'na$e H >(M'S >o% H CL'RF Sal H SC3
'na$e H BORD >o% H (N(L*ST Sal H 4333
'na$e H MILL'R >o% H CL'RF Sal H 2433
SQL- e/e! efM!uso(=STUD'NT=)
Na$e H sa&eth
Na$e H sinu
Na$e H satish
Na$e H 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 H d5deptno)
fo$ e$p e.
t7pe t is ef !uso.
Copy rights are reserved.
203
!2 t.
!1 t.
v2 e$p5ena$eLt7pe.
v1 dept5dna$eLt7pe.
)'8IN
open !.
loop
fet!h !2 into v2.
e/it when !2Lnotfound.
fet!h !1 into v1.
e/it when !1Lnotfound.
d%$sMoutput5putMline(='na$e H = XX v2 XX = Dna$e H = 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 deptLowt7pe is sele!t Efo$ dept.
O
Cuso !2 is sele!t Efo$ dept.
Cuso ! etun !2Lowt7pe is sele!t Efo$ dept.
O
T7pe t is e!od(deptno dept5deptnoLt7pe# dna$e dept5dna$eLt7pe).
Cuso ! etun t is sele!t deptno# dna$e fo$ dept.
BOR UAD(T' (ND 6:'R' CURR'NT OB
No$all7# a sele!t opeation will not ta&e an7 lo!&s on the ows %eing a!!essed5 This will
allow othe sessions !onne!ted to the data%ase to !hange the data %eing sele!ted5 The
Copy rights are reserved.
204
esult set is still !onsistent5 (t open ti$e# when the a!tive set is dete$ined# oa!le ta&es a
snapshot of the ta%le5 (n7 !hanges that have %een !o$$itted pio to this point ae
efle!ted in the a!tive set5 (n7 !hanges $ade afte this point# even if the7 ae !o$$itted#
ae not efle!ted unless the !uso is eopened# whi!h will evaluate the a!tive set again5
:oweve# if the BOR UAD(T' !aluse is pesent# e/!lusive ow lo!&s ae ta&en on the ows in the
a!tive set %efoe the open etuns5 These lo!&s pevent othe sessions fo$ !hanging the
ows in the a!tive set until the tansa!tion is !o$$itted o olled %a!&5 If anothe session
alead7 has lo!&s on the ows in the a!tive set# then S'L'CT R BOR UAD(T' opeation will wait
fo these lo!&s to %e eleased %7 the othe session5 Thee is no ti$e"out fo this waiting
peiod5 The S'L'CTRBOR UAD(T' will hang until the othe session eleases the lo!&5 To handle
this situation# the NO6(IT !lause is availa%le5
S7nta/0
Sele!t Rfo$ R fo update of !olu$nMna$e Nwait nO.
If the !uso is de!laed with the BOR UAD(T' !lause# the 6:'R' CURR'NT OB !lause !an %e
used in an update o delete state$ent5
S7nta/0
6hee !uent of !uso.
'/0
D'CL(R'
!uso ! is sele!t E fo$ dept fo update of dna$e.
)'8IN
fo v in ! loop
update dept set dna$e H =aa= whee !uent of !.
!o$$it.
end loop.
'ND.
)ULF COLL'CT
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?sUl and sUl engines5
Redu!es the ovehead of etieving data5
*ou !an use %ul& !olle!t in %oth d7na$i! and stati! sUl5
*ou !an use %ul& !olle!t in sele!t# fet!h into and etuning into !lauses5
Copy rights are reserved.
205
SQL engine auto$ati!all7 initialiTes 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 Uue75
*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:
'/0
D'CL(R'
T7pe t is ta%le of deptLowt7pe.
nt t.
Cuso ! is sele!t Efo$ dept.
)'8IN
Open !.
Bet!h ! %ul& !olle!t into nt.
Close !.
Bo i in nt5fist55nt5last loop
d%$sMoutput5putMline(=Dna$e H = XX nt(i)5dna$e XX = Lo! H = XX
nt(i)5lo!).
end loop.
'ND.
Output0
Dna$e H (CCOUNTIN8 Lo! H N'6 *ORF
Dna$e H R'S'(RC: Lo! H D(LL(S
Dna$e H S(L'S Lo! H C:IC(8O
Dna$e H OA'R(TIONS Lo! H )OSTON
)ULF COLL'CT IN S'L'CT
'/0
D'CL(R'
T7pe t is ta%le of deptLowt7pe.
Nt t.
)'8IN
Sele!t E %ul& !olle!t into nt fo$ dept.
fo i in nt5fist55nt5last loop
d%$sMoutput5putMline(=Dna$e H = XX nt(i)5dna$e XX = Lo! H = XX
Copy rights are reserved.
206
nt(i)5lo!).
end loop.
'ND.
Output0
Dna$e H (CCOUNTIN8 Lo! H N'6 *ORF
Dna$e H R'S'(RC: Lo! H D(LL(S
Dna$e H S(L'S Lo! H C:IC(8O
Dna$e H OA'R(TIONS Lo! H )OSTON
LIMIT IN )ULF COLL'CT
*ou !an use this to li$it the nu$%e of ows to %e fet!hed5
'/0
D'CL(R'
T7pe t is ta%le of deptLowt7pe.
nt t.
Cuso ! is sele!t Efo$ dept.
)'8IN
Open !.
Bet!h ! %ul& !olle!t into nt li$it 1.
Close !.
Bo i in nt5fist55nt5last loop
d%$sMoutput5putMline(=Dna$e H = XX nt(i)5dna$e XX = Lo! H = XX
nt(i)5lo!).
end loop.
'ND.
Output0
Dna$e H (CCOUNTIN8 Lo! H N'6 *ORF
Dna$e H R'S'(RC: Lo! H D(LL(S
MULTIAL' B'TC:'S IN INTO CL(US'
'/20
D'CL(R'
T7pe t is ta%le of dept5dna$eLt7pe.
nt t.
T7pe t2 is ta%le of dept5lo!Lt7pe.
Copy rights are reserved.
207
nt2 t.
Cuso ! is sele!t dna$e#lo! fo$ dept.
)'8IN
Open !.
Bet!h ! %ul& !olle!t into nt#nt2.
Close !.
Bo i in nt5fist55nt5last loop
d%$sMoutput5putMline(=Dna$e H = XX nt(i)).
end loop.
Bo i in nt25fist55nt25last loop
d%$sMoutput5putMline(=Lo! H = XX nt2(i)).
end loop.
'ND.
Output0
Dna$e H (CCOUNTIN8
Dna$e H R'S'(RC:
Dna$e H S(L'S
Dna$e H OA'R(TIONS
Lo! H N'6 *ORF
Lo! H D(LL(S
Lo! H C:IC(8O
Lo! H )OSTON
'/10
D'CL(R'
t7pe t is ta%le of dept5dna$eLt7pe.
t7pe t2 is ta%le of dept5lo!Lt7pe.
nt t.
nt2 t2.
)'8IN
Sele!t dna$e#lo! %ul& !olle!t into nt#nt2 fo$ dept.
fo i in nt5fist55nt5last loop
d%$sMoutput5putMline(=Dna$e H = XX nt(i)).
end loop.
fo i in nt25fist55nt25last loop
d%$sMoutput5putMline(=Lo! H = XX nt2(i)).
end loop.
'ND.
Copy rights are reserved.
208
Output0
Dna$e H (CCOUNTIN8
Dna$e H R'S'(RC:
Dna$e H S(L'S
Dna$e H OA'R(TIONS
Lo! H N'6 *ORF
Lo! H D(LL(S
Lo! H C:IC(8O
Lo! H )OSTON
R'TURNIN8 CL(US' IN )ULF COLL'CT
*ou !an use this to etun the po!essed data to the ouput vaia%les o t7ped vaia%les5
'/0
D'CL(R'
t7pe t is ta%le of nu$%e(1).
nt t 0H t(2#1#4#@).
t7pe t2 is ta%le of va!ha(1).
nt2 t2.
t7pe t1 is ta%le of studentLowt7pe.
nt1 t1.
)'8IN
sele!t na$e %ul& !olle!t into nt2 fo$ student.
foall v in nt25fist55nt25last
update student set no H nt(v) whee na$e H nt2(v) etuning
no#na$e#$a&s %ul& !olle!t into nt1.
fo v in nt15fist55nt15last loop
d%$sMoutput5putMline(=Ma&s H = XX nt1(v)).
end loop.
'ND.
Output0
Ma&s H 233
Ma&s H 133
Ma&s H 433
Ma&s H @33
AOINTS TO R'M'M)'R
Copy rights are reserved.
209
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
L%ul&Mow!ount and L%ul&Me/!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
Copy rights are reserved.
210
The onl7 state$ents allowed die!tl7 in pl?sUl ae DML and TCL5
)INDIN8
)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?SQL %7 default uses eal7 %inding5
)inding also involves !he!&ing the data%ase fo pe$issions to a!!ess the o%Je!t
Refeen!ed5
D*N(MIC SQL
If 7ou use DDL in pl?sUl it validates the pe$issions and e/isten!e if eUuies 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!hniUues ae availa%le fo D7na$i! SQL5
Native D7na$i! SQL
D)MSMSQL pa!&age
USIN8 N(TI9' D*N(MIC SQL
USIN8 '+'CUT' IMM'DI(T'
'/0
Copy rights are reserved.
211
)'8IN
'/e!ute i$$ediate K!eate ta%le student(no nu$%e(1)#na$e va!ha(23));.
o
'/e!ute i$$ediate (K!eate ta%le student(no nu$%e(1)#na$e va!ha(23));).
'ND.
USIN8 '+'CUT' IMM'DI(T' 6IT: AL?SQL 9(RI()L'S
'/0
D'CL(R'
v va!ha(233).
)'8IN
v 0H =!eate ta%le student(no nu$%e(1)#na$e va!ha(23))=.
e/e!ute i$$ediate v.
'ND.
USIN8 '+'CUT' IMM'DI(T' 6IT: )IND 9(RI()L'S (ND USIN8 CL(US'
'/0
D'CL(R'
v va!ha(233).
)'8IN
v 0H =inset into student values(0v2#0v1#0v4)=.
e/e!ute i$$ediate v using D#=f=#D33.
'ND.
'+'CUTIN8 QU'RI'S 6IT: OA'N BOR (ND USIN8 CL(US'
'/0
CR'(T' OR R'AL(C' AROC'DUR' A(s$a&s in nu$%e) IS
s va!ha(233) 0H =sele!t Efo$ student whee $a&s - 0$=.
t7pe t is ef !uso.
! t.
v studentLowt7pe.
)'8IN
open ! fo s using s$a&s.
loop
fet!h ! into v.
e/it when !Lnotfound.
d%$sMoutput5putMline(=Student Ma&s H = XX v5$a&s).
end loop.
!lose !.
Copy rights are reserved.
212
'ND.

Output0
SQL- e/e! p(233)
Student Ma&s H 133
Student Ma&s H 433
Student Ma&s H @33
QU'RI'S 6IT: '+'CUT' IMM'DI(T'
'/0
D'CL(R'
dMna$e dept5dna$eLt7pe.
l! dept5lo!Lt7pe.
v va!ha(233).
)'8IN
v 0H =sele!t dna$e fo$ dept whee deptno H 23=.
e/e!ute i$$ediate v into dMna$e.
d%$sMoutput5putMline(=Dna$e H =XX dMna$e).
v 0H =sele!t lo! fo$ dept whee dna$e H 0dn=.
e/e!ute i$$ediate v into l! using dMna$e.
d%$sMoutput5putMline(=Lo! H = XX l!).
'ND.
Output0
Dna$e H (CCOUNTIN8
Lo! H N'6 *ORF
9(RI()L' N(M'S
'/0
D'CL(R'
Ma&s nu$%e(4) 0H 233.
)'8IN
Delete student whee $a&s H $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
,,$7M%lo!&--
Copy rights are reserved.
213
D'CL(R'
Ma&s nu$%e(4) 0H 233.
)'8IN
Delete student whee $a&s H $7M%lo!&5$a&s. "" delete ows whi!h has
"" a $a&s of 233
'ND.
8'TTIN8 D(T( INTO AL?SQL 9(RI()L'S
'/0
D'CL(R'
92 nu$%e.
91 va!ha(1).
)'8IN
Sele!t no#na$e into v2#v1 fo$ student whee $a&s H 233.
'ND.
DML (ND R'CORDS
'/0
CR'(T' OR R'AL(C' AROC'DUR' A(sow in studentLowt7pe) IS
)'8IN
inset into student values sow.
'ND A.
D'CL(R'
s studentLowt7pe.
)'8IN
s5no 0H 22.
s5na$e 0H =aa=.
s5$a&s 0H 233.
p(s).
'ND.
R'CORD )(S'D INS'RTS
'/0
D'CL(R'
sow studentLowt7pe.
)'8IN
sow5no 0H V.
sow5na$e 0H =!!=.
sow5$a&s 0H C33.
Copy rights are reserved.
214
inset into student values sow.
'ND.
R'CORD )(S'D UAD(T'S
'/0
D'CL(R'
sow studentLowt7pe.
)'8IN
sow5no 0H D.
sow5na$e 0H =!!=.
sow5$a&s 0H C33.
update student set owHsow whee no H sow5no.
'ND.
USIN8 R'CORDS 6IT: R'TURNIN8 CL(US'
'/0
D'CL(R'
sow studentLowt7pe.
setun studentLowt7pe.
)'8IN
sow5no 0H W.
sow5na$e 0H =dd=.
sow5$a&s 0H C33.
inset into student values sow etuning no#na$e#$a&s into setun.
d%$sMoutput5putMline(=No H = XX setun5no).
d%$sMoutput5putMline(=No H = XX setun5na$e).
d%$sMoutput5putMline(=No H = XX setun5$a&s).
'ND.
Output0
No H W
No H dd
No H C33
USIN8 D)MSMSQL A(CF(8'
D)MSMSQL is used to e/e!ute d7na$i! SQL fo$ with in AL?SQL5 Unli&e native d7na$i!
SQL# it is not %uilt die!tl7 into the language# and thus is less effi!ient5 The D)MSMSQL
pa!&age allows 7ou to die!tl7 !ontol the po!essing of a state$ent within a !uso# with
Copy rights are reserved.
215
opeations su!h as opening and !losing a !uso# pasing a state$ent# %inding input
vaia%le# and defining output vaia%les5
'/20
D'CL(R'
!usoMid nu$%e.
flag nu$%e.
vMst$t va!ha(C3).
)'8IN
!usoMid 0H d%$sMsUl5openM!uso.
vMst$t 0H =!eate ta%le stud(sno nu$%e(1)#sna$e va!ha(23))=.
d%$sMsUl5pase(!usoMid#vMst$t#d%$sMsUl5native).
flag 0H d%$sMsUl5e/e!ute(!usoMid).
d%$sMsUl5!loseM!uso(!usoMid).
d%$sMoutput5putMline(=Ta%le !eated=).
'ND.
Output0
Ta%le !eated
SQL- des! stud
Na$e Null[ T7pe
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" """""""" """"""""
SNO NUM)'R(1)
SN(M' 9(RC:(R1(23)
'/10
CR'(T' OR R'AL(C' AROC'DUR' D)MSMSQLMAROC(v2 student5noLt7pe#
v1 student5$a&sLt7pe) is
!usoMid nu$%e.
flag nu$%e.
vMupdate va!ha(C3).
)'8IN
!usoMid 0H d%$sMsUl5openM!uso.
vMupdate 0H =update student set $a&s H 0s$a&s whee no H 0sno=.
d%$sMsUl5pase(!usoMid#vMupdate#d%$sMsUl5native).
d%$sMsUl5%indMvaia%le(!usoMid#=0sno=#v2).
Copy rights are reserved.
216
d%$sMsUl5%indMvaia%le(!usoMid#=0s$a&s=#v1).
flag 0H d%$sMsUl5e/e!ute(!usoMid).
d%$sMsUl5!loseM!uso(!usoMid).
'ND D)MSMSQLMAROC.
Output0
SQL- sele!t E fo$ student. "" %efoe e/e!ution
NO N( M(RFS
"""" """""" "" """"""""""
2 a 233
1 % 133
4 ! 433
SQL- e/e! d%$sMsUlMpo!(1#111)
SQL- sele!t E fo$ student. "" afte e/e!ution
NO N( M(RFS
"""" """""" "" """"""""""
2 a 233
1 % 111
4 ! 433
BOR(LL ST(T'M'NT
This !an %e used to get the data fo$ the data%ase at on!e %7 edu!ting the nu$%e of
!onte/t swit!hes whi!h is a tansfe of !ontol %etween AL?SQL and SQL engine5
S7nta/0
Boall inde/Mva in
N 4ower_bound55upper_bound X
Indi!es of inde(in"_collection X
9alues of inde(in"_collection O
SQL state$ent.
BOR(LL 6IT: NON"S'QU'NTI(L (RR(*S
'/0
Copy rights are reserved.
217
D'CL(R'
t7pe t is ta%le of student5noLt7pe inde/ %7 %ina7Mintege.
i%t t.
)'8IN
i%t(2) 0H 2.
i%t(23) 0H 1.
foall i in i%t5fist55i%t5last
update student set $a&s H S33 whee no H i%t(i).
'ND.
The a%ove poga$ will give eo li&e Kele$ent at inde/ N1O does not e/ists5
*ou !an e!tif7 it in one of the two following wa7s5
US8(8' OB INDIC'S OB TO (9OID T:' ()O9' )':(9IOUR
This will %e used when 7ou have a !olle!tion whose defined ows spe!if7 whi!h ows in the
%inding aa7 7ou would li&e to po!essed5
'/0
D'CL(R'
t7pe t is ta%le of student5noLt7pe inde/ %7 %ina7Mintege.
i%t t.
t7pe t2 is ta%le of %oolean inde/ %7 %ina7Mintege.
i%t2 t2.
)'8IN
i%t(2) 0H 2.
i%t(23) 0H 1.
i%t(233) 0H 4.
i%t2(2) 0H tue.
i%t2(23) 0H tue.
i%t2(233) 0H tue.
foall i in indi!es of i%t2
update student set $a&s H S33 whee no H i%t(i).
'ND.
Ouput0
SQL- sele!t E fo$ student "" %efoe e/e!ution
NO N( M(RFS
"""""""""" """"""""""""
2 a 233
Copy rights are reserved.
218
1 % 133
4 ! 433
SQL- sele!t E fo$ student "" afte e/e!ution
NO N( M(RFS
"""""""""" """"""""""""
2 a S33
1 % S33
4 ! S33
US8(8' OB 9(LU'S OB TO (9OID T:' ()O9' )':(9IOUR
This will %e used when 7ou have a !olle!tion of integes whose !ontent identifies the
position in the %inding aa7 that 7ou want to %e po!essed %7 the BOR(LL state$ent5
'/0
D'CL(R'
t7pe t is ta%le of student5noLt7pe inde/ %7 %ina7Mintege.
i%t t.
t7pe t2 is ta%le of plsMintege inde/ %7 %ina7Mintege.
i%t2 t2.
)'8IN
i%t(2) 0H 2.
i%t(23) 0H 1.
i%t(233) 0H 4.
i%t2(22) 0H 2.
i%t2(2C) 0H 23.
i%t2(2W) 0H 233.
foall i in values of i%t2
update student set $a&s H CDV whee no H i%t(i).
'ND.
Ouput0
SQL- sele!t E fo$ student "" %efoe e/e!ution
NO N( M(RFS
"""""""""" """"""""""""
2 a 233
1 % 133
4 ! 433
Copy rights are reserved.
219
SQL- sele!t E fo$ student "" afte e/e!ution
NO N( M(RFS
"""""""""" """"""""""""
2 a S33
1 % S33
4 ! S33

AOINTS ()OUT )ULF )INDS
Aassing the entie AL?SQL ta%le to the SQL engine in one step is &nown as %ul& %ind5
)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
AOINTS ()OUT R'TURIN8 CL(US'
This will %e used onl7 with DML state$ents to etun data into AL?SQL 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
Copy rights are reserved.
220
9aa7s
Nested ta%les
Inde/ " %7 ta%les ((sso!iate aa7s)
9(RR(*S
( vaa7 is datat7pe ve7 si$ila to an aa75 ( vaa7 has a fi/ed li$it on its siTe# 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$ siTe of the vaa7 is 1 giga %7tes5
S7nta/0
T7pe ,type_name& is vaa7 X va7ing aa7 (,limit&) of ,element_type&.
'/20
D'CL(R'
t7pe t is vaa7(23) of va!ha(1).
va t 0H t(=a=#=%=#=!=#=d=).
flag %oolean.
)'8IN
d%$sMoutput5putMline(=Li$it H = XX va5li$it).
d%$sMoutput5putMline(=Count H = XX va5!ount).
d%$sMoutput5putMline(=Bist Inde/ H = XX va5fist).
d%$sMoutput5putMline(=Last Inde/ H = XX va5last).
d%$sMoutput5putMline(=Ne/t Inde/ H = XX va5ne/t(1)).
d%$sMoutput5putMline(=Aevious Inde/ H = XX va5pio(4)).
d%$sMoutput5putMline(=9(RR(* 'L'M'NTS=).
fo i in va5fist55va5last loop
d%$sMoutput5putMline(=vaN= XX i XX =O H = XX va(i)).
end loop.
flag 0H va5e/ists(4).
if flag H tue then
d%$sMoutput5putMline(=Inde/ 4 e/ists with an ele$ent = XX va(4)).
else
d%$sMoutput5putMline(=Inde/ 4 does not e/ists=).
end if.
va5e/tend.
d%$sMoutput5putMline(=(fte e/tend of one inde/# Count H = XX va5!ount).
flag 0H va5e/ists(C).
if flag H tue then
d%$sMoutput5putMline(=Inde/ C e/ists with an ele$ent = XX va(C)).
Copy rights are reserved.
221
else
d%$sMoutput5putMline(=Inde/ C does not e/ists=).
end if.
flag 0H va5e/ists(D).
if flag H tue then
d%$sMoutput5putMline(=Inde/ D e/ists with an ele$ent = XX va(D)).
else
d%$sMoutput5putMline(=Inde/ D does not e/ists=).
end if.
va5e/tend(1).
d%$sMoutput5putMline(=(fte e/tend of two inde/es# Count H = XX va5!ount).
d%$sMoutput5putMline(=9(RR(* 'L'M'NTS=).
fo i in va5fist55va5last loop
d%$sMoutput5putMline(=vaN= XX i XX =O H = XX va(i)).
end loop.
va(C) 0H =e=.
va(D) 0H =f=.
va(V) 0H =g=.
d%$sMoutput5putMline(=(BT'R (SSIN8NIN8 9(LU'S TO '+T'ND'D 'L'M'NTS#
9(RR(* 'L'M'NTS=).
fo i in va5fist55va5last loop
d%$sMoutput5putMline(=vaN= XX i XX =O H = XX va(i)).
end loop.
va5e/tend(4#1).
d%$sMoutput5putMline(=(fte e/tend of thee inde/es# Count H = XX va5!ount).
d%$sMoutput5putMline(=9(RR(* 'L'M'NTS=).
fo i in va5fist55va5last loop
d%$sMoutput5putMline(=vaN= XX i XX =O H = XX va(i)).
end loop.
va5ti$.
d%$sMoutput5putMline(=(fte ti$ of one inde/# Count H = XX va5!ount).
va5ti$(4).
d%$sMoutput5putMline(=(fte ti$ of thee inde/s# Count H = XX va5!ount).
d%$sMoutput5putMline(=(BT'R TRIM# 9(RR(* 'L'M'NTS=).
fo i in va5fist55va5last loop
d%$sMoutput5putMline(=vaN= XX i XX =O H = XX va(i)).
end loop.
va5delete.
d%$sMoutput5putMline(=(fte delete of entie vaa7# Count H = XX va5!ount).
'ND.
Copy rights are reserved.
222
Output0
Li$it H 23
Count H @
Bist Inde/ H 2
Last Inde/ H @
Ne/t Inde/ H 4
Aevious Inde/ H 1
9(RR(* 'L'M'NTS
vaN2O H a
vaN1O H %
vaN4O H !
vaN@O H d
Inde/ 4 e/ists with an ele$ent !
(fte e/tend of one inde/# Count H C
Inde/ C e/ists with an ele$ent
Inde/ D does not e/ists
(fte e/tend of two inde/es# Count H V
9(RR(* 'L'M'NTS
vaN2O H a
vaN1O H %
vaN4O H !
vaN@O H d
vaNCO H
vaNDO H
vaNVO H
(BT'R (SSIN8NIN8 9(LU'S TO '+T'ND'D 'L'M'NTS# 9(RR(* 'L'M'NTS
vaN2O H a
vaN1O H %
vaN4O H !
vaN@O H d
vaNCO H e
vaNDO H f
vaNVO H g
(fte e/tend of thee inde/es# Count H 23
9(RR(* 'L'M'NTS
vaN2O H a
vaN1O H %
vaN4O H !
Copy rights are reserved.
223
vaN@O H d
vaNCO H e
vaNDO H f
vaNVO H g
vaNWO H %
vaNSO H %
vaN23O H %
(fte ti$ of one inde/# Count H S
(fte ti$ of thee inde/s# Count H D
(BT'R TRIM# 9(RR(* 'L'M'NTS
vaN2O H a
vaN1O H %
vaN4O H !
vaN@O H d
vaNCO H e
vaNDO H f
(fte delete of entie vaa7# Count H 3
'/10
D'CL(R'
t7pe t is vaa7(@) of studentLowt7pe.
va t 0H t(null#null#null#null).
)'8IN
fo i in 255va5!ount loop
sele!t E into va(i) fo$ student whee sno H i.
d%$sMoutput5putMline(=Sno H = XX va(i)5sno XX = Sna$e H = XX va(i)5sna$e).
end loop.
'ND.
Output0
Sno H 2 Sna$e H sa&eth
Sno H 1 Sna$e H sinu
Sno H 4 Sna$e H div7a
Sno H @ Sna$e H $anogni
'/40
D'CL(R'
t7pe t is vaa7(@) of student5s$a&sLt7pe.
va t 0H t(null#null#null#null).
)'8IN
Copy rights are reserved.
224
fo i in 255va5!ount loop
sele!t s$a&s into va(i) fo$ student whee sno H i.
d%$sMoutput5putMline(=S$a&s H = XX va(i)).
end loop.
'ND.
Output0
S$a&s H 233
S$a&s H 133
S$a&s H 433
S$a&s H @33
'/@0
D'CL(R'
t7pe is e!od(!2 student5sna$eLt7pe#!1 student5s$a&sLt7pe).
t7pe t is vaa7(@) of .
va t 0H 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 H i.
d%$sMoutput5putMline(=Sna$e H = XX va(i)5!2 XX = S$a&s H = XX va(i)5!1).
end loop.
'ND.
Output0
Sna$e H sa&eth S$a&s H 233
Sna$e H sinu S$a&s H 133
Sna$e H div7a S$a&s H 433
Sna$e H $anogni S$a&s H @33
'/C0
D'CL(R'
t7pe t is vaa7(2) of add.
va t 0H t(null).
!uso ! is sele!t E fo$ e$plo7.
i nu$%e 0H 2.
)'8IN
fo v in ! loop
sele!t addess into va(i) fo$ e$plo7 whee ena$e H v5ena$e.
d%$sMoutput5putMline(=:no H = XX va(i)5hno XX = Cit7 H = XX va(i)5!it7).
end loop.
Copy rights are reserved.
225
'ND.
Output0
:no H 22 Cit7 H h7d
:no H 11 Cit7 H %ang
:no H 44 Cit7 H &o!hi
'/D0
D'CL(R'
t7pe t is vaa7(C) of va!ha(1).
va2 t.
va1 t 0H t().
)'8IN
if va2 is null then
d%$sMoutput5putMline(=va2 is null=).
else
d%$sMoutput5putMline(=va2 is not null=).
end if.
if va1 is null then
d%$sMoutput5putMline(=va1 is null=).
else
d%$sMoutput5putMline(=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 siTe5 'le$ents ae
inseted into nested ta%le stating at inde/ 25 The $a/i$u$ siTe of the vaa7 is 1 giga
%7tes5
S7nta/0
T7pe ,type_name& is ta%le of ,table_type&.
Copy rights are reserved.
226
'/20
D'CL(R'
t7pe t is ta%le of va!ha(1).
nt t 0H t(=a=#=%=#=!=#=d=).
flag %oolean.
)'8IN
if nt5li$it is null then
d%$sMoutput5putMline(=No li$it to Nested Ta%les=).
else
d%$sMoutput5putMline(=Li$it H = XX nt5li$it).
end if.
d%$sMoutput5putMline(=Count H = XX nt5!ount).
d%$sMoutput5putMline(=Bist Inde/ H = XX nt5fist).
d%$sMoutput5putMline(=Last Inde/ H = XX nt5last).
d%$sMoutput5putMline(=Ne/t Inde/ H = XX nt5ne/t(1)).
d%$sMoutput5putMline(=Aevious Inde/ H = XX nt5pio(4)).
d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=).
fo i in 255nt5!ount loop
d%$sMoutput5putMline(=ntN= XX i XX =O H = XX nt(i)).
end loop.
flag 0H nt5e/ists(4).
if flag H tue then
d%$sMoutput5putMline(=Inde/ 4 e/ists with an ele$ent = XX nt(4)).
else
d%$sMoutput5putMline(=Inde/ 4 does not e/ists=).
end if.
nt5e/tend.
d%$sMoutput5putMline(=(fte e/tend of one inde/# Count H = XX nt5!ount).
flag 0H nt5e/ists(C).
if flag H tue then
d%$sMoutput5putMline(=Inde/ C e/ists with an ele$ent = XX nt(C)).
else
d%$sMoutput5putMline(=Inde/ C does not e/ists=).
end if.
flag 0H nt5e/ists(D).
if flag H tue then
d%$sMoutput5putMline(=Inde/ D e/ists with an ele$ent = XX nt(D)).
else
d%$sMoutput5putMline(=Inde/ D does not e/ists=).
end if.
Copy rights are reserved.
227
nt5e/tend(1).
d%$sMoutput5putMline(=(fte e/tend of two inde/es# Count H = XX nt5!ount).
d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=).
fo i in 255nt5!ount loop
d%$sMoutput5putMline(=ntN= XX i XX =O H = XX nt(i)).
end loop.
nt(C) 0H =e=.
nt(D) 0H =f=.
nt(V) 0H =g=.
d%$sMoutput5putMline(=(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%$sMoutput5putMline(=ntN= XX i XX =O H = XX nt(i)).
end loop.
nt5e/tend(C#1).
d%$sMoutput5putMline(=(fte e/tend of five inde/es# Count H = XX nt5!ount).
d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=).
fo i in 255nt5!ount loop
d%$sMoutput5putMline(=ntN= XX i XX =O H = XX nt(i)).
end loop.
nt5ti$.
d%$sMoutput5putMline(=(fte ti$ of one inde/# Count H = XX nt5!ount).
nt5ti$(4).
d%$sMoutput5putMline(=(fte ti$ of thee inde/s# Count H = XX nt5!ount).
d%$sMoutput5putMline(=(BT'R TRIM# N'ST'D T()L' 'L'M'NTS=).
fo i in 255nt5!ount loop
d%$sMoutput5putMline(=ntN= XX i XX =O H = XX nt(i)).
end loop.
nt5delete(2).
d%$sMoutput5putMline(=(fte delete of fist inde/# Count H = XX nt5!ount).
d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=).
fo i in 155nt5!ountG2 loop
d%$sMoutput5putMline(=ntN= XX i XX =O H = XX nt(i)).
end loop.
nt5delete(@).
d%$sMoutput5putMline(=(fte delete of fouth inde/# Count H = XX nt5!ount).
d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=).
fo i in 1554 loop
d%$sMoutput5putMline(=ntN= XX i XX =O H = XX nt(i)).
end loop.
Copy rights are reserved.
228
fo i in C55nt5!ountG1 loop
d%$sMoutput5putMline(=ntN= XX i XX =O H = XX nt(i)).
end loop.
nt5delete.
d%$sMoutput5putMline(=(fte delete of entie nested ta%le# Count H = XX
nt5!ount).
'ND.
Output0
No li$it to Nested Ta%les
Count H @
Bist Inde/ H 2
Last Inde/ H @
Ne/t Inde/ H 4
Aevious Inde/ H 1
N'ST'D T()L' 'L'M'NTS
ntN2O H a
ntN1O H %
ntN4O H !
ntN@O H d
Inde/ 4 e/ists with an ele$ent !
(fte e/tend of one inde/# Count H C
Inde/ C e/ists with an ele$ent
Inde/ D does not e/ists
(fte e/tend of two inde/es# Count H V
N'ST'D T()L' 'L'M'NTS
ntN2O H a
ntN1O H %
ntN4O H !
ntN@O H d
ntNCO H
ntNDO H
ntNVO H
(BT'R (SSIN8NIN8 9(LU'S TO '+T'ND'D 'L'M'NTS# N'ST'D T()L'
'L'M'NTS
ntN2O H a
ntN1O H %
ntN4O H !
ntN@O H d
ntNCO H e
Copy rights are reserved.
229
ntNDO H f
ntNVO H g
(fte e/tend of five inde/es# Count H 21
N'ST'D T()L' 'L'M'NTS
ntN2O H a
ntN1O H %
ntN4O H !
ntN@O H d
ntNCO H e
ntNDO H f
ntNVO H g
ntNWO H %
ntNSO H %
ntN23O H %
ntN22O H %
ntN21O H %
(fte ti$ of one inde/# Count H 22
(fte ti$ of thee inde/s# Count H W
(BT'R TRIM# N'ST'D T()L' 'L'M'NTS
ntN2O H a
ntN1O H %
ntN4O H !
ntN@O H d
ntNCO H e
ntNDO H f
ntNVO H g
ntNWO H %
(fte delete of fist inde/# Count H V
N'ST'D T()L' 'L'M'NTS
ntN1O H %
ntN4O H !
ntN@O H d
ntNCO H e
ntNDO H f
ntNVO H g
ntNWO H %
(fte delete of fouth inde/# Count H D
N'ST'D T()L' 'L'M'NTS
ntN1O H %
Copy rights are reserved.
230
ntN4O H !
ntNCO H e
ntNDO H f
ntNVO H g
ntNWO H %
(fte delete of entie nested ta%le# Count H 3
'/10
D'CL(R'
t7pe t is ta%le of studentLowt7pe.
nt t 0H t(null#null#null#null).
)'8IN
fo i in 255nt5!ount loop
sele!t E into nt(i) fo$ student whee sno H i.
d%$sMoutput5putMline(=Sno H = XX nt(i)5sno XX = Sna$e H = XX nt(i)5sna$e).
end loop.
'ND.
Output0
Sno H 2 Sna$e H sa&eth
Sno H 1 Sna$e H sinu
Sno H 4 Sna$e H div7a
Sno H @ Sna$e H $anogni
'/40
D'CL(R'
t7pe t is ta%le of student5s$a&sLt7pe.
nt t 0H t(null#null#null#null).
)'8IN
fo i in 255nt5!ount loop
sele!t s$a&s into nt(i) fo$ student whee sno H i.
d%$sMoutput5putMline(=S$a&s H = XX nt(i)).
end loop.
'ND.
Output0
S$a&s H 233
S$a&s H 133
S$a&s H 433
S$a&s H @33
Copy rights are reserved.
231
'/@0
D'CL(R'
t7pe is e!od(!2 student5sna$eLt7pe#!1 student5s$a&sLt7pe).
t7pe t is ta%le of .
nt t 0H 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 H i.
d%$sMoutput5putMline(=Sna$e H = XX nt(i)5!2 XX = S$a&s H = XX nt(i)5!1).
end loop.
'ND.
Output0
Sna$e H sa&eth S$a&s H 233
Sna$e H sinu S$a&s H 133
Sna$e H div7a S$a&s H 433
Sna$e H $anogni S$a&s H @33
'/C0
D'CL(R'
t7pe t is ta%le of add.
nt t 0H t(null).
!uso ! is sele!t E fo$ e$plo7.
i nu$%e 0H 2.
)'8IN
fo v in ! loop
sele!t addess into nt(i) fo$ e$plo7 whee ena$e H v5ena$e.
d%$sMoutput5putMline(=:no H = XX nt(i)5hno XX = Cit7 H = XX nt(i)5!it7).
end loop.
'ND.
Output0
:no H 22 Cit7 H h7d
:no H 11 Cit7 H %ang
:no H 44 Cit7 H &o!hi
'/D0
D'CL(R'
t7pe t is vaa7(C) of va!ha(1).
Copy rights are reserved.
232
nt2 t.
nt1 t 0H t().
)'8IN
if nt2 is null then
d%$sMoutput5putMline(=nt2 is null=).
else
d%$sMoutput5putMline(=nt2 is not null=).
end if.
if nt1 is null then
d%$sMoutput5putMline(=nt1 is null=).
else
d%$sMoutput5putMline(=nt1 is not null=).
end if.
'ND.
Output0
nt2 is null
nt1 is not null
S'T OA'R(TIONS IN N'ST'D T()L'S
*ou !an pefo$ set opeations in the nested ta%les5 *ou !an also pefo$ eUualit7
!o$paisions %etween nested ta%les5
Aossi%le opeations ae
UNION
UNION DISTINCT
INT'RS'CT
'+C'AT ( a!t li&e MINUS)
'/0
D'CL(R'
t7pe t is ta%le of va!ha(1).
nt2 t 0H t(=a=#=%=#=!=).
nt1 t 0H t(=!=#=%=#=a=).
nt4 t 0H t(=%=#=!=#=a=#=!=).
nt@ t 0H t(=a=#=%=#=d=).
ntC t.
)'8IN
Copy rights are reserved.
233
ntC 0H set(nt2).
d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=).
fo i in ntC5fist55ntC5last loop
d%$sMoutput5putMline(=ntCN = XX i XX = O H = XX ntC(i)).
end loop.
ntC 0H set(nt4).
d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=).
fo i in ntC5fist55ntC5last loop
d%$sMoutput5putMline(=ntCN = XX i XX = O H = XX ntC(i)).
end loop.
ntC 0H nt2 $ultiset union nt@.
d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=).
fo i in ntC5fist55ntC5last loop
d%$sMoutput5putMline(=ntCN = XX i XX = O H = XX ntC(i)).
end loop.
ntC 0H nt2 $ultiset union nt4.
d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=).
fo i in ntC5fist55ntC5last loop
d%$sMoutput5putMline(=ntCN = XX i XX = O H = XX ntC(i)).
end loop.
ntC 0H nt2 $ultiset union distin!t nt4.
d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=).
fo i in ntC5fist55ntC5last loop
d%$sMoutput5putMline(=ntCN = XX i XX = O H = XX ntC(i)).
end loop.
ntC 0H nt2 $ultiset e/!ept nt@.
d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=).
fo i in ntC5fist55ntC5last loop
d%$sMoutput5putMline(=ntCN = XX i XX = O H = XX ntC(i)).
end loop.
ntC 0H nt@ $ultiset e/!ept nt2.
d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=).
fo i in ntC5fist55ntC5last loop
d%$sMoutput5putMline(=ntCN = XX i XX = O H = XX ntC(i)).
end loop.
'ND.
Output0
N'ST'D T()L' 'L'M'NTS
Copy rights are reserved.
234
ntCN 2 O H a
ntCN 1 O H %
ntCN 4 O H !
N'ST'D T()L' 'L'M'NTS
ntCN 2 O H %
ntCN 1 O H !
ntCN 4 O H a
N'ST'D T()L' 'L'M'NTS
ntCN 2 O H a
ntCN 1 O H %
ntCN 4 O H !
ntCN @ O H a
ntCN C O H %
ntCN D O H d
N'ST'D T()L' 'L'M'NTS
ntCN 2 O H a
ntCN 1 O H %
ntCN 4 O H !
ntCN @ O H %
ntCN C O H !
ntCN D O H a
ntCN V O H !
N'ST'D T()L' 'L'M'NTS
ntCN 2 O H a
ntCN 1 O H %
ntCN 4 O H !
N'ST'D T()L' 'L'M'NTS
ntCN 2 O H !
N'ST'D T()L' 'L'M'NTS
ntCN 2 O H d
IND'+")* T()L'S
(n inde/"%7 ta%le has no li$it on its siTe5 'le$ents ae inseted into inde/"%7 ta%le whose
inde/ $a7 stat non"seUuentiall7 in!luding negative integes5
S7nta/0
T7pe ,type_name& is ta%le of ,table_type& inde/ %7 %ina7Mintege.
Copy rights are reserved.
235
'/0
D'CL(R'
t7pe t is ta%le of va!ha(1) inde/ %7 %ina7Mintege.
i%t t.
flag %oolean.
)'8IN
i%t(2) 0H =a=.
i%t("13) 0H =%=.
i%t(43) 0H =!=.
i%t(233) 0H =d=.
if i%t5li$it is null then
d%$sMoutput5putMline(=No li$it to Inde/ %7 Ta%les=).
else
d%$sMoutput5putMline(=Li$it H = XX i%t5li$it).
end if.
d%$sMoutput5putMline(=Count H = XX i%t5!ount).
d%$sMoutput5putMline(=Bist Inde/ H = XX i%t5fist).
d%$sMoutput5putMline(=Last Inde/ H = XX i%t5last).
d%$sMoutput5putMline(=Ne/t Inde/ H = XX i%t5ne/t(1)).
d%$sMoutput5putMline(=Aevious Inde/ H = XX i%t5pio(4)).
d%$sMoutput5putMline(=IND'+ )* T()L' 'L'M'NTS=).
d%$sMoutput5putMline(=i%tN"13O H = XX i%t("13)).
d%$sMoutput5putMline(=i%tN2O H = XX i%t(2)).
d%$sMoutput5putMline(=i%tN43O H = XX i%t(43)).
d%$sMoutput5putMline(=i%tN233O H = XX i%t(233)).
flag 0H i%t5e/ists(43).
if flag H tue then
d%$sMoutput5putMline(=Inde/ 43 e/ists with an ele$ent = XX i%t(43)).
else
d%$sMoutput5putMline(=Inde/ 43 does not e/ists=).
end if.
flag 0H i%t5e/ists(C3).
if flag H tue then
d%$sMoutput5putMline(=Inde/ C3 e/ists with an ele$ent = XX i%t(43)).
else
d%$sMoutput5putMline(=Inde/ C3 does not e/ists=).
end if.
i%t5delete(2).
d%$sMoutput5putMline(=(fte delete of fist inde/# Count H = XX i%t5!ount).
i%t5delete(43).
Copy rights are reserved.
236
d%$sMoutput5putMline(=(fte delete of inde/ thit7# Count H = XX i%t5!ount).
d%$sMoutput5putMline(=IND'+ )* T()L' 'L'M'NTS=).
d%$sMoutput5putMline(=i%tN"13O H = XX i%t("13)).
d%$sMoutput5putMline(=i%tN233O H = XX i%t(233)).
i%t5delete.
d%$sMoutput5putMline(=(fte delete of entie inde/"%7 ta%le# Count H = XX
i%t5!ount).
'ND.
Output0
No li$it to Inde/ %7 Ta%les
Count H @
Bist Inde/ H "13
Last Inde/ H 233
Ne/t Inde/ H 43
Aevious Inde/ H 2
IND'+ )* T()L' 'L'M'NTS
i%tN"13O H %
i%tN2O H a
i%tN43O H !
i%tN233O H d
Inde/ 43 e/ists with an ele$ent !
Inde/ C3 does not e/ists
(fte delete of fist inde/# Count H 4
(fte delete of inde/ thit7# Count H 1
IND'+ )* T()L' 'L'M'NTS
i%tN"13O H %
i%tN233O H d
(fte delete of entie inde/"%7 ta%le# Count H 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 initialiTed %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
Copy rights are reserved.
237
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
Refeen!ing none/istent ele$ents aises SU)SCRIATM)'*ONDMCOUNT in %oth nested
ta%les and vaa7s# %ut in !ase of inde/"%7 ta%les NOMD(T(MBOUND aises5
Fe7s ae seUuential in %oth nested ta%les and vaa7s# non"seUuential 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 %ina7Mintege.
T7pe ,type_name2& is vaa7(,limit-) X ta%le X of ,type_name1& < inde/ %7
%ina7Mintege.
'/20
D'CL(R'
t7pe t2 is ta%le of va!ha(1) inde/ %7 %ina7Mintege.
t7pe t1 is vaa7(C) of t2.
va t1 0H t1().
! nu$%e 0H SV.
flag %oolean.
)'8IN
va5e/tend(@).
d%$sMoutput5putMline(=Count H = XX va5!ount).
d%$sMoutput5putMline(=Li$it H = XX va5li$it).
fo i in 255va5!ount loop
fo J in 255va5!ount loop
va(i)(J) 0H !h(!).
! 0H ! G 2.
end loop.
end loop.
Copy rights are reserved.
238
d%$sMoutput5putMline(=9(RR(* 'L'M'NTS=).
fo i in 255va5!ount loop
fo J in 255va5!ount loop
d%$sMoutput5putMline(=vaN= XX i XX =ON= XX J XX =O H = XX va(i)(J)).
end loop.
end loop.
d%$sMoutput5putMline(=Bist inde/ H = XX va5fist).
d%$sMoutput5putMline(=Last inde/ H = XX va5last).
d%$sMoutput5putMline(=Ne/t inde/ H = XX va5ne/t(1)).
d%$sMoutput5putMline(=Aevious inde/ H = XX va5pio(4)).
flag 0H va5e/ists(1).
if flag H tue then
d%$sMoutput5putMline(=Inde/ 1 e/ists=).
else
d%$sMoutput5putMline(=Inde/ 1 e/ists=).
end if.
va5e/tend.
va(2)(C) 0H =U=.
va(1)(C) 0H ==.
va(4)(C) 0H =s=.
va(@)(C) 0H =t=.
va(C)(2) 0H =u=.
va(C)(1) 0H =v=.
va(C)(4) 0H =w=.
va(C)(@) 0H =/=.
va(C)(C) 0H =7=.
d%$sMoutput5putMline(=(fte e/tend of one inde/# Count H = XX va5!ount).
d%$sMoutput5putMline(=9(RR(* 'L'M'NTS=).
fo i in 255va5!ount loop
fo J in 255va5!ount loop
d%$sMoutput5putMline(=vaN= XX i XX =ON= XX J XX =O H = XX va(i)(J)).
end loop.
end loop.
va5ti$.
d%$sMoutput5putMline(=(fte ti$ of one inde/# Count H = XX va5!ount).
va5ti$(1).
d%$sMoutput5putMline(=(fte ti$ of two inde/es# Count H = XX va5!ount).
d%$sMoutput5putMline(=9(RR(* 'L'M'NTS=).
fo i in 255va5!ount loop
Copy rights are reserved.
239
fo J in 255va5!ount loop
d%$sMoutput5putMline(=vaN= XX i XX =ON= XX J XX =O H = XX va(i)(J)).
end loop.
end loop.
va5delete.
d%$sMoutput5putMline(=(fte delete of entie vaa7# Count H = XX va5!ount).
'ND.
Output0
Count H @
Li$it H C
9(RR(* 'L'M'NTS
vaN2ON2O H a
vaN2ON1O H %
vaN2ON4O H !
vaN2ON@O H d
vaN1ON2O H e
vaN1ON1O H f
vaN1ON4O H g
vaN1ON@O H h
vaN4ON2O H i
vaN4ON1O H J
vaN4ON4O H &
vaN4ON@O H l
vaN@ON2O H $
vaN@ON1O H n
vaN@ON4O H o
vaN@ON@O H p
Bist inde/ H 2
Last inde/ H @
Ne/t inde/ H 4
Aevious inde/ H 1
Inde/ 1 e/ists
(fte e/tend of one inde/# Count H C
9(RR(* 'L'M'NTS
vaN2ON2O H a
vaN2ON1O H %
vaN2ON4O H !
vaN2ON@O H d
Copy rights are reserved.
240
vaN2ONCO H U
vaN1ON2O H e
vaN1ON1O H f
vaN1ON4O H g
vaN1ON@O H h
vaN1ONCO H
vaN4ON2O H i
vaN4ON1O H J
vaN4ON4O H &
vaN4ON@O H l
vaN4ONCO H s
vaN@ON2O H $
vaN@ON1O H n
vaN@ON4O H o
vaN@ON@O H p
vaN@ONCO H t
vaNCON2O H u
vaNCON1O H v
vaNCON4O H w
vaNCON@O H /
vaNCONCO H 7
(fte ti$ of one inde/# Count H @
(fte ti$ of two inde/es# Count H 1
9(RR(* 'L'M'NTS
vaN2ON2O H a
vaN2ON1O H %
vaN1ON2O H e
vaN1ON1O H f
(fte delete of entie vaa7# Count H 3
'/10
D'CL(R'
t7pe t2 is ta%le of va!ha(1) inde/ %7 %ina7Mintege.
t7pe t1 is ta%le of t2.
nt t1 0H t1().
! nu$%e 0H DC.
v nu$%e 0H 2.
flag %oolean.
)'8IN
nt5e/tend(@).
Copy rights are reserved.
241
d%$sMoutput5putMline(=Count H = XX nt5!ount).
if nt5li$it is null then
d%$sMoutput5putMline(=No li$it to Nested Ta%les=).
else
d%$sMoutput5putMline(=Li$it H = XX nt5li$it).
end if.
fo i in 255nt5!ount loop
fo J in 255nt5!ount loop
nt(i)(J) 0H !h(!).
! 0H ! G 2.
if ! H S2 then
! 0H SV.
end if.
end loop.
end loop.
d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=).
fo i in 255nt5!ount loop
fo J in 255nt5!ount loop
d%$sMoutput5putMline(=ntN= XX i XX =ON= XX J XX =O H = XX nt(i)(J)).
end loop.
end loop.
d%$sMoutput5putMline(=Bist inde/ H = XX nt5fist).
d%$sMoutput5putMline(=Last inde/ H = XX nt5last).
d%$sMoutput5putMline(=Ne/t inde/ H = XX nt5ne/t(1)).
d%$sMoutput5putMline(=Aevious inde/ H = XX nt5pio(4)).
flag 0H nt5e/ists(1).
if flag H tue then
d%$sMoutput5putMline(=Inde/ 1 e/ists=).
else
d%$sMoutput5putMline(=Inde/ 1 e/ists=).
end if.
nt5e/tend(1).
nt(2)(C) 0H =Q=.
nt(2)(D) 0H =R=.
nt(1)(C) 0H =S=.
nt(1)(D) 0H =T=.
nt(4)(C) 0H =U=.
nt(4)(D) 0H =9=.
nt(@)(C) 0H =6=.
Copy rights are reserved.
242
nt(@)(D) 0H =+=.
nt(C)(2) 0H =*=.
nt(C)(1) 0H =Z=.
nt(C)(4) 0H =a=.
nt(C)(@) 0H =%=.
nt(C)(C) 0H =!=.
nt(C)(D) 0H =d=.
nt(D)(2) 0H =e=.
nt(D)(1) 0H =f=.
nt(D)(4) 0H =g=.
nt(D)(@) 0H =h=.
nt(D)(C) 0H =i=.
nt(D)(D) 0H =J=.
d%$sMoutput5putMline(=(fte e/tend of one inde/# Count H = XX nt5!ount).
d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=).
fo i in 255nt5!ount loop
fo J in 255nt5!ount loop
d%$sMoutput5putMline(=ntN= XX i XX =ON= XX J XX =O H = XX nt(i)(J)).
end loop.
end loop.
nt5ti$.
d%$sMoutput5putMline(=(fte ti$ of one inde/e# Count H = XX nt5!ount).
nt5ti$(1).
d%$sMoutput5putMline(=(fte ti$ of two inde/es# Count H = XX nt5!ount).
d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=).
fo i in 255nt5!ount loop
fo J in 255nt5!ount loop
d%$sMoutput5putMline(=ntN= XX i XX =ON= XX J XX =O H = XX nt(i)(J)).
end loop.
end loop.
nt5delete(1).
d%$sMoutput5putMline(=(fte delete of se!ond inde/# Count H = XX nt5!ount).
d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=).
loop
e/it when v H @.
fo J in 255nt5!ountG2 loop
d%$sMoutput5putMline(=ntN= XX v XX =ON= XX J XX =O H = XX nt(v)(J)).
end loop.
v 0H v G 2.
Copy rights are reserved.
243
if vH 1 then
v 0H 4.
end if.
end loop.
nt5delete.
d%$sMoutput5putMline(=(fte delete of entie nested ta%le# Count H = XX
nt5!ount).
'ND.
Output0
Count H @
No li$it to Nested Ta%les
N'ST'D T()L' 'L'M'NTS
ntN2ON2O H (
ntN2ON1O H )
ntN2ON4O H C
ntN2ON@O H D
ntN1ON2O H '
ntN1ON1O H B
ntN1ON4O H 8
ntN1ON@O H :
ntN4ON2O H I
ntN4ON1O H >
ntN4ON4O H F
ntN4ON@O H L
ntN@ON2O H M
ntN@ON1O H N
ntN@ON4O H O
ntN@ON@O H A
Bist inde/ H 2
Last inde/ H @
Ne/t inde/ H 4
Aevious inde/ H 1
Inde/ 1 e/ists
(fte e/tend of one inde/# Count H D
N'ST'D T()L' 'L'M'NTS
ntN2ON2O H (
ntN2ON1O H )
Copy rights are reserved.
244
ntN2ON4O H C
ntN2ON@O H D
ntN2ONCO H Q
ntN2ONDO H R
ntN1ON2O H '
ntN1ON1O H B
ntN1ON4O H 8
ntN1ON@O H :
ntN1ONCO H S
ntN1ONDO H T
ntN4ON2O H I
ntN4ON1O H >
ntN4ON4O H F
ntN4ON@O H L
ntN4ONCO H U
ntN4ONDO H 9
ntN@ON2O H M
ntN@ON1O H N
ntN@ON4O H O
ntN@ON@O H A
ntN@ONCO H 6
ntN@ONDO H +
ntNCON2O H *
ntNCON1O H Z
ntNCON4O H a
ntNCON@O H %
ntNCONCO H !
ntNCONDO H d
ntNDON2O H e
ntNDON1O H f
ntNDON4O H g
ntNDON@O H h
ntNDONCO H i
ntNDONDO H J
(fte ti$ of one inde/e# Count H C
(fte ti$ of two inde/es# Count H 4
N'ST'D T()L' 'L'M'NTS
ntN2ON2O H (
ntN2ON1O H )
Copy rights are reserved.
245
ntN2ON4O H C
ntN1ON2O H '
ntN1ON1O H B
ntN1ON4O H 8
ntN4ON2O H I
ntN4ON1O H >
ntN4ON4O H F
(fte delete of se!ond inde/# Count H 1
N'ST'D T()L' 'L'M'NTS
ntN2ON2O H (
ntN2ON1O H )
ntN2ON4O H C
ntN4ON2O H I
ntN4ON1O H >
ntN4ON4O H F
(fte delete of entie nested ta%le# Count H 3
'/40
D'CL(R'
t7pe t2 is ta%le of va!ha(1) inde/ %7 %ina7Mintege.
t7pe t1 is ta%le of t2 inde/ %7 %ina7Mintege.
i%t t1.
flag %oolean.
)'8IN
d%$sMoutput5putMline(=Count H = XX i%t5!ount).
if i%t5li$it is null then
d%$sMoutput5putMline(=No li$it to Inde/"%7 Ta%les=).
else
d%$sMoutput5putMline(=Li$it H = XX i%t5li$it).
end if.
i%t(2)(2) 0H =a=.
i%t(@)(C) 0H =%=.
i%t(C)(2) 0H =!=.
i%t(D)(1) 0H =d=.
i%t(W)(4) 0H =e=.
i%t(4)(@) 0H =f=.
d%$sMoutput5putMline(=IND'+")* T()L' 'L'M'NTS=).
d%$sMoutput5putMline(=i%t(N2ON2O H = XX i%t(2)(2)).
d%$sMoutput5putMline(=i%t(N@ONCO H = XX i%t(@)(C)).
d%$sMoutput5putMline(=i%t(NCON2O H = XX i%t(C)(2)).
Copy rights are reserved.
246
d%$sMoutput5putMline(=i%t(NDON1O H = XX i%t(D)(1)).
d%$sMoutput5putMline(=i%t(NWON4O H = XX i%t(W)(4)).
d%$sMoutput5putMline(=i%t(N4ON@O H = XX i%t(4)(@)).
d%$sMoutput5putMline(=Bist Inde/ H = XX i%t5fist).
d%$sMoutput5putMline(=Last Inde/ H = XX i%t5last).
d%$sMoutput5putMline(=Ne/t Inde/ H = XX i%t5ne/t(4)).
d%$sMoutput5putMline(=Aio Inde/ H = XX i%t5pio(W)).
i%t(2)(1) 0H =g=.
i%t(2)(4) 0H =h=.
i%t(2)(@) 0H =i=.
i%t(2)(C) 0H =&=.
i%t(2)(D) 0H =l=.
i%t(2)(V) 0H =$=.
i%t(2)(W) 0H =n=.
d%$sMoutput5putMline(=Count H = XX i%t5!ount).
d%$sMoutput5putMline(=IND'+")* T()L' 'L'M'NTS=).
fo i in 255W loop
d%$sMoutput5putMline(=i%tN2ON= XX i XX =O H = XX i%t(2)(i)).
end loop.
d%$sMoutput5putMline(=i%t(N@ONCO H = XX i%t(@)(C)).
d%$sMoutput5putMline(=i%t(NCON2O H = XX i%t(C)(2)).
d%$sMoutput5putMline(=i%t(NDON1O H = XX i%t(D)(1)).
d%$sMoutput5putMline(=i%t(NWON4O H = XX i%t(W)(4)).
d%$sMoutput5putMline(=i%t(N4ON@O H = XX i%t(4)(@)).
flag 0H i%t5e/ists(4).
if flag H tue then
d%$sMoutput5putMline(=Inde/ 4 e/ists=).
else
d%$sMoutput5putMline(=Inde/ 4 e/ists=).
end if.
i%t5delete(2).
d%$sMoutput5putMline(=(fte delete of fist inde/# Count H = XX i%t5!ount).
i%t5delete(@).
d%$sMoutput5putMline(=(fte delete of fouth inde/# Count H = XX i%t5!ount).
d%$sMoutput5putMline(=IND'+")* T()L' 'L'M'NTS=).
d%$sMoutput5putMline(=i%t(NCON2O H = XX i%t(C)(2)).
d%$sMoutput5putMline(=i%t(NDON1O H = XX i%t(D)(1)).
d%$sMoutput5putMline(=i%t(NWON4O H = XX i%t(W)(4)).
d%$sMoutput5putMline(=i%t(N4ON@O H = XX i%t(4)(@)).
Copy rights are reserved.
247
i%t5delete.
d%$sMoutput5putMline(=(fte delete of entie inde/"%7 ta%le# Count H = XX
i%t5!ount).
'ND.
Output0
Count H 3
No li$it to Inde/"%7 Ta%les
IND'+")* T()L' 'L'M'NTS
i%t(N2ON2O H a
i%t(N@ONCO H %
i%t(NCON2O H !
i%t(NDON1O H d
i%t(NWON4O H e
i%t(N4ON@O H f
Bist Inde/ H 2
Last Inde/ H W
Ne/t Inde/ H @
Aio Inde/ H D
Count H D
IND'+")* T()L' 'L'M'NTS
i%tN2ON2O H a
i%tN2ON1O H g
i%tN2ON4O H h
i%tN2ON@O H i
i%tN2ONCO H &
i%tN2ONDO H l
i%tN2ONVO H $
i%tN2ONWO H n
i%t(N@ONCO H %
i%t(NCON2O H !
i%t(NDON1O H d
i%t(NWON4O H e
i%t(N4ON@O H f
Inde/ 4 e/ists
(fte delete of fist inde/# Count H C
(fte delete of fouth inde/# Count H @
IND'+")* T()L' 'L'M'NTS
i%t(NCON2O H !
i%t(NDON1O H d
Copy rights are reserved.
248
i%t(NWON4O H e
i%t(N4ON@O H f
(fte delete of entie inde/"%7 ta%le# Count H 3
'/@0
D'CL(R'
t7pe t2 is ta%le of va!ha(1) inde/ %7 %ina7Mintege.
t7pe t1 is ta%le of t2 inde/ %7 %ina7Mintege.
t7pe t4 is ta%le of t1.
nt t4 0H t4().
! nu$%e 0H DC.
)'8IN
nt5e/tend(1).
d%$sMoutput5putMline(=Count H = XX nt5!ount).
fo i in 255nt5!ount loop
fo J in 255nt5!ount loop
fo & in 255nt5!ount loop
nt(i)(J)(&) 0H !h(!).
! 0H ! G 2.
end loop.
end loop.
end loop.
d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=).
fo i in 255nt5!ount loop
fo J in 255nt5!ount loop
fo & in 255nt5!ount loop
d%$sMoutput5putMline(=ntN= XX i XX =ON= XX J XX =ON= XX & XX =O H = XX
nt(i)(J)(&)).
end loop.
end loop.
end loop.
'ND.
Output0
Count H 1
N'ST'D T()L' 'L'M'NTS
ntN2ON2ON2O H (
ntN2ON2ON1O H )
ntN2ON1ON2O H C
ntN2ON1ON1O H D
Copy rights are reserved.
249
ntN1ON2ON2O H '
ntN1ON2ON1O H B
ntN1ON1ON2O H 8
ntN1ON1ON1O H :
O)>'CTS US'D IN T:' '+(MAL'S
SQL- sele!t E fo$ student.
SNO SN(M' SM(RFS
"""""""""" """""""""""""" """"""""""
2 sa&eth 233
1 sinu 133
4 div7a 433
@ $anogni @33
SQL- !eate o epla!e t7pe add as o%Je!t(hno nu$%e(1)#!it7 va!ha(23)).?
SQL- sele!t E fo$ e$plo7.
'N(M' >O) (DDR'SS(:NO# CIT*)
"""""""""" """""""""" """""""""""""""""""""""""""""
RanJit !le& (DDR(22# =h7d=)
Satish $anage (DDR(11# =%ang=)
Sinu enginee (DDR(44# =&o!hi=)
Copy rights are reserved.
250
'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
'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
Copy rights are reserved.
251
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/!eptionMna$e then
SeUuen!eMofMstate$ents.
6hen e/!eptionMna$e then
SeUuen!eMofMstate$ents.
6hen othes then
SeUuen!eMofMstate$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).
vM$a&s nu$%e.
!uso ! is sele!t E fo$ student.
t7pe t is vaa7(4) of va!ha(1).
va t 0H t(=a=#=%=).
va2 t.
)'8IN
"" NOMD(T(MBOUND
)'8IN
sele!t s$a&s into vM$a&s fo$ student whee sno H C3.
'+C'ATION
when noMdataMfound then
Copy rights are reserved.
252
d%$sMoutput5putMline(=Invalid student nu$%e=).
'ND.
"" CURSORM(LR'(D*MOA'N
)'8IN
open !.
open !.
'+C'ATION
when !usoMalead7Mopen then
d%$sMoutput5putMline(=Cuso is alead7 opened=).
'ND.
"" IN9(LIDMCURSOR
)'8IN
!lose !.
open !.
!lose !.
!lose !.
'+C'ATION
when invalidM!uso then
d%$sMoutput5putMline(=Cuso is alead7 !losed=).
'ND.
"" TOOMM(N*MRO6S
)'8IN
sele!t s$a&s into vM$a&s fo$ student whee sno - 2.
'+C'ATION
when tooM$an7Mows then
d%$sMoutput5putMline(=Too $an7 values ae !o$ing to $a&s
vaia%le=).
'ND.
"" Z'ROMDI9ID'
)'8IN
a 0H C?3.
'+C'ATION
when TeoMdivide then
d%$sMoutput5putMline(=Divided %7 Teo " invalid opeation=).
'ND.
"" 9(LU'M'RROR
)'8IN
% 0H =sa&eth=.
'+C'ATION
when valueMeo then
d%$sMoutput5putMline(=Invalid sting length=).
'ND.
"" IN9(LIDMNUM)'R
Copy rights are reserved.
253
)'8IN
inset into student values(=a=#=sinu=#233).
'+C'ATION
when invalidMnu$%e then
d%$sMoutput5putMline(=Invalid nu$%e=).
'ND.
"" SU)SCRIATMOUTSID'MLIMIT
)'8IN
va(@) 0H =!=.
'+C'ATION
when su%s!iptMoutsideMli$it then
d%$sMoutput5putMline(=Inde/ is geate than the li$it=).
'ND.
"" SU)SCRIATM)'*ONDMCOUNT
)'8IN
va(4) 0H =!=.
'+C'ATION
when su%s!iptM%e7ondM!ount then
d%$sMoutput5putMline(=Inde/ is geate than the !ount=).
'ND.
"" COLL'CTIONMISMNULL
)'8IN
va2(2) 0H =a=.
'+C'ATION
when !olle!tionMisMnull then
d%$sMoutput5putMline(=Colle!tion is e$pt7=).
'ND.
""
'ND.
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 Teo " 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
Copy rights are reserved.
254
'/10
D'CL(R'
! nu$%e.
)'8IN
! 0H C?3.
'+C'ATION
when TeoMdivide then
d%$sMoutput5putMline(=Invalid Opeation=).
when othes then
d%$sMoutput5putMline(=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.
)'8IN
aise e.
'+C'ATION
when e then
d%$sMoutput5putMline(=e is aised=).
'ND.
Output0
e is aised
)ULIT"IN 'RROR BUNCTIONS
Copy rights are reserved.
255
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 Puse"deifned
e/!eptionQ5
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.
vMdna$e va!ha(23).
)'8IN
"" US'R"D'BIN'D '+C'ATION
)'8IN
aise e.
'+C'ATION
when e then
d%$sMoutput5putMline(SQLCOD' XX = = XX SQL'RRM).
'ND.
"" AR'D'BIN'D '+C'ATION
)'8IN
sele!t dna$e into vMdna$e fo$ dept whee deptno H C3.
'+C'ATION
when noMdataMfound then
d%$sMoutput5putMline(SQLCOD' XX = = XX SQL'RRM).
'ND.
'ND.
Output0
2 Use"Defined '/!eption
233 OR("32@340 no data found
'/10
)'8IN
d%$sMoutput5putMline(SQL'RRM(233)).
d%$sMoutput5putMline(SQL'RRM(3)).
d%$sMoutput5putMline(SQL'RRM(2)).
d%$sMoutput5putMline(SQL'RRM("233)).
Copy rights are reserved.
256
d%$sMoutput5putMline(SQL'RRM("C33)).
d%$sMoutput5putMline(SQL'RRM(133)).
d%$sMoutput5putMline(SQL'RRM("S33)).
'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!tHRD)MS. fa!ilit7HOR(
"1330 non"OR(CL' e/!eption
OR("33S330 invalid SQL state$ent
D)MSMUTILIT*5BORM(TM'RRORMST(CF
The %uilt"in fun!tion# li&e SQL'RRM# etuns the $essage asso!iated with the !uent
eo5
It diffes fo$ SQL'RRM in two wa7s0
Its length is not esti!ted. it will etun the full eo $essage sting5
*ou !an not pass an eo !ode nu$%e to this fun!tion. it !annot %e used to etun
the $essage fo a ando$ eo !ode5
'/0
D'CL(R'
v nu$%e 0H =a%=.
)'8IN
null.
'+C'ATION
when othes then
d%$sMoutput5putMline(d%$sMutilit75fo$atMeoMsta!&).
'ND.
Output0
de!lae
E
'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
D)MSMUTILIT*5BORM(TMC(LLMST(CF
Copy rights are reserved.
257
This fun!tion etuns a fo$atted sting showing the e/e!ution !all sta!& inside 7ou AL?SQL
appli!ation5 Its usefulness is not esti!ted to eo $anage$ent. 7ou will also find its
hand7 fo ta!ing the e/e!tution of 7ou !ode5 *ou $a7 not use this fun!tion in e/!eption
%lo!&5
'/0
)'8IN
d%$sMoutput5putMline(d%$sMutilit75fo$atM!allMsta!&).
'ND.
Output0
""""" AL?SQL Call Sta!& """""
O%Je!tMhandle lineMnu$%e o%Je!tMna$e
DSVD3@VW 1 anon7$ous %lo!&
D)MSMUTILIT*5BORM(TM'RRORM)(CFTR(C'
It displa7s the e/e!ution sta!& at the point whee an e/!eption was aised5 Thus # 7ou !an
!all this fun!tion with an e/!eption se!tion at the top level of 7ou sta!& and still find out
whee the eo was aised deep within the !all sta!&5
'/0
CR'(T' OR R'AL(C' AROC'DUR' A2 IS
)'8IN
d%$sMoutput5putMline(=fo$ po!edue 2=).
aise valueMeo.
'ND A2.
CR'(T' OR R'AL(C' AROC'DUR' A1 IS
)'8IN
d%$sMoutput5putMline(=fo$ po!edue 1=).
p2.
'ND A1.
CR'(T' OR R'AL(C' AROC'DUR' A4 IS
)'8IN
d%$sMoutput5putMline(=fo$ po!edue 4=).
p1.
'+C'ATION
when othes then
d%$sMoutput5putMline(d%$sMutilit75fo$atMeoM%a!&ta!e).
'ND A4.
Copy rights are reserved.
258
Output0
SQL- e/e! p4
fo$ po!edue 4
fo$ po!edue 1
fo$ po!edue 2
OR("3DC210 at bS(F'T:5A2b# line @
OR("3DC210 at bS(F'T:5A1b# line @
OR("3DC210 at bS(F'T:5A4b# line @
'+C'ATIONMINIT 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'ATIONMINIT(e(ception_name# oracle_error_number).
'/0
D'CL(R'
e e/!eption.
pag$a e/!eptionMinit(e#"2@VD).
! nu$%e.
)'8IN
! 0H C?3.
'+C'ATION
when e then
d%$sMoutput5putMline(=Invalid Opeation=).
'ND.
Output0
Invalid Opeation
R(IS'M(AALIC(TIONM'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'M(AALIC(TIONM'RROR(error_number# error_me!!a"e## Neep_error!_'la"O).
Copy rights are reserved.
259
The )oolean paa$ete eep_error!_'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
! 0H C?3.
'+C'ATION
when TeoMdivide then
aiseMappli!ationMeo("13111#=Invalid Opeation=).
'ND.
Output0
D'CL(R'
E
'RROR at line 20
OR("131110 Invalid Opeation
OR("3DC210 at line V
'+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
when e then
d%$sMoutput5putMline(=e is aised=).
Copy rights are reserved.
260
'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) 0H =a%!d=.
)'8IN
d%$sMoutput5putMline(=:ello=).
'+C'ATION
when othes then
d%$sMoutput5putMline(=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'
Copy rights are reserved.
261
! nu$%e(4) 0H =a%!d=.
)'8IN
d%$sMoutput5putMline(=:ello=).
'+C'ATION
when othes then
d%$sMoutput5putMline(=Invalid sting length=).
'ND.
'+C'ATION
when othes then
d%$sMoutput5putMline(=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%$sMoutput5putMline(=e2 is aised=).
aise e1.
when e1 then
d%$sMoutput5putMline(=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 S
OR("3DC230 AL?SQL0 unhandled use"defined e/!eption
Copy rights are reserved.
262
'/10
D'CL(R'
e2 e/!eption.
e1 e/!eption.
)'8IN
)'8IN
aise e2.
'+C'ATION
when e2 then
d%$sMoutput5putMline(=e2 is aised=).
aise e1.
when e1 then
d%$sMoutput5putMline(=e1 is aised=).
'ND.
'+C'ATION
when e1 then
d%$sMoutput5putMline(=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%$sMoutput5putMline(=e is aised=).
aise e.
'ND.
Output0
e is aised
D'CL(R'
E
'RROR at line 20
OR("3DC230 AL?SQL0 unhandled use"defined e/!eption
OR("3DC210 at line W
OR("3DC230 AL?SQL0 unhandled use"defined e/!eption
Copy rights are reserved.
263
R'STRICTIONS
*ou !an not pass e/!eption as an agu$ent to a su%poga$5
D(T()(S' TRI88'RS
Copy rights are reserved.
264
Tigges ae si$ila to po!edues o fun!tions in that the7 ae na$ed AL?SQL %lo!&s with
de!laative# e/e!uta%le# and e/!eption handling se!tions5 ( tigge is e/e!uted i$pli!itl7
wheneve the tiggeing event happens5 The a!t of e/e!uting a tigge is &nown as fiing the
tigge5
R'STRICTIONS ON TRI88'R'S
Li&e pa!&ages# tigges $ust %e stoed as stand"alone o%Je!ts in the data%ase and
!annot %e lo!al to a %lo!& o pa!&age5
( tigge does not a!!ept agu$ents5
US' OB TRI88'RS
Maintaining !o$ple/ integit7 !onstaints not possi%le though de!laative
!onstaints ena%le at ta%le !eation5
(uditing info$ation in a ta%le %7 e!oding the !hanges $ade and who $ade the$5
(uto$ati!all7 signaling othe poga$s that a!tion needs to ta&e pla!e when !hages
ae $ade to a ta%le5
Aefo$ validation on !hanges %eing $ade to ta%les5
(uto$ate $aintenan!e of the data%ase5
T*A'S OB TRI88'RS
DML Tigges
Instead of Tigges
DDL Tigges
S7ste$ Tigges
Suspend Tigges
C(T'8ORI'S
Ti$ing "" )efoe o (fte
Level "" Row o State$ent
Row level tigge fies on!e fo ea!h ow affe!ted %7 the tiggeing state$ent5 Row level
tigge is identified %7 the BOR '(C: RO6 !lause5
Copy rights are reserved.
265
State$ent level tigge fies on!e eithe %efoe o afte the state$ent5
DML TRI88'R S*NT(+
Ceate o epla!e tigge ,tri""er_name-
^)efoe X afte_ ^inset o update o delete_ on ,table_name-
NBo ea!h owO
N6hen (R)O
NDe!laeO
"" de!laation
)egin
"" tigge %od7
N'/!eptionO
"" e/!eption se!tion
'nd ,tri""er_name-.
DML TRI88'RS
( DML tigge is fied on an INS'RT# UAD(T'# o D'L'T' opeation on a data%ase ta%le5 It !an %e
fied eithe %efoe o afte the state$ent e/e!utes# and !an %e fied on!e pe affe!ted ow#
o on!e pe state$ent5
The !o$%ination of these fa!tos dete$ines the t7pes of the tigges5 These ae a total of
21 possi%le t7pes (4 state$ents E 1 ti$ing E 1 levels)5
ST(T'M'NT L'9'L
State$ent level tigge fies onl7 on!e5
'/0
SQL- !eate ta%le state$entMlevel(!ount va!ha(C3)).
CR'(T' OR R'AL(C' TRI88'R ST(T'M'NTML'9'LMTRI88'R
afte update on student
)'8IN
inset into state$entMlevel values(=State$ent level fied=).
'ND ST(T'M'NTML'9'LMTRI88'R.
Output0
Copy rights are reserved.
266
SQL- update student set s$a&sHC33.
4 ows updated5
SQL- sele!t E fo$ state$entMlevel.
COUNT
""""""""""""""""""""""""""""
State$ent level fied
RO6 L'9'L
Row level tigge fies on!e fo ea!h ow affe!ted %7 the tiggeing state$ent5
'/0
SQL- !eate ta%le owMlevel(!ount va!ha(C3)).
CR'(T' OR R'AL(C' TRI88'R RO6ML'9'LMTRI88'R
afte update on student
)'8IN
inset into owMlevel values(=Row level fied=).
'ND RO6ML'9'LMTRI88'R.
Output0
SQL- update student set s$a&sHC33.
4 ows updated5
SQL- sele!t E fo$ state$entMlevel.
COUNT
""""""""""""""""""""""""""""
Row level fied
Row level fied
Row level fied
ORD'R OB DML TRI88'R BIRIN8
Copy rights are reserved.
267
)efoe state$ent level
)efoe ow level
(fte ow level
(fte state$ent level
'/0
Suppose we have a follwing ta%le5
SQL- sele!t E fo$ student.
NO N(M' M(RFS
""""" """"""" """"""""""
2 a 233
1 % 133
4 ! 433
@ d @33
SQL- !eate ta%le fiingMode(ode va!ha(C3)).
CR'(T' OR R'AL(C' TRI88'R )'BOR'MST(T'M'NT
%efoe inset on student
)'8IN
inset into fiingMode values(=)efoe State$ent Level=).
'ND )'BOR'MST(T'M'NT.
CR'(T' OR R'AL(C' TRI88'R )'BOR'MRO6
%efoe inset on student
fo ea!h ow
)'8IN
inset into fiingMode values(=)efoe Row Level=).
'ND )'BOR'MRO6.
CR'(T' OR R'AL(C' TRI88'R (BT'RMST(T'M'NT
afte inset on student
)'8IN
inset into fiingMode values(=(fte State$ent Level=).
'ND (BT'RMST(T'M'NT.
CR'(T' OR R'AL(C' TRI88'R (BT'RMRO6
afte inset on student
fo ea!h ow
)'8IN
Copy rights are reserved.
268
inset into fiingMode values(=(fte Row Level=).
'ND (BT'RMRO6.
Output0
SQL- sele!t E fo$ fiingMode.
no ows sele!ted
SQL- inset into student values(C#=e=#C33).
2 ow !eated5
SQL- sele!t E fo$ fiingMode.
ORD'R
""""""""""""""""""""""""""""""""""""""""""""""""""
)efoe State$ent Level
)efoe Row Level
(fte Row Level
(fte State$ent Level
SQL- sele!t E fo$ student.
NO N(M' M(RFS
"""" """""""" """"""""""
2 a 233
1 % 133
4 ! 433
@ d @33
C e C33
CORR'L(TION ID'NTIBI'RS IN RO6"L'9'L TRI88'RS
Inside the tigge# 7ou !an a!!ess the data in the ow that is !uentl7 %eing po!essed5 This
is a!!o$plished though two !oelation identifies " 0old and 0new5
( correlation identi'ier is a spe!ial &ind of AL?SQL %ind vaia%le5 The !olon in font of ea!h
indi!ates that the7 ae %ind vaia%les# in the sense of host vaia%les used in e$%edded
Copy rights are reserved.
269
AL?SQL# and indi!ates that the7 ae not egula AL?SQL vaia%les5 The AL?SQL !o$pile will
teat the$ as e!ods of t7pe
TiggeingMta%leLRO6T*A'5
(lthough s7nta!ti!all7 the7 ae teated as e!ods# in ealit7 the7 ae not5 0old and 0new ae
also &nown as p!eudorecord!# fo this eason5
TRI88'RIN8 ST(T'M'NT 0OLD 0N'6
"""""""""""""""""""""""""""""""""""""" """""""""""""""""""""""""""" """""""""""""""""""""""""""""""""""""""""""""""
INS'RT all fields ae NULL5 values that will %e inseted
6hen the state$ent is !o$pleted5
UAD(T' oiginal values fo new values that will %e updated
the ow %efoe the when the state$ent is !o$pleted5
update5
D'L'T' oiginal values %efoe all fields ae NULL5
the ow is deleted5
'/0
SQL- !eate ta%le $a&s(no nu$%e(1) oldM$a&s nu$%e(4)#newM$a&s
nu$%e(4)).
CR'(T' OR R'AL(C' TRI88'R OLDMN'6
%efoe inset o update o delete on student
fo ea!h ow
)'8IN
inset into $a&s values(0old5no#0old5$a&s#0new5$a&s).
'ND OLDMN'6.
Output0
SQL- sele!t E fo$ student.
NO N(M' M(RFS
""""" """"""" """"""""""
2 a 233
1 % 133
4 ! 433
@ d @33
C e C33
SQL- sele!t E fo$ $a&s.
Copy rights are reserved.
270
no ows sele!ted
SQL- inset into student values(D#=f=#D33).
2 ow !eated5
SQL- sele!t E fo$ student.
NO N(M' M(RFS
"""" """""""" """"""""""
2 a 233
1 % 133
4 ! 433
@ d @33
C e C33
D f D33
SQL- sele!t E fo$ $a&s.
NO OLDMM(RFS N'6MM(RFS
"""" """"""""""""""" """""""""""""""
D33
SQL- update student set $a&sHCCC whee noHC.
2 ow updated5
SQL- sele!t E fo$ student.
NO N(M' M(RFS
""""" """"""" """"""""""
2 a 233
1 % 133
4 ! 433
@ d @33
C e CCC
D f D33
Copy rights are reserved.
271
SQL- sele!t E fo$ $a&s.
NO OLDMM(RFS N'6MM(RFS
"""""" """""""""""""""" """""""""""""""
D33
C C33 CCC
SQL- delete student whee no H 1.
2 ow deleted5
SQL- sele!t E fo$ student.
NO N(M' M(RFS
"""" """""""" """"""""""
2 a 233
4 ! 433
@ d @33
C e CCC
D f D33
SQL- sele!t E fo$ $a&s.
NO OLDMM(RFS N'6MM(RFS
""""" """""""""""""" """"""""""""""""
D33
C C33 CCC
1 133
R'B'R'NCIN8 CL(US'
If desied# 7ou !an use the R'B'R'NCIN8 !lause to spe!if7 a diffeent na$e fo 0old ane 0new5
This !lause is found afte the tiggeing event# %efoe the 6:'N !lause5
S7nta/0
R'B'R'NCIN8 Nold as oldMna$eO Nnew as newMna$eO
'/0
CR'(T' OR R'AL(C' TRI88'R R'B'R'NC'MTRI88'R
Copy rights are reserved.
272
%efoe inset o update o delete on student
efeen!ing old as oldMstudent new as newMstudent
fo ea!h ow
)'8IN
inset into $a&s
values(0oldMstudent5no#0oldMstudent5$a&s#0newMstudent5$a&s).
'ND R'B'R'NC'MTRI88'R.
6:'N CL(US'
6:'N !lause is valid fo ow"level tigges onl75 If pesent# the tigge %od7 will %e e/e!uted
onl7 fo those ows that $eet the !ondition spe!ified %7 the 6:'N !lause5
S7nta/0
6:'N tri""er_condition.
6hee tri""er_condition is a )oolean e/pession5 It will %e evaluated fo ea!h ow5 The
=new and =old e!ods !an %e efeen!ed inside tri""er_condition as well# %ut li&e
R'B'R'NCIN8# the !olon is not used thee5 The !olon is onl7 valid in the tigge %od75
'/0
CR'(T' OR R'AL(C' TRI88'R 6:'NMTRI88'R
%efoe inset o update o delete on student
efeen!ing old as oldMstudent new as newMstudent
fo ea!h ow
when (newMstudent5$a&s - C33)
)'8IN
inset into $a&s
values(0oldMstudent5no#0oldMstudent5$a&s#0newMstudent5$a&s).
'ND 6:'NMTRI88'R.
TRI88'R AR'DIC(T'S
Thee ae thee )oolean fun!tions that 7ou !an use to dete$ine what the opeation is5
The pedi!ates ae
INS'RTIN8
UAD(TIN8
D'L'TIN8
'/0
SQL- !eate ta%le pedi!ates(opeation va!ha(13)).
Copy rights are reserved.
273
CR'(T' OR R'AL(C' TRI88'R AR'DIC(T'MTRI88'R
%efoe inset o update o delete on student
)'8IN
if inseting then
inset into pedi!ates values(=Inset=).
elsif updating then
inset into pedi!ates values(=Update=).
elsif deleting then
inset into pedi!ates values(=Delete=).
end if.
'ND AR'DIC(T'MTRI88'R.
Output0
SQL- delete student whee noH2.
2 ow deleted5
SQL- sele!t E fo$ pedi!ates.
MS8
"""""""""""""""
Delete
SQL- inset into student values(V#=g=#V33).
2 ow !eated5
SQL- sele!t E fo$ pedi!ates.
MS8
"""""""""""""""
Delete
Inset
SQL- update student set $a&s H VVV whee noHV.
2 ow updated5
SQL- sele!t E fo$ pedi!ates.
Copy rights are reserved.
274
MS8
"""""""""""""""
Delete
Inset
Update
INST'(D"OB TRI88'RS
Instead"of tigges fie instead of a DML opeation5 (lso# instead"of tigges !an %e defined
onl7 on views5 Instead"of tigges ae used in two !ases0
To allow a view that would othewise not %e $odifia%le to %e $odified5
To $odif7 the !olu$ns of a nested ta%le !olu$n in a view5
'/0
SQL- !eate view e$pMdept as sele!t e$pno#ena$e#Jo%#dna$e#lo!#sal#e5deptno fo$
e$p e# dept d whee e5deptno H d5deptno.
CR'(T' OR R'AL(C' TRI88'R INST'(DMOBMTRI88'R
instead of inset on e$pMdept
)'8IN
inset into dept2 values(C3#=d=#=%ang=).
inset into
e$p2(e$pno#ena$e#Jo%#sal#deptno)values(1111#=sa&eth=#=do!to=#W333#C3).
'ND INST'(DMOBMTRI88'R.
Output0
SQL- inset into e$pMdept values(1111#=sa&eth=#=do!to=#W333#=d=#=%ang=#C3).
SQL- sele!t E fo$ e$pMdept.
'MANO 'N(M' >O) S(L DN(M' LOC D'ATNO
"""""""""" """""""""" """""""""""" """"""""""" """"""""""""" """"""""""""" """"""""""
V4DS SMIT: CL'RF W33 R'S'(RC: D(LL(S 13
V@SS (LL'N S(L'SM(N 2D33 S(L'S C:IC(8O 43
VC12 6(RD S(L'SM(N 21C3 S(L'S C:IC(8O 43
VCDD >ON'S M(N(8'R 1SVC R'S'(RC: D(LL(S 13
VDC@ M(RTIN S(L'SM(N 21C3 S(L'S C:IC(8O 43
VDSW )L(F' M(N(8'R 1WC3 S(L'S C:IC(8O 43
VVW1 CL(RF M(N(8'R 1@C3 (CCOUNTIN8 N'6 *ORF 23
Copy rights are reserved.
275
VVWW SCOTT (N(L*ST 4333 R'S'(RC: D(LL(S 13
VW4S FIN8 AR'SID'NT C333 (CCOUNTIN8 N'6 *ORF 23
VW@@ TURN'R S(L'SM(N 2C33 S(L'S C:IC(8O 43
VWVD (D(MS CL'RF 2233 R'S'(RC: D(LL(S 13
VS33 >(M'S CL'RF SC3 S(L'S C:IC(8O 43
VS31 BORD (N(L*ST 4333 R'S'(RC: D(LL(S 13
VS4@ MILL'R CL'RF 2433 (CCOUNTIN8 N'6 *ORF 23
1111 sa&eth do!to W333 d %ang C3
SQL- sele!t E fo$ dept.
D'ATNO DN(M' LOC
"""""""""" """""""""""""""" """""""""""
23 (CCOUNTIN8 N'6 *ORF
13 R'S'(RC: D(LL(S
43 S(L'S C:IC(8O
@3 OA'R(TIONS )OSTON
C3 d %ang
SQL- sele!t E fo$ e$p.
'MANO 'N(M' >O) M8R :IR'D(T' S(L COMM D'ATNO
"""""""""" """""""""" """"""""""""""" """""""" """""""""""""" """"""""" """"""""" """"""""""
V4DS SMIT: CL'RF VS31 2 V"D'C"W3 W33 13
V@SS (LL'N S(L'SM(N VDSW 13"B')"W2 2D33 433 43
VC12 6(RD S(L'SM(N VDSW 11"B')"W2 21C3 C33 43
VCDD >ON'S M(N(8'R VW4S 31"(AR"W2 1SVC 13
VDC@ M(RTIN S(L'SM(N VDSW 1W"S'A"W2 21C3 2@33 43
VDSW )L(F' M(N(8'R VW4S 32"M(*"W2 1WC3 43
VVW1 CL(RF M(N(8'R VW4S 3S">UN"W2 1@C3 23
VVWW SCOTT (N(L*ST VCDD 2S"(AR"WV 4333 13
VW4S FIN8 AR'SID'NT 2V"NO9"W2 C333 23
VW@@ TURN'R S(L'SM(N VDSW 3W"S'A"W2 2C33 3 43
VWVD (D(MS CL'RF VVWW 14"M(*"WV 2233 13
VS33 >(M'S CL'RF VDSW 34"D'C"W2 SC3 43
VS31 BORD (N(L*ST VCDD 34"D'C"W2 4333 13
VS4@ MILL'R CL'RF VVW1 14">(N"W1 2433 23
Copy rights are reserved.
276
1111 sa&eth do!to W333 C3
DDL TRI88'RS
Oa!le allows 7ou to define tigges that will fie when Data Definition Language state$ents
ae e/e!uted5
S7nta/0
Ceate o epla!e tigge ,tri""er_name-
^)efoe X afte_ ^DDL event_ on ^data%ase X s!he$a_
N6hen (R)O
NDe!laeO
"" de!laation
)egin
"" tigge %od7
N'/!eptionO
"" e/!eption se!tion
'nd ,tri""er_name-.
'/0
SQL- !eate ta%le $7Mo%Je!ts(o%JMna$e va!ha(23)#o%JMt7pe va!ha(23)#o%JMowne
va!ha(23)#o%JMti$e date).
CR'(T' OR R'AL(C' TRI88'R CR'(T'MTRI88'R
afte !eate on data%ase
)'8IN
inset into $7Mo%Je!ts values(s7s5di!tiona7Mo%JMna$e#s7s5di!tiona7Mo%JMt7pe#
s7s5di!tiona7Mo%JMowne# s7sdate).
'ND CR'(T'MTRI88'R.
Output0
SQL- sele!t E fo$ $7Mo%Je!ts.
no ows sele!ted
SQL- !eate ta%le stud2(no nu$%e(1)).
Copy rights are reserved.
277
SQL- sele!t E fo$ $7Mo%Je!ts.
O)>MN(M' O)>MT*A' O)>MO6N'R O)>MTIM'
""""""""""""" """""""""""""" """""""""""""" """"""""""""
STUD2 T()L' S*S 12">UL"3V
SQL- !eate seUuen!e ss.
SQL- !eate view studMview as sele!t E fo$ stud2.
SQL- sele!t E fo$ $7Mo%Je!ts.
O)>MN(M' O)>MT*A' O)>MO6N'R O)>MTIM'
"""""""""""""" """"""""""""" """""""""""""""" """""""""""""
STUD2 T()L' S*S 12">UL"3V
SS S'QU'NC' S*S 12">UL"3V
STUDM9I'6 9I'6 S*S 12">UL"3V
6:'N CL(US'
If 6:'N pesent# the tigge %od7 will %e e/e!uted onl7 fo those that $eet the !ondition
spe!ified %7 the 6:'N !lause5
'/0
CR'(T' OR R'AL(C' TRI88'R CR'(T'MTRI88'R
afte !eate on data%ase
when (s7s5di!tiona7Mo%JMt7pe H KT()L';)
)'8IN
inset into $7Mo%Je!ts values(s7s5di!tiona7Mo%JMna$e#s7s5di!tiona7Mo%JMt7pe#
s7s5di!tiona7Mo%JMowne# s7sdate).
'ND CR'(T'MTRI88'R.
S*ST'M TRI88'RS
S7ste$ tigges will fie wheneve data%ase"wide event o!!us5 The following ae the
data%ase event tigges5 To !eate s7ste$ tigge 7ou need (DMINIST'R D(T()(S' TRI88'R
pivilege5
ST(RTUA
S:UTDO6N
Copy rights are reserved.
278
LO8ON
LO8OBB
S'R9'R'RROR
S7nta/0
Ceate o epla!e tigge ,tri""er_name-
^)efoe X afte_ ^Data%ase event_ on ^data%ase X s!he$a_
N6hen (R)O
NDe!laeO
"" de!laation se!tion
)egin
"" tigge %od7
N'/!eptionO
"" e/!eption se!tion
'nd ,tri""er_name-.
'/0
SQL- !eate ta%le useMlogs(uMna$e va!ha(23)#logMti$e ti$esta$p).
CR'(T' OR R'AL(C' TRI88'R (BT'RMLO8ON
afte logon on data%ase
)'8IN
inset into useMlogs values(use#!uentMti$esta$p).
'ND (BT'RMLO8ON.
Output0
SQL- sele!t E fo$ useMlogs.
no ows sele!ted
SQL- !onn sa&eth?sa&eth
SQL- sele!t E fo$ useMlogs.
UMN(M' LO8MTIM'
"""""""""" """"""""""""""""""""""""""""""""""""""""""""""""
S(F'T: 11">UL"3V 2153V52452@3333 (M
SQL- !onn s7ste$?oa!le
Copy rights are reserved.
279
SQL- sele!t E fo$ useMlogs.
UMN(M' LO8MTIM'
"""""""""" """"""""""""""""""""""""""""""""""""""""""""""""
S(F'T: 11">UL"3V 2153V52452@3333 (M
S*ST'M 11">UL"3V 2153V54@512W333 (M
SQL- !onn s!ott?tige
SQL- sele!t E fo$ useMlogs.
UMN(M' LO8MTIM'
"""""""""" """""""""""""""""""""""""""""""""""""""""""""""
S(F'T: 11">UL"3V 2153V52452@3333 (M
S*ST'M 11">UL"3V 2153V54@512W333 (M
SCOTT 11">UL"3V 2153W5@453S4333 (M
S'R9'R'RROR
The S'R9'R'RROR event !an %e used to ta!& eos that o!!u in the data%ase5 The eo !ode
is availa%le inside the tigge though the S'R9'RM'RROR atti%ute fun!tion5
'/0
SQL- !eate ta%le $7Meos(eoM$sg va!ha(133)).
CR'(T' OR R'AL(C' TRI88'R S'R9'RM'RRORMTRI88'R
afte seveeo on data%ase
)'8IN
inset into $7Meos values(d%$sMutilit75fo$atMeoMsta!&).
'ND S'R9'RM'RRORMTRI88'R.
Output0
SQL- !eate ta%le ss (no)).
!eate ta%le ss (no))
E
'RROR at line 20
OR("33S110 $issing o invalid option
SQL- sele!t E fo$ $7Meos.
Copy rights are reserved.
280
'RRORMMS8
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
OR("33S110 $issing o invalid option
SQL- inset into student values(2#1#4).
inset into student values(2#1#4)
E
'RROR at line 20
OR("33S@10 ta%le o view does not e/ist
SQL- sele!t E fo$ $7Meos.
'RRORMMS8
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
OR("33S110 $issing o invalid option
OR("33S@10 ta%le o view does not e/ist
S'R9'RM'RROR (TTRI)UT' BUNCTION
It ta&es a single nu$%e t7pe of agu$ent and etuns the eo at the position on the eo
sta!& indi!ated %7 the agu$ent5 The position 2 is the top of the sta!&5
'/0
CR'(T' OR R'AL(C' TRI88'R S'R9'RM'RRORMTRI88'R
afte seveeo on data%ase
)'8IN
inset into $7Meos values(seveMeo(2)).
'ND S'R9'RM'RRORMTRI88'R.
SUSA'ND TRI88'RS
This will fie wheneve a state$ent is suspended5 This $ight o!!u as the esult of a spa!e
issue su!h as e/!eeding an allo!ated ta%lepa!e Uuota5 This fun!tionalit7 !an %e used to
addess the po%le$ and allow the opeatin to !ontinue5
S7nta/0
Ceate o epla!e tigge ,tri""er_name-
afte suspend on ^data%ase X s!he$a_
Copy rights are reserved.
281
N6hen (R)O
NDe!laeO
"" de!laation se!tion
)egin
"" tigge %od7
N'/!eptionO
"" e/!eption se!tion
'nd ,tri""er_name-.
'/0
SQL- !eate ta%lespa!e $7Mspa!e datafile =f0d$7Mfile5d%f= siTe 1$.
SQL- !eate ta%le student(sno nu$%e(1)#sna$e va!ha(23)) ta%lespa!e $7Mspa!e.
CR'(T' OR R'AL(C' TRI88'R SUSA'NDMTRI88'R
afte suspend on data%ase
)'8IN
d%$sMoutput5putMline(K No oo$ to inset in 7ou ta%lespa!e=).
'ND SUSA'NDMTRI88'R.
Output0
Inset $oe ows in student ta%le then # 7ou will get
No oo$ to inset in 7ou ta%lespa!e
(UTONOMOUS TR(NS(CTION
Aio to Oa!leWi# thee was no wa7 in whi!h so$e SQL opeations within a tansa!tion
!ould %e !o$$itted independent of the est of the opeations5 Oa!le allows this# howeve#
though autonomou! tran!action!5 (n autonomou! tran!action is a tansa!tion that is
stated within the !onte/t of anothe tansa!tion# &nown as paent tansa!tion# %ut is
independent of it5 The autono$ous tansa!tion !an %e !o$$itted o olled %a!& egadless
ot the state of the paent tansa!tion5
'/0
CR'(T' OR R'AL(C' TRI88'R (UTONOMOUSMTR(NS(CTIONMTRI88'R
afte inset on student
D'CL(R'
pag$a autono$ousMtansa!tion.
Copy rights are reserved.
282
)'8IN
update student set $a&s H CCC.
!o$$it.
'ND (UTONOMOUSMTR(NS(CTIONMTRI88'R.
Output0
SQL- sele!t E fo$ student.
NO N( M(RFS
""""" """"" "" """"""""""
2 a 222
1 % 111
4 ! 433
SQL- inset into student values(@#=d=#@@@).
SQL- sele!t E fo$ student.
NO N( M(RFS
"""" """""" "" """"""""""
2 a CCC
1 % CCC
4 ! CCC
@ d @@@
R'STRICTIONS ON (UTONOMOUS TR(NS(CTION
If an autono$ous tansa!tion atte$pts to a!!ess a esou!e held %7 the $ain
tansa!tion# a deadlo!& !an o!!u in 7ou poga$5
*ou !annot $a& all poga$s in a pa!&age as autono$ous with a single AR(8M(
de!laation5 *ou $ust indi!ate autono$ous tansa!tions e/pli!it7 in ea!h poga$5
To e/it without eos fo$ an autono$ous tansa!tion poga$ that has e/e!uted at
least one INS'RT o UAD(T' o D'L'T'# 7ou $ust pefo$ an e/pli!it !o$$it o oll%a!&5
The COMMIT and ROLL)(CF state$ents end the a!tive autono$ous tansa!tion# %ut the7
do not fo!e the te$ination of the autono$ous outine5 *ou !an have $ultiple COMMIT
and?o ROLL)(CF state$ents inside 7ou autono$ous %lo!&5
*ou !an not oll%a!& to a savepoint set in the $ain tansa!tion5
Copy rights are reserved.
283
The TR(NS(CTIONS paa$ete in the oa!le initialiTation file spe!ifies the $a/i$u$
nu$%e of tansa!tions allowed !on!uentl7 in a session5 The default value is VC fo
this# %ut 7ou !an in!ease the li$it5
MUT(TIN8 T()L'S
Thee ae esti!tions on the ta%les and !olu$ns that a tigge %od7 $a7 a!!ess5 In ode to
define these esti!tions# it is ne!essa7 to undestand $utating and !onstaining ta%les5
( $utating ta%le is ta%le that is !uentlt7 %eing $odified %7 a DML state$ent and the
tigge event also DML state$ent5 ( $utating ta%le eo o!!us when a ow"level tigge
ties to e/a$ine o !hange a ta%le that is alead7 undegoing !hange5
( !onstaining ta%le is a ta%le that $ight need to %e ead fo$ fo a efeential integit7
!onstaint5
'/0
CR'(T' OR R'AL(C' TRI88'R MUT(TIN8MTRI88'R
%efoe delete on student
fo ea!h ow
D'CL(R'
!t nu$%e.
)'8IN
sele!t !ount(E) into !t fo$ student whee no H 0old5no.
'ND MUT(TIN8MTRI88'R.
Output0
SQL- delete student whee no H 2.
delete student whee no H 2
E
'RROR at line 20
OR("3@3S20 ta%le SCOTT5STUD'NT is $utating# tigge?fun!tion $a7 not see it
OR("3DC210 at bSCOTT5Tb# line @
OR("3@3WW0 eo duing e/e!ution of tigge =SCOTT5T=
:O6 TO (9OID MUT(TIN8 T()L' 'RROR [
Copy rights are reserved.
284
)7 using autono$ous tansa!tion
)7 using state$ent level tigge
Copy rights are reserved.
285

Das könnte Ihnen auch gefallen