8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 1/215
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' TL' 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 9LU' 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).
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
1
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 2/215
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 3/215
NO NM' MRFS
""" """""" """"""""
2 Sudha 233
1 Sað 133
2 >agan 433
1 Naen @33
4 Ra$esh
@ Madhu
9isu
Rattu
SQL- sele!t no# na$e# $a&s fo$ student.
NO NM' MRFS
""" """""" """"""""
2 Sudha 233
1 Sað 133
2 >agan 433
1 Naen @33
4 Ra$esh
@ Madhu
9isu
Rattu
SQL- sele!t no# na$e fo$ student.
NO NM'
""" """""""
2 Sudha
1 Sað
2 >agan
1 Naen
4 Ra$esh
@ Madhu
9isu Rattu
CONDITIONL S'L'CTIONS ND OA'RTORS
6e have two !lauses used in this
6hee
Ode %7
USIN8 6:'R'
S7nta/0
3
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 4/215
sele!t E fo$ ,table_name- whee ,condition-.
the following ae the diffeent t7pes of opeatos used in whee !lause5
ith$eti! opeatos
Co$paison opeatos
Logi!al opeatos
ith$eti! opeatos "" highest pe!eden!e
G# "# E# ?
Co$paison opeatos
H# H# -# ,# -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# H# ,-
'/0
SQL- sele!t E fo$ student whee no H 1.
NO NM' MRFS """ """"""" """""""""
1 Sað 133
1 Naen @33
SQL- sele!t E fo$ student whee no , 1.
NO NM' MRFS
""" """"""" """"""""""
2 Sudha 233
2 >agan 433
SQL- sele!t E fo$ student whee no - 1.
NO NM' MRFS
""" """"""" """"""""""
4 Ra$esh
@ Madhu
9isu
Rattu
SQL- sele!t E fo$ student whee no ,H 1.
4
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 5/215
NO NM' MRFS
""" """"""" """"""""""
2 Sudha 233
1 Sað 133
2 >agan 433
1 Naen @33
SQL- sele!t E fo$ student whee no -H 1.
NO NM' MRFS
""" """"""" """""""""
1 Sað 133
1 Naen @33
4 Ra$esh
@ Madhu
9isu
Rattu
SQL- sele!t E fo$ student whee no H 1.
NO NM' MRFS
""" """"""" """"""""""
2 Sudha 233
2 >agan 433
4 Ra$esh
@ Madhu
9isu
Rattu
SQL- sele!t E fo$ student whee no ,- 1.
NO NM' MRFS
""" """"""" """"""""""
2 Sudha 233
2 >agan 433
4 Ra$esh
@ Madhu 9isu
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-.
5
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 6/215
'/0
SQL- sele!t E fo$ student whee no H 1 and $a&s -H 133.
NO NM' MRFS
""" """"""" """"""""
1 Sað 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 NM' MRFS
""" """"""" """""""""
1 Sað 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
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 NM' MRFS
""" """"""" """""""""
1 Sað 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.
6
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 7/215
NO NM' MRFS
""" """"""" """""""""
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 NM' MRFS
""" """"""" """""""""
2 Sudha 233
1 Sað 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 NM' MRFS
""" """"""" """""""""
@ Madhu
9isu
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 NM' MRFS
""" """"""" """""""""
4 Ra$esh
@ Madhu
7
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 8/215
9isu
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 NM' MRFS
""" """"""" """""""""
2 Sudha 233
1 Sað 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 NM' MRFS
""" """"""" """""""""
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 =S=.
NO NM' MRFS
""" """"""" """""""""
2 Sudha 233
1 Sað 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 =h=.
NO NM' MRFS
""" """"""" """""""""
1 Sað 133
4 Ra$esh
i9) This will give the ows whose na$e;s se!ond lette stat with Ka;5
8
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 9/215
SQL- sele!t E fo$ student whee na$e li&e =a=.
NO NM' MRFS
""" """"""" """"""""
1 Sað 133
2 >agan 433
1 Naen @33
4 Ra$esh
@ Madhu
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 =d=.
NO NM' MRFS
""" """"""" """""""""
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 =t=.
NO NM' MRFS
""" """"""" """""""""
1 Sað 133
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 =e=.
NO NM' MRFS
""" """"""" """""""""
1 Sað 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 =a a =.
NO NM' MRFS
""" """"""" """"""""""2 >agan 433
E *ou have to spe!if7 the pattens in lie using undes!oe ( )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
9
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 10/215
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 NM' MRFS
""" """"""" """""""""
2 Sudha 233
2 >agan 433
1 Sað 133
1 Naen @33
4 Ra$esh
@ Madhu
9isu
Rattu
SQL- sele!t E fo$ student ode %7 no des!.
NO NM' MRFS
""" """"""" """""""""
Rattu
9isu
@ Madhu
4 Ra$esh
1 Sað 133
1 Naen @33
2 Sudha 233
2 >agan 433
USIN8 DML
USIN8 UADT'
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 33.
If 7ou ae not spe!if7ing an7 !ondition this will update entie ta%le5
SQL- update student set $a&s H 33 whee no H 1.
SQL- update student set $a&s H 33# na$e H =9enu= whee no H 2.
USIN8 D'L'T'
This !an %e used to delete the ta%le data te$poail75
S7nta/0
Delete ,table_name- whee ,condition-.
10
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 11/215
'/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%.
!) 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().
E To de!ease pe!ision the !olu$n should %e e$pt75
d) MFIN8 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'NMIN8 COLUMN
S7nta/0
11
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 12/215
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 TRUNCT'
This !an %e used to delete the entie ta%le data pe$anentl75
S7nta/0
tun!ate ta%le ,table_name-.
'/0
SQL- tun!ate ta%le student.
USIN8 DROA
This will %e used to dop the data%ase o%Je!t.
S7nta/0
Dop ta%le ,table_name-.
'/0
SQL- dop ta%le student.
USIN8 R'NM'
This will %e used to ena$e the data%ase o%Je!t.
S7nta/0
ena$e ,old table_name- to ,new_table_name-.
'/0
SQL- ena$e student to stud.
USIN8 TCL
USIN8 COMMIT
This will %e used to save the wo&5
Co$$it is of two t7pes5
I$pli!it
'/pli!it
a) IMALICIT
This will %e issued %7 oa!le intenall7 in two situations5
6hen an7 DDL opeation is pefo$ed5
6hen 7ou ae e/iting fo$ SQL E ALUS5
%) '+ALICIT
This will %e issued %7 the use5
S7nta/0
Co$$it o !o$$it wo&.
E 6hen eve 7ou !o$$itted then the tansa!tion was !o$pleted5
USIN8 ROLLCF
This will undo the opeation5
12
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 13/215
This will %e applied in two $ethods5
Upto pevious !o$$it
Upto pevious oll%a!&
S7nta/0
Roll o oll wo&.
O
Roll%a!& o oll%a!& wo&.
E 6hile po!ess is going on# if suddenl7 powe goes then oa!le will oll%a!& the tansa!tion5
USIN8 S9'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 NM' MRFS
""" """"""" """"""""""
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 NM' MRFS
""" """"""" """"""""""
2 a 233
1 % 133
13
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 14/215
USIN8 DCL
DCL !o$$ands ae used to ganting and evo&ing the pe$issions5USIN8 8RNTThis is used to gant the pivileges to othe uses5
S7nta/0
8ant , privile"e!- on ,ob#ect_name- to ,u!er_name- with gant option.'/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ð5student.
The sudha use !an not gant pe$ission on student ta%le to othe uses5 To get this
t7pe of option use the following5
SQL- gant all on student to sudha with gant option.
Now sudha use also gant pe$issions on student ta%le5
USIN8 R'9OF'
This is used to evo&e the pivileges fo$ the uses to whi!h 7ou ganted the pivileges5
S7nta/0
Revo&e , 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 LIS'S
CR'T' 6IT: S'L'CT6e !an !eate a ta%le using e/isting ta%le along with data5
S7nta/0
Ceate ta%le ,new_table_name- col1, col2, col3 ... coln as sele!t E fo$
,old_table_name-.
'/0 SQL- !eate ta%le student2 as sele!t E fo$ student.
Ceating ta%le with 7ou own !olu$n na$es5
SQL- !eate ta%le student1(sno# sna$e# s$a&s) as sele!t E fo$ student.
Ceating ta%le with spe!ified !olu$ns5
SQL- !eate ta%le student4 as sele!t no#na$e fo$ student.
Ceating ta%le with out ta%le data5
SQL- !eate ta%le student1(sno# sna$e# s$a&s) as sele!t E fo$ student whee 2 H 1.
14
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 15/215
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 LIS'S
S7nta/0
$elect %or"inal_col %alia!_name 'rom %table na$e-.
'/0
SQL- sele!t no sno fo$ student.
o
SQL- sele!t no Psno fo$ student.
TL' LIS'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- ,alia!_name-5,coln- fo$
,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'/0SQL- 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
15
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 16/215
ssu$e that student2 has !olu$ns li&e no#na$e#$a&s and student1 has !olu$ns li&e no#
na$e# hno# !it75
SQL- Mege into student2 s2
Using (sele!t EBo$ student1) s1
On(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).
ULTIL' INS'RTS
6e have ta%le !alled D'AT with the following !olu$ns and data
D'ATNO DNM' LOC
"""""""" """""""" """"
23 a!!ounting new 7o&
13 esea!h dallas
43 sales Chi!ago
@3 opeations %oston
a) CR'T' STUD'NT TL'
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
!) 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: DUALICT' RO6S
SQL- inset all
16
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 17/215
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 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 KSL'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
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.
17
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 18/215
"" 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
i) MULTI INS'RT 6IT: MULTIL' TL'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
Bun!tions !an %e !ategoied as follows5 Single ow fun!tions
8oup fun!tions
SIN8L' RO6 BUNCTIONS
Single ow fun!tions !an %e !ategoied 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
18
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 19/215
Sign
St
Mod
Nvl
Aowe
'/p
Ln
Log
Ceil
Bloo
Round
Tun&
itand
8eatest
Least
Coales!e
a) S
%solute value is the $easue of the $agnitude of value5
%solute value is alwa7s a positive nu$%e5
S7nta/0 a%s (value)
'/0
SQL- sele!t a%s()# a%s(")# a%s(3)# a%s(null) fo$ dual.
S() S(") S(3) S(NULL)
"""""""""" """""""""" """""""""" """""""""""""
" 3
%) SI8N
Sign gives the sign of a value5
S7nta/0 sign (value)
'/0
SQL- sele!t sign()# sign(")# sign(3)# sign(null) fo$ dual.
SI8N() SI8N(") SI8N(3) SI8N(NULL)
"""""""""" """""""""" """""""""" """"""""""""""
2 "2 3
!) SQRT
This will give the suae oot of the given value5
S7nta/0 st (value) "" hee value $ust %e positive5
'/0
19
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 20/215
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 21/215
Aowe is the a%ilit7 to aise a value to a given e/ponent5
S7nta/0 powe (value, eponent )
'/0
SQL- sele!t powe(1#)# powe(3#3)# powe(2#2)# powe(null#null)# powe(1#")
fo$ dual.
AO6'R(1#) AO6'R(3#3) AO6'R(2#2) AO6'R(NULL#NULL) AO6'R(1#")
"""""""""""""" """""""""""""" """"" """"""""" """"""""""""""""""""""" """""""""""""""
41 2 2 53421
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 V54W32 2 524441W4
h) LN
This is %ased on natual o %ase e logaith$5
S7nta/0 ln (value) "" hee value $ust %e geate than eo whi!h is positive onl75
'/0
SQL- sele!t ln(2)# ln(1)# ln(null) fo$ dual.
LN(2) LN(1) LN(NULL)
""""""" """"""" """"""""""""
3 542@V2W2
Ln and '/p ae e!ipo!al to ea!h othe5
'+A (4) H 1353W4
LN (1353W4) H 4
i) LO8
This is %ased on 23 %ased logaith$5
S7nta/0 log (23# value)"" hee value $ust %e geate than eo 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 543231 3
LN (value) H LO8 ('+A(2)# value)
SQL- sele!t ln(4)# log(e/p(2)#4) fo$ dual.
21
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 22/215
LN(4) LO8('+A(2)#4)
""""""" """""""""""""""""
253W211 253W211
J) C'IL
This will podu!e a whole nu$%e that is geate than o eual to the spe!ified value5
S7nta/0 !eil (value)
'/0
SQL- sele!t !eil()# !eil(52)# !eil(")# !eil( "52)# !eil(3)# !eil(null) fo$ dual.
C'IL() C'IL(52) C'IL(") C'IL("52) C'IL(3) C'IL(NULL)
""""""""" """"""""""" """""""""" """""""""""" """""""" """"""""""""""
" " 3
&) BLOOR
This will podu!e a whole nu$%e that is less than o eual to the spe!ified value5
S7nta/0 floo (value)
'/0
SQL- sele!t floo()# floo(52)# floo(")# floo( "52)# floo(3)# floo(null) fo$
dual.
BLOOR() BLOOR(52) BLOOR(") BLOOR("52) BLOOR(3) BLOOR(NULL)
""""""""""" """"""""""""" """""""""""" """""""""""""" """"""""""" """"""""""""""""
" " 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@)# ound(214514@#1)# ound(214514@#1) fo$ dual.
ROUND(214514@) ROUND(214514@#3) ROUND(214514@#1) ROUND(214514@#1)
""""""""""""""""""""" """""""""""""""""""""""" """"""""""""""""""""""" """""""""""""""""""""""
214 214 214514 21451@
SQL- sele!t ound(214514@#"2)# ound(214514@#"1)# ound(214514@#"4)#
ound(214514@#"@) fo$ dual.
ROUND(214514@#"2) ROUND(214514@#"1) ROUND(214514@#"4) ROUND(214514@#"@)"""""""""""""""""""""""" """"""""""""""""""""""""" """""""""""""""""""""""" """"""""""""""""""""""""
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
SQL- sele!t ound("214#3)# ound("214#2)# ound("214#1) fo$ dual.
22
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 23/215
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@)# tun!(214514@#1)# tun!(214514@#1) fo$ dual.
TRUNC(214514@) TRUNC(214514@#1) TRUNC(214514@#1)
""""""""""""""""""""" """"""""""""""""""""""" """""""""""""""""""""""
214 214514 214514
SQL- sele!t tun!(214514@#"2)# tun!(214514@#"1)# tun!(214514@#"4)#
tun!(214514@#"@) fo$ dual.
TRUNC(214514@#"2) TRUNC(214514@#"1) TRUNC(214514@#"4) TRUNC(214514@#"@)
"""""""""""""""""""""""" """""""""""""""""""""""" """"""""""""""""""""""" """"""""""""""""""""""""
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 214SQL- 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("
23
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 24/215
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) ITND
This will pefo$ %itwise and opeation5
S7nta/0 %itand (value1, value2)
'/0
SQL- sele!t %itand(1#4)# %itand(3#3)# %itand(2#2)# %itand(null#null)# %itand("1#"4)
fo$ dual.
ITND(1#4) ITND(3#3) ITND(2#2) ITND(NULL#NULL) ITND("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 eos then it will displa7 eo5
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 eos then it will displa7 eo5
If all the paa$etes ae nulls then it will displa7 nothing5
24
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 25/215
If an7 of the paa$etes is null it will displa7 nothing5
) COL'SC'
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#) fo$ dual.
COL'SC'(2#1#4) COL'SC'(NULL#1#NULL#)
""""""""""""""""""" """""""""""""""""""""""""""""""
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) INITCA
This will !apitalie the initial lette of the sting5
S7nta/0 init!ap ( !trin")
'/0
SQL- sele!t init!ap(=!o$pute=) fo$ dual.
25
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 26/215
INITCA
"""""""""""
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")
'/0
SQL- sele!t length(=!o$pute=) fo$ dual.
L'N8T:
"""""""""""
W
e) RAD
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"_car+)
'/0
SQL- sele!t pad(=!o$pute=#2#=E=)# pad(=!o$pute=#2#=EY=) fo$ dual.
RAD(=COMAUT'R= RAD(=COMAUT'R=
"""""""""""""""""""""" """"""""""""""""""""""
!o$puteEEEEEEE !o$puteEYEYEYE
"" Default padding !haa!te was %lan& spa!e5
f) LAD
This will allows 7ou to pad the left side of a !olu$n with an7 set of !haa!tes5
26
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 27/215
S7nta/0 lpad ( !trin", len"t *, paddin"_car+)
'/0
SQL- sele!t lpad(=!o$pute=#2#=E=)# lpad(=!o$pute=#2#=EY=) fo$ dual.
LAD(=COMAUT'R= LAD(=COMAUT'R=
""""""""""""""""""""" """""""""""""""""""""
EEEEEEE!o$pute EYEYEYE!o$pute
"" Default padding !haa!te was %lan& spa!e5
g) LTRIM
This will ti$ off unwanted !haa!tes fo$ the left end of sting5
S7nta/0 lti$ ( !trin" *,unwanted_car!+)
'/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_car!+)
'/0
SQL- sele!t ti$(=!o$pute=#=e=)# ti$(=!o$pute=#=te=) fo$ dual.
RTRIM( RTRIM
"""""""" """""""""
!o$put !o$pu
SQL- sele!t ti$(=!o$pute=#=!o$put;)# ti$(=!o$pute=#=!o$pute=) fo$ dual.
RTRIM(=C RTRIM(=C
"""""""""" """"""""""!o$pute !o$pute
"" If 7ou haven;t spe!if7 an7 unwanted !haa!tes it will displa7 entie sting5
i) TRIM
This will ti$ off unwanted !haa!tes fo$ the %oth sides of sting5
S7nta/0 ti$ (unwanted_car! fo$ !trin")
'/0
SQL- sele!t ti$( =i= fo$ =indiani=) fo$ dual.
27
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 28/215
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) TRNSLT'
This will epla!e the set of !haa!tes# !haa!te %7 !haa!te5
S7nta/0 tanslate ( !trin", old_car!, new_car!)
'/0
SQL- sele!t tanslate(=india=#=in=#=/7=) fo$ dual.
TRNS
""""""""
/7d/a
&) R'ALC'
This will epla!e the set of !haa!tes# sting %7 sting5
S7nta/0 epla!e ( !trin", old_car! *, new_car!+)
'/0
SQL- sele!t epla!e(=india=#=in=#=/7=)# epla!e(Kindia;#;in;) fo$ dual.
R'ALC' R'ALC'
""""""""""" """""""""""
+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 'NM' >O M8R :IR'DT' SL D'ATNO
"""""""" """""""" """"" """"" """""""""""" """"""""" """"""""""
V4 SMIT: CL'RF V31 2V"D'C"W3 33 13
$) CONCT
This will %e used to !o$%ine two stings onl75
28
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 29/215
S7nta/0 !on!at ( !trin"1, !trin"2)
'/0
SQL- sele!t !on!at(=!o$pute=#= opeato=) fo$ dual.
CONCT(=COMAUT'R=
"""""""""""""""""""""""""
!o$pute opeato
If 7ou want to !o$%ine $oe than two stings 7ou have to use !on!atenation
opeato(XX)5
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'=)
"""""""""""" """"""""""""""""""
V V
o) C:R
This will etun the !haa!te having the %ina7 euivalent 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(V) fo$ dual.
C:R
"""""
a
p) SUSTR
This will %e used to e/ta!t su%stings5
S7nta/0 su%st ( !trin", !tart_cr_count *, no_o'_car!+)
'/0
SQL- sele!t su%st(=!o$pute=#1)# su%st(=!o$pute=#1#)# su%st(=!o$pute=#4#V)
fo$ dual.
SUSTR( SUST SUSTR """""""""" """"""" """"""""o$pute o$put $pute
29
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 30/215
If no_o'_car! paa$ete is negative then it will displa7 nothing5
If %oth paa$etes e/!ept !trin" ae null o eos then it will displa7 nothing5
If no_o'_car! paa$ete is geate than the length of the sting then it ignoes
and !al!ulates %ased on the oginal sting length5
If !tart_cr_count is negative then it will e/ta!t the su%sting fo$ ight end5
2 1 4 @ V W
C O M A U T ' R
"W "V " " "@ "4 "1 "2) INSTR
This will allows 7ou fo sea!hing though a sting fo set of !haa!tes5
S7nta/0 inst ( !trin", !earc_!tr *, !tart_cr_count *, occurrence+ +)
'/0
SQL- sele!t inst(=info$ation=#=o=#@#2)# inst(=info$ation=#=o=#@#1) fo$ dual.
INSTR(=INBORMTION=#=O=#@#2) INSTR(=INBORMTION=#=O=#@#1)
"""""""""""""""""""""""""""""""""""" """""""""""""""""""""""""""""""""""""
@ 23
If 7ou ae not spe!if7ing !tart_cr_count and occurrence then it will stat
sea!h fo$ the %eginning and finds fist o!!uen!e onl75
If %oth paa$etes !tart_cr_count and occurrence ae null# it will displa7
nothing5
) D'COD'
De!ode will a!t as value %7 value su%stitution5
Bo eve7 value of field# it will !he!&s fo a $at!h in a seies of if?then tests5
S7nta/0 de!ode (value, i'1, ten1, i'2, ten2, ……. el!e).
'/0
SQL- sele!t sal# de!ode(sal#33#=Low=#333#=:igh=#=Mediu$=) fo$ e$p.
SL D'COD'
""""" """""""""
33 Low
133 Mediu$
1333 Mediu$
433 Mediu$
4333 Mediu$
333 :igh
@333 Mediu$
333 :igh
2W33 Mediu$
30
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 31/215
2133 Mediu$
1333 Mediu$
1V33 Mediu$
1133 Mediu$
4133 Mediu$
SQL- sele!t de!ode(2#2#4)# de!ode(2#1#4#@#@#) fo$ dual.D'COD'(2#2#4) D'COD'(2#1#4#@#@#)""""""""""""""""" """""""""""""""""""""""" 4
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 eos then de!ode will displa7 eo5
s) 8R'T'ST
This will give the geatest sting5
S7nta/0 geatest ( !trn"1, !trin"2, !trin"3 … !trin"n)
'/0
SQL- sele!t geatest(=a=# =%=# =!=)# geatest(=satish=#=sinu=#=sað=) fo$ dual.
8R'T 8R'T
""""""" """""""
! sinu
If all the paa$etes ae nulls then it will displa7 nothing5
If an7 of the paa$etes is null it will displa7 nothing5
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ð=) fo$ dual.
L'ST L'ST
""""""" """""""
a sað
If all the paa$etes ae nulls then it will displa7 nothing5
If an7 of the paa$etes is null it will displa7 nothing5
u) COL'SC'
This will gives the fist non"null sting5
S7nta/0 !oales!e ( !trn"1, !trin"2, !trin"3 … !trin"n)
'/0
31
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 32/215
SQL- sele!t !oales!e(=a=#=%=#=!=)# !oales!e(null#=a=#null#=%=) fo$ dual.
COL'SC' COL'SC'
""""""""""" """""""""""
a a
DT' BUNCTIONS
S7sdate Cuentdate
Cuentti$esta$p
S7sti$esta$p
Lo!alti$esta$p
D%ti$eone
Sessionti$eone
To!ha
Todate
dd$onths
Months%etween
Ne/tda7
Lastda7
'/ta!t
8eatest
Least
Round
Tun!
Newti$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 nlsdatefo$at H KDD"MONT:"****;.
ut this will e/pie on!e the session was !losed5
a) S*SDT'
This will give the !uent date and ti$e5
'/0
SQL- sele!t s7sdate fo$ dual.
S*SDT'
"""""""""""
1@"D'C"3
%) CURR'NTDT'
32
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 33/215
This will etuns the !uent date in the session;s ti$eone5
'/0
SQL- sele!t !uentdate fo$ dual.
CURR'NTDT'
""""""""""""""""""
1@"D'C"3
!) CURR'NTTIM'STMA
This will etuns the !uent ti$esta$p with the a!tive ti$e one info$ation5
'/0
SQL- sele!t !uentti$esta$p fo$ dual.
CURR'NTTIM'STMA
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1@"D'C"3 345@15@254W44 M G3043
d) S*STIM'STMA
This will etuns the s7ste$ date# in!luding fa!tional se!onds and ti$e one of the
data%ase5
'/0
SQL- sele!t s7sti$esta$p fo$ dual.
S*STIM'STMA
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
1@"D'C"3 345@5425W433 M G3043
e) LOCLTIM'STMA
This will etuns lo!al ti$esta$p in the a!tive ti$e one info$ation# with no ti$e
one info$ation shown5
'/0
SQL- sele!t lo!alti$esta$p fo$ dual.
LOCLTIM'STMA"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""1@"D'C"3 345@@52W531WV@ M
f) DTIM'ZON'
This will etuns the !uent data%ase ti$e one in UTC fo$at5 (Coodinated Univesal
Ti$e)
'/0
SQL- sele!t d%ti$eone fo$ dual.DTIM'ZON'""""""""""""""" "3V033
g) S'SSIONTIM'ZON'
This will etuns the value of the !uent session;s ti$e one5
'/0
33
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 34/215
SQL- sele!t sessionti$eone fo$ dual.
S'SSIONTIM'ZON'
""""""""""""""""""""""""""""""""""""
G3043
h) TOC:R
This will %e used to e/ta!t vaious date fo$ats5
The availa%le date fo$ats as follows5
S7nta/0 to!ha (date# 'ormat )
DT' BORMTS
D "" No of da7s in wee&
DD "" No of da7s in $onth
DDD "" No of da7s in 7ea
MM "" No of $onth
MON "" Thee lette a%%eviation of $onth
MONT: "" Bull7 spelled out $onth
RM "" Ro$an nu$eal $onth
D* "" Thee lette a%%eviated da7
D* "" Bull7 spelled out da7
* "" Last one digit of the 7ea
** "" Last two digits of the 7ea
*** "" Last thee digits of the 7ea
**** "" Bull fou digit 7ea
S**** "" Signed 7ea
I "" One digit 7ea fo$ ISO standad
I* "" Two digit 7ea fo$ ISO standad
I** "" Thee digit 7ea fo$ ISO standad
I*** "" Bou digit 7ea fo$ ISO standad
*# *** "" *ea with !o$$a
*'R "" Bull7 spelled out 7ea
CC "" Centu7
Q "" No of uates6 "" 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
34
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 35/215
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 to!ha(s7sdate#=dd $onth 7777 hh0$i0ss a$ d7=) fo$ dual.
TOC:R(S*SDT'#=DD MONT: ****::0MI
""""""""""""""""""""""""""""""""""""""""""""""""""""
1@ de!e$%e 133 31034014 p$ sun
SQL- sele!t to!ha(s7sdate#=dd $onth 7ea=) fo$ dual.
TOC:R(S*SDT'#=DDMONT:*'R=)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""
1@ de!e$%e two thousand si/
SQL- sele!t to!ha(s7sdate#=dd f$$onth 7ea=) fo$ dual.
TOC:R(S*SDT'#=DD BMMONT: *'R=)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""
1@ de!e$%e two thousand si/
SQL- sele!t to!ha(s7sdate#=ddth DDT:=) fo$ dual.
TOC:R(S
""""""""""""
1@th 1@T:
SQL- sele!t to!ha(s7sdate#=ddspth DDSAT:=) fo$ dual.
TOC:R(S*SDT'#=DDSAT:DDSAT:
""""""""""""""""""""""""""""""""""""""""""
twent7"fouth T6'NT*"BOURT: SQL- sele!t to!ha(s7sdate#=ddsp Ddsp DDSA =) fo$ dual.
TOC:R(S*SDT'#=DDSADDSADDSA=)
""""""""""""""""""""""""""""""""""""""""""""""""
twent7"fou Twent7"Bou T6'NT*"BOUR
i) TODT'
This will %e used to !onvet the sting into data fo$at5
S7nta/0 todate (date)
35
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 36/215
'/0
SQL- sele!t to!ha(todate(=1@?de!?133=#=dd?$on?7777=)# =dd E $onth E da7=)
fo$ dual.
TOC:R(TODT'(=1@?D'C?13
""""""""""""""""""""""""""
1@ E de!e$%e E Sunda7
"" If 7ou ae not using to!ha oa!le will displa7 output in default date fo$at5
J) DDMONT:S
This will add the spe!ified $onths to the given date5
S7nta/0 add$onths (date, no_o'_mont!)
'/0
SQL- sele!t add$onths(todate(=22"Jan"23=#=dd"$on"7777=)# ) fo$ dual.
DDMONT:S
""""""""""""""""
22">UN"3
SQL- sele!t add$onths(todate(=22"Jan"23=#=dd"$on"7777=)# ") fo$ dual.
DDMONT:
"""""""""""""""
22"U8"W
If no_o'_mont! is eo then it will displa7 the sa$e date5
If no_o'_mont! is null then it will displa7 nothing5
&) MONT:S'T6''N
This will give diffeen!e of $onths %etween two dates5
S7nta/0 $onths%etween (date1, date2)
'/0
SQL- sele!t $onths%etween(todate(=22"aug"23=#=dd"$on"7777=)# todate(=22"
Jan"23=#=dd"$on"7777=)) fo$ dual.
MONT:S'T6''N(TODT'(=22"U8"23=#=DD"MON"****=)#TODT'(=22">N"
23=#=DD"MON"****=))
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
V
SQL- sele!t $onths%etween(todate(=22"Jan"23=#=dd"$on"7777=)# todate(=22"
aug"23=#=dd"$on"7777=)) fo$ dual.
MONT:S'T6''N(TODT'(=22">N"23=#=DD"MON"****=)#TODT'(=22"U8"23=#=DD"
MON"****=))
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
"V
l) N'+TD*
36
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 37/215
This will podu!e ne/t da7 of the given da7 fo$ the spe!ified date5
S7nta/0 ne/tda7 (date, day )
'/0
SQL- sele!t ne/tda7(todate(=1@"de!"133=#=dd"$on"7777=)#=sun=) fo$ dual.
N'+TD*(
"""""""""""""
42"D'C"3
"" If the da7 paa$ete is null then it will displa7 nothing5
$) LSTD*
This will podu!e last da7 of the given date5
S7nta/0 lastda7 (date)
'/0
SQL- sele!t lastda7(todate(=1@"de!"133=#=dd"$on"7777=)#=sun=) fo$ dual.
LSTD*(
"""""""""""""
42"D'C"3
n) '+TRCT
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.
'+TRCT(*'RBROMS*SDT')
""""""""""""""""""""""""""""""""""""
133
"" *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(todate(=22"Jan"3=#=dd"$on"77=)#todate(=22"$a"3=#=dd"
$on"77=)#todate(=22"ap"3=#=dd"$on"77=)) fo$ dual. 8R'T'ST(
"""""""""""""
22"AR"3
p) L'ST
This will give the least date5
S7nta/0 least (date1, date2, date3 … daten)
'/0
37
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 38/215
SQL- sele!t least(todate(=22"Jan"3=#=dd"$on"77=)#todate(=22"$a"3=#=dd"$on"
77=)#todate(=22"ap"3=#=dd"$on"77=)) fo$ dual.
L'ST(
"""""""""""""
22">N"3
) ROUND
Round will ounds the date to whi!h it was eual 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 "" 2
2 "" 42
If the da7 falls %etween 2 and 2 then it etuns the fist da7 of the !uent $onth5
If the da7 falls %etween 2 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'DT:U "" SUN
If the wee& da7 falls %etween SUN and 6'D then it etuns the pevious sunda75
If the wee&da7 falls %etween T:U and SUN then it etuns the ne/t sunda75
If the se!ond paa$ete was null then it etuns nothing5
If the 7ou ae not spe!if7ing the se!ond paa$ete then ound will esets the ti$e to the
%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(todate(=1@"de!"3@=#=dd"$on"77=)#=7ea=)# ound(todate(=22"$a"
3=#=dd"$on"77=)#=7ea=) fo$ dual.
ROUND(TO ROUND(TO
"""""""""""" """""""""""""""
32">N"3 32">N"3
SQL- sele!t ound(todate(=22"Jan"3@=#=dd"$on"77=)#=$onth=)# ound(todate(=2W"
Jan"3@=#=dd"$on"77=)#=$onth=) fo$ dual.
38
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 39/215
ROUND(TO ROUND(TO
""""""""""""" """""""""""""""
32">N"3@ 32"B'"3@
SQL- sele!t ound(todate(=1"de!"3=#=dd"$on"77=)#=da7=)# ound(todate(=1"de!"
3=#=dd"$on"77=)#=da7=) fo$ dual.
ROUND(TO ROUND(TO
"""""""""""""" """"""""""""""
1@"D'C"3 42"D'C"3
SQL- sele!t to!ha(ound(todate(=1@"de!"3=#=dd"$on"77=))# =dd $on 7777
hh0$i0ss a$=) fo$ dual.
TOC:R(ROUND(TODT'(=
"""""""""""""""""""""""""""""""""
1@ de! 133 21033033 a$
) TRUNC
Tun! will !hops off the date to whi!h it was eual to o less than the given date5
S7nta/0 tun! (date, ( da7 X $onth X 7ea ))
If the se!ond paa$ete was year then it alwa7s etuns the fist da7 of the !uent 7ea5
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!(todate(=1@"de!"3@=#=dd"$on"77=)#=7ea=)# tun!(todate(=22"$a"
3=#=dd"$on"77=)#=7ea=) fo$ dual.
TRUNC(TO TRUNC(TO
""""""""""""" """"""""""""""
32">N"3@ 32">N"3
SQL- sele!t tun!(todate(=22"Jan"3@=#=dd"$on"77=)#=$onth=)# tun!(todate(=2W"Jan"
3@=#=dd"$on"77=)#=$onth=) fo$ dual.
TRUNC(TO TRUNC(TO
""""""""""""" """""""""""""
32">N"3@ 32">N"3@
SQL- sele!t tun!(todate(=1"de!"3=#=dd"$on"77=)#=da7=)# tun!(todate(=1"
de!"3=#=dd"$on"77=)#=da7=) fo$ dual.
TRUNC(TO TRUNC(TO
""""""""""""" """"""""""""""
39
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 40/215
1@"D'C"3 1@"D'C"3
SQL- sele!t to!ha(tun!(todate(=1@"de!"3=#=dd"$on"77=))# =dd $on 7777 hh0$i0ss
a$=) fo$ dual.
TOC:R(TRUNC(TODT'(=
"""""""""""""""""""""""""""""""""
1@ de! 133 21033033 a$
s) N'6TIM'
This will give the desied ti$eone;s date and ti$e5
S7nta/0 newti$e (date, current_timeone, de!ired_timeone)
vaila%le ti$eones 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 to!ha(newti$e(s7sdate#=g$t=#=7st=)#=dd $on 7777 hh0$i0ss a$=) fo$
dual.
TOC:R(N'6TIM'(S*SDT
"""""""""""""""""""""""""""""""""""
1@ de! 133 3102013 p$
SQL- sele!t to!ha(newti$e(s7sdate#=g$t=#=est=)#=dd $on 7777 hh0$i0ss a$=) fo$
dual.
TOC:R(N'6TIM'(S*SDT
"""""""""""""""""""""""1@ de! 133 30201 p$
t) COL'SC'
This will give the fist non"null date5
S7nta/0 !oales!e (date1, date2, date3 … daten)
'/0
SQL- sele!t !oales!e(=21"Jan"3=#=24"Jan"=)# !oales!e(null#=21"Jan"3=#=14"$a"
W=#null) fo$ dual.
40
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 41/215
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 42/215
4 4333
@ 1V
1W3
1@3
V 233
W 233
2433
23 213
22 213
21 2233
24 2333
2@ 3
2 W33
SQL- sele!t an&(1V) within goup(ode %7 sal des!) fo$ e$p.
RNF(1V)6IT:IN8ROUA(ORD'R*SLD'SC)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""
@
d) D'NS'RNF
This will give the seuential an&ing5
'/0
SQL- sele!t densean&(1V) within goup(ode %7 sal des!) fo$ e$p.
D'NS'RNF(1V)6IT:IN8ROUA(ORD'R*SLD'SC)
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
4
CON9'RSION BUNCTIONS
intonu$
Chatoowid
Rowidto!ha
Tonu$%e
To!ha
Todate
a) INTONUM
This will !onvet the %ina7 value to its nu$ei!al euivalent5
S7nta/0 %intonu$( binary_bit!)
'/0
SQL- sele!t %intonu$(2#2#3) fo$ dual.
INTONUM(2#2#3)
""""""""""""""""""""""""
42
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 43/215
If all the %its ae eo then it podu!es eo5
If all the %its ae null then it podu!es an eo5
%) C:RTORO6ID
This will !onvet a !haa!te sting to a!t li&e an intenal oa!le ow identifie o owid5
!) RO6IDTOC:R
This will !onvet an intenal oa!le ow identifie o owid to !haa!te sting5
d) TONUM'R
This will !onvet a !ha o va!ha to nu$%e5
e) TOC:R
This will !onvet a nu$%e o date to !haa!te sting5
f) TODT'
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(SL)
""""""""""
4W33
%) 98
This will give the aveage of the values of the spe!ified !olu$n5
S7nta/0 avg (column)
'/0
SQL- sele!t avg(sal) fo$ e$p.
98(SL)
"""""""""""""""
1VV52@1W
!) M+
This will give the $a/i$u$ of the values of the spe!ified !olu$n5
43
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 44/215
S7nta/0 $a/ (column)
'/0
SQL- sele!t $a/(sal) fo$ e$p.
M+(SL)
""""""""""
333
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(SL)
""""""""""
33
e) COUNT
This will give the !ount of the values of the spe!ified !olu$n5
S7nta/0 !ount (column)
'/0
SQL- sele!t !ount(sal)#!ount(E) fo$ e$p.
COUNT(SL) COUNT(E)
"""""""""""""" """"""""""""
2@ 2@
CONSTRINTSConstaints ae !ategoied as follows5
Do$ain integit7 !onstaints
Not null
Che!&
'ntit7 integit7 !onstaints
Uniue
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
44
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 45/215
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 46/215
uniue(no)).
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
!onstaint un uniue(no)).
LT'R L'9'L
SQL- alte ta%le student add uniue(no).
SQL- alte ta%le student add !onstaint un uniue(no).ARIMR* F'*
This is used to avoid dupli!ates and nulls5 This will wo& as !o$%ination of uniue 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)).
TL' L'9'L
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
pi$a7 &e7(no)).
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
!onstaint p& pi$a7 &e7(no)).
LT'R L'9'L
SQL- alte ta%le student add pi$a7 &e7(no).
SQL- alte ta%le student add !onstaint p& pi$a7 &e7(no).
BOR'I8N F'*
This is used to efeen!e the paent ta%le pi$a7 &e7 !olu$n whi!h allows dupli!ates5
Boeign &e7 alwa7s atta!hed to the !hild ta%le5
6e !an add this !onstaint in ta%le and alte levels onl75
'/0
TL' 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).
46
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 47/215
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' CSCD'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
TL' L'9'L
SQL- !eate ta%le e$p(e$pno nu$%e(1)# ena$e va!ha(23)# deptno nu$%e(1)#
pi$a7 &e7(e$pno)# foeign &e7(deptno) efeen!es dept(deptno) on delete
!as!ade).
SQL- !eate ta%le e$p(e$pno nu$%e(1)# ena$e va!ha(23)# deptno nu$%e(1)#
!onstaint p& pi$a7 &e7(e$pno)# !onstaint f& foeign &e7(deptno) efeen!es
dept(deptno) on delete !as!ade).
LT'R L'9'L
SQL- alte ta%le e$p add foeign &e7(deptno) efeen!es dept(deptno) on delete
!as!ade.
SQL- alte ta%le e$p add !onstaint f& foeign &e7(deptno) efeen!es dept(deptno) on
delete !as!ade.
COMAOSIT' F'*S
!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' (TL' L'9'L)
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
uniue(no#na$e)).
SQL- !eate ta%le student(no nu$%e(1) # na$e va!ha(23)# $a&s nu$%e(4)#
!onstaint un uniue(no#na$e)).
UNIQU' (LT'R L'9'L)
SQL- alte ta%le student add uniue(no#na$e).
SQL- alte ta%le student add !onstaint un uniue(no#na$e). ARIMR* F'* (TL' 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)).
ARIMR* 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).
47
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 48/215
BOR'I8N F'* (TL' L'9'L)
SQL- !eate ta%le e$p(e$pno nu$%e(1)# ena$e va!ha(23)# deptno nu$%e(1)#
dna$e va!ha(23)# pi$a7 &e7(e$pno)# foeign &e7(deptno#dna$e) efeen!es
dept(deptno#dna$e)).
SQL- !eate ta%le e$p(e$pno nu$%e(1)# ena$e va!ha(23)# deptno nu$%e(1)#
dna$e va!ha(23)# !onstaint p& pi$a7 &e7(e$pno)# !onstaint f& foeign&e7(deptno#dna$e) efeen!es dept(deptno#dna$e)).
BOR'I8N F'* (LT'R L'9'L)
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'RRL' CONSTRINTS
'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 uniue(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 uniue(no) defeed initiall7 defeed).
SQL- alte ta%le student add !onstaint un uniue(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'RTIONS 6IT: CONSTRINTS
Aossi%le opeations with !onstaints as follows5
'na%le
Disa%le
'nfo!e
Dop
'NL'
This will ena%le the !onstaint5 efoe ena%le# the !onstaint will !he!& the e/isting data5
'/0
SQL- alte ta%le student ena%le !onstaint un.
DISL'
This will disa%le the !onstaint5
48
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 49/215
'/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
CS' ND D'BULT
CS'
Case is si$ila to de!ode %ut easie to undestand while going though !oding
'/0
SQL- Sele!t sal#
Case sal
6hen 33 then Klow;
6hen 333 then Khigh;
'lse K$ediu$;
'nd !ase
Bo$ e$p.
SL CS'
""""" """"""""
33 low
133 $ediu$
1333 $ediu$
433 $ediu$
4333 $ediu$
333 high @333 $ediu$
333 high
2W33 $ediu$
2133 $ediu$
1333 $ediu$
1V33 $ediu$
1133 $ediu$
49
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 50/215
4133 $ediu$
D'BULT
-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 NM'
"""""" """""""""
2 a
22 %
SQL- inset into student values(null# K!;).
SQL- sele!t E fo$ student.
NO NM'
"""""" """""""""
2 a
22 %
C
"" Default !an not oveide nulls5
STRCT DT T*A'S
So$e ti$es 7ou $a7 want t7pe whi!h holds all t7pes of data in!luding nu$%es# !has and
spe!ial !haa!tes so$ething li&e this5 *ou !an not a!hieve this using pe"defined t7pes5
*ou !an define !usto$ t7pes whi!h holds 7ou desied data5
'/0
Suppose in a ta%le we have addess !olu$n whi!h holds hno and !it7 info$ation5
6e will define a !usto$ t7pe whi!h holds %oth nu$ei! as well as !ha data5
CR'TIN8 DT
SQL- !eate t7pe add as o%Je!t(hno nu$%e(4)#!it7 va!ha(23)). ?
CR'TIN8 TL' S'D ON DT
SQL- !eate ta%le student(no nu$%e(1)#na$e va!ha(1)#addess add).
INS'RTIN8 DT INTO DT TL'S
SQL- inset into student values(2#=a=#add(222#=h7d=)).
50
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 51/215
SQL- inset into student values(1#=%=#add(111#=%ang=)).
SQL- inset into student values(4#=!=#add(444#=delhi=)).
S'L'CTIN8 DT BROM DT TL'S
SQL- sele!t E fo$ student.
NO NM' 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 NM' DDR'SS5:NO DDR'SS5CIT*
"""" """"""" """"""""""""""""" """"""""""""""""
2 a 222 h7d
1 % 111 %ang
4 ! 444 delhi
UADT' 6IT: DT TL'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.
NO NM' DDR'SS5:NO DDR'SS5CIT*
"""" """"""" """"""""""""""""" """"""""""""""""
2 a 222 h7d
1 % 111 %ang
4 ! 444 %o$%a7D'L'T' 6IT: DT TL'S
SQL- delete student s whee s5addess5hno H 222.
SQL- sele!t no#na$e#s5addess5hno#s5addess5!it7 fo$ student s.
NO NM' 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 view 5
O%Je!t views ae used to elate the use defined o%Je!ts to the e/isting ta%le5
'/0
51
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 52/215
2) ssu$e that the ta%le student has alead7 %een !eated with the following !olu$ns
SQL- !eate ta%le student(no nu$%e(1)#na$e va!ha(23)#hno nu$%e(4)#!it7
va!ha(23)).
1) Ceate the following t7pes
SQL- !eate t7pe add as o%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 studentov(no#studinfo) 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 studentov 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&sf($a&s in nu$%e) etun nu$%e#
Aag$a esti!tefeen!es($a&sf#wnds#nds#wnps#fnps)).?
1) Defining t7pe %od7
SQL- Ceate t7pe %od7 stud as
Me$%e fun!tion $a&sf($a&s in nu$%e) etun nu$%e is
egin
Retun ($a&sG233).
'nd $a&sf.
'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)).
) Using $ethod in sele!t
SQL- Sele!t s5info5$a&sf(s5info5$a&s) fo$ student s.
"" :ee we ae using the pag$a esti!tefeen!es to avoid the wites to the
Data%ase5
9RR*S ND N'ST'D TL'S
52
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 53/215
9RR*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() 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() 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.
"" This will displa7 in geneal fo$at
) 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 TL'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
53
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 54/215
addess stoe as studentte$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
) 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.
) Inseting nested ta%le data to the e/isting ow
SQL- Inset into ta%le(sele!t addess fo$ student whee noH2)
values(add(#;!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.
DT MOD'L LLCOLLT*A'S
LLT*A'S
DCOLLT*A'S
DT*A'S
US'RCOLLT*A'S
US'RT*A'S
BLS: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.
54
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 55/215
"" :ee it will displa7 nothing
e) Then e/e!ute the following po!edues
SQL- '/e! d%$sflash%a!&5ena%leatti$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%$sflash%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%$sflash%a!&5gets7ste$!hangenu$%e
!) To see the SCN
SQL- Aint s
d) Then e/e!ute the following po!edues
SQL- '/e! d%$sflash%a!&5ena%leats7ste$!hangenu$%e(0s)
SQL- '/e! d%$sflash%a!&5disa%le
'+T'RNL TL'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 ue7 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'RNL TL' DT
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- Slplus s7ste$?$anage
SQL- Ceate die!to7 saðdi as K?9isd%?visd%?5153?e/tenal;.
SQL- 8ant all on die!to7 saðdi to sað.
SQL- Conn sað?sað
SQL- Spool dept5lst
SQL- Sele!t deptno XX K#; XX dna$e XX K#; XX lo! fo$ dept.
SQL- Spool off
55
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 56/215
CR'TIN8 '+T'RNL TL'
SQL- Ceate ta%le depte/t
(deptno nu$%e(1)#
Dna$e va!ha(2@)#
Lo! va!ha(24))
Oganiation e/tenal ( t7pe oa!leloade Default die!to7 saðdi
!!ess paa$etes
( e!ods deli$ited %7 newline
Bields te$inated %7 P#
( deptno nu$%e(1)#
Dna$e va!ha(2@)#
Lo! va!ha(24)))
Lo!ation (K?9isd%?visd%?5153?dept5lst;)).
S'L'CTIN8 DT BROM '+T'RNL TL'
SQL- sele!t E fo$ depte/t.
This will ead fo$ dept5lst whi!h is a opeating s7ste$ level file5
LIMITTIONS ON '+T'RNL TL'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'RNL TL'S
a) Queies of e/tenal ta%les !o$plete ve7 ui!&l7 even though a full ta%le s!an id euied
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 9LU'
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
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
9LU'
'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 vendotadt t7pe
56
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 57/215
SQL- Ceate t7pe vendoadt as o%Je!t (vendo!ode nu$%e(1)# vendona$e
va!ha(1)# vendoaddess va!ha(23)).?
1) !eate o%Je!t ta%les vendos and vendos2
SQL- Ceate ta%le vendos of vendoadt.
SQL- Ceate ta%le vendos2 of vendoadt.
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 vendoadt t7pe also5
SQL- Ceate ta%le odes (odeno nu$%e(1)# vendoinfo ef vendoadt).
O
SQL- Ceate ta%le odes (odeno nu$%e(1)# vendoinfo ef vendoadt with
owid).
) inset the data into odes ta%le
The vendoinfo !olu$n in the following s7nta/es will stoe o%Je!t id of an7 ta%le
whi!h is efeen!ed %7 vendoadt o%Je!t ( %oth vendos and vendos2)5
SQL- inset into odes values(22#(sele!t ef(v) fo$ vendos v whee vendo!ode
H 2)).
SQL- inset into odes values(21#(sele!t ef(v) fo$ vendos v whee vendo!ode
H 1)).
SQL- inset into odes values(24#(sele!t ef(v2) fo$ vendos2 v2 whee
vendo!ode H 2)).
SQL- inset into odes values(2@#(sele!t ef(v2) fo$ vendos2 v2 whee
vendo!ode H 2)).
) To see the o%Je!t ids of vendo ta%le
SQL- Sele!t ef(9) fo$ vendos v.
V) If 7ou see the vendoinfo of odes it will show onl7 the o%Je!t ids not the values#
to see the values
SQL- Sele!t deef(o5vendoinfo) 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 CONSTRINTS
57
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 58/215
Ref !an also a!ts as !onstaint5
'ven though vendos2 also holding vendoadt# 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 vendoinfo !olu$n in the following s7nta/es will stoe o%Je!t ids of vendos
onl75
SQL- Ceate ta%le odes (odeno nu$%e(1)# vendoinfo ef vendoadt s!ope is
vendos).
O
SQL- Ceate ta%le odes (odeno nu$%e(1)# vendoinfo ef vendoadt !onstaint f&
efeen!es vendos).
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).
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 student2ov 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 student1ov as sele!t no#hno#!it7#
$a&eef(student2ov#id) id fo$ Student1.
d) Que7 the following
SQL- sele!t Efo$ student2ov.
58
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 59/215
SQL- sele!t ef(s) fo$ student2ov s.
SQL- sele!t values(s) fo$ student2ov.
SQ- sele!t Efo$ student1ov.
SQL- sele!t deef(s5id) fo$ student1ov s.
ARTITIONS
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
D9NT8'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
D9NT8'S OB ARTITIONS * STORIN8 T:'M IN DIBB'R'NT TL'SAC'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
DISD9NT8'S
Aatitioned ta%les !annot !ontain an7 !olu$ns with long o long aw datat7pes# LO
t7pes o o%Je!t t7pes5
RN8' ARTITIONS
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@
59
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 60/215
!) 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 p values less than(@3).
f) Dopping a patition
SQL- lte ta%le student dop patition p@.
g) Rena$ing a patition
SQL- lte ta%le student ena$e patition p4 to p.
h) Tun!ate a patition
SQL- lte ta%le student tun!ate patition p.
i) Splitting a patition
SQL- lte ta%le student split patition p1 at(2) into (patition p12#patition p11).
J) '/!hanging a patitionSQL- 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ðts.
LIST ARTITIONS
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#@#)# patition p1 values(#V#W##23)#patition p4
values(22#21#24#2@#2)# patition p@ values(2#2V#2W#2#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(#;%;). "" this will go to p1
SQL- Inset into student values(22#;!;). "" this will go to p4
SQL- Inset into student values(2#;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).
60
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 61/215
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 p values(12#11#14#1@#1).
f) Dopping a patition
SQL- lte ta%le student dop patition p@.
g) Rena$ing a patition
SQL- lte ta%le student ena$e patition p4 to p.
h) Tun!ate a patition
SQL- lte ta%le student tun!ate patition p.
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ðts.
:S: ARTITIONS
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 .
:ee oa!le auto$ati!all7 gives patition na$es li&e
S*SA2
S*SA1
S*SA4
S*SA@
S*SA
%) 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(#;%;).
SQL- Inset into student values(22#;!;).
SQL- Inset into student values(2#;d;).
!) Retieving e!ods fo$ hash patitioned ta%le
SQL- Sele!t Efo$ student.
SQL- Sele!t Efo$ student patition(s7sp2).
61
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 62/215
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 p .
f) Rena$ing a patition
SQL- lte ta%le student ena$e patition p to pV.
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 s7sp2 with ta%le student1.
i) Moving a patition
SQL- lte ta%le student $ove patition s7sp1 ta%lespa!e saðts.
SU"ARTITIONS 6IT: RN8' 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*SSUA2
S*SSUA1
S*SSUA4
A1 \ S*SSUA@
S*SSUA
S*SSUA
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(s7ssu%p2).
62
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 63/215
d) Aossi%le opeations with su%patitions
dd
Dop
Tun!ate
Rena$e
Split
e) dding a patition
SQL- lte ta%le student add patition p4 values less than(43).
f) Dopping a patition
SQL- lte ta%le student dop patition p4.
g) Rena$ing a patition
SQL- lte ta%le student ena$e patition p1 to p4.
h) Tun!ate a patition
SQL- lte ta%le student tun!ate patition p2.
i) Splitting a patition
SQL- lte ta%le student split patition p4 at(2) into (patition p42#patition p41).
DT MOD'L
LLINDARTITIONS
LLINDSUARTITIONS
LLTARTITIONS
LLTSUARTITIONS
DINDARTITIONS
DINDSUARTITIONS
DTARTITIONS
DTSUARTITIONS
US'RINDARTITIONS
US'RINDSUARTITIONS
US'RTARTITIONS
US'RTSUARTITIONS
8ROUA * ND :9IN8
8ROUA *
Using goup %7# we !an !eate goups of elated info$ation5
Colu$ns used in sele!t $ust %e used with goup %7# othewise it was not a goup %7
e/pession5
'/0
SQL- sele!t deptno# su$(sal) fo$ e$p goup %7 deptno.
63
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 64/215
D'ATNO SUM(SL)
"""""""""" """"""""""
23 WV3
13 23WV
43 @33
SQL- sele!t deptno#Jo%#su$(sal) fo$ e$p goup %7 deptno#Jo%.
D'ATNO >O SUM(SL)
"""""""""" """"""""" """"""""""
23 CL'RF 2433
23 MN8'R 1@3
23 AR'SID'NT 333
13 NL*ST 333
13 CL'RF 233
13 MN8'R 1V
43 CL'RF 3
43 MN8'R 1W3
43 SL'SMN 33
: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 TSL
"""""""""" """"""""" """"""""""
23 AR'SID'NT 333
13 NL*ST 333
43 SL'SMN 33
SQL- sele!t deptno#Jo%#su$(sal) tsal fo$ e$p goup %7 deptno#Jo% having su$(sal) -
4333 ode %7 Jo%.
D'ATNO >O TSL """""""""" """"""""" """"""""""
13 NL*ST 333
23 AR'SID'NT 333
43 SL'SMN 33
ORD'R OB '+'CUTION
8oup the ows togethe %ased on goup %7 !lause5
Cal!ulate the goup fun!tions fo ea!h goup5
64
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 65/215
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(SL)
"""""""""" """"""""" """"""""""
23 CL'RF 2433
23 MN8'R 1@3
23 AR'SID'NT 333
23 WV3
13 NL*ST 333
13 CL'RF 233
13 MN8'R 1V
13 23WV
43 CL'RF 3
43 MN8'R 1W3
43 SL'SMN 33
43 @33 131
USIN8 8ROUAIN8
In the a%ove ue7 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(SL)
""""""""""""""""""""""""""""""""""" """""""""""""""""""""""""""""""""" """"""""""""""
23 CL'RF 2433
23 MN8'R 1@3
23 AR'SID'NT 333
23 ll Jo%s WV3
13 NL*ST 333
13 CL'RF 233
65
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 66/215
13 MN8'R 1V
13 ll Jo%s 23WV
43 CL'RF 3
43 MN8'R 1W3
43 SL'SMN 33
43 ll Jo%s @33
ll Depts ll Jo%s 131
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(SL)
""""""""""""""""""""""""""""""""""" """""""""""""""""""""""""""""""""""" """"""""""""
23 CL'RF 2433
23 MN8'R 1@3
23 AR'SID'NT 333
23 ll >o%s WV3
13 NL*ST 333
13 CL'RF 233
13 MN8'R 1V
13 ll >o%s 23WV
43 CL'RF 3
43 MN8'R 1W3
43 SL'SMN 33
43 ll >o%s @33
ll Depts NL*ST 333ll Depts CL'RF @23
ll Depts MN8'R W1V
ll Depts AR'SID'NT 333
ll Depts SL'SMN 33
ll Depts ll >o%s 131
S'T OA'RTORS
T*A'S
66
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 67/215
Union
Union all
Intese!t
Minus
UNION
This will !o$%ine the e!ods of $ultiple ta%les having the sa$e stu!tue5
'/0
SQL- sele!t E fo$ student2 union sele!t E fo$ student1.
UNION LL
This will !o$%ine the e!ods of $ultiple ta%les having the sa$e stu!tue %ut in!luding
dupli!ates5
'/0
SQL- sele!t E fo$ student2 union all sele!t E fo$ student1.
INT'RS'CT
This will give the !o$$on e!ods of $ultiple ta%les having the sa$ stu!tue5
'/0
SQL- sele!t E fo$ student2 intese!t sele!t E fo$ student1.
MINUS
This will give the e!ods of a ta%le whose e!ods ae not in othe ta%les having the sa$e
stu!tue5
'/0
SQL- sele!t E fo$ student2 $inus sele!t E fo$ student1.
9I'6S view is a data%ase o%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 ue7 and teats it as a ta%le# theefoe a view !an %e thought of
as a stoed ue7 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
67
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 68/215
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 deptv as sele!t Efo$ dept with ead onl7.
SQL- Ceate view deptv as sele!t deptno# su$(sal) tsal fo$ e$p goup %7 deptno.
SQL- Ceate view stud as sele!t ownu$ no# na$e# $a&s fo$ student.
SQL- Ceate view student as sele!t Efo$ student2 union sele!t Efo$ student1.
SQL- Ceate view stud as sele!t distin!t no#na$e fo$ student.
9I'6S 6IT: DML
9iew with not null !olu$n "" inset with out not null !olu$n not possi%le
"" update not null !olu$n to null is not possi%le
"" delete possi%le
9iew with out not null !olu$n whi!h was in %ase ta%le "" inset not possi%le
"" 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 5
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' TL'
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 CONSTRINT
SQL- Ceate view stud as sele!t Efo$ student whee $a&s H 33 with !he!& option
!onstaint C&.
" Inset possi%le with $a&s value as 33
" Update possi%le e/!luding $a&s !olu$n
" Delete possi%le
68
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 69/215
DROAAIN8 9I'6S
SQL- dop view deptv.
DT MOD'L
LL9I'6
D9I'6
US'R9I'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 seuen!e5
T*A'S
Aivate
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 uses5D9NT8'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'
seuen!e is a data%ase o%Je!t# whi!h !an geneate uniue# seuential intege values5
It !an %e used to auto$ati!all7 geneate pi$a7 &e7 o uniue &e7 values5
seuen!e !an %e eithe in an as!ending o des!ending ode5
S7nta/0
Ceate seuen!e , !e_name- in!e$ent %t7 n stat with n $a/value n
$invalue n !7!le?no!7!le !a!he?no!a!he.
7 defalult the seuen!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 seuen!e nu$%es and etains the$ in $e$o7 fo faste
a!!ess5
'/0
SQL- !eate seuen!e s.
SQL- !eate seuen!e s in!e$ent %7 23 stat with 233 $invalue $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)).
69
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 70/215
SQL- inset into student values(s5ne/tval# Ksað).
Initiall7 !uval is not defined and ne/tval is stating value5
fte that ne/tval and !uval ae alwa7s eual5
CR'TIN8 LA:"NUM'RIC S'QU'NC'
SQL- !eate seuen!e s stat with 22214@.
SQL- Inset into student values (s5ne/tval XX tanslate
(s5ne/tval#;214@VW3;#;a%!defghiJ;)).
LT'RIN8 S'QU'NC'
6e !an alte the seuen!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 seuen!e nu$%es5
'/0
SQL- alte seuen!e s $invalue .
SQL- alte seuen!e s in!e$ent %7 1.
SQL- alte seuen!e s !a!he 23.
DROAAIN8 S'QU'NC'
SQL- dop seuen!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
'ui Join
Non"eui Join
Self Join
Natual Join
Coss Join
Oute Join
Left oute
Right oute
Bull oute
Inne Join
Using !lause
On !lause
ssu$e that we have the following ta%les5
70
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 71/215
SQL- sele!t E fo$ dept.
D'ATNO DNM' LOC
"""""" """""""""" """"""""""
23 $&t h7d
13 fin %ang
43 h %o$%a7
SQL- sele!t E fo$ e$p.
'MANO 'NM' >O M8R D'ATNO
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sað 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 'NM' >O DNM' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sað anal7st $&t h7d
444 Jagan $anage $&t h7d
111 sudha !le& fin %ang
USIN8 CLUS'
SQL- sele!t e$pno#ena$e#Jo% #dna$e#lo! fo$ e$p e Join dept d using(deptno).
'MANO 'NM' >O DNM' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sað anal7st $&t h7d
444 Jagan $anage $&t h7d
111 sudha !le& fin %ang
ON CLUS'
SQL- sele!t e$pno#ena$e#Jo%#dna$e#lo! fo$ e$p e Join dept d on(e5deptnoHd5deptno). 'MANO 'NM' >O DNM' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sað 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
71
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 72/215
'/0
SQL- sele!t e$pno#ena$e#Jo%#dna$e#lo! fo$ e$p e#dept d whee e5deptno -
d5deptno.
'MANO 'NM' >O DNM' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
111 sudha !le& $&t h7d
@@@ $adhu enginee $&t h7d
@@@ $adhu enginee fin %ang
@@@ $adhu enginee h %o$%a7
S'LB >OIN
>oining the ta%le itself is !alled self Join5
'/0
SQL- sele!t e25e$pno#e15ena$e#e25Jo%#e15deptno fo$ e$p e2#e$p e1 whee
e25e$pnoHe15$g.
'MANO 'NM' >O D'ATNO
"""""""""" """""""""" """""""""" """"""""""
222 Jagan anal7st 23
111 $adhu !le& @3
444 sudha $anage 13
@@@ sað enginee 23
NTURL >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 'NM' >O DNM' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sað 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 'NM' >O DNM' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sað anal7st $&t h7d
111 sudha !le& $&t h7d
444 Jagan $anage $&t h7d
72
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 73/215
@@@ $adhu enginee $&t h7d
222 sað anal7st fin %ang
111 sudha !le& fin %ang
444 Jagan $anage fin %ang
@@@ $adhu enginee fin %ang
222 sað 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 'NM' >O DNM' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sað 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
SQL- sele!t e$pno#ena$e#Jo%#dna$e#lo! fo$ e$p e#dept d whee e5deptno(G) H
d5deptno.
'MANO 'NM' >O DNM' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sað anal7st $&t h7d
444 Jagan $anage $&t h7d
73
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 74/215
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 'NM' >O DNM' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
444 Jagan $anage $&t h7d
222 sað anal7st $&t h7d
111 sudha !le& fin %ang
@@@ $adhu enginee
h %o$%a7
INN'R >OIN
This will displa7 all the e!ods that have $at!hed5
'/0
SQL- sele!t e$pno#ena$e#Jo%#dna$e#lo! fo$ e$p inne Join dept using(deptno).
'MANO 'NM' >O DNM' LOC
"""""""""" """""""""" """""""""" """""""""" """"""""""
222 sað anal7st $&t h7d
444 Jagan $anage $&t h7d
111 sudha !le& fin %ang
SUQU'RI'S ND '+ISTS
SUQU'RI'S
Nesting of ueies# one within the othe is te$ed as a su%ue75
state$ent !ontaining a su%ue7 is !alled a paent ue75
Su%ueies ae used to etieve data fo$ ta%les that depend on the values in the ta%le
itself5
T*A'S
Single ow su%ueies
Multi ow su%ueies
Multiple su%ueies
Coelated su%ueies
SIN8L' RO6 SUQU'RI'S
In single ow su%ue7# 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 V).
'MANO 'NM' >O M8R :IR'DT' SL COMM D'ATNO
74
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 75/215
"""""""""" """""""""" """"""""" """""""""" """""""""""" """"""" """""""""" """"""""""
VVWW SCOTT NL*ST V 2"AR"WV 4333 13
VW4 FIN8 AR'SID'NT 2V"NO9"W2 333 23
V31 BORD NL*ST V 34"D'C"W2 4333 13
MULTI RO6 SUQU'RI'S
In $ulti ow su%ue7# it will etun $oe than one value5 In su!h !ases we should in!ludeopeatos li&e an7# all# in o not in %etween the !o$paision opeato and the su%ue75
'/0
SQL- sele!t E fo$ e$p whee sal - an7 (sele!t sal fo$ e$p whee sal %etween 133
and @333).
'MANO 'NM' >O M8R :IR'DT' SL COMM D'ATNO
"""""""""" """""""""" """"""""" """""""""" """"""""""" """""""" """""""""" """"""""""
V >ON'S MN8'R VW4 31"AR"W2 1V 13
VVWW SCOTT NL*ST V 2"AR"WV 4333 13
VW4 FIN8 AR'SID'NT 2V"NO9"W2 333 23
V31 BORD NL*ST V 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 133
and @333).
'MANO 'NM' >O M8R :IR'DT' SL COMM D'ATNO
"""""""""" """""""""" """"""""" """""""""" """"""""""""" """""" """""""""" """"""""""
VW4 FIN8 AR'SID'NT 2V"NO9"W2 333 23
MULTIAL' SUQU'RI'S
Thee is no li$it on the nu$%e of su%ueies in!luded in a whee !lause5 It allows nesting of aue7 within a su%ue75
'/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 'NM' >O M8R :IR'DT' SL COMM D'ATNO
"""""""""" """""""""" """"""""" """""""""" """""""""""" """"""" """""""""" """"""""""
VVWW SCOTT NL*ST V 2"AR"WV 4333 13
V31 BORD NL*ST V 34"D'C"W2 4333 13
CORR'LT'D SUQU'RI'S
su%ue7 is evaluated on!e fo the entie paent state$ent whee as a !oelated su%ue7 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 ,H (sele!t !ount(ena$e) fo$ e$p
whee e5deptno H deptno).
D'ATNO
""""""""""
75
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 76/215
13
43
'+ISTS
'/ists fun!tion is a test fo e/isten!e5 This is a logi!al test fo the etun of ows fo$ a ue75
'/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
43
Bo$ the a%ove ue7 !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 ue7 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 'NM'
"""""""""" """"""""""
13 DMS
13 BORD
13 >ON'S
13 SCOTT
13 SMIT:
43 LL'N
43 LF' 43 >M'S
43 MRTIN
43 TURN'R
43 6RD
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
76
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 77/215
%7 deptno#ena$e.
D'ATNO 'NM'
""""""""" """"""""""
23 CLRF
23 FIN8
23 MILL'R
6LFUA TR''S ND INLIN' 9I'6
6LFUA TR''S
Using hiea!hi!al ueies# 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.
'NM'XX=HH-=XXARIOR'NM L'9'L
"""""""""""""""""""""""""""""""""""" """"""""
FIN8HH- 2
>ON'SHH-FIN8 1
SCOTTHH->ON'S 4
DMSHH-SCOTT @
BORDHH->ON'S 4
SMIT:HH-BORD @
LF'HH-FIN8 1
LL'NHH-LF' 4
6RDHH-LF' 4
MRTINHH-LF' 4
TURN'RHH-LF' 4
>M'SHH-LF' 4
CLRFHH-FIN8 1
MILL'RHH-CLRF 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 NL*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
77
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 78/215
SQL- Sele!t ena$e# sal# ownu$ an& fo$ (sele!t Efo$ e$p ode %7 sal).
'NM' SL RNF
"""""""""" """""""""" """"""""""
SMIT: W33 2
>M'S 3 1
DMS 2233 4
6RD 213 @
MRTIN 213
MILL'R 2433
TURN'R 233 V
LL'N 233 W
CLRF 1@3
LF' 1W3 23
>ON'S 1V 22
SCOTT 4333 21
BORD 4333 24
FIN8 333 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.
TL' L'9'L LOCFS
ta%le level lo!& will pote!t ta%le data thee%7 guaanteeing data integit7 when data is %eing
a!!essed !on!uentl7 %7 $ultiple uses5 ta%le lo!& !an %e held in seveal $odes5
Shae lo!&
Shae update lo!&
'/!lusive lo!&
S:R' LOCF
shae lo!& lo!&s the ta%le allowing othe uses to onl7 ue7 %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
78
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 79/215
'/0
SQL- lo!& ta%le e$p in shae $ode.
S:R' UADT' LOCF
It lo!&s ows that ae to %e updated in a ta%le5 It pe$its othe uses to !on!uentl7 ue7#
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 ue75 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.
NO6IT
If one use lo!&ed the ta%le without nowait then anothe use t7ing to lo!& the sa$e ta%le
then he has to wait until the use who has initiall7 lo!&ed the ta%le issues a !o$$it o oll%a!&
state$ent5 This dela7 !ould %e avoided %7 appending a nowait !lause in the lo!& ta%le
!o$$and5
'/0
SQL- lo!& ta%le e$p in e/!lusive $ode nowait5
D'DLOCF
deadlo!& o!!us when tow uses have a lo!& ea!h on sepaate o%Je!t# and the7 want toa!uie 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 value56:* 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 eualit75
T*A'S
Uniue inde/
Non"uniue inde/
tee inde/
79
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 80/215
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/ oganied ta%le
Aatition inde/
Lo!al inde/
Lo!al pefi/ed
Lo!al non"pefi/ed
8lo%al inde/
8lo%al pefi/ed
8lo%al non"pefi/ed
UNIQU' IND'+
Uniue inde/es guaantee that no two ows of a ta%le have dupli!ate values in the !olu$ns
that define the inde/5 Uniue inde/ is auto$ati!all7 !eated when pi$a7 &e7 o uniue
!onstaint is !eated5
'/0
SQL- !eate uniue inde/ studind on student(sno).
NON"UNIQU' IND'+
Non"Uniue inde/es do not i$pose the a%ove esti!tion on the !olu$n values5
'/0
SQL- !eate inde/ studind 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 ui!& 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/ studind on student(sno).
ITMA 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
80
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 81/215
'/0
SQL- !eate %it$ap inde/ studind on student(se/).
COMAOSIT' IND'+
!o$posite inde/ also !alled a !on!atenated inde/ is an inde/ !eated on $ultiple !olu$ns of
a ta%le5 Colu$ns in a !o$posite inde/ !an appea in an7 ode and need not %e adJa!ent
!olu$ns of the ta%le5
'/0
SQL- !eate %it$ap inde/ studind 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/ studind on student(sno# evese).
6e !an e%uild a evese &e7 inde/ into no$al inde/ using the noevese &e7wod5
'/0
SQL- alte inde/ studind 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/ studind on student(uppe(sna$e)).
D'SC'NDIN8 IND'+
The ode used %7 "tee inde/es has %een as!ending ode5 *ou !an !ategoie data in "tee
inde/ in des!ending ode as well5 This featue !an %e useful in appli!ations whee soting
opeations ae euied5
'/0
SQL- !eate inde/ studind on student(sno des!).
T'+T IND'+
Que7ing te/t is diffeent fo$ ue7ing 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 ueies 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/ uestions a%out the te/t5
To use oa!le te/t# 7ou need to !eate a tet 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
81
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 82/215
T*A'S
Thee ae seveal diffeent t7pes of inde/es availa%le in oa!le i5 The fist# CONT'+T is
suppoted in oa!le Wi as well as oa!le i5 s of oa!le i# 7ou !an use the CT+CT te/t inde/ fo
futhe enhan!e 7ou te/t inde/ $anage$ent and ue7 !apa%ilities5
CONT'+T
CT+CT
CT+RUL'
The CT+CT inde/ t7pe suppots the tansa!tional s7n!honiation 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+CT inde/ t7pes do not geneate
s!oe values duing the te/t ueies5
: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
SQL- !eate inde/ %oo&inde/ on %oo&s(info) inde/t7pe is !t/s7s5!onte/t.
SQL- !eate inde/ %oo&inde/ 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
CONTINS < CTS'RC:
CONTINS fun!tion ta&es two paa$etes \ the !olu$n na$e and the sea!h sting5
S7nta/0
Contains(indeed_column, !earc_!tr ).
If 7ou !eate a CT+CT inde/# use the CTS'RC: fun!tion in pla!e of CONTINS5 CTS'RC: ta&es
thee paa$etes \ the !olu$n na$e# the sea!h sting and the inde/ set5
S7nta/0
Contains(indeed_column, !earc_!tr, inde_!et ).
:O6 T'+T Q'UR* 6ORFS[
6hen a fun!tion su!h as CONTINS o CTS'RC: is used in ue7# the te/t potion of the ue7 is
po!essed %7 oa!le te/t5 The e$ainde of the ue7 is po!essed Just li&e a egula ue7
within the data%ase5 The esult of the te/t ue7 po!essing and the egula ue7 po!essing
ae $eged to etun a single set of e!ods to the use5
S'RC:IN8 BOR N '+CT MTC: OB 6ORD
The following ueies will sea!h fo a wod !alled Kppet7; whose s!oe is geate than eo5
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.
82
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 83/215
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.
S'RC:IN8 BOR N '+CT MTC: OB MULTIAL' 6ORDS
The following ueies will sea!h fo two wods5SQL- 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 ueies 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 ueies 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 ueies 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 ueies 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
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 MTC: OB A:RS'
The following ueies 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.
83
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 84/215
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 ueies 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/ ueies# 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 6ILDCRDS DURIN8 S'RC:'S
*ou !an use wild!ads to e/pand the list of valid sea!h te$s used duing 7ou ue75 >ust as
in egula te/t"sting wild!ad po!essing# two wild!ads ae availa%le5
" pe!ent sign. $ultiple"!haa!te wild!ad
" undes!oe. single"!haa!te wild!ad
SQL- sele!t E fo$ %oo&s whee !ontains(info# Kwo&e;) - 3.
SQL- sele!t E fo$ %oo&s whee !ontains(info# Kwo&;) - 3.
S'RC:IN8 BOR 6ORDS T:T S:R' T:' SM' 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* MTC:'S
fu7 $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 Bu7 $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 ue75
The following ueies 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 fu7 $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 fu7 $at!h# pe!ede the sea!h te$ with a uestion $a&# with no spa!e %etween
the uestion $a& and the %eginning of the sea!h te$5
84
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 85/215
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
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&()5
SQL- sele!t E fo$ %oo&s whee !ontains(info# Kgate;) - 3.IND'+ S*NC:RONIZTION
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*NCIND'+ po!edue of the
CT+DDL pa!&age5
SQL- e/e! CT+DDL5S*NCIND'+(K%oo&inde/;).
IND'+ S'TS
:istoi!all7# po%le$s with ueies 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 ue7 !apa%ilit7#
oa!le featues inde/ sets5 The inde/es within the inde/ set $a7 %e stu!tued elationa
!olu$ns o on te/t !olu$ns5
To !eate an inde/ set# use the CT+DDL 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+DDL5CR'T'IND'+S'T(K%oo&sinde/set;).
The add non"te/t inde/es5
SQL- e/e! CT+DDL5DDIND'+(K%oo&sinde/set;# Ktitleinde/;).
Now !eate a CT+CT 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&inde/ on %oo&s(info) inde/t7pe is !t/s7s5!t/!at
paa$etes(Kinde/ set %oo&sinde/set;).
IND'+"OR8NIZ'D TL'
n inde/"oganied ta%le &eeps its data soted a!!oding to the pi$a7 &e7 !olu$n values fo
the ta%le5 Inde/"oganied ta%les stoe thei data as if the entie ta%le was stoed in an inde/5
n inde/"oganied 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) oganiation inde/.
ARTITION 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
LOCL IND'+'S
Lo!al &e7wod tells oa!le to !eate a sepate inde/ fo ea!h patition5
85
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 86/215
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 uniue o non uniue5
Lo!al inde/es $a7 %e easie to $anage than glo%al inde/es5
'/0
SQL- !eate inde/ studinde/ on student(sno) lo!al.
8LOL 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$ uniueness !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/ studinde/ on student(sno) glo%al.
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 euies individual e!onstu!tion of the inde/ o
ea!h patition (onl7 in the !ase of glo%al inde/)5
'/0
SQL- alte inde/ studind 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 patitionedta%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>'CTUS8' view5
If 7ou want to stop $onitoing use the following5
S7nta/0
alte inde/ inde_name no$onitoing usage.
DT MOD'L
LLIND'+'S
DIND'+'S
US'RIND'+'S
86
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 87/215
LLIND"COLUMNS
D"INDCOLUMNS
US'RINDCOLUMNS
LLARTIND'+'S
DARTIND'+'S
US'RARTIND'+'S
9`O>'CTUS8'
SQLEALUS COMMNNDS
These !o$$ands does not euie state$ent te$inato and appli!a%le to the sessions # those
will %e auto$ati!all7 !leaed when session was !losed5
R'F
This will %e used to %ea&up the data depending on the gouping5
S7nta/0
ea& o %e on ,!olu$n _name- on epot
COMAUT'
This will %e used to pefo$ goup fun!tions on the data5
S7nta/0
Co$pute o !o$p goupfun!tion of column_name on breain"_column_name o
epot
TTITL'
This will give the top title fo 7ou epot5 *ou !an on o off the ttitle5
S7nta/0
Ttitle o ttit left X !ente X ight title_name s&ip n oter_caracter!
Ttitle o ttit on o off
TITL'
This will give the %otto$ title fo 7ou epot5 *ou !an on o off the %title5
S7nta/0
title o %tit left X !ente X ight title_name s&ip n oter_caracter!
title o %tit on o off
'/0SQL- %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'TILS= s&ip2 !ente =""""""""""""""""=
SQL- %title !ente =EE T:NFQ EE=
SQL- sele!t E fo$ e$p ode %7 deptno.
Output0
87
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 88/215
'MALO*'' D'TILS
"""""""""""""""""""""""
'MANO 'NM' >O M8R :IR'DT' SL COMM D'ATNO
"""""""""" """""""""" """"""""" """"""" """""""""""""" """""""" """""""""" """"""""""
VVW1 CLRF MN8'R VW4 3">UN"W2 1@3 23
VW4 FIN8 AR'SID'NT 2V"NO9"W2 333
V4@ MILL'R CL'RF VVW1 14">N"W1 2433
"""""""""" EEEEEEEEEE
WV3 su$
V4 SMIT: CL'RF V31 2V"D'C"W3 W33 13
VWV DMS CL'RF VVWW 14"M*"WV 2233
V31 BORD NL*ST V 34"D'C"W2 4333
VVWW SCOTT NL*ST V 2"AR"WV 4333
V >ON'S MN8'R VW4 31"AR"W2 1V
"""""""""" EEEEEEEEEE
23WV su$
V@ LL'N SL'SMN VW 13"B'"W2 233 433 43
VW LF' MN8'R VW4 32"M*"W2 1W3
V@ MRTIN SL'SMN VW 1W"S'A"W2 213 2@33
V33 >M'S CL'RF VW 34"D'C"W2 3
VW@@ TURN'R SL'SMN VW 3W"S'A"W2 233 3
V12 6RD SL'SMN VW 11"B'"W2 213 33
"""""""""" EEEEEEEEEE
@33 su$
""""""""""
su$ 131
EE T:NFQ EE
CL'R
This will !lea the e/isting %uffes o %ea& o !o$putations o !olu$ns fo$atting5
S7nta/0Clea o !le %uffe X %e X !o$p X !ol.
'/0
SQL- !lea %uffe
uffe !leaed
SQL- !lea %e
ea&s !leaed
SQL- !lea !o$p
88
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 89/215
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 90/215
Ceated file ss5sl
SQL- save ss epla!e
6ote file ss5sl
'+'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$plepo!
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
Spool on X off X out X ,'ile_name-5'/tension
'/0
SQL- spool on
SQL- sele!t E fo$ dept.
D'ATNO DNM' LOC
""""""""" """""""""""""" """"""""""
23 CCOUNTIN8 N'6 *ORF
13 R'S'RC: DLLS
43 SL'S C:IC8O
@3 OA'RTIONS OSTON
SQL- spool off
SQL- ed on5lst
SQL- sele!t E fo$ dept.
D'ATNO DNM' LOC
""""""""" """""""""""""" """"""""""
23 CCOUNTIN8 N'6 *ORF
13 R'S'RC: DLLS
43 SL'S C:IC8O @3 OA'RTIONS OSTON
SQL- spool off
LIST
This will give the !uent SQL state$ent5
S7nta/0
List o li !tart_line_number end_line_number
'/0
90
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 91/215
SQL- sele!t
1 E
4 fo$
@ dept.
SQL- list
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!e5S7nta/0
ppend o app , !trin"-
'/0
SQL- sele!t E
SQL- list
2E sele!t E
SQL- append fo$ dept
91
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 92/215
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 - ,end_line_number -
'/0
SQL- sele!t
1 E
4 fo$
@ dept
whee
deptno
V -23.
SQL- list
2 sele!t
1 E
4 fo$
@ dept
whee
deptno
VE -23
SQL- del 2
SQL- list
2 E
1 fo$
4 dept
@ whee
deptno
E -23SQL- del 1
SQL- list
2 E
1 dept
4 whee
@ deptno
E -23
92
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 93/215
SQL- del 1 @
SQL- list
2 E
1E -23
SQL- del
SQL- list
2 E
9RIL'
This will %e used to de!lae a vaia%le5
S7nta/0
9aia%le o va ,variable_name- ,variable_type-
'/0
SQL- va deptna$e va!ha(2)
SQL- sele!t dna$e into deptna$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 deptna$e
D'ATNM'
""""""""""""""
CCOUNTIN8
STRT
This will %e used to e/e!ute SQL s!ipts5
S7nta/0
stat ,filena$e _name-5sl
'/0
SQL- stat ss5sl
SQL- ss5sl "" this will e/e!ute sl s!ipt files onl75
:OSTThis will %e used to intea!t with the OS level fo$ SQL5
S7nta/0
:ost operation
'/0
SQL- host
SQL- host di
S:O6
93
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 94/215
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
aa7sie 2
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 NTI9'
!on!at b5b (he/ 1e)
!op7!o$$it 3
COA*T*A'C:'CF is ON
define b<b (he/ 1)
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''DCF ON fo o $oe ows
flagge OBB
flush ON
SQL- sho veif7
veif7 OBB
RUNThis will uns the !o$$and in the %uffe5
S7nta/0
Run X ?
'/0
SQL- un
SQL- ?
STOR'
94
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 95/215
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 96/215
S7nta/0
Define vaia%lena$e
'/0
SQL- define
D'BIN' DT' H b2"M*"3Vb (C:R)
D'BIN' CONN'CTID'NTIBI'R H boa!leb (C:R)
D'BIN' US'R H bSCOTTb (C:R)
D'BIN' ARI9IL'8' H bb (C:R)
D'BIN' SQLALUSR'L'S' H b2332333133b (C:R)
D'BIN' 'DITOR H bNotepadb (C:R)
D'BIN' O9'RSION H bOa!le Data%ase 23g 'ntepise 'dition Release
2352535153 \ Aodu!tion 6ith the Aatitioning# OLA and
Data Mining optionsb (C:R)
D'BIN' OR'L'S' H b2332333133b (C:R)
S'T COMMNDS
These !o$$ands does not euie 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 linesie5 Default linesie is W35
S7nta/0
Set linesie ,value-
'/0
SQL- set linesie 233
A8'SIZ'
This will %e used to set the pagesie5 Default pagesie is 2@5
S7nta/0
Set pagesie ,value-
'/0
SQL- set pagesie 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)
96
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 97/215
DNM' 9RC:R1(2@)
LOC 9RC:R1(24)
AUS'
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 pagesie 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''DCF
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 ows5
S7nta/0
Set feed%a!& ,value-
'/0
SQL- set feed%a!& @
SQL- sele!t E fo$ dept.
D'ATNO DNM' LOC
"""""""""" """""""""""""" """""""""""""
23 CCOUNTIN8 N'6 *ORF
13 R'S'RC: DLLS
43 SL'S C:IC8O
@3 OA'RTIONS 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: DLLS
43 SL'S C:IC8O
@3 OA'RTIONS OSTON
97
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 98/215
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
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
20044 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 DNM' LOC
"""""""""" """""""""""""" """""""""""""
23 CCOUNTIN8 N'6 *ORF
13 R'S'RC: DLLS
43 SL'S C:IC8O
@3 OA'RTIONS OSTON
'lapsed0 3303303353
SQLAROMAT
This will %e used to !hange the SQL po$pt5
S7nta/0
Set slpo$pt , prompt -'/0
SQL- set slpo$pt =ORCL'-=
ORCL'-
SQLCS'
This will %e used to !hange the !ase of the SQL state$ents5 7 default the !ase is $i/ed5
S7nta/0
Set sl!ase uppe X $i/ed X lowe
98
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 99/215
'/0
SQL- set sl!ase uppe
SQLT'RMINTOR
This will %e used to !hange the te$inato of the SQL state$ents5 7 default the te$inato is .5
S7nta/0
Set slte$inato ,termination_caracter -
'/0
SQL- set slte$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(3#=R<D=#=:*D=).
'nte value fo d0
old 20 inset into dept values(3#=R<D=#=:*D=)
new 20 INS'RT INTO D'AT 9LU'S(3#=R=#=:*D=)
SQL- set define off
SQL-inset into dept values(3#=R<D=#=:*D=). "" hee it won;t as& fo value
N'6A8'
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 eo value fo newpage does not podu!e eo %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 page5Most $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_car -
99
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 100/215
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 101/215
D'ATNO DNM' LOC
"""""""""" """"""""""""" """""""""""
13 R'S'RC: DLLS
ANO
This will give displa7s the page nu$%es5 7 default the value would %e eo5
'/0
SQL- !ol hiedate newvalue /toda7 nopint fo$at a2 tun!
SQL- ttitle left /toda7 ight =page= sl5pno
SQL- sele!t E fo$ e$p whee deptno H 23.
3">UN"W2 page 2
'MANO 'NM' >O M8R SL COMM D'ATNO
"""""""""" """""""""" """"""""""""""" """"""""" """"" """""""""" """"""""""
VVW1 CLRF MN8'R VW4 1@3 23
VW4 FIN8 AR'SID'NT 333 23
V4@ 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 TOC:R get a default width of a%out 233
!haa!tes5 7 !hanging the fo$at to a2 tun!# 7ou $ini$ie this effe!t5 N'69LU' insets
!ontents of the !olu$n etieved %7 the SQL state$ent into a vaia%le !alled /toda75
SA'CIL BIL'S
LO8IN5sl
If 7ou would li&e SQLALUS to define 7ou own envion$ental settings# put all the euied
!o$$ands in a file na$ed login5sl5 This is a spe!ial filena$e that SQLALUS alwa7s loo&s fo
wheneve it stats up5 If it finds login5sl# it e/e!utes an7 !o$$ands in it as if 7ou had enteed
then %7 hand5 *ou !an put an7 !o$$and in login5sl 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
8LO8IN5sl
This is used in the sa$e wa7s as LO8IN5sl %ut to esta%lish default SQLALUS settings fo all uses
of a data%ase5
IMAORTNT 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.
101
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 102/215
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.
) :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).
) 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 133 e$plo7ees[
SQL- Sele!t deptno#!ount(E) fo$ e$p goup %7 deptno having !ount(E) -133.
W) To eset the ti$e to the %eginning of the da7SQL- Sele!t to!ha(tun!(s7sdate)#;dd"$on"7777 hh0$i0ss a$;) fo$ dual.
) 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 ).
INTRODUCTION
C:RCT'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
/ptimied compiler
.To !hange the opti$ie settings fo the entie data%ase# set the data%ase paa$ete
ALSQLOATIMIZ'L'9'L5 9alid settings ae as follows
3 " No opti$iation
2 " Modeate opti$iation
1 " ggessive opti$iation
102
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 103/215
These settings ae also $odifia%le fo the !uent session5
SQL- alte session set plslopti$elevelH1.
Oa!le etains opti$ie 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 plslopti$ielevelH2.
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 cecin"5
To ena%le these wanings fo the entie data%ase# set the data%ase paa$ete
ALSQL6RNIN8S5 These settings ae also $odifia%le fo the !uent session5SQL- alte session set plslwanings H Kena%le0all;.
The a%ove !an %e a!hieved using the %uilt"in pa!&age DMS6RNIN85
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!euential collection! in /44.
5mproved datatype !upport.
6actrace an eception 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 DMSUTILIT*5BORMT'RRORCFTRC' 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 epre!!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'+ALIF'
R'8'+AINSTR
R'8'+ASUSTR
R'8'+AR'ALC'
7ro"rammer0de'ined uotin" mecani!m.
103
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 104/215
Stating with oa!le data%ase 23g elease 2# 7ou !an define 7ou own uoting
$e!hanis$ fo sting liteals in %oth SQL and AL?SQL5
Use the !haa!tes ;( followed %7 a single uote) to note the poga$$e"defined
delie$ete fo 7ou sting liteal5
'/0
D'CLR'
v va!ha(23) 0H =!o$pute=.
'8IN
d%$soutput5putline(=Ev H E= XX v).
d%$soutput5putline(=`v H `= XX v).
'ND.
Output0
v H !o$pute
v H !o$pute
8any new built0in paca"e!.
DMSSC:'DUL'R
Repesents a $aJo update to DMS>O5 DMSSC:'DUL'R povides $u!h i$poved
fun!tionalit7 fo s!heduling and e/e!uting Jo%s defined via stoed po!edues5
DMSCR*ATO
Offes the a%ilit7 to en!7pt and de!7pt !o$$on oa!le datat7pe# in!luding R6s# LOs#
and CLOs5 It also povides glo%aliation suppot fo en!7pting data a!oss diffeent
!haa!tesets5
DMSMONITOR Aovides an AI to !ontol additional ta!ing and statisti!s gatheing of sessions5
DMS6RNIN8
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
STNDRD ACF8'
Oa!le has defined in this spe!ial pa!&age5 Oa!le defines uite 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 STNDRD5
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 seuentiall7 o nested5
LOCF STRUCTUR'
De!lae
"" de!laative se!tion
egin
"" e/e!uta%le se!tion
104
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 105/215
'/!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%$soutput5putline(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
'/0
,,$7%loo!&--
'8IN
D%$soutput5putline(KM7 fist poga$;)0
'ND.
SUARO8RMS
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$s5Identifies !onsists of a lette# optionall7 followed %7 an7 seuen!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 uotation $a&s5
'/0
105
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 106/215
D'CLR'
bab nu$%e 0H .
bb nu$%e 0H .
'8IN
d%$soutput5putline(=a H = XX a).
d%$soutput5putline(= H = XX ).
'ND.
Output0
a H
H
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%$soutput5putline(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%$soutput5putline(Khello;). ?E sa$ple poga$ E?
'ND.
9RIL' D'CL'RTIONS
9aia%les !an %e de!laed in de!laative se!tion of the %lo!&.
'/0
D'CLR'
a nu$%e.
% nu$%e 0H .
! nu$%e default .
CONSTNT D'CL'RTIONS
To de!lae a !onstant# 7ou in!lude the CONSTNT &e7wod# and 7ou $ust suppl7 a default value5
'/0
D'CLR'
% !onstant nu$%e 0H .
106
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 107/215
! !onstant nu$%e default .
NOT NULL CLUS'
*ou !an also spe!if7 that the vaia%le $ust %e not null5
'/0
D'CLR'
% !onstant nu$%e not null0H . ! nu$%e not null default .
NC:OR'D D'CL'RTIONS
AL?SQL offes two &inds of a!hoing5
S!ala an!hoing
Re!od an!hoing
SCLR NC:ORIN8
Use the T*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'CLR'
dno dept5deptnot7pe.
Su%t7pe tnu$%e is nu$%e.
a tnu$%e.
Su%t7pe tsno is student5snot7pe.
9sno tsno.
R'CORD NC:ORIN8
Use the RO6T*A' atti%ute to define 7ou e!od stu!tue %ased on a ta%le5
'/0
cD'CLR'
9dept deptowt7pe.
'N'BITS OB NC:OR'D D'CLRTIONS
S7n!honiation with data%ase !olu$ns5
No$aliation of lo!al vaia%les5
ARO8RMM'R"D'BIN'D T*A'S
6ith the SUT*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
CONSTRIN'D SUT*A'
su%t7pe that esti!ts o !onstains the values no$all7 allowd %7 the datat7pe itself5
'/0
Su%t7pe positive is %ina7intege ange 25512@V@W4@V.
107
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 108/215
In the a%ove de!laation a vaia%le that is de!laed as positive !an stoe onl7 ingege geate
than eo even though %ina7intege anges fo$ "12@V@W4@V55G12@V@W4@V5
UNCONSTRIN'D SUT*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.
DTT*A' CON9'RSIONS
AL?SQL !an handle !onvesions %etween diffeent fa$ilies a$ong the datat7pes5
Convesion !an %e done in two wa7s5
'/pli!it !onvesion
I$pli!it !onvesion
'+ALICIT CON9'RSION
This !an %e done using the %uilt"in fun!tions availa%le5
IMALICIT CON9'RSION
AL?SQL will auto$ati!all7 !onvet %etween datat7pe fa$ilies when possi%le5
'/0
D'CLR'
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
9RIL' SCOA' ND 9ISIILIT*
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 ualif7 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'CLR'
a nu$%e. "" s!ope of a
'8IN
""""""""
D'CLR'
108
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 109/215
% nu$%e. "" s!ope of %
'8IN
"""""
'ND.
""""""
'ND.
'/10
D'CLR'
a nu$%e.
% nu$%e.
'8IN
"" a # % availa%le hee
D'CLR'
% !ha(23).
'8IN
"" a and !ha t7pe % is availa%le hee
'ND.
"""""
'ND.
'/40
,,$7%lo!&--
D'CLR'
a nu$%e.
% nu$%e.
'8IN
"" a # % availa%le hee
D'CLR'
% !ha(23).
'8IN
"" a and !ha t7pe % is availa%le hee
"" nu$%e t7pe % is availa%le using ,,$7%lo!&--5%
'ND.
""""""
'ND.
AL?SQL CONTROL STRUCTUR'S
AL?SQL has a vaiet7 of !ontol stu!tues that allow 7ou to !ontol the %ehaviou of the %lo!& as
it uns5 These stu!tues in!lude !onditional state$ents and loops5
If"then"else
Case
Case with no else
La%eled !ase
109
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 110/215
Sea!hed !ase
Si$ple loop
6hile loop
Bo loop
8oto and La%els
IB"T:'N"'LS'
S7nta/0
If ,condition1- then
$euence o' !tatement!;
'lsif ,condition1- then
$euence o' !tatement!;
'lse
$euence o' !tatement!;
'nd if.
'/0
D'CLR'
dno nu$%e(1).
'8IN
sele!t deptno into dno fo$ dept whee dna$e H =CCOUNTIN8=.
if dno H 23 then
d%$soutput5putline(=Lo!ation is N'6 *ORF=).
elsif dno H 13 then
d%$soutput5putline(=Lo!ation is DLLS=).
elsif dno H 43 then
d%$soutput5putline(=Lo!ation is C:IC8O=).
else
d%$soutput5putline(=Lo!ation is OSTON=).
end if.
'ND.
Output0Lo!ation is N'6 *ORF
CS'
S7nta/0
Case te!t0variable
6hen value1 then !euence o' !tatement!;
6hen value2 then !euence o' !tatement!;
110
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 111/215
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 112/215
when 43 then
d%$soutput5putline(=Lo!ation is C:IC8O=).
when @3 then
d%$soutput5putline(=Lo!ation is OSTON=).
end !ase.
'ND.
Output0
Lo!ation is N'6 *ORF
L'L'D CS'
S7nta/0
,,label --
Case te!t0variable
6hen value1 then !euence o' !tatement!;
6hen value2 then !euence o' !tatement!;
6hen valuen then !euence o' !tatement!;
'nd !ase.
'/0
D'CLR'
dno nu$%e(1).
'8IN
sele!t deptno into dno fo$ dept whee dna$e H =CCOUNTIN8=.
,,$7!ase--
!ase dno
when 23 then
d%$soutput5putline(=Lo!ation is N'6 *ORF=).
when 13 then
d%$soutput5putline(=Lo!ation is DLLS=).
when 43 then
d%$soutput5putline(=Lo!ation is C:IC8O=).
when @3 then
d%$soutput5putline(=Lo!ation is OSTON=).
end !ase $7!ase.
'ND.
Output0
Lo!ation is N'6 *ORF
S'RC:'D CS'
S7nta/0
Case
112
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 113/215
6hen ,condition1- then !euence o' !tatement!;
6hen ,condition2- then !euence o' !tatement!;
6hen ,conditionn- then !euence o' !tatement!;
'nd !ase.
'/0D'CLR'
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%$soutput5putline(=Lo!ation is N'6 *ORF=).
when dno H 13 then
d%$soutput5putline(=Lo!ation is DLLS=).
when dno H 43 then
d%$soutput5putline(=Lo!ation is C:IC8O=).
when dno H @3 then
d%$soutput5putline(=Lo!ation is OSTON=).
end !ase.
'ND.
Output0
Lo!ation is N'6 *ORF
SIMAL' LOOA
S7nta/0
Loop
$euence o' !tatement!;
'/it when ,condition-.
'nd loop.
In the s7nta/ e/it when ,condition- is euivalent to
If ,condition- then
'/it.
'nd if.
'/0
D'CLR'
i nu$%e 0H 2.
'8IN
loop
d%$soutput5putline(=i H = XX i).
113
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 114/215
i 0H i G 2.
e/it when i - .
end loop.
'ND.
Output0
i H 2i H 1
i H 4
i H @
i H
6:IL' LOOA
S7nta/0
6hile ,!ondition- loop
$euence o' !tatement!;
'nd loop.
'/0
D'CLR'
i nu$%e 0H 2.
'8IN
6hile i ,H loop
d%$soutput5putline(=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
BOR LOOA
S7nta/0
Bo ,loop_counter_variable- in low_bound 55i"_bound loop
$euence o' !tatement!;
'nd loop.
'/20
'8IN
Bo i in 255 loop
d%$soutput5putline(=i H = XX i).
114
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 115/215
end loop.
'ND.
Output0
i H 2
i H 1
i H 4i H @
i H
'/10
'8IN
Bo i in evese 255 loop
d%$soutput5putline(=i H = XX i).
end loop.
'ND.
Output0
i H
i H @
i H 4
i H 1
i H 2
NULL STT'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 additiona
state$ents5 Bo e/a$ple# I use a 8OTO state$ent to ui!&l7 $ove to the end of $7 poga$
if the state of $7 data indi!ates that no futhe po!essing is euied5 e!ause I do not
115
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 116/215
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 euied thee5 'ven though NULL deos
nothing# it is still an e/e!uta%le state$ent5
8OTO ND L'LS
S7nta/0
8oto label .
6hee label is a la%el defined in the AL?SQL %lo!&5 La%els ae en!losed in dou%le angle %a!&ets5
6hen a goto state$ent is evaluated# !ontol i$$ediatel7 passes to the state$ent identified %7
the la%el5
'/0
'8IN
Bo i in 255 loop
d%$soutput5putline(=i H = XX i).
if i H @ then
goto e/itloop.
end if.
end loop.
,,e/itloop--
Null.
'ND.
Output0
i H 2
i H 1i 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
AR8MS
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
AR8M in!truction_to_compiler 5
AL?SQL offes seveal pag$as0
UTONOMOUSTRNSCTION
116
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 117/215
'+C'ATIONINIT
R'STRICTR'B'R'NC'S
S'RILL*R'USL'
SUARO8RMS
AROC'DUR'S
po!edue is a $odule that pefo$s one o $oe a!tions5
S7nta/0
Ao!edue s!he$a5name ( parameter1 # parameter2 )
authid define X !uentuse is
"" de!laations
egin
"" e/e!uta%le state$ents
'/!eption
"" e/!eption handles
'nd na$e.
In the a%ove autid !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 s!he$a5name ( parameter1 # parameter2 )
Retun etundatat7pe authid define X !uentuse
dete$inisti!
paallelena%le is
"" de!laations
egin
"" e/e!uta%le state$ents
'/!eption
"" e/!eption handles
'nd na$e.
In the a%ove autid !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
-etermini!tic !lause defines# an opti$iation hint that lets the s7ste$ use a saved !op7 of the
fun!tion;s etun esult# if availa%le5 The uet7 opti$ie !an !hoose whethe to use the saved
!op7 o e"!all the fun!tion5
7arallel_enable !lause defines# an opti$iation hint that ena%les the fun!tion to %e e/e!uted in
paallel when !alled fo$ within S'L'CT state$ent5
117
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 118/215
ARM'T'R MOD'S
In (Default)
Out
In out
IN
In paa$ete will a!t as pl9!l con!tant 5
OUT
Out paa$ete will a!t as unintialied 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 initialied variable5
n a!tual paa$ete !oesponding to an in out fo$al paa$ete $ust %e a vaia%le5
D'BULT ARM'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 # % in nu$%e default # ! in nu$%e default V) \ valid
po!edue p(a in nu$%e# % in nu$%e default # ! in nu$%e default V) \ valild
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 # ! in nu$%e) \ invalild
po!edue p(a in nu$%e default # % in nu$%e default # ! in nu$%e) \ invalild
po!edue p(a in nu$%e default # % in nu$%e# ! in nu$%e) \ invalild
NOTTIONS
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
BORML ND CTUL ARM'T'RS
Aaa$etes whi!h ae in !alling su%poga$ ae actual parameter!5
118
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 119/215
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'ALC' AROC'DUR' SMAL'(a in nu$%e#% out nu$%e#! in out
nu$%e) is
'8IN
d%$soutput5putline(=fte !all=).
d%$soutput5putline(=a H = XX a XX= % H = XX % XX = ! H = XX !).
% 0H 23.
! 0H 13.
d%$soutput5putline(=fte assign$ent=).
d%$soutput5putline(=a H = XX a XX= % H = XX % XX = ! H = XX !).
'ND SMAL'.
D'CLR'
v2 nu$%e 0H @.
v1 nu$%e 0H .
v4 nu$%e 0H .
'8IN
d%$soutput5putline(=efoe !all=).
d%$soutput5putline(=v2 H = XX v2 XX = v1 H = XX v1 XX = v4 H = XX v4).
sa$ple(v2#v1#v4).
d%$soutput5putline(=fte !o$pletion of !all=).
d%$soutput5putline(=v2 H = XX v2 XX = v1 H = XX v1 XX = v4 H = XX v4).
'ND.
Output0
efoe !all
v2 H @ v1 H v4 H
fte !all
a H @ % H ! H
fte assign$enta H @ % H 23 ! H 13
fte !o$pletion of !all
v2 H @ v1 H 23 v4 H 13
'/10
CR'T' OR R'ALC' BUN(a in nu$%e#% out nu$%e#! in out nu$%e) etun
nu$%e IS
'8IN
119
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 120/215
d%$soutput5putline(=fte !all=).
d%$soutput5putline(=a H = XX a XX = % H = XX % XX = ! H = XX !).
d%$soutput5putline(=efoe assigne$ent Result H = XX (aEnvl(%#2)E!)).
% 0H .
! 0H V.
d%$soutput5putline(=fte assign$ent=).
d%$soutput5putline(=a H = XX a XX = % H = XX % XX = ! H = XX !).
etun (aE%E!).
'ND BUN.
D'CLR'
v2 nu$%e 0H 2.
v1 nu$%e 0H 1.
v4 nu$%e 0H 4.
v nu$%e.'8IN
d%$soutput5putline(=efoe !all=).
d%$soutput5putline(=v2 H = XX v2 XX = v1 H = XX v1 XX = v4 H = XX v4).
v 0H fun(v2#v1#v4).
d%$soutput5putline(=fte !all !o$pleted=).
d%$soutput5putline(=v2 H = XX v2 XX = v1 H = XX v1 XX = v4 H = XX v4).
d%$soutput5putline(=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 ! H V
fte !all !o$pletedv2 H 2 v1 H v4 H V
Result H 4
R'STRICTIONS ON BORML ARM'T'RS
7 de!laing with spe!ified sie in a!tual paa$etes5
7 de!laing fo$al paa$etes with t7pe spe!ifie5
USIN8 NOCOA*
120
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 121/215
:ocopy is a hint# not a !o$$and5 This $eans that the !o$pile $ight silentl7 de!ide that
it !an;t fulfill 7ou euest fo a nocopy paa$ete5
The !op7ing fo$ fo$al to a!tual !an %e esti!ted %7 issuing nocopy ualifie5
To pass the out and in out paa$etes %7 efeen!e use no!op7 ualifie5
'/0
CR'T' OR R'ALC' AROC'DUR' AROC(a in out no!op7 nu$%e) IS
'8IN
""""
'ND AROC.
CLL ND '+'C
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(ar"ument_li!t ) into o!t_variable.
The paantheses ae alwa7s euied# 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'ALC' AROC IS
'8IN
d%$soutput5putline(=hello wold=).'ND AROC.
Output0
SQL- !all po!().
hello wold
'/10
CR'T' OR R'ALC' AROC(a in nu$%e#% in nu$%e) IS
'8IN
d%$soutput5putline(=a H = XX a XX = % H = XX %).
'ND AROC.
Output0
SQL- !all po!(#).
a H % H
'/40
CR'T' OR R'ALC' BUNCTION BUN R'TURN 9RC:R IS
'8IN
121
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 122/215
etun =hello wold=.
'ND BUN.
Output0
SQL- vaia%le v va!ha(13)
SQL- !all fun() into 0v.
SQL- pint v hello wold
CLL * R'B'R'NC' ND CLL * 9LU'
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
SUARO8RMS O9'RLODIN8
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'CLR'
i t2 0H t2().
J t1 0H t1().
AROC'DUR' A($ t2) IS
'8IN
d%$soutput5putline(=a H = XX $5a).
'ND A.
AROC'DUR' A(n t1) IS
'8IN
d%$soutput5putline(=% H = XX n5%).
'ND A.
AROC'DUR' ARODUCT(a nu$%e#% nu$%e) IS
'8IN
d%$soutput5putline(=Aodu!t of a#% H = XX a E %).
'ND ARODUCT.
AROC'DUR' ARODUCT(a nu$%e#% nu$%e#! nu$%e) IS
'8IN
122
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 123/215
d%$soutput5putline(=Aodu!t of a#% H = XX a E % E !).
'ND ARODUCT.
'8IN
p(i).
p(J).
podu!t(@#).
podu!t(@##).
'ND.
Output0
a H
% H
Aodu!t of a#% H 13
Aodu!t of a#% H 213
'N'BITS OB O9'RLODIN8
Suppoting $an7 data !o$%inations Bitting the poga$ to the use5
R'STRICTIONS ON O9'RLODIN8
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
IMAORTNT AOINTS OUT SUARO8RMS 6hen a stoed su%poga$ is !eated# it is stoed in the data dictionary 5
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
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 poo
a!!oding to a least e!entl7 used (LRU) algoith$5
Su%poga$s !an %e local 5
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
123
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 124/215
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 125/215
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 126/215
Ti$esta$p Model
Signatue Model
TIM'STMA 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 ones# 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
SI8NTUR' MOD'L
6hen a po!edue is !eated# a signatue is stoed in the data di!tiona7 in addition to
the p"!ode5
The signatue en!odes the t7pes and ode of the paa$etes5
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'D'A'ND'NCI'SMOD' $ust %e
set to SI8NTUR'5 This is a paa$ete in the data%ase initialiation file5
T:R'' 6*S OB S'TTIN8 T:IS MOD'
dd the line R'MOT'D'A'ND'NCI'SMOD'HSI8NTUR' to the data%ase initialiation file5 The
ne/t ti$e the data%ase is stated# the $ode will %e set to SI8NTUR' fo all sessions5
lte s7ste$ set e$otedependen!ies$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$otedependen!ies$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
126
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 127/215
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
BOR6RD D'CL'RTION
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'CLR'
AROC'DUR' A2 IS
'8IN
d%$soutput5putline(=Bo$ po!edue p2=).
p1.
'ND A2.
AROC'DUR' A1 IS
'8IN d%$soutput5putline(=Bo$ po!edue p1=).
p4.
'ND A1.
AROC'DUR' A4 IS
'8IN
d%$soutput5putline(=Bo$ po!edue p4=).
'ND A4.
'8IN
p2.
'ND.
Output0
p1.
E
'RROR at line 0
OR"330 line # !olu$n 20
ALS"334240 =A1= not de!laed in this s!ope
OR"330 line # !olu$n 20
AL?SQL0 State$ent ignoed
OR"330 line 23# !olu$n 20
ALS"334240 =A4= not de!laed in this s!ope
OR"330 line 23# !olu$n 20
AL?SQL0 State$ent ignoed
'/10
D'CLR'
AROC'DUR' A1. "" fowad de!laation
AROC'DUR' A4.
127
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 128/215
AROC'DUR' A2 IS
'8IN
d%$soutput5putline(=Bo$ po!edue p2=).
p1.
'ND A2.
AROC'DUR' A1 IS
'8IN
d%$soutput5putline(=Bo$ po!edue p1=).
p4.
'ND A1.
AROC'DUR' A4 IS
'8IN
d%$soutput5putline(=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 SUARO8RMS
'+'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$ppo! then use gants e/e!ute pivilege onpo!edue to use with the following !o$$and5
SQL- 8ant e/e!ute on e$ppo! to use 5
Then use !an un the po!edue %7 issuing
SQL- '/e! use 5e$ppo!
use !eated the following po!edue
CR'T' OR R'ALC' AROC'DUR' A IS
!uso is sele!t Efo$ student2.
'8IN
fo v in ! loop
inset into student1 values(v5no#v5na$e#v5$a&s).
end loop.
'ND A.
use ganted e/e!ute pivilege to use using
SQL- gant e/e!ute on p to use
Then use e/e!uted the po!edue
SQL- '/e! use5p
128
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 129/215
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 AOAULT' US'R ;s TL'
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'ALC' AROC'DUR' A
UT:ID CURR'NTUS'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 use5
'/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 !uentuse of authid !lause# if 7ou use define then it will %e !alled define
po!edue5
STOR'D SUARO8RMS ND ROL'S
we have two uses sað and sudha in whi!h sað has student ta%le and sudha does not5
Sudha is going to !eate a po!edue %ased on student ta%le owned %7 sað5 efoe doing
this sað $ust gant the pe$issions on this ta%le to sudha5
SQL- !onn sað?sað
SQL- gant all on student to sudha.
then sudha !an !eate po!edue
SQL- !onn sudha?sudha
CR'T' OR R'ALC' AROC'DUR' A IS
!uso ! is sele!t Efo$ sað5student.
'8IN
fo v in ! loop
d%$soutput5putline(KNo H K XX v5no).
end loop.
129
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 130/215
'ND A.
hee po!edue will %e !eated5
If the sa$e pivilege was ganted though a ole it wont !eate the po!edue5
'/a$ine the following !ode
SQL- !onn sað?sað
SQL- !eate ole saðole.SQL- gant all on student to saðole.
SQL- gant saðole to sudha.
then !onn sudha?sudha
CR'T' OR R'ALC' AROC'DUR' A IS
!uso ! is sele!t Efo$ sað5student.
'8IN
fo v in ! loop
d%$soutput5putline(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
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
ACF8'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
ACF8' 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
130
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 131/215
egin
"" initialiation se!tion
'/!eption
"" '/!eption handling se!iton
'nd , paca"e_name-.
IMAORTNT AOIN8S OUT ACF8'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 initialiation needs to %e un the fist ti$e the pa!&age is instantiated
within a session5 This !an %e done %7 adding initialiation 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 S85
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 initialiation 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
COMAILIN8 ACF8'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.
ACF8' 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
ACF8' RUNTIM' STT'
Aa!&age unti$e state is diffe fo the following pa!&ages5
Seiall7 eusa%le pa!&ages
Non seiall7 eusa%le pa!&ages
S'RILL* R'USL' ACF8'S
131
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 132/215
To fo!e the oa!le to use seiall7 eusa%le vesion then in!lude AR8M S'RILL*R'USL' in
%oth pa!&age spe! and %od7# '/a$ine the following pa!&age5
CR'T' OR R'ALC' ACF8' AF8 IS
pag$a seiall7eusa%le.
po!edue e$ppo!.
'ND AF8.CR'T' OR R'ALC' ACF8' OD* AF8 IS
pag$a seiall7eusa%le.
!uso ! is sele!t ena$e fo$ e$p.
AROC'DUR' 'MAAROC IS
vena$e e$p5ena$et7pe.
vflag %oolean 0H tue.
vnu$ows nu$%e 0H 3.
'8IN
if not !isopen then
open !.
end if.
while vflag loop
fet!h ! into vena$e.
vnu$ows 0H vnu$ows G 2.
if vnu$ows H then
vflag 0H false.
end if.
d%$soutput5putline(='na$e H = XX vena$e).
end loop.
'ND 'MAAROC.
'ND AF8.
SQL- e/e! p&g5e$ppo!
'na$e H SMIT:
'na$e H LL'N
'na$e H 6RD
'na$e H >ON'S
'na$e H MRTIN
SQL- e/e! p&g5e$ppo!
'na$e H SMIT:
'na$e H LL'N
'na$e H 6RD
'na$e H >ON'S
'na$e H MRTIN
132
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 133/215
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'RILL * R'USL' ACF8'S
This is the default vesion used %7 the oa!le# e/a$ine the following pa!&age5CR'T' OR R'ALC' ACF8' AF8 IS
po!edue e$ppo!.
'ND AF8.
CR'T' OR R'ALC' ACF8' OD* AF8 IS
!uso ! is sele!t ena$e fo$ e$p.
AROC'DUR' 'MAAROC IS
vena$e e$p5ena$et7pe.
vflag %oolean 0H tue.
vnu$ows nu$%e 0H 3.'8IN
if not !isopen then
open !.
end if.
while vflag loop
fet!h ! into vena$e.
vnu$ows 0H vnu$ows G 2.
if vnu$ows H then
vflag 0H false.
end if.
d%$soutput5putline(='na$e H = XX vena$e).
end loop.
'ND 'MAAROC.
'ND AF8.
SQL- e/e! p&g5e$ppo!
'na$e H SMIT:
'na$e H LL'N
'na$e H 6RD
'na$e H >ON'S
'na$e H MRTIN
SQL- e/e! p&g5e$ppo!
'na$e H LF'
'na$e H CLRF
'na$e H SCOTT
'na$e H FIN8
133
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 134/215
'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
D'A'ND'NCI'S OB ACF8' RUNTIM' STT'
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'ALC' ACF8' AF8 IS
v nu$%e 0H .
po!edue p.
'ND AF8.
CR'T' OR R'ALC' ACF8' OD* AF8 IS
AROC'DUR' A IS'8IN
d%$soutput5putline(=v H = XX v).
v 0H 23.
d%$soutput5putline(=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
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
134
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 135/215
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$ents
: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 followingeuie$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'STRICTR'B'R'NC'S
Bo pa!&aged fun!tions# howeve# the R'STRICTR'B'R'NC'S pag$a is euied to spe!if7 the
puit7 level of a given fun!tion5
S7nta/0
AR8M R'STRICTR'B'R'NC'S( !ubpro"ram_name or paca"e_name# 6NDS #6NAS
#RNDS #RNAS).
'/0
CR'T' OR R'ALC' ACF8' AF8 IS
fun!tion fun2 etun va!ha.
pag$a esti!tefeen!es(fun2#wnds).
fun!tion fun1 etun va!ha.
pag$a esti!tefeen!es(fun1#wnds).
'ND AF8.
CR'T' OR R'ALC' ACF8' OD* AF8 IS
BUNCTION BUN2 etun va!ha IS
135
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 136/215
'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@10 Su%poga$ =BUN2= violates its asso!iated pag$a
ALS"33@10 Su%poga$ =BUN1= violates its asso!iated pag$a
CR'T' OR R'ALC' ACF8' 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'BULT
If thee is no R'STRICTR'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'BULT &e7wod is used instead of the su%poga$ na$e in the pag$a5
'/0
CR'T' OR R'ALC' ACF8' AF8 IS
pag$a esti!tefeen!es(default#wnds).
fun!tion fun2 etun va!ha.
fun!tion fun1 etun va!ha.
'ND AF8.
CR'T' OR R'ALC' ACF8' 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
136
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 137/215
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@10 Su%poga$ =BUN2= violates its asso!iated pag$a
ALS"33@10 Su%poga$ =BUN1= violates its asso!iated pag$a
CR'T' OR R'ALC' ACF8' 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
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'ALC' ACF8' AF8 IS
fun!tion fun2 etun va!ha.
pag$a esti!tefeen!es(fun2#wnds#tust).
fun!tion fun1 etun va!ha.
pag$a esti!tefeen!es(fun1#wnds#tust).
'ND AF8.
CR'T' OR R'ALC' ACF8' 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
137
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 138/215
IMAORTNT AOINTS OUT R'STRICTR'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 euied 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
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# thep"!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 DMSS:R'DAOOL 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 euest 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
DMSS:R'DAOOL has fou po!edues
F''A
UNF''A
SIZ'S
ORT'DR'QU'STT:R'S:OLD
F''A
The DMSS:R'DAOOL5F''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 "" Seuen!e
R "" Tigge
C "" SQL Cuso
T "" O%Je!t t7pe
>S "" >ava sou!e
>C "" >ava !lass
>R "" >ava esou!e
138
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 139/215
>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!ie nu$%e).
O%Je!ts with geate than the min!ie will %e etuned5 SIZ'S uses DMSOUTAUT to etun the
data5
ORT'DR'QU'STT:R'S:OLD
6hen the data%ase dete$ines that thee is not enough $e$o7 in the shaed pool to satisf7 a
given euest# 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'DR'QU'STT:R'S:OLD !an %e used to e$ed7 this5
S7nta/0
AROC'DUR' ORT'DR'QU'STT:R'S:OLD(tre!old_!ie nu$%e).
On!e this po!edue is !alled# oa!le will not stat aging o%Je!ts fo$ the pool unless at least
tre!old_!ie %7tes is needed5
DT MOD'L BOR SUARO8RMS ND ACF8'S
US'RO>'CTS
US'RSOURC'
US'R'RRORS
DO>'CTS
DSOURC'
D'RRORS
LLO>'CTS
LLSOURC'
LL'RRORS
CURSORS
Cur!or is a pointe to $e$o7 lo!ation whi!h is !alled as contet 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 ue75
Cuso !ontains two pats
:eade
139
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 140/215
od7
:eade in!ludes !uso na$e# an7 paa$etes and the t7pe of data %eing loaded5
od7 in!ludes the sele!t state$ent5
'/0
Cuso !(dno in nu$%e) etun deptowt7pe is sele!t Efo$ dept.
In the a%ove
:eade \ !uso !(dno in nu$%e) etun deptowt7pe
od7 \ sele!t Efo$ dept
CURSOR T*A'S
I$pli!it (SQL)
'/pli!it
Aaa$eteied !usos
R'B !usos
CURSOR ST8'S
Open
Bet!h
Close
CURSOR TTRIUT'S
found
notfound
ow!ount
isopen
%ul&ow!ount
%ul&e/!eptions
CURSOR D'CL'RTION
S7nta/0
Cuso ,cur!or_name- is !elect !tatement .
'/0
Cuso ! is sele!t Efo$ dept.
CURSOR LOOAS
Si$ple loop
6hile loop
Bo loop
SIMAL' LOOA
S7nta/0
Loop
Bet!h ,cur!or_name- into ,record_variable-.
140
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 141/215
'/it when ,cur!or_name- notfound.
, !tatement!-.
'nd loop.
'/0
D'CLR'
!uso ! is sele!t E fo$ student. vstud studentowt7pe.
'8IN
open !.
loop
fet!h ! into vstud.
e/it when !notfound.
d%$soutput5putline(=Na$e H = XX vstud5na$e).
end loop.
!lose !.
'ND.
Output0
Na$e H sað
Na$e H sinu
Na$e H satish
Na$e H sudha
6:IL' LOOA
S7nta/0
6hile ,cur!or_name- found loop
Bet!h ,cur!or_name- nto ,record_variable-.
, !tatement!-.
'nd loop.
'/0
D'CLR'
!uso ! is sele!t E fo$ student.
vstud studentowt7pe.
'8IN open !.
fet!h ! into vstud.
while !found loop
fet!h ! into vstud.
d%$soutput5putline(=Na$e H = XX vstud5na$e).
end loop.
!lose !.
'ND.
141
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 142/215
Output0
Na$e H sað
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'CLR'
!uso ! is sele!t E fo$ student.
'8IN
fo vstud in ! loop
d%$soutput5putline(=Na$e H = XX vstud5na$e).
end loop.
'ND.
Output0
Na$e H sað
Na$e H sinu
Na$e H satish
Na$e H sudha
ARM'TRIZ'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'CLR'
!uso !(dno in nu$%e) is sele!t E fo$ dept whee deptno H dno.
vdept deptowt7pe.
'8IN
open !(13).
loop
fet!h ! into vdept.
e/it when !notfound.
d%$soutput5putline(=Dna$e H = XX vdept5dna$e XX = Lo! H = XX vdept5lo!).
142
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 143/215
end loop.
!lose !.
'ND.
Output0
Dna$e H R'S'RC: Lo! H DLLS
ACF8'D CURSORS 6IT: :'D'R IN SA'C ND OD* IN ACF8' 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
'/20CR'T' OR R'ALC' ACF8' AF8 IS
!uso ! etun deptowt7pe is sele!t E fo$ dept.
po!edue po! is
'ND AF8.
CR'T' OR R'ALC' AFC8' OD* AF8 IS
!uso ! etun deptowt7pe is sele!t E fo$ dept.
AROC'DUR' AROC IS
'8IN
fo v in ! loop
d%$soutput5putline(=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 DLLS
Deptno H 43 Dna$e H SL'S Lo! H C:IC8O
Deptno H @3 Dna$e H OA'RTIONS Lo! H OSTON
'/10
CR'T' OR R'ALC' AFC8' OD* AF8 IS
!uso ! etun deptowt7pe is sele!t E fo$ dept whee deptno - 13.
AROC'DUR' AROC IS
'8IN
fo v in ! loop
143
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 144/215
d%$soutput5putline(=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 SL'S Lo! H C:IC8O
Deptno H @3 Dna$e H OA'RTIONS Lo! H OSTON
R'B CURSORS ND CURSOR 9RIL'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!or 5
Ref !uso with out etun t7pe is !alled wea cur!or 5
*ou !an de!lae ef !uso t7pe in pa!&age spe! as well as %od75
*ou !an de!lae ef !uso t7pes in lo!al su%poga$s o anon7$ous %lo!&s5
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 eualit7# ineualit7 and nullit75
'/0
CR'T' OR R'ALC' AROC'DUR' R'BCURSOR(TL'NM' IN 9RC:R) IS
t7pe t is ef !uso.
! t.
vdept deptowt7pe.
t7pe is e!od(ena$e e$p5ena$et7pe#Jo% e$p5Jo%t7pe#sal e$p5salt7pe).
ve$p .
vstud student5na$et7pe.
144
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 145/215
'8IN
if ta%lena$e H =D'AT= then
open ! fo sele!t E fo$ dept.
elsif ta%lena$e H ='MA= then
open ! fo sele!t ena$e#Jo%#sal fo$ e$p.
elsif ta%lena$e H =STUD'NT= thenopen ! fo sele!t na$e fo$ student.
end if.
loop
if ta%lena$e H =D'AT= then
fet!h ! into vdept.
e/it when !notfound.
d%$soutput5putline(=Deptno H = XX vdept5deptno XX = Dna$e H = XX
vdept5dna$e XX = Lo! H = XX vdept5lo!).
elsif ta%lena$e H ='MA= then
fet!h ! into ve$p.
e/it when !notfound.
d%$soutput5putline(='na$e H = XX ve$p5ena$e XX = >o% H = XX ve$p5Jo%
XX = Sal H = XX ve$p5sal).
elsif ta%lena$e H =STUD'NT= then
fet!h ! into vstud.
e/it when !notfound.
d%$soutput5putline(=Na$e H = XX vstud).
end if.
end loop.
!lose !.
'ND.
Output0
SQL- e/e! ef!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 DLLS
Deptno H 43 Dna$e H SL'S Lo! H C:IC8O
Deptno H @3 Dna$e H OA'RTIONS Lo! H OSTON
SQL- e/e! ef!uso(='MA=)
'na$e H SMIT: >o% H CL'RF Sal H W33
'na$e H LL'N >o% H SL'SMN Sal H 233
'na$e H 6RD >o% H SL'SMN Sal H 213
'na$e H >ON'S >o% H MN8'R Sal H 1V
'na$e H MRTIN >o% H SL'SMN Sal H 213
145
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 146/215
'na$e H LF' >o% H MN8'R Sal H 1W3
'na$e H CLRF >o% H MN8'R Sal H 1@3
'na$e H SCOTT >o% H NL*ST Sal H 4333
'na$e H FIN8 >o% H AR'SID'NT Sal H 333
'na$e H TURN'R >o% H SL'SMN Sal H 233
'na$e H DMS >o% H CL'RF Sal H 2233
'na$e H >M'S >o% H CL'RF Sal H 3
'na$e H BORD >o% H NL*ST Sal H 4333
'na$e H MILL'R >o% H CL'RF Sal H 2433
SQL- e/e! ef!uso(=STUD'NT=)
Na$e H sað
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'CLR'
!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.
!2 t.
!1 t.
146
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 147/215
v2 e$p5ena$et7pe.
v1 dept5dna$et7pe.
'8IN
open !.
loop
fet!h !2 into v2. e/it when !2notfound.
fet!h !1 into v1.
e/it when !1notfound.
d%$soutput5putline(='na$e H = XX v2 XX = Dna$e H = XX v1).
end loop.
end loop.
!lose !.
'ND.
CURSOR CLUS'S
Retun
Bo update
6hee !uent of
ul& !olle!t
R'TURN
Cuso ! etun deptowt7pe is sele!t Efo$ dept.
O
Cuso !2 is sele!t Efo$ dept.
Cuso ! etun !2owt7pe is sele!t Efo$ dept.
O
T7pe t is e!od(deptno dept5deptnot7pe# dna$e dept5dna$et7pe).
Cuso ! etun t is sele!t deptno# dna$e fo$ dept.
BOR UADT' 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 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 UADT' !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 BOR UADT' 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
147
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 148/215
S'L'CTBOR UADT' will hang until the othe session eleases the lo!&5 To handle this situation#
the NO6IT !lause is availa%le5
S7nta/0
Sele!t fo$ fo update of !olu$nna$e wait n.
If the !uso is de!laed with the BOR UADT' !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'CLR'
!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?sl and sl engines5
Redu!es the ovehead of etieving data5
*ou !an use %ul& !olle!t in %oth d7na$i! and stati! sl5
*ou !an use %ul& !olle!t in sele!t# fet!h into and etuning into !lauses5
SQL engine auto$ati!all7 initialies 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 ue75
*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!tion5ULF COLL'CT IN B'TC:
'/0
D'CLR'
T7pe t is ta%le of deptowt7pe.
nt t.
Cuso ! is sele!t Efo$ dept.
'8IN
Open !.
148
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 149/215
Bet!h ! %ul& !olle!t into nt.
Close !.
Bo i in nt5fist55nt5last loop
d%$soutput5putline(=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 DLLS
Dna$e H SL'S Lo! H C:IC8O
Dna$e H OA'RTIONS Lo! H OSTON
ULF COLL'CT IN S'L'CT
'/0
D'CLR'
T7pe t is ta%le of deptowt7pe.
Nt t.
'8IN
Sele!t E %ul& !olle!t into nt fo$ dept.
fo i in nt5fist55nt5last loop
d%$soutput5putline(=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 DLLS
Dna$e H SL'S Lo! H C:IC8O
Dna$e H OA'RTIONS 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'CLR'
T7pe t is ta%le of deptowt7pe.
nt t.
Cuso ! is sele!t Efo$ dept.
'8IN
Open !.
Bet!h ! %ul& !olle!t into nt li$it 1.
Close !.
149
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 150/215
Bo i in nt5fist55nt5last loop
d%$soutput5putline(=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 DLLS
MULTIAL' B'TC:'S IN INTO CLUS'
'/20
D'CLR'
T7pe t is ta%le of dept5dna$et7pe.
nt t.
T7pe t2 is ta%le of dept5lo!t7pe.
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%$soutput5putline(=Dna$e H = XX nt(i)).
end loop.
Bo i in nt25fist55nt25last loop
d%$soutput5putline(=Lo! H = XX nt2(i)).
end loop.
'ND.
Output0
Dna$e H CCOUNTIN8
Dna$e H R'S'RC:
Dna$e H SL'S
Dna$e H OA'RTIONSLo! H N'6 *ORF
Lo! H DLLS
Lo! H C:IC8O
Lo! H OSTON
'/10
D'CLR'
t7pe t is ta%le of dept5dna$et7pe.
t7pe t2 is ta%le of dept5lo!t7pe.
150
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 151/215
nt t.
nt2 t2.
'8IN
Sele!t dna$e#lo! %ul& !olle!t into nt#nt2 fo$ dept.
fo i in nt5fist55nt5last loop
d%$soutput5putline(=Dna$e H = XX nt(i)). end loop.
fo i in nt25fist55nt25last loop
d%$soutput5putline(=Lo! H = XX nt2(i)).
end loop.
'ND.
Output0
Dna$e H CCOUNTIN8
Dna$e H R'S'RC:
Dna$e H SL'S
Dna$e H OA'RTIONS
Lo! H N'6 *ORF
Lo! H DLLS
Lo! H C:IC8O
Lo! H OSTON
R'TURNIN8 CLUS' 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'CLR'
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 studentowt7pe.
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%$soutput5putline(=Ma&s H = XX nt1(v)).
end loop.
'ND.
151
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 152/215
Output0
Ma&s H 233
Ma&s H 133
Ma&s H 433
Ma&s H @33
AOINTS TO R'M'M'R
Cuso na$e !an %e up to 43 !haa!tes in length5
Cusos de!laed in anon7$ous %lo!&s o su%poga$s !loses auto$ati!all7 when that
%lo!& te$inates e/e!ution5
%ul&ow!ount and %ul&e/!eptions !an %e used onl7 with foall !onstu!t5
Cuso de!laations $a7 have e/pessions with !olu$n aliases5
These e/pessions ae !alled vitual !olu$ns o !al!ulated !olu$ns5
SQL IN AL?SQL
The onl7 state$ents allowed die!tl7 in pl?sl 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*NMIC SQL
If 7ou use DDL in pl?sl it validates the pe$issions and e/isten!e if euies 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!hniues ae availa%le fo D7na$i! SQL5
Native D7na$i! SQL
DMSSQL pa!&age
152
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 153/215
USIN8 NTI9' D*NMIC SQL
USIN8 '+'CUT' IMM'DIT'
'/0
'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'DIT' 6IT: AL?SQL 9RIL'S
'/0
D'CLR'
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'DIT' 6IT: IND 9RIL'S ND USIN8 CLUS'
'/0
D'CLR'
v va!ha(233).
'8IN
v 0H =inset into student values(0v2#0v1#0v4)=.
e/e!ute i$$ediate v using #=f=#33.
'ND.
'+'CUTIN8 QU'RI'S 6IT: OA'N BOR ND USIN8 CLUS'
'/0
CR'T' OR R'ALC' 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 studentowt7pe.
'8IN
open ! fo s using s$a&s.
loop
fet!h ! into v.
e/it when !notfound.
d%$soutput5putline(=Student Ma&s H = XX v5$a&s).
end loop.
!lose !.
'ND.
Output0
153
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 154/215
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'DIT'
'/0 D'CLR'
dna$e dept5dna$et7pe.
l! dept5lo!t7pe.
v va!ha(233).
'8IN
v 0H =sele!t dna$e fo$ dept whee deptno H 23=.
e/e!ute i$$ediate v into dna$e.
d%$soutput5putline(=Dna$e H =XX dna$e).
v 0H =sele!t lo! fo$ dept whee dna$e H 0dn=.
e/e!ute i$$ediate v into l! using dna$e.
d%$soutput5putline(=Lo! H = XX l!).
'ND.
Output0
Dna$e H CCOUNTIN8
Lo! H N'6 *ORF
9RIL' NM'S
'/0
D'CLR'
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
,,$7%lo!&--
D'CLR'
Ma&s nu$%e(4) 0H 233.
'8IN
Delete student whee $a&s H $7%lo!&5$a&s. "" delete ows whi!h has
"" a $a&s of 233
'ND.
8'TTIN8 DT INTO AL?SQL 9RIL'S
'/0
D'CLR'
154
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 155/215
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'ALC' AROC'DUR' A(sow in studentowt7pe) IS
'8IN
inset into student values sow.
'ND A.
D'CLR'
s studentowt7pe.
'8IN
s5no 0H 22.
s5na$e 0H =aa=.
s5$a&s 0H 233.
p(s).
'ND.
R'CORD S'D INS'RTS
'/0
D'CLR'
sow studentowt7pe.
'8IN
sow5no 0H V.
sow5na$e 0H =!!=.
sow5$a&s 0H 33.
inset into student values sow.
'ND.
R'CORD S'D UADT'S
'/0
D'CLR'
sow studentowt7pe.
'8IN
sow5no 0H .
sow5na$e 0H =!!=.
sow5$a&s 0H 33.
update student set owHsow whee no H sow5no.
'ND.
USIN8 R'CORDS 6IT: R'TURNIN8 CLUS'
'/0
D'CLR'
155
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 156/215
sow studentowt7pe.
setun studentowt7pe.
'8IN
sow5no 0H W.
sow5na$e 0H =dd=.
sow5$a&s 0H 33. inset into student values sow etuning no#na$e#$a&s into setun.
d%$soutput5putline(=No H = XX setun5no).
d%$soutput5putline(=No H = XX setun5na$e).
d%$soutput5putline(=No H = XX setun5$a&s).
'ND.
Output0
No H W
No H dd
No H 33
USIN8 DMSSQL ACF8'
DMSSQL 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 DMSSQL pa!&age
allows 7ou to die!tl7 !ontol the po!essing of a state$ent within a !uso# with opeations
su!h as opening and !losing a !uso# pasing a state$ent# %inding input vaia%le# and
defining output vaia%les5
'/20
D'CLR' !usoid nu$%e.
flag nu$%e.
vst$t va!ha(3).
'8IN
!usoid 0H d%$ssl5open!uso.
vst$t 0H =!eate ta%le stud(sno nu$%e(1)#sna$e va!ha(23))=.
d%$ssl5pase(!usoid#vst$t#d%$ssl5native).
flag 0H d%$ssl5e/e!ute(!usoid).
d%$ssl5!lose!uso(!usoid).
d%$soutput5putline(=Ta%le !eated=).
'ND.
Output0
Ta%le !eated
SQL- des! stud
Na$e Null[ T7pe
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" """""""" """"""""
156
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 157/215
SNO NUM'R(1)
SNM' 9RC:R1(23)
'/10
CR'T' OR R'ALC' AROC'DUR' DMSSQLAROC(v2 student5not7pe#
v1 student5$a&st7pe) is
!usoid nu$%e.
flag nu$%e.
vupdate va!ha(3).
'8IN
!usoid 0H d%$ssl5open!uso.
vupdate 0H =update student set $a&s H 0s$a&s whee no H 0sno=.
d%$ssl5pase(!usoid#vupdate#d%$ssl5native).
d%$ssl5%indvaia%le(!usoid#=0sno=#v2).
d%$ssl5%indvaia%le(!usoid#=0s$a&s=#v1).
flag 0H d%$ssl5e/e!ute(!usoid).
d%$ssl5!lose!uso(!usoid).
'ND DMSSQLAROC.
Output0
SQL- sele!t E fo$ student. "" %efoe e/e!ution
NO N MRFS
"""" """""" "" """"""""""
2 a 233
1 % 133 4 ! 433
SQL- e/e! d%$sslpo!(1#111)
SQL- sele!t E fo$ student. "" afte e/e!ution
NO N MRFS
"""" """""" "" """"""""""
2 a 233
1 % 111
4 ! 433
BORLL STT'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/va in
4ower_bound 55upper_bound X
Indi!es of indein"_collection X
9alues of indein"_collection
157
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 158/215
SQL state$ent.
BORLL 6IT: NON"S'QU'NTIL RR*S
'/0
D'CLR'
t7pe t is ta%le of student5not7pe inde/ %7 %ina7intege.
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 33 whee no H i%t(i).
'ND.
The a%ove poga$ will give eo li&e Kele$ent at inde/ 1 does not e/ists5
*ou !an e!tif7 it in one of the two following wa7s5
US88' 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'CLR'
t7pe t is ta%le of student5not7pe inde/ %7 %ina7intege.
i%t t.
t7pe t2 is ta%le of %oolean inde/ %7 %ina7intege.
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 33 whee no H i%t(i).
'ND.
Ouput0
SQL- sele!t E fo$ student "" %efoe e/e!ution
NO N MRFS
"""""""""" """"""""""""
2 a 233
1 % 133
4 ! 433
158
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 159/215
SQL- sele!t E fo$ student "" afte e/e!ution
NO N MRFS
"""""""""" """"""""""""
2 a 33
1 % 33
4 ! 33US88' OB 9LU'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 BORLL state$ent5
'/0
D'CLR'
t7pe t is ta%le of student5not7pe inde/ %7 %ina7intege.
i%t t.
t7pe t2 is ta%le of plsintege inde/ %7 %ina7intege.
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(2) 0H 23.
i%t2(2W) 0H 233.
foall i in values of i%t2 update student set $a&s H V whee no H i%t(i).
'ND.
Ouput0
SQL- sele!t E fo$ student "" %efoe e/e!ution
NO N MRFS
"""""""""" """"""""""""
2 a 233
1 % 133
4 ! 433
SQL- sele!t E fo$ student "" afte e/e!ution
NO N MRFS
"""""""""" """"""""""""
2 a 33
1 % 33
4 ! 33
AOINTS OUT ULF INDS
159
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 160/215
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 CLUS'
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 RDMS &enel5
COLL'CTIONS
Colle!tions ae also !o$posite t7pes# in that the7 allow 7ou to teat seveal vaia%les as a unit5
!olle!tion !o$%ines vaia%les of the sa$e t7pe5
T*A'S
9aa7s
Nested ta%les
Inde/ " %7 ta%les (sso!iate aa7s)
9RR*S
vaa7 is datat7pe ve7 si$ila to an aa75 vaa7 has a fi/ed li$it on its sie# 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$ sie of the vaa7 is 1 giga %7tes5
S7nta/0
T7pe ,type_name is vaa7 X va7ing aa7 (,limit) of ,element_type.
'/20
D'CLR'
t7pe t is vaa7(23) of va!ha(1).
va t 0H t(=a=#=%=#=!=#=d=).
flag %oolean.
'8IN
d%$soutput5putline(=Li$it H = XX va5li$it).
d%$soutput5putline(=Count H = XX va5!ount).
d%$soutput5putline(=Bist Inde/ H = XX va5fist).
d%$soutput5putline(=Last Inde/ H = XX va5last).
d%$soutput5putline(=Ne/t Inde/ H = XX va5ne/t(1)).
d%$soutput5putline(=Aevious Inde/ H = XX va5pio(4)).
d%$soutput5putline(=9RR* 'L'M'NTS=).
fo i in va5fist55va5last loop
160
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 161/215
d%$soutput5putline(=va= XX i XX = H = XX va(i)).
end loop.
flag 0H va5e/ists(4).
if flag H tue then
d%$soutput5putline(=Inde/ 4 e/ists with an ele$ent = XX va(4)).
else
d%$soutput5putline(=Inde/ 4 does not e/ists=).
end if.
va5e/tend.
d%$soutput5putline(=fte e/tend of one inde/# Count H = XX va5!ount).
flag 0H va5e/ists().
if flag H tue then
d%$soutput5putline(=Inde/ e/ists with an ele$ent = XX va()).
else
d%$soutput5putline(=Inde/ does not e/ists=).
end if.
flag 0H va5e/ists().
if flag H tue then
d%$soutput5putline(=Inde/ e/ists with an ele$ent = XX va()).
else
d%$soutput5putline(=Inde/ does not e/ists=).
end if.
va5e/tend(1).
d%$soutput5putline(=fte e/tend of two inde/es# Count H = XX va5!ount).
d%$soutput5putline(=9RR* 'L'M'NTS=).
fo i in va5fist55va5last loop
d%$soutput5putline(=va= XX i XX = H = XX va(i)).
end loop.
va() 0H =e=.
va() 0H =f=.
va(V) 0H =g=. d%$soutput5putline(=BT'R SSIN8NIN8 9LU'S TO '+T'ND'D 'L'M'NTS#
9RR* 'L'M'NTS=).
fo i in va5fist55va5last loop
d%$soutput5putline(=va= XX i XX = H = XX va(i)).
end loop.
va5e/tend(4#1).
d%$soutput5putline(=fte e/tend of thee inde/es# Count H = XX va5!ount).
161
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 162/215
d%$soutput5putline(=9RR* 'L'M'NTS=).
fo i in va5fist55va5last loop
d%$soutput5putline(=va= XX i XX = H = XX va(i)).
end loop.
va5ti$.
d%$soutput5putline(=fte ti$ of one inde/# Count H = XX va5!ount).
va5ti$(4).
d%$soutput5putline(=fte ti$ of thee inde/s# Count H = XX va5!ount).
d%$soutput5putline(=BT'R TRIM# 9RR* 'L'M'NTS=).
fo i in va5fist55va5last loop
d%$soutput5putline(=va= XX i XX = H = XX va(i)).
end loop.
va5delete.
d%$soutput5putline(=fte delete of entie vaa7# Count H = XX va5!ount).
'ND.
Output0
Li$it H 23
Count H @
Bist Inde/ H 2
Last Inde/ H @
Ne/t Inde/ H 4
Aevious Inde/ H 1
9RR* 'L'M'NTS
va2 H a
va1 H %
va4 H !
va@ H d
Inde/ 4 e/ists with an ele$ent !
fte e/tend of one inde/# Count H
Inde/ e/ists with an ele$ent
Inde/ does not e/ists
fte e/tend of two inde/es# Count H V
9RR* 'L'M'NTS
va2 H a
va1 H %
va4 H !
va@ H d
va H
162
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 163/215
va H
vaV H
BT'R SSIN8NIN8 9LU'S TO '+T'ND'D 'L'M'NTS# 9RR* 'L'M'NTS
va2 H a
va1 H %
va4 H !
va@ H d
va H e
va H f
vaV H g
fte e/tend of thee inde/es# Count H 23
9RR* 'L'M'NTS
va2 H a
va1 H %
va4 H !
va@ H d
va H e
va H f
vaV H g
vaW H %
va H %
va23 H %
fte ti$ of one inde/# Count H
fte ti$ of thee inde/s# Count H
BT'R TRIM# 9RR* 'L'M'NTS
va2 H a
va1 H %
va4 H !
va@ H d
va H e
va H f fte delete of entie vaa7# Count H 3
'/10
D'CLR'
t7pe t is vaa7(@) of studentowt7pe.
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.
163
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 164/215
d%$soutput5putline(=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ð
Sno H 1 Sna$e H sinuSno H 4 Sna$e H div7a
Sno H @ Sna$e H $anogni
'/40
D'CLR'
t7pe t is vaa7(@) of student5s$a&st7pe.
va t 0H t(null#null#null#null).
'8IN
fo i in 255va5!ount loop
sele!t s$a&s into va(i) fo$ student whee sno H i.
d%$soutput5putline(=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'CLR'
t7pe is e!od(!2 student5sna$et7pe#!1 student5s$a&st7pe).
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%$soutput5putline(=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ð 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
'/0
164
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 165/215
D'CLR'
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%$soutput5putline(=:no H = XX va(i)5hno XX = Cit7 H = XX va(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
'/0
D'CLR'
t7pe t is vaa7() of va!ha(1).
va2 t.
va1 t 0H t().
'8IN
if va2 is null then
d%$soutput5putline(=va2 is null=).
else
d%$soutput5putline(=va2 is not null=).
end if.
if va1 is null then
d%$soutput5putline(=va1 is null=).
else
d%$soutput5putline(=va1 is not null=).
end if.
'ND.
Output0
va2 is null
va1 is not null
N'ST'D TL'S
nested ta%le is thought of a data%ase ta%le whi!h has no li$it on its sie5 'le$ents ae
inseted into nested ta%le stating at inde/ 25 The $a/i$u$ sie of the vaa7 is 1 giga %7tes5
S7nta/0
T7pe ,type_name is ta%le of ,table_type.
165
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 166/215
'/20
D'CLR'
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%$soutput5putline(=No li$it to Nested Ta%les=).
else
d%$soutput5putline(=Li$it H = XX nt5li$it).
end if.
d%$soutput5putline(=Count H = XX nt5!ount).
d%$soutput5putline(=Bist Inde/ H = XX nt5fist).
d%$soutput5putline(=Last Inde/ H = XX nt5last).
d%$soutput5putline(=Ne/t Inde/ H = XX nt5ne/t(1)).
d%$soutput5putline(=Aevious Inde/ H = XX nt5pio(4)).
d%$soutput5putline(=N'ST'D TL' 'L'M'NTS=).
fo i in 255nt5!ount loop
d%$soutput5putline(=nt= XX i XX = H = XX nt(i)).
end loop.
flag 0H nt5e/ists(4).
if flag H tue then
d%$soutput5putline(=Inde/ 4 e/ists with an ele$ent = XX nt(4)). else
d%$soutput5putline(=Inde/ 4 does not e/ists=).
end if.
nt5e/tend.
d%$soutput5putline(=fte e/tend of one inde/# Count H = XX nt5!ount).
flag 0H nt5e/ists().
if flag H tue then
d%$soutput5putline(=Inde/ e/ists with an ele$ent = XX nt()).
else
d%$soutput5putline(=Inde/ does not e/ists=).
end if.
flag 0H nt5e/ists().
if flag H tue then
d%$soutput5putline(=Inde/ e/ists with an ele$ent = XX nt()).
else
d%$soutput5putline(=Inde/ does not e/ists=).
166
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 167/215
end if.
nt5e/tend(1).
d%$soutput5putline(=fte e/tend of two inde/es# Count H = XX nt5!ount).
d%$soutput5putline(=N'ST'D TL' 'L'M'NTS=).
fo i in 255nt5!ount loop
d%$soutput5putline(=nt= XX i XX = H = XX nt(i)).
end loop.
nt() 0H =e=.
nt() 0H =f=.
nt(V) 0H =g=.
d%$soutput5putline(=BT'R SSIN8NIN8 9LU'S TO '+T'ND'D 'L'M'NTS# N'ST'D
TL' 'L'M'NTS=).
fo i in 255nt5!ount loop
d%$soutput5putline(=nt= XX i XX = H = XX nt(i)).
end loop.
nt5e/tend(#1).
d%$soutput5putline(=fte e/tend of five inde/es# Count H = XX nt5!ount).
d%$soutput5putline(=N'ST'D TL' 'L'M'NTS=).
fo i in 255nt5!ount loop
d%$soutput5putline(=nt= XX i XX = H = XX nt(i)).
end loop.
nt5ti$.
d%$soutput5putline(=fte ti$ of one inde/# Count H = XX nt5!ount).
nt5ti$(4).
d%$soutput5putline(=fte ti$ of thee inde/s# Count H = XX nt5!ount).
d%$soutput5putline(=BT'R TRIM# N'ST'D TL' 'L'M'NTS=).
fo i in 255nt5!ount loop
d%$soutput5putline(=nt= XX i XX = H = XX nt(i)).
end loop.
nt5delete(2).
d%$soutput5putline(=fte delete of fist inde/# Count H = XX nt5!ount).
d%$soutput5putline(=N'ST'D TL' 'L'M'NTS=).
fo i in 155nt5!ountG2 loop
d%$soutput5putline(=nt= XX i XX = H = XX nt(i)).
end loop.
nt5delete(@).
d%$soutput5putline(=fte delete of fouth inde/# Count H = XX nt5!ount).
d%$soutput5putline(=N'ST'D TL' 'L'M'NTS=).
167
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 168/215
fo i in 1554 loop
d%$soutput5putline(=nt= XX i XX = H = XX nt(i)).
end loop.
fo i in 55nt5!ountG1 loop
d%$soutput5putline(=nt= XX i XX = H = XX nt(i)).
end loop.
nt5delete.
d%$soutput5putline(=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 TL' 'L'M'NTS
nt2 H a
nt1 H %
nt4 H !
nt@ H d
Inde/ 4 e/ists with an ele$ent !
fte e/tend of one inde/# Count H
Inde/ e/ists with an ele$ent
Inde/ does not e/ists
fte e/tend of two inde/es# Count H V
N'ST'D TL' 'L'M'NTS
nt2 H a
nt1 H %
nt4 H !
nt@ H d
nt H
nt H
ntV H
BT'R SSIN8NIN8 9LU'S TO '+T'ND'D 'L'M'NTS# N'ST'D TL' 'L'M'NTS
nt2 H a
nt1 H %
168
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 169/215
nt4 H !
nt@ H d
nt H e
nt H f
ntV H g
fte e/tend of five inde/es# Count H 21
N'ST'D TL' 'L'M'NTS
nt2 H a
nt1 H %
nt4 H !
nt@ H d
nt H e
nt H f
ntV H g
ntW H %
nt H %
nt23 H %
nt22 H %
nt21 H %
fte ti$ of one inde/# Count H 22
fte ti$ of thee inde/s# Count H W
BT'R TRIM# N'ST'D TL' 'L'M'NTS
nt2 H a
nt1 H %
nt4 H !
nt@ H d
nt H e
nt H f
ntV H g
ntW H %
fte delete of fist inde/# Count H VN'ST'D TL' 'L'M'NTS
nt1 H %
nt4 H !
nt@ H d
nt H e
nt H f
ntV H g
169
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 170/215
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 171/215
S$a&s H @33
'/@0
D'CLR'
t7pe is e!od(!2 student5sna$et7pe#!1 student5s$a&st7pe).
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%$soutput5putline(=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ð 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
'/0
D'CLR'
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%$soutput5putline(=: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
'/0
D'CLR'
t7pe t is vaa7() of va!ha(1).
nt2 t.
nt1 t 0H t().
'8IN
if nt2 is null then
171
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 172/215
d%$soutput5putline(=nt2 is null=).
else
d%$soutput5putline(=nt2 is not null=).
end if.
if nt1 is null then
d%$soutput5putline(=nt1 is null=).
else
d%$soutput5putline(=nt1 is not null=).
end if.
'ND.
Output0
nt2 is null
nt1 is not null
S'T OA'RTIONS IN N'ST'D TL'S
*ou !an pefo$ set opeations in the nested ta%les5 *ou !an also pefo$ eualit7
!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'CLR'
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=).
nt t.
'8IN
nt 0H set(nt2).
d%$soutput5putline(=N'ST'D TL' 'L'M'NTS=).
fo i in nt5fist55nt5last loop
d%$soutput5putline(=nt = XX i XX = H = XX nt(i)).
end loop.
nt 0H set(nt4).
d%$soutput5putline(=N'ST'D TL' 'L'M'NTS=).
fo i in nt5fist55nt5last loop
d%$soutput5putline(=nt = XX i XX = H = XX nt(i)).
172
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 173/215
end loop.
nt 0H nt2 $ultiset union nt@.
d%$soutput5putline(=N'ST'D TL' 'L'M'NTS=).
fo i in nt5fist55nt5last loop
d%$soutput5putline(=nt = XX i XX = H = XX nt(i)).
end loop.
nt 0H nt2 $ultiset union nt4.
d%$soutput5putline(=N'ST'D TL' 'L'M'NTS=).
fo i in nt5fist55nt5last loop
d%$soutput5putline(=nt = XX i XX = H = XX nt(i)).
end loop.
nt 0H nt2 $ultiset union distin!t nt4.
d%$soutput5putline(=N'ST'D TL' 'L'M'NTS=).
fo i in nt5fist55nt5last loop
d%$soutput5putline(=nt = XX i XX = H = XX nt(i)).
end loop.
nt 0H nt2 $ultiset e/!ept nt@.
d%$soutput5putline(=N'ST'D TL' 'L'M'NTS=).
fo i in nt5fist55nt5last loop
d%$soutput5putline(=nt = XX i XX = H = XX nt(i)).
end loop.
nt 0H nt@ $ultiset e/!ept nt2.
d%$soutput5putline(=N'ST'D TL' 'L'M'NTS=).
fo i in nt5fist55nt5last loop
d%$soutput5putline(=nt = XX i XX = H = XX nt(i)).
end loop.
'ND.
Output0
N'ST'D TL' 'L'M'NTS
nt 2 H a
nt 1 H %
nt 4 H !
N'ST'D TL' 'L'M'NTS
nt 2 H %
nt 1 H !
nt 4 H a
N'ST'D TL' 'L'M'NTS
nt 2 H a
173
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 174/215
nt 1 H %
nt 4 H !
nt @ H a
nt H %
nt H d
N'ST'D TL' 'L'M'NTS
nt 2 H a
nt 1 H %
nt 4 H !
nt @ H %
nt H !
nt H a
nt V H !
N'ST'D TL' 'L'M'NTS
nt 2 H a
nt 1 H %
nt 4 H !
N'ST'D TL' 'L'M'NTS
nt 2 H !
N'ST'D TL' 'L'M'NTS
nt 2 H d
IND'+"* TL'S
n inde/"%7 ta%le has no li$it on its sie5 'le$ents ae inseted into inde/"%7 ta%le whose
inde/ $a7 stat non"seuentiall7 in!luding negative integes5
S7nta/0
T7pe ,type_name is ta%le of ,table_type inde/ %7 %ina7intege.
'/0
D'CLR'
t7pe t is ta%le of va!ha(1) inde/ %7 %ina7intege.
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%$soutput5putline(=No li$it to Inde/ %7 Ta%les=).
else
174
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 175/215
d%$soutput5putline(=Li$it H = XX i%t5li$it).
end if.
d%$soutput5putline(=Count H = XX i%t5!ount).
d%$soutput5putline(=Bist Inde/ H = XX i%t5fist).
d%$soutput5putline(=Last Inde/ H = XX i%t5last).
d%$soutput5putline(=Ne/t Inde/ H = XX i%t5ne/t(1)).
d%$soutput5putline(=Aevious Inde/ H = XX i%t5pio(4)).
d%$soutput5putline(=IND'+ * TL' 'L'M'NTS=).
d%$soutput5putline(=i%t"13 H = XX i%t("13)).
d%$soutput5putline(=i%t2 H = XX i%t(2)).
d%$soutput5putline(=i%t43 H = XX i%t(43)).
d%$soutput5putline(=i%t233 H = XX i%t(233)).
flag 0H i%t5e/ists(43).
if flag H tue then
d%$soutput5putline(=Inde/ 43 e/ists with an ele$ent = XX i%t(43)).
else
d%$soutput5putline(=Inde/ 43 does not e/ists=).
end if.
flag 0H i%t5e/ists(3).
if flag H tue then
d%$soutput5putline(=Inde/ 3 e/ists with an ele$ent = XX i%t(43)).
else
d%$soutput5putline(=Inde/ 3 does not e/ists=).
end if.
i%t5delete(2).
d%$soutput5putline(=fte delete of fist inde/# Count H = XX i%t5!ount).
i%t5delete(43).
d%$soutput5putline(=fte delete of inde/ thit7# Count H = XX i%t5!ount).
d%$soutput5putline(=IND'+ * TL' 'L'M'NTS=).
d%$soutput5putline(=i%t"13 H = XX i%t("13)).
d%$soutput5putline(=i%t233 H = XX i%t(233)). i%t5delete.
d%$soutput5putline(=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 @
175
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 176/215
Bist Inde/ H "13
Last Inde/ H 233
Ne/t Inde/ H 43
Aevious Inde/ H 2
IND'+ * TL' 'L'M'NTS
i%t"13 H %
i%t2 H a
i%t43 H !
i%t233 H d
Inde/ 43 e/ists with an ele$ent !
Inde/ 3 does not e/ists
fte delete of fist inde/# Count H 4
fte delete of inde/ thit7# Count H 1
IND'+ * TL' 'L'M'NTS
i%t"13 H %
i%t233 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 initialied %efoe assign$ent of ele$ents# in inde/"%7
ta%les we !an die!tl7 assign ele$ents5
9aa7s and nested ta%les stoed in data%ase# %ut inde/"%7 ta%les !an not5
Nested ta%les and inde/"%7 ta%les ae AL?SQL ta%les# %ut vaa7s !an not5
Fe7s $ust %e positive in !ase of nested ta%les and vaa7s# in !ase of inde/"%7 ta%les
&e7s !an %e positive o negative5
Refeen!ing none/istent ele$ents aises SUSCRIAT'*ONDCOUNT in %oth nested ta%les
and vaa7s# %ut in !ase of inde/"%7 ta%les NODTBOUND aises5
Fe7s ae seuential in %oth nested ta%les and vaa7s# non"seuential 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
176
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 177/215
T7pe ,type_name1 is ta%le of ,table_type inde/ %7 %ina7intege.
T7pe ,type_name2 is vaa7(,limit -) X ta%le X of ,type_name1 < inde/ %7
%ina7intege.
'/20
D'CLR'
t7pe t2 is ta%le of va!ha(1) inde/ %7 %ina7intege. t7pe t1 is vaa7() of t2.
va t1 0H t1().
! nu$%e 0H V.
flag %oolean.
'8IN
va5e/tend(@).
d%$soutput5putline(=Count H = XX va5!ount).
d%$soutput5putline(=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.
d%$soutput5putline(=9RR* 'L'M'NTS=).
fo i in 255va5!ount loop
fo J in 255va5!ount loop d%$soutput5putline(=va= XX i XX == XX J XX = H = XX va(i)(J)).
end loop.
end loop.
d%$soutput5putline(=Bist inde/ H = XX va5fist).
d%$soutput5putline(=Last inde/ H = XX va5last).
d%$soutput5putline(=Ne/t inde/ H = XX va5ne/t(1)).
d%$soutput5putline(=Aevious inde/ H = XX va5pio(4)).
flag 0H va5e/ists(1).
if flag H tue then
d%$soutput5putline(=Inde/ 1 e/ists=).
else
d%$soutput5putline(=Inde/ 1 e/ists=).
end if.
va5e/tend.
va(2)() 0H ==.
va(1)() 0H ==.
177
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 178/215
va(4)() 0H =s=.
va(@)() 0H =t=.
va()(2) 0H =u=.
va()(1) 0H =v=.
va()(4) 0H =w=.
va()(@) 0H =/=.
va()() 0H =7=.
d%$soutput5putline(=fte e/tend of one inde/# Count H = XX va5!ount).
d%$soutput5putline(=9RR* 'L'M'NTS=).
fo i in 255va5!ount loop
fo J in 255va5!ount loop
d%$soutput5putline(=va= XX i XX == XX J XX = H = XX va(i)(J)).
end loop.
end loop.
va5ti$.
d%$soutput5putline(=fte ti$ of one inde/# Count H = XX va5!ount).
va5ti$(1).
d%$soutput5putline(=fte ti$ of two inde/es# Count H = XX va5!ount).
d%$soutput5putline(=9RR* 'L'M'NTS=).
fo i in 255va5!ount loop
fo J in 255va5!ount loop
d%$soutput5putline(=va= XX i XX == XX J XX = H = XX va(i)(J)).
end loop.
end loop.
va5delete.
d%$soutput5putline(=fte delete of entie vaa7# Count H = XX va5!ount).
'ND.
Output0
Count H @
Li$it H
9RR* 'L'M'NTS
va22 H a
va21 H %
va24 H !
va2@ H d
va12 H e
va11 H f
va14 H g
178
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 179/215
va1@ H h
va42 H i
va41 H J
va44 H &
va4@ H l
va@2 H $
va@1 H n
va@4 H o
va@@ 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
9RR* 'L'M'NTS
va22 H a
va21 H %
va24 H !
va2@ H d
va2 H
va12 H e
va11 H f
va14 H g
va1@ H h
va1 H
va42 H i
va41 H J
va44 H &
va4@ H l
va4 H sva@2 H $
va@1 H n
va@4 H o
va@@ H p
va@ H t
va2 H u
va1 H v
179
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 180/215
va4 H w
va@ H /
va H 7
fte ti$ of one inde/# Count H @
fte ti$ of two inde/es# Count H 1
9RR* 'L'M'NTS
va22 H a
va21 H %
va12 H e
va11 H f
fte delete of entie vaa7# Count H 3
'/10
D'CLR'
t7pe t2 is ta%le of va!ha(1) inde/ %7 %ina7intege.
t7pe t1 is ta%le of t2.
nt t1 0H t1().
! nu$%e 0H .
v nu$%e 0H 2.
flag %oolean.
'8IN
nt5e/tend(@).
d%$soutput5putline(=Count H = XX nt5!ount).
if nt5li$it is null then d%$soutput5putline(=No li$it to Nested Ta%les=).
else
d%$soutput5putline(=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 2 then
! 0H V.
end if.
end loop.
end loop.
d%$soutput5putline(=N'ST'D TL' 'L'M'NTS=).
fo i in 255nt5!ount loop
fo J in 255nt5!ount loop
180
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 181/215
d%$soutput5putline(=nt= XX i XX == XX J XX = H = XX nt(i)(J)).
end loop.
end loop.
d%$soutput5putline(=Bist inde/ H = XX nt5fist).
d%$soutput5putline(=Last inde/ H = XX nt5last).
d%$soutput5putline(=Ne/t inde/ H = XX nt5ne/t(1)).
d%$soutput5putline(=Aevious inde/ H = XX nt5pio(4)).
flag 0H nt5e/ists(1).
if flag H tue then
d%$soutput5putline(=Inde/ 1 e/ists=).
else
d%$soutput5putline(=Inde/ 1 e/ists=).
end if.
nt5e/tend(1).
nt(2)() 0H =Q=.
nt(2)() 0H =R=.
nt(1)() 0H =S=.
nt(1)() 0H =T=.
nt(4)() 0H =U=.
nt(4)() 0H =9=.
nt(@)() 0H =6=.
nt(@)() 0H =+=.
nt()(2) 0H =*=.
nt()(1) 0H =Z=.
nt()(4) 0H =a=.
nt()(@) 0H =%=.
nt()() 0H =!=.
nt()() 0H =d=.
nt()(2) 0H =e=.
nt()(1) 0H =f=.
nt()(4) 0H =g=. nt()(@) 0H =h=.
nt()() 0H =i=.
nt()() 0H =J=.
d%$soutput5putline(=fte e/tend of one inde/# Count H = XX nt5!ount).
d%$soutput5putline(=N'ST'D TL' 'L'M'NTS=).
fo i in 255nt5!ount loop
fo J in 255nt5!ount loop
181
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 182/215
d%$soutput5putline(=nt= XX i XX == XX J XX = H = XX nt(i)(J)).
end loop.
end loop.
nt5ti$.
d%$soutput5putline(=fte ti$ of one inde/e# Count H = XX nt5!ount).
nt5ti$(1).
d%$soutput5putline(=fte ti$ of two inde/es# Count H = XX nt5!ount).
d%$soutput5putline(=N'ST'D TL' 'L'M'NTS=).
fo i in 255nt5!ount loop
fo J in 255nt5!ount loop
d%$soutput5putline(=nt= XX i XX == XX J XX = H = XX nt(i)(J)).
end loop.
end loop.
nt5delete(1).
d%$soutput5putline(=fte delete of se!ond inde/# Count H = XX nt5!ount).
d%$soutput5putline(=N'ST'D TL' 'L'M'NTS=).
loop
e/it when v H @.
fo J in 255nt5!ountG2 loop
d%$soutput5putline(=nt= XX v XX == XX J XX = H = XX nt(v)(J)).
end loop.
v 0H v G 2.
if vH 1 then
v 0H 4.
end if.
end loop.
nt5delete.
d%$soutput5putline(=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 TL' 'L'M'NTS
nt22 H
nt21 H
nt24 H C
nt2@ H D
182
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 183/215
nt12 H '
nt11 H B
nt14 H 8
nt1@ H :
nt42 H I
nt41 H >
nt44 H F
nt4@ H L
nt@2 H M
nt@1 H N
nt@4 H O
nt@@ 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
N'ST'D TL' 'L'M'NTS
nt22 H
nt21 H
nt24 H C
nt2@ H D
nt2 H Q
nt2 H R
nt12 H '
nt11 H B
nt14 H 8
nt1@ H :
nt1 H S
nt1 H Tnt42 H I
nt41 H >
nt44 H F
nt4@ H L
nt4 H U
nt4 H 9
nt@2 H M
183
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 184/215
nt@1 H N
nt@4 H O
nt@@ H A
nt@ H 6
nt@ H +
nt2 H *
nt1 H Z
nt4 H a
nt@ H %
nt H !
nt H d
nt2 H e
nt1 H f
nt4 H g
nt@ H h
nt H i
nt H J
fte ti$ of one inde/e# Count H
fte ti$ of two inde/es# Count H 4
N'ST'D TL' 'L'M'NTS
nt22 H
nt21 H
nt24 H C
nt12 H '
nt11 H B
nt14 H 8
nt42 H I
nt41 H >
nt44 H F
fte delete of se!ond inde/# Count H 1
N'ST'D TL' 'L'M'NTSnt22 H
nt21 H
nt24 H C
nt42 H I
nt41 H >
nt44 H F
fte delete of entie nested ta%le# Count H 3
184
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 185/215
'/40
D'CLR'
t7pe t2 is ta%le of va!ha(1) inde/ %7 %ina7intege.
t7pe t1 is ta%le of t2 inde/ %7 %ina7intege.
i%t t1.
flag %oolean.'8IN
d%$soutput5putline(=Count H = XX i%t5!ount).
if i%t5li$it is null then
d%$soutput5putline(=No li$it to Inde/"%7 Ta%les=).
else
d%$soutput5putline(=Li$it H = XX i%t5li$it).
end if.
i%t(2)(2) 0H =a=.
i%t(@)() 0H =%=.
i%t()(2) 0H =!=.
i%t()(1) 0H =d=.
i%t(W)(4) 0H =e=.
i%t(4)(@) 0H =f=.
d%$soutput5putline(=IND'+"* TL' 'L'M'NTS=).
d%$soutput5putline(=i%t(22 H = XX i%t(2)(2)).
d%$soutput5putline(=i%t(@ H = XX i%t(@)()).
d%$soutput5putline(=i%t(2 H = XX i%t()(2)). d%$soutput5putline(=i%t(1 H = XX i%t()(1)).
d%$soutput5putline(=i%t(W4 H = XX i%t(W)(4)).
d%$soutput5putline(=i%t(4@ H = XX i%t(4)(@)).
d%$soutput5putline(=Bist Inde/ H = XX i%t5fist).
d%$soutput5putline(=Last Inde/ H = XX i%t5last).
d%$soutput5putline(=Ne/t Inde/ H = XX i%t5ne/t(4)).
d%$soutput5putline(=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)() 0H =&=.
i%t(2)() 0H =l=.
i%t(2)(V) 0H =$=.
i%t(2)(W) 0H =n=.
d%$soutput5putline(=Count H = XX i%t5!ount).
d%$soutput5putline(=IND'+"* TL' 'L'M'NTS=).
185
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 186/215
fo i in 255W loop
d%$soutput5putline(=i%t2= XX i XX = H = XX i%t(2)(i)).
end loop.
d%$soutput5putline(=i%t(@ H = XX i%t(@)()).
d%$soutput5putline(=i%t(2 H = XX i%t()(2)).
d%$soutput5putline(=i%t(1 H = XX i%t()(1)).
d%$soutput5putline(=i%t(W4 H = XX i%t(W)(4)).
d%$soutput5putline(=i%t(4@ H = XX i%t(4)(@)).
flag 0H i%t5e/ists(4).
if flag H tue then
d%$soutput5putline(=Inde/ 4 e/ists=).
else
d%$soutput5putline(=Inde/ 4 e/ists=).
end if.
i%t5delete(2).
d%$soutput5putline(=fte delete of fist inde/# Count H = XX i%t5!ount).
i%t5delete(@).
d%$soutput5putline(=fte delete of fouth inde/# Count H = XX i%t5!ount).
d%$soutput5putline(=IND'+"* TL' 'L'M'NTS=).
d%$soutput5putline(=i%t(2 H = XX i%t()(2)).
d%$soutput5putline(=i%t(1 H = XX i%t()(1)).
d%$soutput5putline(=i%t(W4 H = XX i%t(W)(4)).
d%$soutput5putline(=i%t(4@ H = XX i%t(4)(@)).
i%t5delete.
d%$soutput5putline(=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'+"* TL' 'L'M'NTS
i%t(22 H a
i%t(@ H %
i%t(2 H !
i%t(1 H d
i%t(W4 H e
i%t(4@ H f
Bist Inde/ H 2
186
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 187/215
Last Inde/ H W
Ne/t Inde/ H @
Aio Inde/ H
Count H
IND'+"* TL' 'L'M'NTS
i%t22 H a
i%t21 H g
i%t24 H h
i%t2@ H i
i%t2 H &
i%t2 H l
i%t2V H $
i%t2W H n
i%t(@ H %
i%t(2 H !
i%t(1 H d
i%t(W4 H e
i%t(4@ H f
Inde/ 4 e/ists
fte delete of fist inde/# Count H
fte delete of fouth inde/# Count H @
IND'+"* TL' 'L'M'NTS
i%t(2 H !
i%t(1 H d
i%t(W4 H e
i%t(4@ H f
fte delete of entie inde/"%7 ta%le# Count H 3
'/@0
D'CLR'
t7pe t2 is ta%le of va!ha(1) inde/ %7 %ina7intege.
t7pe t1 is ta%le of t2 inde/ %7 %ina7intege.
t7pe t4 is ta%le of t1.
nt t4 0H t4().
! nu$%e 0H .
'8IN
nt5e/tend(1).
d%$soutput5putline(=Count H = XX nt5!ount).
fo i in 255nt5!ount loop
fo J in 255nt5!ount loop
187
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 188/215
fo & in 255nt5!ount loop
nt(i)(J)(&) 0H !h(!).
! 0H ! G 2.
end loop.
end loop.
end loop.
d%$soutput5putline(=N'ST'D TL' 'L'M'NTS=).
fo i in 255nt5!ount loop
fo J in 255nt5!ount loop
fo & in 255nt5!ount loop
d%$soutput5putline(=nt= XX i XX == XX J XX == XX & XX = H = XX
nt(i)(J)(&)).
end loop.
end loop.
end loop.
'ND.
Output0
Count H 1
N'ST'D TL' 'L'M'NTS
nt222 H
nt221 H
nt212 H C
nt211 H D
nt122 H '
nt121 H B
nt112 H 8
nt111 H :
O>'CTS US'D IN T:' '+MAL'S
SQL- sele!t E fo$ student.
SNO SNM' SMRFS
"""""""""" """""""""""""" """"""""""
2 sað 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.
'NM' >O DDR'SS(:NO# CIT*)
188
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 189/215
"""""""""" """""""""" """""""""""""""""""""""""""""
RanJit !le& DDR(22# =h7d=)
Satish $anage DDR(11# =%ang=)
Sinu enginee DDR(44# =&o!hi=)
'RROR :NDLIN8
AL?SQL i$ple$ents eo handling with e/!eptions and e/!eption handles5 '/!eptions !an %e
asso!iated with oa!le eos o with 7ou own use"defined eos5 7 using e/!eptions and
e/!eption handles# 7ou !an $a&e 7ou AL?SQL poga$s o%ust and a%le to deal with %oth
une/pe!ted and e/pe!ted eos duing e/e!ution5
'RROR T*A'S
Co$pile"ti$e eos
Runti$e eos
'os that o!!u duing the !o$pilation phase ae dete!ted %7 the AL?SQL engine and epoted
%a!& to the use# we have to !oe!t the$5
Runti$e eos ae dete!ted %7 the AL?SQL unti$e engine whi!h !an poga$$ati!all7 aise
and !aught %7 e/!eption handles5
'/!eptions ae designed fo un"ti$e eo handling# athe than !o$pile"ti$e eo handling5
:NDLIN8 '+C'ATIONS
6hen e/!eption is aised# !ontol passes to the e/!eption se!tion of the %lo!&5 The e/!eption
se!tion !onsists of handles fo so$e o all of the e/!eptions5 n e/!eption handle !ontains
the !ode that is e/e!uted when the eo asso!iated with the e/!eption o!!us# and the
e/!eption is aised5S7nta/0
'+C'ATION
6hen e/!eptionna$e then
Seuen!eofstate$ents.
6hen e/!eptionna$e then
Seuen!eofstate$ents.
6hen othes then
Seuen!eofstate$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 STNDRD
189
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 190/215
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'CLR'
a nu$%e.
% va!ha(1). v$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
"" NODTBOUND
'8IN
sele!t s$a&s into v$a&s fo$ student whee sno H 3.'+C'ATION
when nodatafound then
d%$soutput5putline(=Invalid student nu$%e=).
'ND.
"" CURSORLR'D*OA'N
'8IN
open !.
open !.
'+C'ATION
when !usoalead7open then
d%$soutput5putline(=Cuso is alead7 opened=).
'ND.
"" IN9LIDCURSOR
'8IN
!lose !.
open !.
!lose !.
!lose !.
'+C'ATION
when invalid!uso then
d%$soutput5putline(=Cuso is alead7 !losed=).
'ND.
"" TOOMN*RO6S
'8IN
sele!t s$a&s into v$a&s fo$ student whee sno - 2.
'+C'ATION
190
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 191/215
when too$an7ows then
d%$soutput5putline(=Too $an7 values ae !o$ing to $a&s
vaia%le=).
'ND.
"" Z'RODI9ID'
'8IN
a 0H ?3.
'+C'ATION
when eodivide then
d%$soutput5putline(=Divided %7 eo " invalid opeation=).
'ND.
"" 9LU''RROR
'8IN
% 0H =sað=.
'+C'ATION
when valueeo then
d%$soutput5putline(=Invalid sting length=).
'ND.
"" IN9LIDNUM'R
'8IN
inset into student values(=a=#=sinu=#233).
'+C'ATION
when invalidnu$%e then
d%$soutput5putline(=Invalid nu$%e=).
'ND. "" SUSCRIATOUTSID'LIMIT
'8IN
va(@) 0H =!=.
'+C'ATION
when su%s!iptoutsideli$it then
d%$soutput5putline(=Inde/ is geate than the li$it=).
'ND.
"" SUSCRIAT'*ONDCOUNT
'8IN
va(4) 0H =!=.
'+C'ATION
when su%s!ipt%e7ond!ount then
d%$soutput5putline(=Inde/ is geate than the !ount=).
'ND.
"" COLL'CTIONISNULL
'8IN
va2(2) 0H =a=.
'+C'ATION
191
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 192/215
when !olle!tionisnull then
d%$soutput5putline(=Colle!tion is e$pt7=).
'ND.
""
'ND.
Output0Invalid student nu$%e
Cuso is alead7 opened
Cuso is alead7 !losed
Too $an7 values ae !o$ing to $a&s vaia%le
Divided %7 eo " invalid opeation
Invalid sting length
Invalid nu$%e
Inde/ is geate than the li$it
Inde/ is geate than the !ount
Colle!tion is e$pt7
'/10
D'CLR'
! nu$%e.
'8IN
! 0H ?3.
'+C'ATION
when eodivide then
d%$soutput5putline(=Invalid Opeation=).
when othes then
d%$soutput5putline(=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
RISIN8 '+C'ATIONS
Use"defined e/!eptions ae aised e/pli!itl7 via the RIS' state$ent5
'/0
'CLR'
e e/!eption.
192
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 193/215
'8IN
aise e.
'+C'ATION
when e then
d%$soutput5putline(=e is aised=).
'ND.
Output0
e is aised
ULIT"IN 'RROR BUNCTIONS
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/!eption5
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'CLR'
e e/!eption.
vdna$e va!ha(23).
'8IN
"" US'R"D'BIN'D '+C'ATION '8IN
aise e.
'+C'ATION
when e then
d%$soutput5putline(SQLCOD' XX = = XX SQL'RRM).
'ND.
"" AR'D'BIN'D '+C'ATION
'8IN
sele!t dna$e into vdna$e fo$ dept whee deptno H 3.
'+C'ATION
when nodatafound then
d%$soutput5putline(SQLCOD' XX = = XX SQL'RRM).
'ND.
'ND.
Output0
2 Use"Defined '/!eption
233 OR"32@340 no data found
193
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 194/215
'/10
'8IN
d%$soutput5putline(SQL'RRM(233)).
d%$soutput5putline(SQL'RRM(3)).
d%$soutput5putline(SQL'RRM(2)).
d%$soutput5putline(SQL'RRM("233)). d%$soutput5putline(SQL'RRM("33)).
d%$soutput5putline(SQL'RRM(133)).
d%$soutput5putline(SQL'RRM("33)).
'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"33330 Message 33 not found. podu!tHRDMS. fa!ilit7HOR
"1330 non"ORCL' e/!eption
OR"33330 invalid SQL state$ent
DMSUTILIT*5BORMT'RRORSTCF
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'CLR'
v nu$%e 0H =a%=.
'8IN
null.
'+C'ATION
when othes then d%$soutput5putline(d%$sutilit75fo$ateosta!&).
'ND.
Output0
de!lae
E
'RROR at line 20
OR"3310 AL?SQL0 nu$ei! o value eo0 !haa!te to nu$%e !onvesion eo
194
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 195/215
OR"3210 at line 1
DMSUTILIT*5BORMTCLLSTCF
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%$soutput5putline(d%$sutilit75fo$at!allsta!&).
'ND.
Output0
""""" AL?SQL Call Sta!& """""
O%Je!thandle linenu$%e o%Je!tna$e
V3@VW 1 anon7$ous %lo!&
DMSUTILIT*5BORMT'RRORCFTRC'
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'ALC' AROC'DUR' A2 IS
'8IN
d%$soutput5putline(=fo$ po!edue 2=).
aise valueeo.
'ND A2.
CR'T' OR R'ALC' AROC'DUR' A1 IS
'8IN
d%$soutput5putline(=fo$ po!edue 1=).
p2.
'ND A1.
CR'T' OR R'ALC' AROC'DUR' A4 IS
'8IN
d%$soutput5putline(=fo$ po!edue 4=).
p1.
'+C'ATION
when othes then
d%$soutput5putline(d%$sutilit75fo$ateo%a!&ta!e).
'ND A4.
Output0
SQL- e/e! p4
fo$ po!edue 4
fo$ po!edue 1
195
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 196/215
fo$ po!edue 2
OR"3210 at bSF'T:5A2b# line @
OR"3210 at bSF'T:5A1b# line @
OR"3210 at bSF'T:5A4b# line @
'+C'ATIONINIT AR8M
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
AR8M '+C'ATIONINIT(eception_name# oracle_error_number ).
'/0
D'CLR'
e e/!eption.
pag$a e/!eptioninit(e#"2@V).
! nu$%e.
'8IN
! 0H ?3.
'+C'ATION
when e then
d%$soutput5putline(=Invalid Opeation=).
'ND.
Output0
Invalid Opeation
RIS'AALICTION'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
RIS'AALICTION'RROR (error_number # error_me!!a"e## eep_error!_'la").
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 BLS'# whi!h is default# the new eo will epla!e the !uent
list of eos5
'/0D'CLR'
! nu$%e.
'8IN
! 0H ?3.
'+C'ATION
when eodivide then
aiseappli!ationeo("13111#=Invalid Opeation=).
'ND.
196
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 197/215
Output0
D'CLR'
E
'RROR at line 20
OR"131110 Invalid Opeation
OR"3210 at line V'+C'ATION AROA8TION
'/!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 RIS'D IN T:' '+'CUTL' S'CTION
'/!eptions aised in e/e!uata%le se!tion !an %e handled in !uent %lo!& o oute %lo!&5
'/20
D'CLR'
e e/!eption.
'8IN
'8IN
aise e.
'ND.
'+C'ATION
when e then
d%$soutput5putline(=e is aised=).
'ND.
Output0
e is aised
'/10
D'CLR'
e e/!eption.
'8IN
'8IN
aise e.
'ND.
'ND.
Output0
'RROR at line 20
OR"3230 AL?SQL0 unhandled use"defined e/!eption
OR"3210 at line
'+C'ATION RIS'D IN T:' D'CLRTI9' S'CTION
'/!eptions aised in the de!laative se!ion $ust %e handled in the oute %lo!&5
'/20
D'CLR'
! nu$%e(4) 0H =a%!d=.
197
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 198/215
'8IN
d%$soutput5putline(=:ello=).
'+C'ATION
when othes then
d%$soutput5putline(=Invalid sting length=).
'ND.
Output0
'RROR at line 20
OR"3310 AL?SQL0 nu$ei! o value eo0 !haa!te to nu$%e !onvesion eo
OR"3210 at line 1
'/10
'8IN
D'CLR'
! nu$%e(4) 0H =a%!d=.
'8IN
d%$soutput5putline(=:ello=).
'+C'ATION
when othes then
d%$soutput5putline(=Invalid sting length=).
'ND.
'+C'ATION
when othes then
d%$soutput5putline(=Bo$ oute %lo!&0 Invalid sting length=).
'ND.
Output0
Bo$ oute %lo!&0 Invalid sting length
'+C'ATION RIS'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'CLR'
e2 e/!eption.
e1 e/!eption.
'8IN
aise e2.
'+C'ATION
when e2 then
d%$soutput5putline(=e2 is aised=).
aise e1.
when e1 then
d%$soutput5putline(=e1 is aised=).
198
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 199/215
'ND.
Output0
e2 is aised
D'CLR'
E
'RROR at line 20OR"3230 AL?SQL0 unhandled use"defined e/!eption
OR"3210 at line
OR"3230 AL?SQL0 unhandled use"defined e/!eption
'/10
D'CLR'
e2 e/!eption.
e1 e/!eption.
'8IN
'8IN
aise e2.
'+C'ATION
when e2 then
d%$soutput5putline(=e2 is aised=).
aise e1.
when e1 then
d%$soutput5putline(=e1 is aised=).
'ND.
'+C'ATION
when e1 then
d%$soutput5putline(=Bo$ oute %lo!&0 e1 is aised=).
'ND.
Output0
e2 is aised
Bo$ oute %lo!&0 e1 is aised
'/40
D'CLR'
e e/!eption.'8IN
aise e.
'+C'ATION
when e then
d%$soutput5putline(=e is aised=).
aise e.
'ND.
Output0
199
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 200/215
e is aised
D'CLR'
E
'RROR at line 20
OR"3230 AL?SQL0 unhandled use"defined e/!eption
OR"3210 at line W
OR"3230 AL?SQL0 unhandled use"defined e/!eption
R'STRICTIONS
*ou !an not pass e/!eption as an agu$ent to a su%poga$5
DTS' TRI88'RS
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
CT'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 leve
tigge is identified %7 the BOR 'C: RO6 !lause5
200
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 201/215
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-
Bo ea!h ow
6hen ()
De!lae
"" de!laation
egin
"" tigge %od7
'/!eption
"" e/!eption se!tion
'nd ,tri""er_name-.
DML TRI88'RS
DML tigge is fied on an INS'RT# UADT'# 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
STT'M'NT L'9'L
State$ent level tigge fies onl7 on!e5
'/0
SQL- !eate ta%le state$entlevel(!ount va!ha(3)).CR'T' OR R'ALC' TRI88'R STT'M'NTL'9'LTRI88'R
afte update on student
'8IN
inset into state$entlevel values(=State$ent level fied=).
'ND STT'M'NTL'9'LTRI88'R.
Output0
SQL- update student set s$a&sH33.
4 ows updated5
SQL- sele!t E fo$ state$entlevel.
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 owlevel(!ount va!ha(3)).
201
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 202/215
CR'T' OR R'ALC' TRI88'R RO6L'9'LTRI88'R
afte update on student
'8IN
inset into owlevel values(=Row level fied=).
'ND RO6L'9'LTRI88'R.
Output0
SQL- update student set s$a&sH33.
4 ows updated5
SQL- sele!t E fo$ state$entlevel.
COUNT
""""""""""""""""""""""""""""
Row level fied
Row level fied
Row level fied
ORD'R OB DML TRI88'R BIRIN8
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 NM' MRFS """"" """"""" """"""""""
2 a 233
1 % 133
4 ! 433
@ d @33
SQL- !eate ta%le fiingode(ode va!ha(3)).
CR'T' OR R'ALC' TRI88'R 'BOR'STT'M'NT
%efoe inset on student
'8IN
inset into fiingode values(=efoe State$ent Level=).
'ND 'BOR'STT'M'NT.
CR'T' OR R'ALC' TRI88'R 'BOR'RO6
%efoe inset on student
fo ea!h ow
'8IN
inset into fiingode values(=efoe Row Level=).
202
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 203/215
'ND 'BOR'RO6.
CR'T' OR R'ALC' TRI88'R BT'RSTT'M'NT
afte inset on student
'8IN
inset into fiingode values(=fte State$ent Level=).
'ND BT'RSTT'M'NT.
CR'T' OR R'ALC' TRI88'R BT'RRO6
afte inset on student
fo ea!h ow
'8IN
inset into fiingode values(=fte Row Level=).
'ND BT'RRO6.
Output0
SQL- sele!t E fo$ fiingode.
no ows sele!ted
SQL- inset into student values(#=e=#33).
2 ow !eated5
SQL- sele!t E fo$ fiingode.
ORD'R
""""""""""""""""""""""""""""""""""""""""""""""""""
efoe State$ent Level
efoe Row Level
fte Row Level
fte State$ent LevelSQL- sele!t E fo$ student.
NO NM' MRFS
"""" """""""" """"""""""
2 a 233
1 % 133
4 ! 433
@ d @33
e 33
CORR'LTION 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 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
Tiggeingta%leRO6T*A'5
203
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 204/215
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 STT'M'NT 0OLD 0N'6
"""""""""""""""""""""""""""""""""""""" """""""""""""""""""""""""""" """""""""""""""""""""""""""""""""""""""""""""""
INS'RT all fields ae NULL5 values that will %e inseted
6hen the state$ent is !o$pleted5
UADT' 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) old$a&s nu$%e(4)#new$a&s
nu$%e(4)).CR'T' OR R'ALC' TRI88'R OLDN'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 OLDN'6.
Output0
SQL- sele!t E fo$ student.
NO NM' MRFS
""""" """"""" """"""""""
2 a 233
1 % 133
4 ! 433
@ d @33
e 33
SQL- sele!t E fo$ $a&s.
no ows sele!ted
SQL- inset into student values(#=f=#33).
2 ow !eated5
SQL- sele!t E fo$ student.
NO NM' MRFS
"""" """""""" """"""""""
2 a 233
1 % 133
204
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 205/215
4 ! 433
@ d @33
e 33
f 33
SQL- sele!t E fo$ $a&s.
NO OLDMRFS N'6MRFS
"""" """"""""""""""" """""""""""""""
33
SQL- update student set $a&sH whee noH.
2 ow updated5
SQL- sele!t E fo$ student.
NO NM' MRFS
""""" """"""" """"""""""
2 a 233
1 % 133
4 ! 433
@ d @33
e
f 33
SQL- sele!t E fo$ $a&s.
NO OLDMRFS N'6MRFS
"""""" """""""""""""""" """""""""""""""
33
33
SQL- delete student whee no H 1.
2 ow deleted5
SQL- sele!t E fo$ student.
NO NM' MRFS
"""" """""""" """"""""""
2 a 233
4 ! 433 @ d @33
e
f 33
SQL- sele!t E fo$ $a&s.
NO OLDMRFS N'6MRFS
""""" """""""""""""" """"""""""""""""
33
205
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 206/215
33
1 133
R'B'R'NCIN8 CLUS'
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 old as oldna$e new as newna$e
'/0
CR'T' OR R'ALC' TRI88'R R'B'R'NC'TRI88'R
%efoe inset o update o delete on student
efeen!ing old as oldstudent new as newstudent
fo ea!h ow
'8IN
inset into $a&s
values(0oldstudent5no#0oldstudent5$a&s#0newstudent5$a&s).'ND R'B'R'NC'TRI88'R.
6:'N CLUS'
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'ALC' TRI88'R 6:'NTRI88'R
%efoe inset o update o delete on student
efeen!ing old as oldstudent new as newstudent
fo ea!h ow
when (newstudent5$a&s - 33)
'8IN
inset into $a&s
values(0oldstudent5no#0oldstudent5$a&s#0newstudent5$a&s).
'ND 6:'NTRI88'R.
TRI88'R AR'DICT'S
Thee ae thee oolean fun!tions that 7ou !an use to dete$ine what the opeation is5
The pedi!ates ae
INS'RTIN8
UADTIN8
D'L'TIN8
206
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 207/215
'/0
SQL- !eate ta%le pedi!ates(opeation va!ha(13)).
CR'T' OR R'ALC' TRI88'R AR'DICT'TRI88'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'DICT'TRI88'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.
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
207
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 208/215
To $odif7 the !olu$ns of a nested ta%le !olu$n in a view5
'/0
SQL- !eate view e$pdept 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'ALC' TRI88'R INST'DOBTRI88'R
instead of inset on e$pdept
'8IN
inset into dept2 values(3#=d=#=%ang=).
inset into
e$p2(e$pno#ena$e#Jo%#sal#deptno)values(1111#=sað=#=do!to=#W333#3).
'ND INST'DOBTRI88'R.
Output0
SQL- inset into e$pdept values(1111#=sað=#=do!to=#W333#=d=#=%ang=#3).
SQL- sele!t E fo$ e$pdept. 'MANO 'NM' >O SL DNM' LOC D'ATNO
"""""""""" """""""""" """""""""""" """"""""""" """"""""""""" """"""""""""" """"""""""
V4 SMIT: CL'RF W33 R'S'RC: DLLS 13
V@ LL'N SL'SMN 233 SL'S C:IC8O 43
V12 6RD SL'SMN 213 SL'S C:IC8O 43
V >ON'S MN8'R 1V R'S'RC: DLLS 13
V@ MRTIN SL'SMN 213 SL'S C:IC8O 43
VW LF' MN8'R 1W3 SL'S C:IC8O 43
VVW1 CLRF MN8'R 1@3 CCOUNTIN8 N'6 *ORF 23
VVWW SCOTT NL*ST 4333 R'S'RC: DLLS 13
VW4 FIN8 AR'SID'NT 333 CCOUNTIN8 N'6 *ORF 23
VW@@ TURN'R SL'SMN 233 SL'S C:IC8O 43
VWV DMS CL'RF 2233 R'S'RC: DLLS 13
V33 >M'S CL'RF 3 SL'S C:IC8O 43
V31 BORD NL*ST 4333 R'S'RC: DLLS 13
V4@ MILL'R CL'RF 2433 CCOUNTIN8 N'6 *ORF 23
1111 sað do!to W333 d %ang 3
SQL- sele!t E fo$ dept.
D'ATNO DNM' LOC
"""""""""" """""""""""""""" """""""""""
23 CCOUNTIN8 N'6 *ORF
13 R'S'RC: DLLS
43 SL'S C:IC8O
@3 OA'RTIONS OSTON
208
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 209/215
3 d %ang
SQL- sele!t E fo$ e$p.
'MANO 'NM' >O M8R :IR'DT' SL COMM D'ATNO
"""""""""" """""""""" """"""""""""""" """""""" """""""""""""" """"""""" """"""""" """"""""""
V4 SMIT: CL'RF V31 2 V"D'C"W3 W33 13
V@ LL'N SL'SMN VW 13"B'"W2 233 433 43
V12 6RD SL'SMN VW 11"B'"W2 213 33 43
V >ON'S MN8'R VW4 31"AR"W2 1V 13
V@ MRTIN SL'SMN VW 1W"S'A"W2 213 2@33 43
VW LF' MN8'R VW4 32"M*"W2 1W3 43
VVW1 CLRF MN8'R VW4 3">UN"W2 1@3 23
VVWW SCOTT NL*ST V 2"AR"WV 4333 13
VW4 FIN8 AR'SID'NT 2V"NO9"W2 333 23
VW@@ TURN'R SL'SMN VW 3W"S'A"W2 233 3 43
VWV DMS CL'RF VVWW 14"M*"WV 2233 13
V33 >M'S CL'RF VW 34"D'C"W2 3 43
V31 BORD NL*ST V 34"D'C"W2 4333 13
V4@ MILL'R CL'RF VVW1 14">N"W1 2433 23
1111 sað do!to W333 3
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_
6hen ()
De!lae
"" de!laation
egin
"" tigge %od7
'/!eption"" e/!eption se!tion
'nd ,tri""er_name-.
'/0
SQL- !eate ta%le $7o%Je!ts(o%Jna$e va!ha(23)#o%Jt7pe va!ha(23)#o%Jowne
va!ha(23)#o%Jti$e date).
CR'T' OR R'ALC' TRI88'R CR'T'TRI88'R
afte !eate on data%ase
'8IN
209
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 210/215
inset into $7o%Je!ts values(s7s5di!tiona7o%Jna$e#s7s5di!tiona7o%Jt7pe#
s7s5di!tiona7o%Jowne# s7sdate).
'ND CR'T'TRI88'R.
Output0
SQL- sele!t E fo$ $7o%Je!ts.
no ows sele!tedSQL- !eate ta%le stud2(no nu$%e(1)).
SQL- sele!t E fo$ $7o%Je!ts.
O>NM' O>T*A' O>O6N'R O>TIM'
""""""""""""" """""""""""""" """""""""""""" """"""""""""
STUD2 TL' S*S 12">UL"3V
SQL- !eate seuen!e ss.
SQL- !eate view studview as sele!t E fo$ stud2.
SQL- sele!t E fo$ $7o%Je!ts.
O>NM' O>T*A' O>O6N'R O>TIM'
"""""""""""""" """"""""""""" """""""""""""""" """""""""""""
STUD2 TL' S*S 12">UL"3V
SS S'QU'NC' S*S 12">UL"3V
STUD9I'6 9I'6 S*S 12">UL"3V
6:'N CLUS'
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
'/0CR'T' OR R'ALC' TRI88'R CR'T'TRI88'R
afte !eate on data%ase
when (s7s5di!tiona7o%Jt7pe H KTL';)
'8IN
inset into $7o%Je!ts values(s7s5di!tiona7o%Jna$e#s7s5di!tiona7o%Jt7pe#
s7s5di!tiona7o%Jowne# s7sdate).
'ND CR'T'TRI88'R.
S*ST'M TRI88'RS
S7ste$ tigges will fie wheneve data%ase"wide event o!!us5 The following ae the data%aseevent tigges5 To !eate s7ste$ tigge 7ou need DMINIST'R DTS' TRI88'R pivilege5
STRTUA
S:UTDO6N
LO8ON
LO8OBB
S'R9'R'RROR
S7nta/0
Ceate o epla!e tigge ,tri""er_name-
210
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 211/215
^efoe X afte_ ^Data%ase event_ on ^data%ase X s!he$a_
6hen ()
De!lae
"" de!laation se!tion
egin
"" tigge %od7
'/!eption
"" e/!eption se!tion
'nd ,tri""er_name-.
'/0
SQL- !eate ta%le uselogs(una$e va!ha(23)#logti$e ti$esta$p).
CR'T' OR R'ALC' TRI88'R BT'RLO8ON
afte logon on data%ase
'8IN
inset into uselogs values(use#!uentti$esta$p).
'ND BT'RLO8ON.
Output0
SQL- sele!t E fo$ uselogs.
no ows sele!ted
SQL- !onn sað?sað
SQL- sele!t E fo$ uselogs.
UNM' LO8TIM'
"""""""""" """"""""""""""""""""""""""""""""""""""""""""""""
SF'T: 11">UL"3V 2153V52452@3333 M
SQL- !onn s7ste$?oa!le
SQL- sele!t E fo$ uselogs.
UNM' LO8TIM'
"""""""""" """"""""""""""""""""""""""""""""""""""""""""""""
SF'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$ uselogs.
UNM' LO8TIM'
"""""""""" """""""""""""""""""""""""""""""""""""""""""""""
SF'T: 11">UL"3V 2153V52452@3333 M
S*ST'M 11">UL"3V 2153V54@512W333 M
SCOTT 11">UL"3V 2153W5@4534333 M
S'R9'R'RROR
211
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 212/215
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'R'RROR atti%ute fun!tion5
'/0
SQL- !eate ta%le $7eos(eo$sg va!ha(133)).
CR'T' OR R'ALC' TRI88'R S'R9'R'RRORTRI88'R
afte seveeo on data%ase'8IN
inset into $7eos values(d%$sutilit75fo$ateosta!&).
'ND S'R9'R'RRORTRI88'R.
Output0
SQL- !eate ta%le ss (no)).
!eate ta%le ss (no))
E
'RROR at line 20
OR"33110 $issing o invalid option
SQL- sele!t E fo$ $7eos.
'RRORMS8
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
OR"33110 $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"33@10 ta%le o view does not e/ist
SQL- sele!t E fo$ $7eos.
'RRORMS8
"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""
OR"33110 $issing o invalid option
OR"33@10 ta%le o view does not e/ist
S'R9'R'RROR TTRIUT' 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'ALC' TRI88'R S'R9'R'RRORTRI88'R
afte seveeo on data%ase
'8IN
inset into $7eos values(seveeo(2)).
'ND S'R9'R'RRORTRI88'R.
SUSA'ND TRI88'RS
212
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 213/215
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 uota5 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_
6hen ()
De!lae
"" de!laation se!tion
egin
"" tigge %od7
'/!eption
"" e/!eption se!tion
'nd ,tri""er_name-.
'/0
SQL- !eate ta%lespa!e $7spa!e datafile =f0$7file5d%f= sie 1$.
SQL- !eate ta%le student(sno nu$%e(1)#sna$e va!ha(23)) ta%lespa!e $7spa!e.
CR'T' OR R'ALC' TRI88'R SUSA'NDTRI88'R
afte suspend on data%ase
'8IN
d%$soutput5putline(K No oo$ to inset in 7ou ta%lespa!e=).
'ND SUSA'NDTRI88'R.
Output0
Inset $oe ows in student ta%le then # 7ou will get
No oo$ to inset in 7ou ta%lespa!e
UTONOMOUS TRNSCTION
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'ALC' TRI88'R UTONOMOUSTRNSCTIONTRI88'R
afte inset on student
D'CLR'
pag$a autono$oustansa!tion.
'8IN
update student set $a&s H .
213
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 214/215
!o$$it.
'ND UTONOMOUSTRNSCTIONTRI88'R.
Output0
SQL- sele!t E fo$ student.
NO N MRFS
""""" """"" "" """""""""" 2 a 222
1 % 111
4 ! 433
SQL- inset into student values(@#=d=#@@@).
SQL- sele!t E fo$ student.
NO N MRFS
"""" """""" "" """"""""""
2 a
1 %
4 !
@ d @@@
R'STRICTIONS ON UTONOMOUS TRNSCTION
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 AR8M
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 UADT' o D'L'T'# 7ou $ust pefo$ an e/pli!it !o$$it o oll%a!&5
The COMMIT and ROLLCF 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 ROLLCF state$ents inside 7ou autono$ous %lo!&5
*ou !an not oll%a!& to a savepoint set in the $ain tansa!tion5
The TRNSCTIONS paa$ete in the oa!le initialiation file spe!ifies the $a/i$u$
nu$%e of tansa!tions allowed !on!uentl7 in a session5 The default value is V fo
this# %ut 7ou !an in!ease the li$it5MUTTIN8 TL'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
214
8/12/2019 SQL Plsql Beginners
http://slidepdf.com/reader/full/sql-plsql-beginners 215/215
!onstaining ta%le is a ta%le that $ight need to %e ead fo$ fo a efeential integit7
!onstaint5
'/0
CR'T' OR R'ALC' TRI88'R MUTTIN8TRI88'R
%efoe delete on student
fo ea!h owD'CLR'
!t nu$%e.
'8IN
sele!t !ount(E) into !t fo$ student whee no H 0old5no.
'ND MUTTIN8TRI88'R.
O t t
215