You are on page 1of 301

1

I
NTRODUCTI
ON

SQLi
sdi
vi
dedi
ntot
hef
oll
owi
ng

 Dat
aDef
ini
ti
onLanguage(
DDL)
 Dat
aMani
pul
ati
onLanguage(
DML)
 Dat
aRet
ri
evalLanguage(
DRL)
 Tr
ansact
ionCont
rol
Language(
TCL)
 Dat
aCont
rolLanguage(
DCL)

DDL-
-cr
eat
e,al
ter
,dr
op,
truncat
e,r
ename
DML-
-inser
t,updat
e,del
ete
DRL-
-sel
ect
TCL-
-commi
t,r
oll
back,
sav
epoi
nt
DCL-
-gr
ant
,rev
oke

CREATETABLESYNTAX

Cr
eat
et e<t
abl abl
e_name>(
col
1dat
aty
pe1,
col
2dat
aty
pe2…col
ndat
aty
pen)
;
Ex:
SQL>cr
eat
etabl
est
udent(
nonumber(
2),
namev
archar(
10)
,mar
ksnumber(
3))
;

I
NSERT

Thi
swi
llbeusedt
oinser
tther
ecor
dsi
ntot
abl
e.
Wehav
etwomet
hodst
oinser
t.
 Byv
aluemet
hod
 Byaddr
essmet
hod

a)USI
NGVALUEMETHOD

Sy
ntax:
i
nser
ti o<t
nt abl
e_name)val val
ues( ue1,
val
ue2,
val
ue3….Val
uen)
;

Ex:

.
2

SQL>i
nser
tint
ost
udentv
alues(
1,’
sudha’
,100)
;
SQL>i
nser
tint
ost
udentv
alues(
2,’
saket
h’,
200)
;

Toi
nser
tanewr
ecor
dagai
nyouhav
etot
ypeent
ir
einser
tcommand,
ift
her
ear
elotof
r
ecor
dst
hiswi
llbedi
ff
icul
t.
Thi
swi
llbeav
oidedbyusi
ngaddr
essmet
hod.

b)USI
NGADDRESSMETHOD

Sy
ntax:
i
nser
ti o<t
nt abl
e_name)val
ues(
&col
1,&col
2,&col
3….&col
n);
Thi
swi
llpr
ompty
ouf
ort
hev
aluesbutf
orev
eryi
nser
tyouhav
etousef
orwar
dsl
ash.

Ex:
SQL>i
nser
tint
ost
udentv
alues(
&no,
'&name'
,&mar
ks)
;

Ent
erv
aluef
orno:1
Ent
erv
aluef
orname:Jagan
Ent
erv
aluef
ormar
ks:300
ol
d 1:i
nser
tint
ost
udentv
alues(
&no,
'&name'
,&mar
ks)
new 1:i
nser
tint
ost
udentv
alues(
1,'
Jagan'
,300)

SQL>/

Ent
erv
aluef
orno:2
Ent
erv
aluef
orname:Nar
en
Ent
erv
aluef
ormar
ks:400
ol
d 1:i
nser
tint
ost
udentv
alues(
&no,
'&name'
,&mar
ks)
new 1:i
nser
tint
ost
udentv
alues(
2,'
Nar
en'
,400)

c)I
NSERTI
NGDATAI
NTOSPECI
FIEDCOLUMNSUSI
NGVALUEMETHOD

Sy
ntax:
i
nser
ti o<t
nt abl
e_name)
(col
1,col
2,col
3…Col
n)val val
ues( ue1,
val
ue2,
val
ue3….
Val
uen)
;
Ex:
SQL>i
nser
tint
ost
udent(
no,
name)v
alues(
3,’
Ramesh’
);
SQL>i
nser
tint
ost
udent(
no,
name)v
alues(
4,’
Madhu’
);

.
3

d)I
NSERTI
NGDATAI
NTOSPECI
FIEDCOLUMNSUSI
NGADDRESSMETHOD

Sy
ntax:
i
nser
ti o<t
nt abl
e_name)
(col
1,col
2,col
3…col
n)val
ues(
&col
1,&col
2….
&col
n);
Thi
swi
llpr
ompty
ouf
ort
hev
aluesbutf
orev
eryi
nser
tyouhav
etousef
orwar
dsl
ash.

Ex:
SQL>i
nser
tint
ost
udent(
no,
name)v
alues(
&no,
'&name'
);
Ent
erv
aluef
orno:5
Ent
erv
aluef
orname:Vi
su
ol
d 1:i
nser
tint
ost
udent(
no,
name)v
alues(
&no,
'&name'
)
new 1:i
nser
tint
ost
udent(
no,
name)v
alues(
5,'
Visu'
)

SQL>/

Ent
erv
aluef
orno:6
Ent
erv
aluef
orname:Rat
tu
ol
d 1:i
nser
tint
ost
udent(
no,
name)v
alues(
&no,
'&name'
)
new 1:i
nser
tint
ost
udent(
no,
name)v
alues(
6,'
Rat
tu'
)

SELECTI
NGDATA

Sy
ntax:
Sel
ect*f
rom <t
abl
e_name>; -
-her
e*i
ndi
cat
esal
lcol
umns
or
ectcol
Sel 1,col
2,…col
nfom <t
r abl
e_name>;

Ex:
SQL>sel
ect*f
rom st
udent
;

NONAME MARKS
-
---
--
--
- -
--
--
--
-
1 Sudha 100
2 Saket
h 200
1 Jagan 300
2 Nar
en 400
3 Ramesh

.
4

4 Madhu
5 Vi
su
6 Rat
tu

SQL>sel
ectno,
name,
mar
ksf
rom st
udent
;

NONAME MARKS
-
---
--
--
- -
--
--
--
-
1 Sudha 100
2 Saket
h 200
1 Jagan 300
2 Nar
en 400
3 Ramesh
4 Madhu
5 Vi
su
6 Rat
tu

SQL>sel
ectno,
namef
rom st
udent
;

NONAME
-
---
--
--
--
1 Sudha
2 Saket
h
1 Jagan
2 Nar
en
3 Ramesh
4 Madhu
5 Vi
su
6 Rat
tu

CONDI
TIONALSELECTI
ONSANDOPERATORS

Wehav
etwocl
ausesusedi
nthi
s
 Wher
e
 Or
derby

.
5

USI
NGWHERE

Sy
ntax:
sel
ect*f
rom <t
abl
e_name>wher
e<condi
ti
on>;

t
hef
oll
owi
ngar
ethedi
ff
erentt
ypesofoper
ator
susedi
nwher
ecl
ause.

 Ar
it
hmet
icoper
ator
s
 Compar
isonoper
ator
s
 Logi
caloper
ator
s

 Ar
it
hmet
icoper
ator
s -
-hi
ghestpr
ecedence
+,
-,*
,/
 Compar
isonoper
ator
s
 =,
!=,
>,<,
>=,
<=,
<>
 bet
ween,
notbet
ween
 i
n,noti
n
 nul
l,
notnul
l
 l
ike
 Logi
caloper
ator
s
 And
 Or -
-lowestpr
ecedence
 not

a)USI
NG=,>,
<,>=,
<=,
!=,
<>

Ex:
SQL>sel
ect*f
rom st
udentwher
eno=2;

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
2 Saket
h 200
2 Nar
en 400

SQL>sel
ect*f
rom st
udentwher
eno<2;

NONAME MARKS

.
6

-
---
--
--
-- -
--
--
--
--
-
1 Sudha 100
1 Jagan 300

SQL>sel
ect*f
rom st
udentwher
eno>2;

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
-
3 Ramesh
4 Madhu
5 Vi
su
6 Rat
tu

SQL>sel
ect*f
rom st
udentwher
eno<=2;

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
-
1 Sudha 100
2 Saket
h 200
1 Jagan 300
2 Nar
en 400

SQL>sel
ect*f
rom st
udentwher
eno>=2;

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
2 Saket
h 200
2 Nar
en 400
3 Ramesh
4 Madhu
5 Vi
su
6 Rat
tu

SQL>sel
ect*f
rom st
udentwher
eno!
=2;

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
-

.
7

1 Sudha 100
1 Jagan 300
3 Ramesh
4 Madhu
5 Vi
su
6 Rat
tu

SQL>sel
ect*f
rom st
udentwher
eno<>2;

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
-
1 Sudha 100
1 Jagan 300
3 Ramesh
4 Madhu
5 Vi
su
6 Rat
tu

b)USI
NGAND

Thi
swi
llgi
vest
heout
putwhenal
lthecondi
ti
onsbecomet
rue.

Sy
ntax:
sel
ect*f
rom <t
abl
e_name>wher
e<condi
ti
on1>and<condi
ti
on2>and.
.
<condi
ti
onn>;
Ex:
SQL>sel
ect*f
rom st
udentwher
eno=2andmar
ks>=200;

NONAME MARKS
-
---
--
--
-- -
--
--
--
-
2 Saket
h 200
2 Nar
en 400

c)USI
NGOR

Thi
swi
llgi
vest
heout
putwhenei
theroft
hecondi
ti
onsbecomet
rue.

.
8

Sy
ntax:
sel
ect*f
rom <t
abl
e_name>wher
e<condi
ti
on1>and<condi
ti
on2>or.
.
<condi
ti
onn>;
Ex:
SQL>sel
ect*f
rom st
udentwher
eno=2ormar
ks>=200;

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
2 Saket
h 200
1 Jagan 300
2 Nar
en 400

d)USI
NGBETWEEN

Thi
swi
llgi
vest
heout
putbasedont
hecol
umnandi
tsl
owerbound,
upper
bound.

Sy
ntax:
sel
ect*f
rom <t
abl
e_name>wher
e<col ween<l
>bet owerbound>and<upper
bound>;

Ex:
SQL>sel
ect*f
rom st
udentwher
emar
ksbet
ween200and400;

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
2 Saket
h 200
1 Jagan 300
2 Nar
en 400

e)USI
NGNOTBETWEEN

Thi
swi
llgi
vest
heout
putbasedont
hecol
umnwhi
chv
aluesar
enoti
nit
slowerbound,
upper
bound.

Sy
ntax:
sel
ect*f
rom <t
abl
e_name>wher
e<col ween<l
>notbet owerbound>and<upper
bound>;

.
9

Ex:
SQL>sel
ect*f
rom st
udentwher
emar
ksnotbet
ween200and400;

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
1 Sudha 100

f
)USI
NGI
N

Thi
swi
llgi
vest
heout
putbasedont
hecol
umnandi
tsl
i
stofv
aluesspeci
fi
ed.

Sy
ntax:
sel
ect*f
rom <t
abl
e_name>wher
e<col
>in(v
alue1,
val
ue2,
val
ue3…val
uen)
;

Ex:
SQL>sel
ect*f
rom st
udentwher
enoi
n(1,
2,3)
;

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
1 Sudha 100
2 Saket
h 200
1 Jagan 300
2 Nar
en 400
3 Ramesh

g)USI
NGNOTI
N

Thi
swi
llgi
vest
heout
putbasedont
hecol
umnwhi
chv
aluesar
enoti
nthel
istof
v
aluesspeci
fi
ed.

Sy
ntax:
sel
ect*f
rom <t
abl
e_name>wher
e<col n(v
>noti alue1,
val
ue2,
val
ue3…val
uen)
;

Ex:
SQL>sel
ect*f
rom st
udentwher
enonoti
n(1,
2,3)
;

NONAME MARKS

.
10

-
---
--
--
-- -
--
--
--
--
4 Madhu
5 Vi
su
6 Rat
tu

h)USI
NGNULL

Thi
swi
llgi
vest
heout
putbasedont
henul
lval
uesi
nthespeci
fi
edcol
umn.

Sy
ntax:
sel
ect*f
rom <t
abl
e_name>wher
e<col
>isnul
l
;

Ex:
SQL>sel
ect*f
rom st
udentwher
emar
ksi
snul
l
;

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
3 Ramesh
4 Madhu
5 Vi
su
6 Rat
tu

i
)USI
NGNOTNULL

Thi
swi
llgi
vest
heout
putbasedont
henotnul
lval
uesi
nthespeci
fi
edcol
umn.

Sy
ntax:
sel
ect*f
rom <t
abl
e_name>wher
e<col
>isnotnul
l
;

Ex:
SQL>sel
ect*f
rom st
udentwher
emar
ksi
snotnul
l
;
NONAME MARKS
-
---
--
--
-- -
--
--
--
--
1 Sudha 100
2 Saket
h 200
1 Jagan 300
2 Nar
en 400

.
11

j
)USI
NGLI
KE

Thi
swi
llbeusedt
osear
cht
hrought
her
owsofdat
abasecol
umnbasedont
hepat
ter
n
y
ouspeci
fy.

Sy
ntax:
sel
ect*f
rom <t
abl
e_name>wher
e<col
>lke<pat
i ter
n>;

Ex:
i
)Thi
swi
llgi
vet
her
owswhosemar
ksar
e100.

SQL>sel
ect*f
rom st
udentwher
emar
ksl
ike100;

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
1 Sudha 100
i
i)Thi
swi
llgi
vet
her
owswhosenamest
artwi
th‘
S’.

SQL>sel
ect*f
rom st
udentwher
enamel
ike'
S%'
;

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
1 Sudha 100
2 Saket
h 200

i
ii
)Thi
swi
llgi
vet
her
owswhosenameendswi
th‘
h’
.

SQL>sel
ect*f
rom st
udentwher
enamel
ike'
%h'
;

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
2 Saket
h 200
3 Ramesh

i
V)Thi
swi
llgi
vet
her
owswhosename’
ssecondl
ett
erst
artwi
th‘
a’
.

.
12

SQL>sel
ect*f
rom st
udentwher
enamel
ike'
_a%'
;

NONAME MARKS
-
---
--
--
-- -
--
--
--
-
2 Saket
h 200
1 Jagan 300
2 Nar
en 400
3 Ramesh
4 Madhu
6 Rat
tu

V)Thi
swi
llgi
vet
her
owswhosename’
sthi
rdl
ett
erst
artwi
th‘
d’
.

SQL>sel
ect*f
rom st
udentwher
enamel
ike'
__d%'
;

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
1 Sudha 100
4 Madhu

Vi
)Thi
swi
llgi
vet
her
owswhosename’
ssecondl
ett
erst
artwi
th‘
t’f
rom endi
ng.

SQL>sel
ect*f
rom st
udentwher
enamel
ike'
%_t
%';

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
2 Saket
h 200
6 Rat
tu

Vi
i)Thi
swi
llgi
vet
her
owswhosename’
sthi
rdl
ett
erst
artwi
th‘
e’f
rom endi
ng.

SQL>sel
ect*f
rom st
udentwher
enamel
ike'
%e_
_%'
;

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
2 Saket
h 200
3 Ramesh

.
13

Vi
ii
)Thi
swi
llgi
vet
her
owswhosenamecot
ains2a’
s.

SQL>sel
ect*f
rom st
udentwher
enamel
ike'
%a%a%'
;

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
-
1 Jagan 300

*Youhav
etospeci
fyt
hepat
ter nl
nsi i
keusi
ngunder
scor
e(_)
.

USI
NGORDERBY

Thi
swi
llbeusedt
oor
der
ingt
hecol
umnsdat
a(ascendi
ngordescendi
ng)
.

Sy
ntax:
Sel
ect*f
rom <t
abl
e_name>or
derby<col
>desc;
Bydef
aul
tor
acl
ewi
lluseascendi
ngor
der
.
I
fyouwantout
puti
ndescendi
ngor
dery
ouhav
etousedesckey
wor
daf
tert
hecol
umn.

Ex:
SQL>sel
ect*f
rom st
udentor
derbyno;

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
1 Sudha 100
1 Jagan 300
2 Saket
h 200
2 Nar
en 400
3 Ramesh
4 Madhu
5 Vi
su
6 Rat
tu

SQL>sel
ect*f
rom st
udentor
derbynodesc;

.
14

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
6Rat
tu
5Vi
su
4Madhu
3Ramesh
2Saket
h 200
2Nar
en 400
1Sudha 100
1Jagan 300

USI
NGDML

USI
NGUPDATE

Thi
scanbeusedt
omodi
fyt
het
abl
edat
a.

Sy
ntax:
e<t
Updat abl
e_name>set<col
1>=val <col
ue1, 2>=val e<condi
ue2wher ti
on>;

Ex:
SQL>updat
est
udentsetmar
ks=500;
I
fyouar
enotspeci
fyi
nganycondi
ti
ont
hiswi
llupdat
eent
ir
etabl
e.

SQL>updat
est
udentsetmar
ks=500wher
eno=2;
SQL>updat
est
udentsetmar
ks=500,
name='
Venu'
wher
eno=1;

USI
NGDELETE

Thi
scanbeusedt
odel
etet
het
abl
edat
atempor
ari
ly
.

Sy
ntax:
Del
ete<t
abl
e_name>wher
e<condi
ti
on>;

Ex:
SQL>del
etest
udent
;

.
15

I
fyouar
enotspeci
fyi
nganycondi
ti
ont
hiswi
lldel
eteent
ir
etabl
e.

SQL>del
etest
udentwher
eno=2;

USI
NGDDL

USI
NGALTER

Thi
scanbeusedt
oaddorr
emov
ecol
umnsandt
omodi
fyt
hepr
eci
sionoft
hedat
aty
pe.

a)ADDI
NGCOLUMN

Sy
ntax:
al
tert e<t
abl abl
e_name>add<coldat
aty
pe>;

Ex:
SQL>al
tert
abl
est
udentaddsdobdat
e;

b)REMOVI
NGCOLUMN

Sy
ntax:
al
tert e<t
abl abl
e_name>dr
op<coldat
aty
pe>;

Ex:
SQL>al
tert
abl
est
udentdr
opcol
umnsdob;

c)I
NCREASI
NGORDECREASI
NGPRECI
SIONOFACOLUMN

Sy
ntax:
al
tert e<t
abl abl
e_name>modi
fy<coldat
aty
pe>;
Ex:
SQL>al
tert
abl
est
udentmodi
fymar
ksnumber
(5)
;

.
16

*Todecr
easepr
eci
siont
hecol
umnshoul
dbeempt
y.

d)MAKI
NGCOLUMNUNUSED

Sy
ntax:
al
tert e<t
abl abl
e_name>setunusedcol
umn<col
>;
Ex:
SQL>al
tert
abl
est
udentsetunusedcol
umnmar
ks;

Ev
ent
hought
hecol
umni
sunusedst
il
litwi
lloccupymemor
y.

d)DROPPI
NGUNUSEDCOLUMNS

Sy
ntax:
al
tert e<t
abl abl
e_name>dr
opunusedcol
umns;

Ex:
SQL>al
tert
abl
est
udentdr
opunusedcol
umns;
*Youcannotdr
opi
ndi
vi
dualunusedcol
umnsofat
abl
e.

e)RENAMI
NGCOLUMN

Sy
ntax:
al
tert e<t
abl abl
e_name>r umn<ol
enamecol d_col
_name>t
o<new_
col
_name>;

Ex:
SQL>al
tert
abl
est
udentr
enamecol
umnmar
kst
osmar
ks;

USI
NGTRUNCATE

Thi
scanbeusedt
odel
etet
heent
ir
etabl
edat
aper
manent
ly.
Sy
ntax:
t
runcat
et e<t
abl abl
e_name>;

Ex:
SQL>t
runcat
etabl
est
udent
;

.
17

USI
NGDROP

Thi
swi
llbeusedt
odr
opt
hedat
abaseobj
ect
;

Sy
ntax:
Dr
opt e<t
abl abl
e_name>;

Ex:
SQL>dr
opt
abl
est
udent
;

USI
NGRENAME

Thi
swi
llbeusedt
orenamet
hedat
abaseobj
ect
;

Sy
ntax:
r
ename<ol
d_t
abl
e_name>t
o<new_
tabl
e_name>;

Ex:
SQL>r
enamest
udentt
ost
ud;

.
18

USI
NGTCL

USI
NGCOMMI
T

Thi
swi
llbeusedt
osav
ethewor
k.
Commi
tisoft
wot
ypes.
 I
mpl
ici
t
 Expl
ici
t

a)I
MPLI
CIT

Thi
swi
llbei
ssuedbyor
acl
eint
ernal
lyi
ntwosi
tuat
ions.
 WhenanyDDLoper
ati
oni
sper
for
med.
 Wheny
ouar
eexi
ti
ngf
rom SQL*PLUS.

b)EXPLI
CIT

Thi
swi
llbei
ssuedbyt
heuser
.

Sy
ntax:
Commi
torcommi
twor
k;
*Whenev
ery
oucommi
tt
edt
hent
het
ransact
ionwascompl
eted.

USI
NGROLLBACK

Thi
swi
llundot
heoper
ati
on.
Thi
swi
llbeappl
iedi
ntwomet
hods.
 Upt
opr
evi
ouscommi
t
 Upt
opr
evi
ousr
oll
back

Sy
ntax:
Rol
lorr
ollwor
k;

.
19

Or
Rol
lbackorr
oll
backwor
k;
*Whi
lepr
ocessi
sgoi
ngon,
ifsuddenl
ypowergoest
henor
acl
ewi
llr
oll
backt
het
ransact
ion.
USI
NGSAVEPOI
NT

Youcanusesav
epoi
ntst
orol
lbackpor
ti
onsofy
ourcur
rentsetoft
ransact
ions.

Sy
ntax:
Sav nt<sav
epoi epoi
nt_
name>;

Ex:
SQL>sav
epoi
nts1;
SQL>i
nser
tint
ost
udentv
alues(
1,‘
a’
,100)
;
SQL>sav
epoi
nts2;
SQL>i
nser
tint
ost
udentv
alues(
2,‘
b’
,200)
;
SQL>sav
epoi
nts3;
SQL>i
nser
tint
ost
udentv
alues(
3,‘
c’
,300)
;
SQL>sav
epoi
nts4;
SQL>i
nser
tint
ost
udentv
alues(
4,‘
d’
,400)
;

Bef
orer
oll
back

SQL>sel
ect*f
rom st
udent
;

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
-
1 a 100
2 b 200
3 c 300
4 d 400

SQL>r
oll
backt
osav
epoi
nts3;
Or
SQL>r
oll
backt
os3;

Thi
swi
llr
oll
backl
astt
wor
ecor
ds.
SQL>sel
ect*f
rom st
udent
;

.
20

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
-
1 a 100
2 b 200

USI
NGDCL

.
21

DCLcommandsar
eusedt
ogr
ant
ingandr
evoki
ngt
heper
missi
ons.

USI
NGGRANT

Thi
sisusedt
ogr
antt
hepr
ivi
legest
oot
heruser
s.

Sy
ntax:
ant<pr
Gr ivi
leges>on<obj
ect
_name>t
o<user
_name>[
wit
hgr
antopt
ion]
;

Ex:
SQL>gr
antsel
ectonst
udentt
osudha; -
-youcangi
vei
ndi
vi
dual
pri
vi
lege
SQL>gr
antsel
ect
,inser
tonst
udentt
osudha;-
-youcangi
vesetofpr
ivi
leges
SQL>gr
antal
lonst
udentt
osudha; -
-youcangi
veal
lpr
ivi
leges

Thesudhauserhast
ousedotmet
hodt
oaccesst
heobj
ect
.
SQL>sel
ect*f
rom saket
h.st
udent
;
Thesudhausercannotgr
antper
missi
ononst
udentt
abl
etoot
heruser
s.Togett
his
t
ypeofopt
ionuset
hef
oll
owi
ng.
SQL>gr
antal
lonst
udentt
osudhawi
thgr
antopt
ion;
Nowsudhauseral
sogr
antper
missi
onsonst
udentt
abl
e.

USI
NGREVOKE

Thi
sisusedt
orev
oket
hepr
ivi
legesf
rom t
heuser
stowhi
chy
ougr
ant
edt
hepr
ivi
leges.

Sy
ntax:
oke<pr
Rev ivi
leges>on<obj
ect
_name>f
rom <user
_name>;

Ex:
SQL>r
evokesel
ectonst
udentf
orm sudha; -
-youcanr
evokei
ndi
vi
dual
pri
vi
lege
SQL>r
evokesel
ect
,inser
tonst
udentf
rom sudha;-
-youcanr
evokesetofpr
ivi
leges
SQL>r
evokeal
lonst
udentf
rom sudha; -
-youcanr
evokeal
lpr
ivi
leges

USI
NGALI
ASES

CREATEWI
THSELECT

.
22

Wecancr
eat
eat
abl
eusi
ngexi
sti
ngt
abl
e[al
ongwi
thdat
a].

Sy
ntax:
Cr
eat
et e<new_
abl tabl
e_name>[
col
1,col
2,col
3..
.col
n]assel
ect*f
rom
<ol
d_t
abl
e_name>;
Ex:
SQL>cr
eat
etabl
est
udent
1assel
ect*f
rom st
udent
;

Cr
eat
ingt
abl
ewi
thy
ourowncol
umnnames.
SQL>cr
eat
etabl
est
udent
2(sno,
sname,
smar
ks)assel
ect*f
rom st
udent
;

Cr
eat
ingt
abl
ewi
thspeci
fi
edcol
umns.
SQL>cr
eat
etabl
est
udent
3assel
ectno,
namef
rom st
udent
;

Cr
eat
ingt
abl
ewi
thoutt
abl
edat
a.
SQL>cr
eat
etabl
est
udent
2(sno,
sname,
smar
ks)assel
ect*f
rom st
udentwher
e1=2;
I
ntheabov
ewher
ecl
ausegi
veanycondi
ti
onwhi
chdoesnotsat
isf
y.

I
NSERTWI
THSELECT

Usi
ngt
hiswecani
nser
texi
sti
ngt
abl
edat
atoaanot
hert
abl
einasi
ngl
etr
ip.Butt
het
abl
est
ruct
ureshoul
dbe
same.

Sy
ntax:
I
nser
ti o<t
nt abl
e1>sel
ect*f
rom <t
abl
e2>;

Ex:
SQL>i
nser
tint
ost
udent
1sel
ect*f
rom st
udent
;

I
nser
ti
ngdat
aint
ospeci
fi
edcol
umns
SQL>i
nser
tint
ost
udent
1(no,
name)sel
ectno,
namef
rom st
udent
;

COLUMNALI
ASES

Sy
ntax:
Sel
ect<or
ginal
_col
><al
ias_
name>f
rom <t
abl
e_name>;

.
23

Ex:
SQL>sel
ectnosnof
rom st
udent
;
or
SQL>sel
ectno“
sno”f
rom st
udent
;

TABLEALI
ASES

I
fyouar
eusi
ngt
abl
eal
iasesy
oucanusedotmet
hodt
othecol
umns.

Sy
ntax:
ect<al
Sel ias_
name>.
<col
1>,
<al
ias_
name>.
<col
2>…<al
ias_
name>.
<col
n>f
rom
<t
abl
e_name><al
ias_
name>;
Ex:
SQL>sel
ects.
no,
s.namef
rom st
udents;

USI
NGMERGE

MERGE

Youcanusemer
gecommandt
oper
for
minser
tandupdat
einasi
ngl
ecommand.

Ex:

.
24

SQL>Mer
gei
ntost
udent
1s1
Usi
ng(
sel
ect*
From st
udent
2)s2
On(
s1.
no=s2.
no)
Whenmat
chedt
hen
Updat
esetmar
ks=s2.
mar
ks
Whennotmat
chedt
hen
I
nser
t(s1.
no,
s1.
name,
s1.
mar
ks)
Val
ues(
s2.
no,
s2.
name,
s2.
mar
ks)
;

I
ntheabov
ethet
wot
abl
esar
ewi
tht
hesamest
ruct
urebutwecanmer
gedi
ff
erentst
ruct
uredt
abl
esal
sobutt
he
dat
aty
peoft
hecol
umnsshoul
dmat
ch.

Assumet
hatst
udent
1hascol
umnsl
ikeno,
name,
mar
ksandst
udent
2hascol
umnsl
ikeno,
name,
hno,
cit
y.

SQL>Mer
gei
ntost
udent
1s1
Usi
ng(
sel
ect*
From st
udent
2)s2
On(
s1.
no=s2.
no)
Whenmat
chedt
hen
Updat
esetmar
ks=s2.
hno
Whennotmat
chedt
hen
I
nser
t(s1.
no,
s1.
name,
s1.
mar
ks)
Val
ues(
s2.
no,
s2.
name,
s2.
hno)
;

MULTI
PLEI
NSERTS

Wehav
etabl
ecal
ledDEPTwi
tht
hef
oll
owi
ngcol
umnsanddat
a

DEPTNO DNAMELOC
-
--
--
--
- -
--
--
--
- -
--
-
10 account
ing newy
ork

.
25

20 r
esear
ch dal
las
30 sal
es Chi
cago
40 oper
ati
ons bost
on

a)CREATESTUDENTTABLE

SQL>Cr
eat
etabl
est
udent
(nonumber
(2)
,namev
archar
(2)
,mar
ksnumber
(3)
);

b)MULTII
NSERTWI
THALLFI
ELDS

SQL>I
nser
tal
l
I
ntost
udentv
alues(
1,’
a’
,100)
I
ntost
udentv
alues(
2,’
b’
,200)
I
ntost
udentv
alues(
3,’
c’
,300)
Sel
ect*f
rom deptwher
edept
no=10;

-
-Thi
sinser
ts3r
ows

c)MULTII
NSERTWI
THSPECI
FIEDFI
ELDS

SQL>i
nser
tal
l
I
ntost
udent(
no,
name)v
alues(
4,’
d’
)
I
ntost
udent
(name,
mar
ks)v
alues(
’e’
,
400)
I
ntost
udentv
alues(
3,’
c’
,300)
Sel
ect*
from deptwher
edept
no=10;

-
-Thi
sinser
ts3r
ows

d)MULTII
NSERTWI
THDUPLI
CATEROWS

SQL>i
nser
tal
l
I
ntost
udentv
alues(
1,’
a’
,100)
I
ntost
udentv
alues(
2,’
b’
,200)
I
ntost
udentv
alues(
3,’
c’
,300)
Sel
ect*
from deptwher
edept
no>10;

-
-Thi
sinser
ts9r
owsbecausei
nthesel
ectst
atementr
etr
iev
es3r
ecor
ds(
3inser
tsf
or

.
26

eachr
owr
etr
iev
ed)

e)MULTII
NSERTWI
THCONDI
TIONSBASED

SQL>I
nser
tal
l
Whendept
no>10t
hen
I
ntost
udent
1val
ues(
1,’
a’
,100)
Whendname=‘
SALES’
then
I
ntost
udent
2val
ues(
2,’
b’
,200)
Whenl
oc=‘
NEW YORK’
then
I
ntost
udent
3val
ues(
3,’
c’
,300)
Sel
ect*
from deptwher
edept
no>10;

-
-Thi
sinser
ts4r
owsbecauset
hef
ir
stcondi
ti
onsat
isf
ied3t
imes,
secondcondi
ti
on
sat
isf
iedonceandt
hel
astnone.

f
)MULTII
NSERTWI
THCONDI
TIONSBASEDANDELSE

SQL>I
nser
tal
l
Whendept
no>100t
hen
I
ntost
udent
1val
ues(
1,’
a’
,100)
Whendname=‘
S’t
hen
I
ntost
udent
2val
ues(
2,’
b’
,200)
Whenl
oc=‘
NEW YORK’
then
I
ntost
udent
3val
ues(
3,’
c’
,300)
El
se
I
ntost
udentv
alues(
4,’
d’
,400)
Sel
ect*
from deptwher
edept
no>10;

-
-Thi
sinser
ts3r
ecor
dsbecauset
heel
sesat
isf
ied3t
imes

g)MULTII
NSERTWI
THCONDI
TIONSBASEDANDFI
RST

SQL>I
nser
tfi
rst
Whendept
no=20t
hen
I
ntost
udent
1val
ues(
1,’
a’
,100)
Whendname=‘
RESEARCH’
then

.
27

I
ntost
udent
2val
ues(
2,’
b’
,200)
Whenl
oc=‘
NEW YORK’
then
I
ntost
udent
3val
ues(
3,’
c’
,300)
Sel
ect*
from deptwher
edept
no=20;

-
-Thi
sinser
ts1r
ecor
dbecauset
hef
ir
stcl
auseav
oidt
ocheckt
her
emai
ning
condi
ti
onsoncet
hecondi
ti
oni
ssat
isf
ied.

h)MULTII
NSERTWI
THCONDI
TIONSBASED,
FIRSTANDELSE

SQL>I
nser
tfi
rst
Whendept
no=30t
hen
I
ntost
udent
1val
ues(
1,’
a’
,100)
Whendname=‘
R’t
hen
I
ntost
udent
2val
ues(
2,’
b’
,200)
Whenl
oc=‘
NEW YORK’
then
I
ntost
udent
3val
ues(
3,’
c’
,300)
El
se
I
ntost
udentv
alues(
4,’
d’
,400)
Sel
ect*
from deptwher
edept
no=20;

-
-Thi
sinser
ts1r
ecor
dbecauset
heel
secl
ausesat
isf
iedonce

i
)MULTII
NSERTWI
THMULTI
BLETABLES

SQL>I
nser
tal
l
I
ntost
udent
1val
ues(
1,’
a’
,100)
I
ntost
udent
2val
ues(
2,’
b’
,200)
I
ntost
udent
3val
ues(
3,’
c’
,300)
Sel
ect*
from deptwher
edept
no=10;

-
-Thi
sinser
ts3r
ows

*
*Youcanusemul
tit
abl
eswi
thspeci
fi
edf
iel
ds,
wit
hdupl
icat
erows,
wit
hcondi
ti
ons,

.
28

wi
thf
ir
standel
secl
auses.

FUNCTI
ONS

Funct
ionscanbecat
egor
izedasf
oll
ows.

 Si
ngl
erowf
unct
ions
 Gr
oupf
unct
ions

SI
NGLEROW FUNCTI
ONS

Si
ngl
erowf
unct
ionscanbecat
egor
izedi
ntof
ive.Thesewi
llbeappl
iedf
oreachr
owandpr
oducesi
ndi
vi
dual
out
putf
oreachr
ow.

.
29

 Numer
icf
unct
ions
 St
ri
ngf
unct
ions
 Dat
efunct
ions
 Mi
scel
laneousf
unct
ions
 Conv
ersi
onf
unct
ions

NUMERI
CFUNCTI
ONS
 Abs
 Si
gn
 Sqr
t
 Mod
 Nv
l
 Power
 Exp
 Ln
 Log
 Cei
l
 Fl
oor
 Round
 Tr
unk
 Bi
tand
 Gr
eat
est
 Least
 Coal
esce
a)ABS

Absol
utev
aluei
sthemeasur
eoft
hemagni
tudeofv
alue.
Absol
utev
aluei
sal
way
saposi
ti
venumber
.

Sy
nt val
ax:abs( ue)

Ex:
SQL>sel
ectabs(
5),
abs(
-5)
,abs(
0),
abs(
nul
l
)fr
om dual
;

ABS(
5) ABS(
-5) ABS(
0)ABS(
NULL)
-
--
--
--
--
---
--
--
--
---
--
--
--
--
---
--
--
--
--
--
-
5 5 0

.
30

b)SI
GN

Si
gngi
vest
hesi
gnofav
alue.

Sy
ntax:si val
gn( ue)

Ex:
SQL>sel
ectsi
gn(
5),
sign(
-5)
,si
gn(
0),
sign(
nul
l
)fr
om dual
;

SI
GN(
5) SI
GN(
-5) SI
GN(
0)SI
GN(
NULL)
-
--
--
--
--
---
--
--
--
-- -
--
--
--
--
---
--
--
--
--
--
--
1 -
1 0

c)SQRT

Thi
swi
llgi
vet
hesquar
erootoft
hegi
venv
alue.

Sy
ntax:sqr
t(val
ue) -
-her
eval
uemustbeposi
ti
ve.

Ex:
SQL>sel
ectsqr
t(4)
,sqr
t(0)
,sqr
t(nul
l
),sqr
t(1)f
rom dual
;
SQRT(
4) SQRT(
0)SQRT(
NULL) SQRT(
1)
-
--
--
--
--
---
--
--
--
---
--
--
--
--
--
--
---
--
--
--
--
-
2 0 1

d)MOD

Thi
swi
llgi
vet
her
emai
nder
.

Sy
nt val
ax:mod( ue,
divi
sor
)

Ex:
SQL>sel
ectmod(
7,4)
,mod(
1,5)
,mod(
nul
l
,nul
l
),mod(
0,0)
,mod(
-7,
4)f
rom dual
;

MOD(
7,4) MOD(
1,5)MOD(
NULL,
NULL) MOD(
0,0)MOD(
-7,
4)
-
--
--
--
--
--
---
--
--
--
---
--
--
--
--
--
--
--
--
--
---
--
--
--
--
---
--
--
--
--
--
--

.
31

3 1 0 -
3

e)NVL

Thi
swi
llsubst
it
utest
hespeci
fi
edv
aluei
nthepl
aceofnul
lval
ues.

Sy
ntax:nv
l(nul
l_
col
,repl
acement
_val
ue)

Ex:
SQL>sel
ect*f
rom st
udent
; -
-her
efor3rdr
owmar
ksv
aluei
snul
l

NONAME MARKS
-
---
--
--
-- -
--
--
--
--
1 a 100
2 b 200
3 c

SQL>sel
ectno,
name,
nvl
(mar
ks,
300)f
rom st
udent
;

NONAMENVL(
MARKS,
300)
-
---
--
--
---
--
--
--
--
--
--
--
--
--
--
1 a 100
2b 200
3c 300

SQL>sel
ectnv
l(1,
2),
nvl
(2,
3),
nvl
(4,
3),
nvl
(5,
4)f
rom dual
;

NVL(
1,2) NVL(
2,3) NVL(
4,3) NVL(
5,4)
-
--
--
--
--
---
--
--
--
---
--
--
--
--
---
--
--
--
--
1 2 4 5

SQL>sel
ectnv
l(0,
0),
nvl
(1,
1),
nvl
(nul
l
,nul
l
),nv
l(4,
4)f
rom dual
;

NVL(
0,0) NVL(
1,1)NVL(
nul
l
,nul
l
)NVL(
4,4)
-
--
--
--
--
---
--
--
--
---
--
--
--
--
--
--
--
---
--
--
--
--
-
0 1 4

.
32

f
)POWER

Poweri
stheabi
li
tyt
orai
seav
aluet
oagi
venexponent
.

Sy
nt val
ax:power( ue,
exponent
)

Ex:
SQL>sel
ectpower
(2,
5),
power
(0,
0),
power
(1,
1),
power
(nul
l,
nul
l)
,power
(2,
-5)
f
rom dual
;

POWER(
2,5)POWER(
0,0)POWER(
1,1)POWER(
NULL,
NULL)POWER(
2,-
5)
-
--
--
--
--
--
--
---
--
--
--
--
--
---
--
---
--
--
--
---
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
32 1 1 .
03125

g)EXP

Thi
swi
llr
aiseev
aluet
othegi
vepower
.

Sy
nt val
ax:exp( ue)
Ex:
SQL>sel
ectexp(
1),
exp(
2),
exp(
0),
exp(
nul
l
),exp(
-2)f
rom dual
;

EXP(
1) EXP(
2) EXP(
0)EXP(
NULL) EXP(
-2)
-
--
--
--
- -
--
--
--
-- -
--
--
--
---
--
--
--
--
--
---
--
--
--
--
2.
718281837.
3890561 1 .
135335283

h)LN

Thi
sisbasedonnat
uralorbaseel
ogar
it
hm.

Sy
ntax:l
n(val
ue) -
-her
eval
uemustbegr
eat
ert
hanzer
owhi
chi
sposi
ti
veonl
y.

Ex:
SQL>sel
ectl
n(1)
,ln(
2),
ln(
nul
l
)fr
om dual
;

LN(
1) LN(
2) LN(
NULL)
-
--
--
-- -
--
--
-- -
--
--
--
--
--
-

.
33

0 .
693147181

LnandExpar
ereci
procal
toeachot
her
.
EXP(
3)=20.
0855369
LN (
20.
0855369)=3

i
)LOG

Thi
sisbasedon10basedl
ogar
it
hm.

Sy
ntax:l
og( val
10, ue) -
-her
eval
uemustbegr
eat
ert
hanzer
owhi
chi
sposi
ti
veonl
y.

Ex:
SQL>sel
ectl
og(
10,
100)
,log(
10,
2),
log(
10,
1),
log(
10,
nul
l)f
rom dual
;

LOG(
10,
100)LOG(
10,
2)LOG(
10,
1)LOG(
10,
NULL)
-
--
--
--
--
--
--
---
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
--
--
--
-
2 .
301029996 0

LN (
val
ue)=LOG(
EXP(
1),
val
ue)

SQL>sel
ectl
n(3)
,log(
exp(
1),
3)f
rom dual
;

LN(
3) LOG(
EXP(
1),
3)
-
--
--
-- -
--
--
--
--
--
--
--
--
1.
09861229 1.
09861229

j
)CEI
L

Thi
swi
llpr
oduceawhol
enumbert
hati
sgr
eat
ert
hanorequalt
othespeci
fi
edv
alue.

Sy
ntax:cei
l(val
ue)

Ex:
SQL>sel
ectcei
l(
5),
cei
l
(5.
1),
cei
l(
-5)
,cei
l(-
5.1)
,cei
l(
0),
cei
l(
nul
l)f
rom dual
;

CEI
L(5)CEI
L(5.
1) CEI
L(-
5)CEI
L(-
5.1) CEI
L(0)CEI
L(NULL)

.
34

-
--
--
--
---
--
--
--
--
---
--
--
--
--
---
--
--
--
--
-- -
--
--
--
---
--
--
--
--
--
--
5 6 -
5 -
5 0
k)FLOOR

Thi
swi
llpr
oduceawhol
enumbert
hati
slesst
hanorequalt
othespeci
fi
edv
alue.

Sy
ntax:f
l val
oor( ue)

Ex:
SQL>sel
ectf
loor
(5)
,fl
oor
(5.
1),
floor
(-
5),
floor
(-5.
1),
floor
(0)
,fl
oor
(nul
l)f
rom
dual
;

FLOOR(
5)FLOOR(
5.1)FLOOR(
-5)FLOOR(
-5.
1) FLOOR(
0)FLOOR(
NULL)
-
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
---
--
--
--
--
--
--
--
-
5 5 -
5 -
6 0
l
)ROUND

Thi
swi
llr
oundsnumber
stoagi
vennumberofdi
git
sofpr
eci
sion.

Sy
ntax:r val
ound( ue,
preci
sion)

Ex:
SQL>sel
ectr
ound(
123.
2345)
,round(
123.
2345,
2),
round(
123.
2354,
2)f
rom dual
;

ROUND(
123.
2345)ROUND(
123.
2345,
0)ROUND(
123.
2345,
2)ROUND(
123.
2354,
2)
-
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
-
123 123 123.
23 123.
24

SQL>sel
ectr
ound(
123.
2345,
-1)
,round(
123.
2345,
-2)
,round(
123.
2345,
-3)
,
r
ound(
123.
2345,
-4)f
rom dual
;

ROUND(
123.
2345,
-1)ROUND(
123.
2345,
-2)ROUND(
123.
2345,
-3)ROUND(
123.
2345,
-4)
-
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
-
120 100 0 0

SQL>sel
ectr
ound(
123,
0),
round(
123,
1),
round(
123,
2)f
rom dual
;

.
35

ROUND(
123,
0)ROUND(
123,
1)ROUND(
123,
2)
-
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
-
123 123 123

SQL>sel
ectr
ound(
-123,
0),
round(
-123,
1),
round(
-123,
2)f
rom dual
;

ROUND(
-123,
0)ROUND(
-123,
1)ROUND(
-123,
2)
-
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
-
-
123 -
123 -
123

SQL>sel
ectr
ound(
123,
-1)
,round(
123,
-2)
,round(
123,
-3)
,round(
-123,
-1)
,round(
-
123,
-2)
,round(
-123,
-3)f
rom dual
;

ROUND(
123,
-1)ROUND(
123,
-
2)ROUND(
123,
-3)ROUND(
-123,
-1)ROUND(
-123,
-2)
ROUND(
-123,
-3)
-
--
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
-
120 100 0 -
120 -
100 0

SQL>sel
ectr
ound(
nul
l,
nul
l
),r
ound(
0,0)
,round(
1,1)
,round(
-1,
-1)
,round(
-2,
-2)
f
rom dual
;

ROUND(
NULL,
NULL)ROUND(
0,0)ROUND(
1,1)ROUND(
-1,
-1)ROUND(
-2,
-2)
-
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
0 1 0 0

m)TRUNC

Thi
swi
llt
runcat
esorchopsof
fdi
git
sofpr
eci
sionf
rom anumber
.

Sy
ntax:t
r val
unc( ue,
preci
sion)

Ex:
SQL>sel
ectt
runc(
123.
2345)
,tr
unc(
123.
2345,
2),
trunc(
123.
2354,
2)f
rom dual
;

TRUNC(
123.
2345)TRUNC(
123.
2345,
2)TRUNC(
123.
2354,
2)
-
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--

.
36

123 123.
23 123.
23

SQL>sel
ectt
runc(
123.
2345,
-1)
,tr
unc(
123.
2345,
-2)
,tr
unc(
123.
2345,
-3)
,
t
runc(
123.
2345,
-4)f
rom dual
;

TRUNC(
123.
2345,
-1)TRUNC(
123.
2345,
-2)TRUNC(
123.
2345,
-3)TRUNC(
123.
2345,
-4)
-
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
-
120 100 0 0

SQL>sel
ectt
runc(
123,
0),
trunc(
123,
1),
trunc(
123,
2)f
rom dual
;

TRUNC(
123,
0)TRUNC(
123,
1)TRUNC(
123,
2)
-
--
--
--
--
--
--
--
---
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
123 123 123

SQL>sel
ectt
runc(
-123,
0),
trunc(
-123,
1),
trunc(
-123,
2)f
rom dual
;

TRUNC(
-123,
0)TRUNC(
-123,
1)TRUNC(
-123,
2)
-
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
-
123 -
123 -
123

SQL>sel
ectt
runc(
123,
-1)
,tr
unc(
123,
-2)
,tr
unc(
123,
-3)
,tr
unc(
-123,
-1)
,tr
unc(
-
123,
2),
trunc(
-123,
-3)f
rom dual
;

TRUNC(
123,
-1)TRUNC(
123,
-
2)TRUNC(
123,
-3)TRUNC(
-123,
-1)TRUNC(
-123,
2)TRUNC(
-
123,
-3)
-
--
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
120 100 0 -
120 -
123 0

SQL>sel
ectt
runc(
nul
l
,nul
l
),t
runc(
0,0)
,tr
unc(
1,1)
,tr
unc(
-1,
-1)
,tr
unc(
-2,
-2)f
rom
dual
;

TRUNC(
NULL,
NULL)TRUNC(
0,0)TRUNC(
1,1)TRUNC(
-1,
-1)TRUNC(
-2,
-2)
-
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
--
--
---
--
--
--
--
--
--
--
-
0 1 0 0

.
37

n)BI
TAND

Thi
swi
llper
for
m bi
twi
seandoper
ati
on.

Sy
ntax:bi
t val
and( ue1,
val
ue2)

Ex:
SQL>sel
ectbi
tand(
2,3)
,bi
tand(
0,0)
,bi
tand(
1,1)
,bi
tand(
nul
l,
nul
l)
,bi
tand(
-2,
-3)
f
rom dual
;
BI
TAND(
2,3)BI
TAND(
0,0)BI
TAND(
1,1)BI
TAND(
NULL,
NULL)BI
TAND(
-2,
-3)
-
--
--
--
--
--
--
---
--
--
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
-
2 0 1 -
4

o)GREATEST

Thi
swi
llgi
vet
hegr
eat
estnumber
.

Sy
ntax:gr
eat val
est( ue1,
val
ue2,
val
ue3…val
uen)

Ex:
SQL>sel
ectgr
eat
est
(1,
2,3)
,gr
eat
est
(-
1,-
2,-
3)f
rom dual
;

GREATEST(
1,2,
3)GREATEST(
-1,
-2,
-3)
-
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
-
3 -
1

 I
fal
lthev
aluesar
ezer
ost
heni
twi
lldi
spl
ayzer
o.
 I
fal
lthepar
amet
ersar
enul
lst
heni
twi
lldi
spl
aynot
hing.
 I
fanyoft
hepar
amet
ersi
snul
li
twi
lldi
spl
aynot
hing.

p)LEAST

Thi
swi
llgi
vet
hel
eastnumber
.

Sy
ntax:l val
east( ue1,
val
ue2,
val
ue3…val
uen)

.
38

Ex:
SQL>sel
ectl
east
(1,
2,3)
,least
(-
1,-
2,-
3)f
rom dual
;

LEAST(
1,2,
3) LEAST(
-1,
-2,
-3)
-
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
-
1 -
3
 I
fal
lthev
aluesar
ezer
ost
heni
twi
lldi
spl
ayzer
o.
 I
fal
lthepar
amet
ersar
enul
lst
heni
twi
lldi
spl
aynot
hing.
 I
fanyoft
hepar
amet
ersi
snul
li
twi
lldi
spl
aynot
hing.
q)COALESCE

Thi
swi
llr
etur
nfi
rstnon-
nul
lval
ue.

Sy
ntax:coal val
esce( ue1,
val
ue2,
val
ue3…val
uen)

Ex:
SQL>sel
ectcoal
esce(
1,2,
3),
coal
esce(
nul
l
,2,
nul
l
,5)f
rom dual
;

COALESCE(
1,2,
3)COALESCE(
NULL,
2,
NULL,
5)
-
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
1 2

STRI
NGFUNCTI
ONS

 I
nit
cap
 Upper
 Lower
 Lengt
h
 Rpad
 Lpad
 Lt
ri
m
 Rt
ri
m
 Tr
im
 Tr
ansl
ate
 Repl
ace
 Soundex
 Concat(‘
||‘
Concat
enat
ionoper
ator
)

.
39

 Asci
i
 Chr
 Subst
r
 I
nst
r
 Decode
 Gr
eat
est
 Least
 Coal
esce
a)I
NITCAP

Thi
swi
llcapi
tal
izet
hei
nit
iall
ett
eroft
hest
ri
ng.

Sy
ntax:i
nit st
cap( r
ing)

Ex:
SQL>sel
ecti
nit
cap(
'comput
er'
)fr
om dual
;

I
NITCAP
-
--
--
--
--
--
Comput
er

b)UPPER

Thi
swi
llconv
ertt
hest
ri
ngi
ntoupper
case.

Sy
nt st
ax:upper( r
ing)

Ex:
SQL>sel
ectupper
('
comput
er'
)fr
om dual
;
UPPER
-
--
--
--
--
--
COMPUTER

c)LOWER

Thi
swi
llconv
ertt
hest
ri
ngi
ntol
ower
case.

.
40

Sy
ntax:l st
ower( r
ing)

Ex:
SQL>sel
ectl
ower
('
COMPUTER'
)fr
om dual
;

LOWER
-
--
--
--
--
--
comput
er

d)LENGTH

Thi
swi
llgi
vel
engt
hoft
hest
ri
ng.

Sy
ntax:l
engt
h(st
ri
ng)

Ex:
SQL>sel
ectl
engt
h('
comput
er'
)fr
om dual
;

LENGTH
-
--
--
--
--
--
8

e)RPAD

Thi
swi
llal
lowsy
out
opadt
her
ightsi
deofacol
umnwi
thanysetofchar
act
ers.

Sy
ntax:r st
pad( r
ing,
lengt
h[,
paddi
ng_
char
])

Ex:
SQL>sel
ectr
pad(
'comput
er'
,
15,
'
*')
,rpad(
'comput
er'
,
15,
'
*#'
)fr
om dual
;

RPAD(
'COMPUTER'RPAD(
'COMPUTER'
-
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
comput
er*
***
*** comput
er*
#*#*
#*

.
41

-
-Def
aul
tpaddi
ngchar
act
erwasbl
ankspace.

f
)LPAD

Thi
swi
llal
lowsy
out
opadt
hel
eftsi
deofacol
umnwi
thanysetofchar
act
ers.
Sy
ntax:l st
pad( r
ing,
lengt
h[,
paddi
ng_
char
])

Ex:
SQL>sel
ectl
pad(
'comput
er'
,
15,
'
*')
,lpad(
'comput
er'
,
15,
'
*#'
)fr
om dual
;

LPAD(
'COMPUTER'LPAD(
'COMPUTER'
-
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
*
***
***
comput
er*
#*#*
#*comput
er

-
-Def
aul
tpaddi
ngchar
act
erwasbl
ankspace.

g)LTRI
M

Thi
swi
llt
ri
m of
funwant
edchar
act
ersf
rom t
hel
eftendofst
ri
ng.

Sy
ntax:l
tr
i st
m( r
ing[
,
unwant
ed_
char
s])

Ex:
SQL>sel
ectl
tr
im(
'comput
er'
,
'co'
),l
tr
im(
'comput
er'
,
'com'
)fr
om dual
;

LTRI
M(LTRI
M
-
--
--
--
---
--
--
--
-
mput
er put
er

SQL>sel
ectl
tr
im(
'comput
er'
,
'put
er'
),l
tr
im(
'comput
er'
,
'omput
er'
)fr
om dual
;

LTRI
M('
C LTRI
M('
C
-
--
--
--
--
---
--
--
--
--
comput
er comput
er

-
-Ify
ouhav
en’
tspeci
fyanyunwant
edchar
act
ersi
twi
lldi
spl
ayent
ir
est
ri
ng.

.
42

h)RTRI
M

Thi
swi
llt
ri
m of
funwant
edchar
act
ersf
rom t
her
ightendofst
ri
ng.
Sy
ntax:r
tri
m(st
ri
ng[
,unwant
ed_
char
s])

Ex:
SQL>sel
ectr
tri
m('
comput
er'
,
'er
')
,rt
ri
m('
comput
er'
,
't
er'
)fr
om dual
;
RTRI
M(RTRI
M
-
--
--
--
---
--
--
--
-
computcompu

SQL>sel
ectr
tri
m('
comput
er'
,
'comput
’)
,rt
ri
m('
comput
er'
,
'comput
e')f
rom dual
;

RTRI
M('
C RTRI
M('
C
-
--
--
--
--
---
--
--
--
--
comput
er comput
er
-
-Ify
ouhav
en’
tspeci
fyanyunwant
edchar
act
ersi
twi
lldi
spl
ayent
ir
est
ri
ng.

i
)TRI
M

Thi
swi
llt
ri
m of
funwant
edchar
act
ersf
rom t
hebot
hsi
desofst
ri
ng.

Sy
ntax:t
ri
m(unwant
ed_
char
sfom st
r ri
ng)

Ex:
SQL>sel
ectt
ri
m('
i
'fr
om '
i
ndi
ani
'
)fr
om dual
;

TRI
M(
-
--
--
ndi
an

SQL>sel
ectt
ri
m(l
eadi
ng'
i
'fr
om '
i
ndi
ani
'
)fr
om dual
; -
-thi
swi
llwor
kasLTRI
M

TRI
M(L
-
--
--
-
ndi
ani

.
43

SQL>sel
ectt
ri
m(t
rai
li
ng'
i
'fr
om '
i
ndi
ani
'
)fr
om dual
; -
-thi
swi
llwor
kasRTRI
M

TRI
M(T
-
--
--
-
I
ndi
an

j
)TRANSLATE

Thi
swi
llr
epl
acet
hesetofchar
act
ers,
char
act
erbychar
act
er.

Sy
ntax:t
ransl
at st
e( r
ing,
old_
char
s,new_
char
s)

Ex:
SQL>sel
ectt
ransl
ate(
'i
ndi
a'
,'
i
n',
'
xy'
)fr
om dual
;

TRANS
-
--
--
--
-
xy
dxa

k)REPLACE

Thi
swi
llr
epl
acet
hesetofchar
act
ers,
str
ingbyst
ri
ng.

Sy
ntax:r
epl st
ace( r
ing,
old_
char
s[,
new_
char
s])

Ex:
SQL>sel
ectr
epl
ace(
'i
ndi
a'
,'
i
n',
'
xy'
),r
epl
ace(
‘i
ndi
a’,

in’
)fr
om dual
;

REPLACE REPLACE
-
--
--
--
--
---
--
--
--
--
--
Xy
dia di
a

l
)SOUNDEX

Thi
swi
llbeusedt
ofi
ndwor
dst
hatsoundl
ikeot
herwor
ds,
excl
usi
vel
yusedi
nwher
e
cl
ause.

.
44

Sy
nt st
ax:soundex( r
ing)

Ex:
SQL>sel
ect*f
rom empwher
esoundex(
ename)=soundex(
'SMI
T')
;

EMPNOENAME JOB MGRHI


REDATE SAL DEPTNO
-
--
--
--
---
--
--
-- -
--
-- -
--
---
--
--
--
--
--
- -
--
--
--
---
--
--
--
--
-
7369 SMI
TH CLERK 7902 17-
DEC-
80 500 20

m)CONCAT

Thi
swi
llbeusedt
ocombi
net
wost
ri
ngsonl
y.

Sy
nt st
ax:concat( r
ing1,
str
ing2)

Ex:
SQL>sel
ectconcat
('
comput
er'
,
'oper
ator
')f
rom dual
;

CONCAT(
'COMPUTER'
-
--
--
--
--
--
--
--
--
--
--
--
--
comput
eroper
ator

I
fyouwantt
ocombi
nemor
ethant
wost
ri
ngsy
ouhav
etouseconcat
enat
ion
oper
ator
(|
|)
.

SQL>sel
ect'
how'
||'
are'
||'
you'
from dual
;

'
HOW'
|
|'
ARE
-
--
--
--
--
--
--
--
howar
eyou

n)ASCI
I

Thi
swi
llr
etur
nthedeci
malr
epr
esent
ati
oni
nthedat
abasechar
act
ersetoft
hef
ir
st
char
act
eroft
hest
ri
ng.

Sy
ntax:asci
i(st
ri
ng)

.
45

Ex:
SQL>sel
ectasci
i
('a'
),asci
i(
'appl
e'
)fr
om dual
;

ASCI
I(
'A'
)ASCI
I(
'APPLE'
)
-
--
--
--
--
--
---
--
--
--
--
--
--
--
--
97 97

o)CHR

Thi
swi
llr
etur
nthechar
act
erhav
ingt
hebi
nar
yequi
val
entt
othest
ri
ngi
nei
thert
he
dat
abasechar
act
ersetort
henat
ional
char
act
erset
.

Sy
nt number
ax:chr( )

Ex:
SQL>sel
ectchr
(97)f
rom dual
;

CHR
-
--
--
a

p)SUBSTR

Thi
swi
llbeusedt
oext
ractsubst
ri
ngs.

Sy
ntax:subst
r(st
ri
ng,
star
t_chr
_count[
,no_
of_
char
s])

Ex:
SQL>sel
ectsubst
r('
comput
er'
,
2),
subst
r('
comput
er'
,
2,5)
,subst
r('
comput
er'
,
3,7)
f
rom dual
;

SUBSTR(SUBST SUBSTR
--
--
--
--
---
--
--
---
--
--
--
-
omputeromputmput er

.
46

fno_
 I of_
char
spar
amet
eri
snegat
ivet
heni
twi
lldi
spl
aynot
hing.
 I
fbot
hpar
amet
ersexceptst
ri
ngar
enul
lorzer
ost
heni
twi
lldi
spl
aynot
hing.
fno_
 I of_
char
spar
amet
eri
sgr
eat
ert
hant
hel
engt
hoft
hest
ri
ngt
heni
tignor
esandcal
cul
ates
basedont
heor
ginalst
ri
ngl
engt
h.
fst
 I art
_chr
_counti
snegat
ivet
heni
twi
llext
ractt
hesubst
ri
ngf
rom r
ightend.

1 2 3 4 5 6 7 8

C O M P U T E R

-
8 -
7 -
6 -
5 -
4 -
3 -
2 -
1

q)I
NSTR

Thi
swi
llal
lowsy
ouf
orsear
chi
ngt
hroughast
ri
ngf
orsetofchar
act
ers.

Sy
ntax:i
nst
r(st
ri
ng,
sear
ch_
str[
,st
art
_chr
_count[
,occur
rence]]
)

Ex:
SQL>sel
ecti
nst
r('
i
nfor
mat
ion'
,
'o'
,
4,1)
,inst
r('
i
nfor
mat
ion'
,
'o'
,
4,2)f
rom dual
;

I
NSTR(
'I
NFORMATI
ON'
,
'O'
,
4,1)I
NSTR(
'I
NFORMATI
ON'
,
'O'
,
4,2)
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
4 10

 I
fyouar
enotspeci
fyngst
i art
_chr
_countandoccur
rencet
heni
twi
llst
art
sear
chf
rom t
hebegi
nni
ngandf
indsf
ir
stoccur
renceonl
y.
 I
fbot
hpar
amet
ersst
art
_chr
_countandoccur
rencear
enul
l,
itwi
lldi
spl
ay
not
hing.

r
)DECODE

Decodewi
llactasv
aluebyv
aluesubst
it
uti
on.
Forev
eryv
alueoff
iel
d,i
twi
llchecksf
oramat
chi
naser
iesofi
f/t
hent
est
s.

Sy
nt val
ax:decode( ue,
if1,
then1,
if2,
then2,
…….el
se)
;

Ex:
SQL>sel
ectsal
,decode(
sal
,
500,
'
Low'
,
5000,
'
High'
,
'Medi
um'
)fr
om emp;

.
47

SAL DECODE
-
--
---
--
--
--
--
500 Low
2500 Medi
um
2000 Medi
um
3500 Medi
um
3000 Medi
um
5000 Hi
gh
4000 Medi
um
5000 Hi
gh
1800 Medi
um
1200 Medi
um
2000 Medi
um
2700 Medi
um
2200 Medi
um
3200 Medi
um

SQL>sel
ectdecode(
1,1,
3),
decode(
1,2,
3,
4,
4,
6)f
rom dual
;

DECODE(1,
1,
3)DECODE( 1,
2,3,
4,
4,
6)
-
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
---
--
3 6

 I
fthenumberofpar
amet
ersar
eoddanddi
ff
erentt
hendecodewi
lldi
spl
ay
not
hing.
 I
fthenumberofpar
amet
ersar
eev
enanddi
ff
erentt
hendecodewi
lldi
spl
ayl
ast
v
alue.
 I
fal
lthepar
amet
ersar
enul
lthendecodewi
lldi
spl
aynot
hing.
 I
fal
lthepar
amet
ersar
ezer
ost
hendecodewi
lldi
spl
ayzer
o.

s)GREATEST

Thi
swi
llgi
vet
hegr
eat
estst
ri
ng.

Sy
ntax:gr
eat st
est( r
ng1,
str
ing2,
str
ing3…st
ri
ngn)

Ex:

.
48

SQL>sel
ectgr
eat
est
('
a'
,'
b'
,'
c'
),gr
eat
est
('
sat
ish'
,
'sr
inu'
,
'saket
h')f
rom dual
;

GREATGREAT
-
--
--
---
--
--
--
c sr
inu

 I
fal
lthepar
amet
ersar
enul
lst
heni
twi
lldi
spl
aynot
hing.
 I
fanyoft
hepar
amet
ersi
snul
li
twi
lldi
spl
aynot
hing.

t
)LEAST

Thi
swi
llgi
vet
hel
eastst
ri
ng.

Sy
ntax:gr
eat st
est( r
ng1,
str
ing2,
str
ing3…st
ri
ngn)

Ex:
SQL>sel
ectl
east
('
a'
,'
b'
,'
c'
),l
east
('
sat
ish'
,
'sr
inu'
,
'saket
h')f
rom dual
;

LEASTLEAST
-
--
--
---
--
--
--
a saket
h

 I
fal
lthepar
amet
ersar
enul
lst
heni
twi
lldi
spl
aynot
hing.
 I
fanyoft
hepar
amet
ersi
snul
li
twi
lldi
spl
aynot
hing.

u)COALESCE

Thi
swi
llgi
vest
hef
ir
stnon-
nul
lst
ri
ng.

Sy
ntax:coal st
esce( r
ng1,
str
ing2,
str
ing3…st
ri
ngn)

Ex:
SQL>sel
ectcoal
esce(
'a'
,
'b'
,
'c'
),coal
esce(
nul
l
,'
a'
,nul
l
,'
b'
)fr
om dual
;

COALESCECOALESCE
-
--
--
--
--
---
--
--
--
--
--

.
49

a a

DATEFUNCTI
ONS

 Sy
sdat
e
 Cur
rent
_dat
e
 Cur
rent
_ti
mest
amp
 Sy
sti
mest
amp
 Local
ti
mest
amp
 Dbt
imezone
 Sessi
ont
imezone
 To_
char
 To_
dat
e
 Add_
mont
hs
 Mont
hs_
bet
ween
 Next
_day
 Last
_day
 Ext
ract
 Gr
eat
est
 Least
 Round
 Tr
unc
 New_
time
 Coal
esce

Or
acl
edef
aul
tdat
efor
mati
sDD- YY.
MON-

Wecanchanget
hedef
aul
tfor
matt
oourdesi
redf
ormatbyusi
ngt
hef
oll
owi
ngcommand.

SQL>al
tersessi
onsetnl
s_dat
e_f
ormat=‘
DD- YYYY’
MONTH- ;
Butt
hiswi
llexpi
reoncet
hesessi
onwascl
osed.

a)SYSDATE

Thi
swi
llgi
vet
hecur
rentdat
eandt
ime.
Ex:
SQL>sel
ectsy
sdat
efr
om dual
;

SYSDATE

.
50

-
--
--
--
--
--
24-
DEC-
06

b)CURRENT_DATE

Thi
swi
llr
etur
nst
hecur
rentdat
eint
hesessi
on’
sti
mezone.

Ex:
SQL>sel
ectcur
rent
_dat
efr
om dual
;

CURRENT_
DATE
-
--
--
--
--
--
--
--
--
-
24-
DEC-
06

c)CURRENT_TI
MESTAMP

Thi
swi
llr
etur
nst
hecur
rentt
imest
ampwi
tht
heact
ivet
imezonei
nfor
mat
ion.

Ex:
SQL>sel
ectcur
rent
_ti
mest
ampf
rom dual
;

CURRENT_
TIMESTAMP
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
24-
DEC-
0603.
42.
41.
383369AM +05:
30

d)SYSTI
MESTAMP

Thi
swi
llr
etur
nst
hesy
stem dat
e,i
ncl
udi
ngf
ract
ional
secondsandt
imezoneoft
he
dat
abase.

Ex:
SQL>sel
ectsy
sti
mest
ampf
rom dual
;
SYSTI
MESTAMP
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
24-
DEC-
0603.
49.
31.
830099AM +05:
30

.
51

e)LOCALTI
MESTAMP

Thi
swi
llr
etur
nsl
ocal
timest
ampi
ntheact
ivet
imezonei
nfor
mat
ion,
wit
hnot
ime
zonei
nfor
mat
ionshown.

Ex:
SQL>sel
ectl
ocal
ti
mest
ampf
rom dual
;

LOCALTI MESTAMP
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
24-DEC-0603.44.
18.502874AM

f
)DBTI
MEZONE

Thi
swi
llr
etur
nst
hecur
rentdat
abaset
imezonei
nUTCf
ormat
.(Coor
dinat
edUni
ver
sal
Ti
me)

Ex:
SQL>sel
ectdbt
imezonef
rom dual
;

DBTIMEZONE
-
--
---
--
--
--
--
-
-07:
00

g)SESSI
ONTI
MEZONE

Thi
swi
llr
etur
nst
hev
alueoft
hecur
rentsessi
on’
sti
mezone.

Ex:
SQL>sel
ectsessi
ont
imezonef
rom dual
;

SESSI
ONTI
MEZONE
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
+05:
30

h)TO_CHAR

Thi
swi
llbeusedt
oext
ractv
ari
ousdat
efor
mat
s.
Theav
ail
abl
edat
efor
mat
sasf
oll
ows.

.
52

Sy
ntax:t
o_ dat
char( e,f
ormat
)

DATEFORMATS

D -
- Noofday
sinweek
DD -
- Noofday
sinmont
h
DDD -
- Noofday
siny
ear
MM -
- Noofmont
h
MON -
- Thr
eel
ett
erabbr
evi
ati
onofmont
h
MONTH -
- Ful
lyspel
ledoutmont
h
RM -
- Romannumer
almont
h
DY -
- Thr
eel
ett
erabbr
evi
atedday
DAY -
- Ful
lyspel
ledoutday
Y -
- Lastonedi
gitoft
hey
ear
YY -
- Lastt
wodi
git
soft
hey
ear
YYY -
- Lastt
hreedi
git
soft
hey
ear
YYYY -
- Ful
lfourdi
gity
ear
SYYYY -
-Si
gnedy
ear
I -
- Onedi
gity
earf
rom I
SOst
andar
d
I
Y -
- Twodi
gity
earf
rom I
SOst
andar
d
I
YY -
- Thr
eedi
gity
earf
rom I
SOst
andar
d
I
YYY -
- Fourdi
gity
earf
rom I
SOst
andar
d
Y,
YYY -
- Yearwi
thcomma
YEAR -
- Ful
lyspel
ledouty
ear
CC -
- Cent
ury
Q -
- Noofquar
ter
s
W -
- Noofweeksi
nmont
h
WW -
- Noofweeksi
nyear
I
W -
- Noofweeksi
nyearf
rom I
SOst
andar
d
HH -
- Hour
s
MI -
- Mi
nut
es
SS -
- Seconds
FF -
- Fr
act
ional
seconds
AM orPM -
- Di
spl
aysAM orPM dependi
ngupont
imeofday
Mo
A. rP.
M -
- Di
spl
aysA.
M orP.
M dependi
ngupont
imeofday
ADorBC-
- Di
spl
aysADorBCdependi
ngupont
hedat
e

.
53

Do
A. rB.
C -
- Di
spl
aysADorBCdependi
ngupont
hedat
e
FM -
- Pr
efi
xtomont
horday
,suppr
essespaddi
ngofmont
horday
TH -
- Suf
fi
xtoanumber
SP -
- suf
fi
xtoanumbert
obespel
ledout
SPTH -
- Suf
fi
xcombi
nat
ionofTHandSPt
obebot
hspel
ledout
THSP -
- sameasSPTH

Ex:
SQL>sel
ectt
o_char
(sy
sdat
e,'
ddmont
hyy
yyhh:
mi:
ssam dy
')f
rom dual
;

TO_
CHAR(
SYSDATE,
'
DDMONTHYYYYHH:
MI
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
24december200602:
03:
23pm sun

SQL>sel
ectt
o_char
(sy
sdat
e,'
ddmont
hyear
')f
rom dual
;

TO_
CHAR(
SYSDATE,
'
DDMONTHYEAR'
)
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
24decembert
wot
housandsi
x

SQL>sel
ectt
o_char
(sy
sdat
e,'
ddf
mmont
hyear
')f
rom dual
;

TO_
CHAR(
SYSDATE,
'
DDFMMONTHYEAR'
)
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
24decembert
wot
housandsi
x

SQL>sel
ectt
o_char
(sy
sdat
e,'
ddt
hDDTH'
)fr
om dual
;

TO_
CHAR(
S
-
--
--
--
--
--
-
h24TH
24t

SQL>sel
ectt
o_char
(sy
sdat
e,'
ddspt
hDDSPTH'
)fr
om dual
;

TO_
CHAR(
SYSDATE,
'
DDSPTHDDSPTH
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-

.
54

t
went
y-f
our
thTWENTY-
FOURTH

SQL>sel
ectt
o_char
(sy
sdat
e,'
ddspDdspDDSP'
)fr
om dual
;

TO_
CHAR(
SYSDATE,
'
DDSPDDSPDDSP'
)
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
t
went
y-f
ourTwent
y-FourTWENTY-
FOUR

i
)TO_DATE

Thi
swi
llbeusedt
oconv
ertt
hest
ri
ngi
ntodat
afor
mat
.

Sy
ntax:t
o_dat
e(dat
e)

Ex:
SQL>sel
ectt
o_char
(to_
dat
e('
24/
dec/
2006'
,
'dd/
mon/
yyy
y')
,'dd*mont
h*day
')
f
rom dual
;

TO_
CHAR(
TO_
DATE(
'24/
DEC/
20
-
--
--
--
--
--
--
--
--
--
--
--
--
-
24*december*Sunday

-
-Ify
ouar
enotusi
ngt
o_charor
acl
ewi
lldi
spl
ayout
puti
ndef
aul
tdat
efor
mat
.

j
)ADD_MONTHS

Thi
swi
lladdt
hespeci
fi
edmont
hst
othegi
vendat
e.

Sy
ntax:add_
mont dat
hs( e,no_
of_
mont
hs)

Ex:
SQL>sel
ectadd_
mont
hs(
to_
dat
e('
11-
jan-
1990'
,
'dd-
mon-
yyy
y')
,5)f
rom dual
;

ADD_
MONTHS
-
--
--
--
--
--
--
--
-
11-
JUN-
90

.
55

SQL>sel
ectadd_
mont
hs(
to_
dat
e('
11-
jan-
1990'
,
'dd-
mon-
yyy
y')
,-5)f
rom dual
;

ADD_
MONTH
-
--
--
--
--
--
--
--
11-
AUG-
89

fno_
 I of_
mont
hsi
szer
otheni
twi
lldi
spl
ayt
hesamedat
e.
fno_
 I of_
mont
hsi
snul
ltheni
twi
lldi
spl
aynot
hing.

k)MONTHS_BETWEEN

Thi
swi
llgi
vedi
ff
erenceofmont
hsbet
weent
wodat
es.

Sy
ntax:mont
hs_
bet dat
ween( e1,
dat
e2)

Ex:
SQL>sel
ectmont
hs_
bet
ween(
to_
dat
e('
11-
aug-
1990'
,
'dd-
mon-
yyy
y')
,to_
dat
e('
11-
j
an-
1990'
,
'dd-
mon-
yyy
y')
)fr
om dual
;

MONTHS_
BETWEEN(
TO_
DATE(
'11-
AUG-
1990'
,
'DD-
MON-
YYYY'
),
TO_
DATE(
'11-
JAN-
1990'
,
'DD-
MON-
YYYY'
))
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
7
SQL>sel
ectmont
hs_
bet
ween(
to_
dat
e('
11-
jan-
1990'
,
'dd-
mon-
yyy
y')
,to_
dat
e('
11-
aug-
1990'
,
'dd-
mon-
yyy
y')
)fr
om dual
;

MONTHS_
BETWEEN(
TO_
DATE(
'11-
JAN-
1990'
,
'DD-
MON-
YYYY'
),
TO_
DATE(
'11-
AUG-
1990'
,
'DD-
MON-
YYYY'
))
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
7

l
)NEXT_DAY

Thi
swi
llpr
oducenextdayoft
hegi
vendayf
rom t
hespeci
fi
eddat
e.

Sy
ntax:next
_ dat
day( e,day
)

.
56

Ex:
SQL>sel
ectnext
_day
(to_
dat
e('
24-
dec-
2006'
,
'dd-
mon-
yyy
y')
,'
sun'
)fr
om dual
;

NEXT_
DAY(
-
--
--
--
--
--
--
31-
DEC-
06

-
-Ift
hedaypar
amet
eri
snul
ltheni
twi
lldi
spl
aynot
hing.

m)LAST_DAY

Thi
swi
llpr
oducel
astdayoft
hegi
vendat
e.

Sy
ntax:l
ast
_ dat
day( e)

Ex:
SQL>sel
ectl
ast
_day
(to_
dat
e('
24-
dec-
2006'
,
'dd-
mon-
yyy
y')
,'
sun'
)fr
om dual
;
LAST_
DAY(
-
--
--
--
--
--
--
31-
DEC-
06

n)EXTRACT

Thi
sisusedt
oext
ractapor
ti
onoft
hedat
eval
ue.

Sy
ntax:ext
ract(
(year|mont
h|day|hour|mi
nut
e|second)
,dat
e)

Ex:
SQL>sel
ectext
ract
(yearf
rom sy
sdat
e)f
rom dual
;
EXTRACT(
YEARFROMSYSDATE)
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
2006

-
-Youcanext
ractonl
yonev
alueatat
ime.

o)GREATEST

.
57

Thi
swi
llgi
vet
hegr
eat
estdat
e.

Sy
ntax:gr
eat dat
est( e1,
dat
e2,
dat
e3…dat
en)

Ex:
SQL>sel
ectgr
eat
est
(to_
dat
e('
11-
jan-
90'
,
'dd-
mon-
yy'
),
to_
dat
e('
11-
mar
-90'
,
'dd-
mon-
yy'
),
to_
dat
e('
11-
apr
-90'
,
'dd-
mon-
yy'
))f
rom dual
;

GREATEST(
-
--
--
--
--
--
--
11-
APR-
90

p)LEAST

Thi
swi
llgi
vet
hel
eastdat
e.
Sy
ntax:l dat
east( e1,
dat
e2,
dat
e3…dat
en)

Ex:
SQL>sel
ectl
east
(to_
dat
e('
11-
jan-
90'
,
'dd-
mon-
yy'
),
to_
dat
e('
11-
mar
-90'
,
'dd-
mon-
y
y')
,t
o_dat
e('
11-
apr
-90'
,
'dd-
mon-
yy'
))f
rom dual
;

LEAST(
-
--
--
--
--
--
--
11-
JAN-
90

q)ROUND

Roundwi
llr
oundst
hedat
etowhi
chi
twasequalt
oorgr
eat
ert
hant
hegi
vendat
e.

Sy
ntax:r dat
ound( e,(
day|mont
h|y )
ear)

I
fthesecondpar erwasy
amet eart
henr
oundwi
llcheckst
hemont
hoft
hegi
vendat
ein
t
hef
oll
owi
ngr
anges.

JAN -
- JUN
JUL -
- DEC

.
58

I
fthemont
hfal
lsbet
weenJANandJUNt
heni
tret
urnst
hef
ir
stdayoft
hecur
renty
ear
.
I
fthemont
hfal
lsbet
weenJULandDECt
heni
tret
urnst
hef
ir
stdayoft
henexty
ear
.

I
fthesecondpar erwasmont
amet hthenr
oundwi
llcheckst
hedayoft
hegi
vendat
ein
t
hef
oll
owi
ngr
anges.

1 -
- 15
16 -
- 31

I
fthedayf
all
sbet
ween1and15t
heni
tret
urnst
hef
ir
stdayoft
hecur
rentmont
h.
I
fthedayf
all
sbet
ween16and31t
heni
tret
urnst
hef
ir
stdayoft
henextmont
h.

I
fthesecondpar erwasdayt
amet henr
oundwi
llcheckst
heweekdayoft
hegi
vendat
e
i
nthef
oll
owi
ngr
anges.

SUN -
- WED
THU -
- SUN

I
ftheweekdayf
all
sbet
weenSUNandWEDt
heni
tret
urnst
hepr
evi
oussunday
.
I
ftheweekdayf
all
sbet
weenTHUandSUNt
heni
tret
urnst
henextsunday
.

 I
fthesecondpar
amet
erwasnul
ltheni
tret
urnsnot
hing.
 I
fthey
ouar
enotspeci
fyi
ngt
hesecondpar
amet
ert
henr
oundwi
llr
eset
sthet
imet
othebegi
ningoft
he
cur
rentdayi
ncaseofuserspeci
fi
eddat
e.
 I
fthey
ouar
enotspeci
fyi
ngt
hesecondpar
amet
ert
henr
oundwi
llr
eset
sthet
imet
othebegi
ningoft
he
nextdayi
ncaseofsy
sdat
e.

Ex:
SQL>sel
ectr
ound(
to_
dat
e('
24-
dec-
04'
,
'dd-
mon-
yy'
),
'
year
')
,round(
to_
dat
e('
11-
mar
-
06'
,
'dd-
mon-
yy'
),
'
year
')f
rom dual
;

ROUND(
TO_ROUND(
TO_
-
--
--
--
--
--
---
--
--
--
--
--
--
-
01-
JAN-
05 01-
JAN-
06

SQL>sel
ectr
ound(
to_
dat
e('
11-
jan-
04'
,
'dd-
mon-
yy'
),
'
mont
h')
,round(
to_
dat
e('
18-
j
an-
04'
,
'dd-
mon-
yy'
),
'
mont
h')f
rom dual
;

.
59

ROUND(
TO_ROUND(
TO_
-
--
--
--
--
--
---
--
--
--
--
--
--
--
01-
JAN-
04 01-
FEB-
04

SQL>sel
ectr
ound(
to_
dat
e('
26-
dec-
06'
,
'dd-
mon-
yy'
),
'
day
'),
round(
to_
dat
e('
29-
dec-
06'
,
'dd-
mon-
yy'
),
'
day
')f
rom dual
;

ROUND(
TO_ROUND(
TO_
-
--
--
--
--
--
--
---
--
--
--
--
--
--
24-
DEC-
06 31-
DEC-
06

SQL>sel
ectt
o_char
(round(
to_
dat
e('
24-
dec-
06'
,
'dd-
mon-
yy'
))
,'ddmony
yyy
hh:
mi:
ssam'
)fr
om dual
;
TO_
CHAR(
ROUND(
TO_
DATE(
'
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
24dec200612:
00:
00am
r
)TRUNC

Tr
uncwi
llchopsof
fthedat
etowhi
chi
twasequalt
oorl
esst
hant
hegi
vendat
e.

Sy
ntax:t
r dat
unc( e,(
day|mont
h|y )
ear)

 I
fthesecondpar erwasy
amet eart
heni
tal
way
sret
urnst
hef
ir
stdayoft
hecur
renty
ear
.
 I
fthesecondpar erwasmont
amet htheni
tal
way
sret
urnst
hef
ir
stdayoft
hecur
rentmont
h.
 I
fthesecondpar erwasdayt
amet heni
tal
way
sret
urnst
hepr
evi
oussunday
.
 I
fthesecondpar
amet
erwasnul
ltheni
tret
urnsnot
hing.
 I
fthey
ouar
enotspeci
fyi
ngt
hesecondpar
amet
ert
hent
runkwi
llr
eset
sthet
imet
othebegi
ningoft
he
cur
rentday
.

Ex:
SQL>sel
ectt
runc(
to_
dat
e('
24-
dec-
04'
,
'dd-
mon-
yy'
),
'
year
')
,tr
unc(
to_
dat
e('
11-
mar
-
06'
,
'dd-
mon-
yy'
),
'
year
')f
rom dual
;

TRUNC(
TO_TRUNC(
TO_
-
--
--
--
--
--
---
--
--
--
--
--
--
-
01-
JAN-
04 01-
JAN-
06

.
60

SQL>sel
ectt
runc(
to_
dat
e('
11-
jan-
04'
,
'dd-
mon-
yy'
),
'
mont
h')
,tr
unc(
to_
dat
e('
18-
jan-
04'
,
'dd-
mon-
yy'
),
'
mont
h')f
rom dual
;

TRUNC(
TO_TRUNC(
TO_
-
--
--
--
--
--
---
--
--
--
--
--
--
01-
JAN-
04 01-
JAN-
04

SQL>sel
ectt
runc(
to_
dat
e('
26-
dec-
06'
,
'dd-
mon-
yy'
),
'
day
'),
trunc(
to_
dat
e('
29-
dec-
06'
,
'dd-
mon-
yy'
),
'
day
')f
rom dual
;
TRUNC(
TO_TRUNC(
TO_
-
--
--
--
--
--
---
--
--
--
--
--
--
-
24-
DEC-
0624-
DEC-
06

SQL>sel
ectt
o_char
(tr
unc(
to_
dat
e('
24-
dec-
06'
,
'dd-
mon-
yy'
))
,'ddmony
yyyhh:
mi:
ss
am'
)fr
om dual
;

TO_
CHAR(
TRUNC(
TO_
DATE(
'
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
24dec200612:
00:
00am

s)NEW_TI
ME

Thi
swi
llgi
vet
hedesi
redt
imezone’
sdat
eandt
ime.

Sy
ntax:new_
ti dat
me( e,cur
rent
_ti
mezone,
desi
red_
timezone)

Av
ail
abl
eti
mezonesar
easf
oll
ows.

TI
MEZONES

ADT-
AST/ - At
lant
icst
andar
d/dayl
ightt
ime
BDT-
BST/ - Ber
ingst
andar
d/dayl
ightt
ime
CDT-
CST/ - Cent
ralst
andar
d/dayl
ightt
ime
EDT -
EST/ - East
ernst
andar
d/dayl
ightt
ime
GMT -
- Gr
eenwi
chmeant
ime
HDT-
HST/ - Al
aska-
Hawai
ist
andar
d/dayl
ightt
ime
MST/
MDT -
- Mount
ainst
andar
d/dayl
ightt
ime

.
61

NST -
-Newf
oundl
andst
andar
dti
me
PDT-
PST/ - Paci
fi
cst
andar
d/dayl
ightt
ime
YDT-
YST/ - Yukonst
andar
d/dayl
ightt
ime

Ex:
SQL>sel
ectt
o_char
(new_
time(
sysdat
e,'
gmt
',
'
yst
')
,'
ddmony
yyyhh:
mi:
ssam'
)fr
om
dual
;

TO_
CHAR(
NEW_
TIME(
SYSDAT
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
24dec200602:
51:
20pm

SQL>sel
ectt
o_char
(new_
time(
sysdat
e,'
gmt
',
'
est
')
,'
ddmony
yyyhh:
mi:
ssam'
)fr
om
dual
;

TO_
CHAR(
NEW_
TIME(
SYSDAT
-
--
--
--
--
--
--
--
--
--
--
--
24dec200606:
51:
26pm

t
)COALESCE

Thi
swi
llgi
vet
hef
ir
stnon-
nul
ldat
e.

Sy
ntax:coal dat
esce( e1,
dat
e2,
dat
e3…dat
en)

Ex:
SQL>sel
ectcoal
esce(
'12-
jan-
90'
,
'13-
jan-
99'
),coal
esce(
nul
l,
'
12-
jan-
90'
,
'23-
mar
-
98'
,
nul
l)f
rom dual
;

COALESCE(COALESCE(
-
--
--
--
--
--
---
--
--
--
--
--
-
12-
jan-
90 12-
jan-
90

MI
SCELLANEOUSFUNCTI
ONS

 Ui
d
 User

.
62

 Vsi
ze
 Rank
 Dense_
rank

a)UI
D

Thi
swi
llr
etur
nst
hei
ntegerv
aluecor
respondi
ngt
otheusercur
rent
lyl
oggedi
n.

Ex:
SQL>sel
ectui
dfr
om dual
;

UI
D
-
--
--
--
--
-
319

b)USER

Thi
swi
llr
etur
nst
hel
ogi
n’susername.

Ex:
SQL>sel
ectuserf
rom dual
;

USER
-
--
--
--
--
--
--
--
-
SAKETH

c)VSI
ZE

Thi
swi
llr
etur
nst
henumberofby
tesi
ntheexpr
essi
on.

Ex:
SQL>sel
ectv
size(
123)
,vsi
ze(
'comput
er'
),v
size(
'12-
jan-
90'
)fr
om dual
;

VSI
ZE(
123)VSI
ZE(
'COMPUTER'
)VSI
ZE(
'12-
JAN-
90'
)
-
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
-
3 8 9

.
63

d)RANK

Thi
swi
llgi
vet
henon-
sequent
ial
ranki
ng.

Ex:
SQL>sel
ectr
ownum,
salf
rom (
sel
ectsalf
rom empor
derbysaldesc)
;
ROWNUM SAL
-
--
--
--
--
---
--
--
--
--
1 5000
2 3000
3 3000
4 2975
5 2850
6 2450
7 1600
8 1500
9 1300
10 1250
11 1250
12 1100
13 1000
14 950
15 800

SQL>sel
ectr
ank(
2975)wi
thi
ngr
oup(
orderbysaldesc)f
rom emp;

RANK(
2975)
WITHI
NGROUP(
ORDERBYSALDESC)
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
4

d)DENSE_RANK

Thi
swi
llgi
vet
hesequent
ialr
anki
ng.

Ex:
SQL>sel
ectdense_
rank(
2975)wi
thi
ngr
oup(
orderbysaldesc)f
rom emp;

.
64

DENSE_
RANK(
2975)
WITHI
NGROUP(
ORDERBYSALDESC)
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
3

CONVERSI
ONFUNCTI
ONS

 Bi
n_t
o_num
 Char
tor
owi
d
 Rowi
dtochar
 To_
number
 To_
char
 To_
dat
e
a)BI
N_TO_
NUM

Thi
swi
llconv
ertt
hebi
nar
yval
uet
oit
snumer
icalequi
val
ent
.

Sy
ntax:bi
n_t
o_num(bi
nar
y_bi
ts)

Ex:
SQL>sel
ectbi
n_t
o_num(
1,1,
0)f
rom dual
;

BI
N_TO_
NUM(
1,1,
0)
-
--
--
--
--
--
--
--
--
--
--
--
-
6

 I
fal
lthebi
tsar
ezer
otheni
tpr
oduceszer
o.
 I
fal
lthebi
tsar
enul
ltheni
tpr
oducesaner
ror
.

b)CHARTOROWI
D

Thi
swi
llconv
ertachar
act
erst
ri
ngt
oactl
ikeani
nter
nalor
acl
erowi
dent
if
ierorr
owi
d.

c)ROWI
DTOCHAR

Thi
swi
llconv
ertani
nter
nalor
acl
erowi
dent
if
ierorr
owi
dtochar
act
erst
ri
ng.

.
65

d)TO_NUMBER

Thi
swi
llconv
ertacharorv
archart
onumber
.

e)TO_CHAR

Thi
swi
llconv
ertanumberordat
etochar
act
erst
ri
ng.

f
)TO_DATE

Thi
swi
llconv
ertanumber
,charorv
archart
oadat
e.

GROUPFUNCTI
ONS

 Sum
 Av
g
 Max
 Mi
n
 Count

Gr
oupf
unct
ionswi
llbeappl
i
edonal
lther
owsbutpr
oducessi
ngl
eout
put
.

a)SUM

Thi
swi
llgi
vet
hesum oft
hev
aluesoft
hespeci
fi
edcol
umn.

Sy
nt col
ax:sum ( umn)

Ex:
SQL>sel
ectsum(
sal
)fr
om emp;

SUM(
SAL)
-
--
--
--
--
-
38600

b)AVG

.
66

Thi
swi
llgi
vet
heav
erageoft
hev
aluesoft
hespeci
fi
edcol
umn.

Sy
ntax:av
g(col
umn)

Ex:
SQL>sel
ectav
g(sal
)fr
om emp;

AVG(
SAL)
-
--
--
--
--
--
--
--
2757.
14286

c)MAX

Thi
swi
llgi
vet
hemaxi
mum oft
hev
aluesoft
hespeci
fi
edcol
umn.

Sy
nt col
ax:max( umn)

Ex:
SQL>sel
ectmax(
sal
)fr
om emp;

MAX(
SAL)
-
--
--
--
--
-
5000

d)MI
N

Thi
swi
llgi
vet
hemi
nimum oft
hev
aluesoft
hespeci
fi
edcol
umn.

Sy
ntax:mi
n(col
umn)

Ex:
SQL>sel
ectmi
n(sal
)fr
om emp;

MI
N(SAL)
-
--
--
--
--
-
500

.
67

e)COUNT

Thi
swi
llgi
vet
hecountoft
hev
aluesoft
hespeci
fi
edcol
umn.

Sy
nt col
ax:count( umn)

Ex:
SQL>sel
ectcount
(sal
),
count
(*)f
rom emp;

COUNT(
SAL) COUNT(
*)
-
--
--
--
--
--
--
---
--
--
--
--
--
14 14

.
68

CONSTRAI
NTS

Const
rai
ntsar
ecat
egor
izedasf
oll
ows.

Domai
nint
egr
it
yconst
rai
nts
 Notnul
l
 Check

Ent
it
yint
egr
it
yconst
rai
nts
 Uni
que
 Pr
imar
ykey

Ref
erent
iali
ntegr
it
yconst
rai
nts
 For
eignkey

Const
rai
ntsar
eal
way
sat
tachedt
oacol
umnnotat
abl
e.
Wecanaddconst
rai
ntsi
nthr
eeway
s.

 Col
umnl
evel -
-al
ongwi
tht
hecol
umndef
ini
ti
on
 Tabl
elev
el -
-af
tert
het
abl
edef
ini
ti
on
 Al
terl
evel -
-usi
ngal
tercommand

Whi
leaddi
ngconst
rai
ntsy
ouneednotspeci
fyt
henamebutt
het
ypeonl
y,or
acl
ewi
lli
nter
nal
lynamet
he
const
rai
nt.
I
fyouwantt
ogi
veanamet
otheconst
rai
nt,
youhav
etouset
heconst
rai
ntcl
ause.

NOTNULL

Thi
sisusedt
oav
oidnul
lval
ues.
Wecanaddt
hisconst
rai
nti
ncol
umnl
evelonl
y.

Ex:
SQL>cr
eat
etabl
est
udent
(nonumber
(2)notnul
l
,namev
archar
(10)
,mar
ks
number
(3)
);

SQL>cr
eat
etabl
est
udent
(nonumber
(2)const
rai
ntnnnotnul
l,
namev
archar
(10)
,

.
69

mar
ksnumber
(3)
);

CHECK

Thi
sisusedt
oinser
tthev
aluesbasedonspeci
fi
edcondi
ti
on.
Wecanaddt
hisconst
rai
nti
nal
lthr
eel
evel
s.

Ex:
COLUMNLEVEL

SQL>cr
eat
etabl
est
udent
(nonumber
(2),
namev
archar
(10)
,mar
ksnumber
(3)check
(
mar
ks>300)
);
SQL>cr
eat
etabl
est
udent
(nonumber
(2),
namev
archar
(10)
,mar
ksnumber
(3)
const
rai
ntchcheck(
mar
ks>300)
);

TABLELEVEL

SQL>cr
eat
etabl
est
udent
(nonumber
(2),
namev
archar
(10)
,mar
ksnumber
(3)
,check
(
mar
ks>300)
);
SQL>cr
eat
etabl
est
udent
(nonumber
(2),
namev
archar
(10)
,mar
ksnumber
(3)
,
const
rai
ntchcheck(
mar
ks>300)
);

ALTERLEVEL

SQL>al
tert
abl
est
udentaddcheck(
mar
ks>300)
;
SQL>al
tert
abl
est
udentaddconst
rai
ntchcheck(
mar
ks>300)
;

UNI
QUE

Thi
sisusedt
oav
oiddupl
icat
esbuti
tal
lownul
ls.
Wecanaddt
hisconst
rai
nti
nal
lthr
eel
evel
s.

Ex:
COLUMNLEVEL

.
70

SQL>cr
eat
etabl
est
udent
(nonumber
(2)uni
que,
namev
archar
(10)
,mar
ks
number
(3)
);
SQL>cr
eat
etabl
est
udent
(nonumber
(2)const
rai
ntununi
que,
namev
archar
(10)
,
mar
ksnumber
(3)
);

TABLELEVEL

SQL>cr
eat
etabl
est
udent
(nonumber
(2),
namev
archar
(10)
,mar
ksnumber
(3)
,
uni
que(
no)
);
SQL>cr
eat
etabl
est
udent
(nonumber
(2),
namev
archar
(10)
,mar
ksnumber
(3)
,
const
rai
ntununi
que(
no)
);

ALTERLEVEL

SQL>al
tert
abl
est
udentadduni
que(
no)
;
SQL>al
tert
abl
est
udentaddconst
rai
ntununi
que(
no)
;

PRI
MARYKEY

 Thi
sisusedt
oav
oiddupl
icat
esandnul
ls.Thi
swi
llwor
kascombi
nat
ionofuni
queandnotnul
l.
 Pr
imar
ykeyal
way
sat
tachedt
othepar
entt
abl
e.
 Wecanaddt
hisconst
rai
nti
nal
lthr
eel
evel
s.

Ex:
COLUMNLEVEL

SQL>cr
eat
etabl
est
udent
(nonumber
(2)pr
imar
ykey
,namev
archar
(10)
,mar
ks
number
(3)
);
SQL>cr
eat
etabl
est
udent
(nonumber
(2)const
rai
ntpkpr
imar
ykey
,namev
archar
(10)
,
mar
ksnumber
(3)
);

TABLELEVEL

SQL>cr
eat
etabl
est
udent
(nonumber
(2),
namev
archar
(10)
,mar
ksnumber
(3)
,
pr
imar
ykey
(no)
);
SQL>cr
eat
etabl
est
udent
(nonumber
(2),
namev
archar
(10)
,mar
ksnumber
(3)
,

.
71

const
rai
ntpkpr
imar
ykey
(no)
);

ALTERLEVEL

SQL>al
tert
abl
est
udentaddpr
imar
ykey
(no)
;
SQL>al
tert
abl
est
udentaddconst
rai
ntpkpr
imar
ykey
(no)
;

FOREI
GNKEY

 Thi
sisusedt
oref
erencet
hepar
entt
abl
epr
imar
ykeycol
umnwhi
chal
lowsdupl
icat
es.
 For
eignkeyal
way
sat
tachedt
othechi
ldt
abl
e.
 Wecanaddt
hisconst
rai
nti
ntabl
eandal
terl
evel
sonl
y.

Ex:
TABLELEVEL

SQL>cr
eat
etabl
eemp(
empnonumber
(2)
,enamev
archar
(10)
,dept
nonumber
(2)
,
pr
imar
ykey
(empno)
,for
eignkey
(dept
no)r
efer
encesdept
(dept
no)
);
SQL>cr
eat
etabl
eemp(
empnonumber
(2)
,enamev
archar
(10)
,dept
nonumber
(2)
,
const
rai
ntpkpr
imar
ykey
(empno)
,const
rai
ntf
kfor
eignkey
(dept
no)r
efer
ences
dept
(dept
no)
);

ALTERLEVEL

SQL>al
tert
abl
eempaddf
orei
gnkey
(dept
no)r
efer
encesdept
(dept
no)
;
SQL>al
tert
abl
eempaddconst
rai
ntf
kfor
eignkey
(dept
no)r
efer
encesdept
(dept
no)
;

Oncet
hepr
imar
ykeyandf
orei
gnkeyr
elat
ionshi
phasbeencr
eat
edt
heny
oucannotr
emov
eanypar
entr
ecor
dif
t
hedependentchi
ldsexi
sts.

USI
NGONDELTECASCADE

Byusi
ngt
hiscl
ausey
oucanr
emov
ethepar
entr
ecor
dev
eni
tchi
ldsexi
sts.
Becausewhenev
ery
our
emov
epar
entr
ecor
dor
acl
eaut
omat
ical
lyr
emov
esal
lit
sdependentr
ecor
dsf
rom chi
ld
t
abl
e,i
fthi
scl
ausei
spr
esentwhi
lecr
eat
ingf
orei
gnkeyconst
rai
nt.

Ex:

.
72

TABLELEVEL

SQL>cr
eat
etabl
eemp(
empnonumber
(2)
,enamev
archar
(10)
,dept
nonumber
(2)
,
pr
imar
ykey
(empno)
,for
eignkey
(dept
no)r
efer
encesdept
(dept
no)ondel
ete
cascade)
;
SQL>cr
eat
etabl
eemp(
empnonumber
(2)
,enamev
archar
(10)
,dept
nonumber
(2)
,
const
rai
ntpkpr
imar
ykey
(empno)
,const
rai
ntf
kfor
eignkey
(dept
no)r
efer
ences
dept
(dept
no)ondel
etecascade)
;

ALTERLEVEL

SQL>al
tert
abl
eempaddf
orei
gnkey
(dept
no)r
efer
encesdept
(dept
no)ondel
ete
cascade;
SQL>al
tert
abl
eempaddconst
rai
ntf
kfor
eignkey
(dept
no)r
efer
encesdept
(dept
no)on
del
etecascade;

COMPOSI
TEKEYS

Acomposi
tekeycanbedef
inedonacombi
nat
ionofcol
umns.
Wecandef
inecomposi
tekey
sonent
it
yint
egr
it
yandr
efer
ent
iali
ntegr
it
yconst
rai
nts.
Composi
tekeycanbedef
inedi
ntabl
eandal
terl
evel
sonl
y.

Ex:
UNI
QUE(
TABLELEVEL)

SQL>cr
eat
etabl
est
udent
(nonumber
(2),
namev
archar
(10)
,mar
ksnumber
(3)
,
uni
que(
no,
name)
);
SQL>cr
eat
etabl
est
udent
(nonumber
(2),
namev
archar
(10)
,mar
ksnumber
(3)
,
const
rai
ntununi
que(
no,
name)
);

UNI
QUE(
ALTERLEVEL)

SQL>al
tert
abl
est
udentadduni
que(
no,
name)
;
SQL>al
tert
abl
est
udentaddconst
rai
ntununi
que(
no,
name)
;

PRI
MARYKEY(
TABLELEVEL)

SQL>cr
eat
etabl
est
udent
(nonumber
(2),
namev
archar
(10)
,mar
ksnumber
(3)
,
pr
imar
ykey
(no,
name)
);

.
73

SQL>cr
eat
etabl
est
udent
(nonumber
(2),
namev
archar
(10)
,mar
ksnumber
(3)
,
const
rai
ntpkpr
imar
ykey
(no,
name)
);

PRI
MARYKEY(
ALTERLEVEL)

SQL>al
tert
abl
est
udentaddpr
imar
ykey
(no,
anme)
;
SQL>al
tert
abl
est
udentaddconst
rai
ntpkpr
imar
ykey
(no,
name)
;

FOREI
GNKEY(
TABLELEVEL)

SQL>cr
eat
etabl
eemp(
empnonumber
(2)
,enamev
archar
(10)
,dept
nonumber
(2)
,
dnamev
archar
(10)
,pr
imar
ykey
(empno)
,for
eignkey
(dept
no,
dname)r
efer
ences
dept
(dept
no,
dname)
);
SQL>cr
eat
etabl
eemp(
empnonumber
(2)
,enamev
archar
(10)
,dept
nonumber
(2)
,
dnamev
archar
(10)
,const
rai
ntpkpr
imar
ykey
(empno)
,const
rai
ntf
kfor
eign
key
(dept
no,
dname)r
efer
encesdept
(dept
no,
dname)
);

FOREI
GNKEY(
ALTERLEVEL)

SQL>al
tert
abl
eempaddf
orei
gnkey
(dept
no,
dname)r
efer
encesdept
(dept
no,
dname)
;
SQL>al
tert
abl
eempaddconst
rai
ntf
kfor
eignkey
(dept
no,
dname)r
efer
ences
dept
(dept
no,
dname)
;

DEFERRABLECONSTRAI
NTS

Eachconst
rai
nthast
woaddi
ti
onalat
tri
but
est
osuppor
tdef
err
edchecki
ngofconst
rai
nts.
 Def
err
edi
nit
ial
lyi
mmedi
ate
 Def
err
edi
nit
ial
lydef
err
ed
Def
err
edi
nit
ial
lyi
mmedi
atechecksf
orconst
rai
ntv
iol
ati
onatt
het
imeofi
nser
t.
Def
err
edi
nit
ial
lydef
err
edchecksf
orconst
rai
ntv
iol
ati
onatt
het
imeofcommi
t.

Ex:
SQL>cr
eat
etabl
est
udent
(nonumber
(2)
,namev
archar
(10)
,mar
ksnumber
(3)
,
const
rai
ntununi
que(
no)def
err
edi
nit
ial
lyi
mmedi
ate)
;
SQL>cr
eat
etabl
est
udent
(nonumber
(2)
,namev
archar
(10)
,mar
ksnumber
(3)
,
const
rai
ntununi
que(
no)def
err
edi
nit
ial
lydef
err
ed)
;
SQL>al
tert
abl
est
udentaddconst
rai
ntununi
que(
no)def
err
abl
eini
ti
all
ydef
err
ed;

.
74

SQL>setconst
rai
ntsal
limmedi
ate;
Thi
swi
llenabl
eal
ltheconst
rai
ntsv
iol
ati
onsatt
het
imeofi
nser
ti
ng.

SQL>setconst
rai
ntsal
ldef
err
ed;
Thi
swi
llenabl
eal
ltheconst
rai
ntsv
iol
ati
onsatt
het
imeofcommi
t.

OPERATI
ONSWI
THCONSTRAI
NTS

Possi
bleoper
ati
onswi
thconst
rai
ntsasf
oll
ows.

 Enabl
e
 Di
sabl
e
 Enf
orce
 Dr
op

ENABLE

Thi
swi
llenabl
etheconst
rai
nt.Bef
oreenabl
e,t
heconst
rai
ntwi
llcheckt
heexi
sti
ngdat
a.

Ex:
SQL>al
tert
abl
est
udentenabl
econst
rai
ntun;

DI
SABLE

Thi
swi
lldi
sabl
etheconst
rai
nt.

Ex:
SQL>al
tert
abl
est
udentenabl
econst
rai
ntun;

ENFORCE

Thi
swi
llenf
orcet
heconst
rai
ntr
athert
hanenabl
eforf
utur
einser
tsorupdat
es.
Thi
swi
llnotcheckf
orexi
sti
ngdat
awhi
leenf
orci
ngdat
a.

Ex:
SQL>al
tert
abl
est
udentenf
orceconst
rai
ntun;

DROP

.
75

Thi
swi
llr
emov
etheconst
rai
nt.

Ex:
SQL>al
tert
abl
est
udentdr
opconst
rai
ntun;
Oncet
het
abl
eisdr
opped,
const
rai
ntsaut
omat
ical
lywi
lldr
op.

CASEANDDEFAULT

CASE

Casei
ssi
mil
art
odecodebuteasi
ert
ounder
standwhi
legoi
ngt
hroughcodi
ng

Ex:
SQL>Sel
ectsal
,
Casesal
When500t
hen‘
l
ow’
When5000t
hen‘
high’

.
76

El
se‘
medi
um’
Endcase
Fr
om emp;

SAL CASE
-
--
-- -
--
--
--
-
500 l
ow
2500 medi
um
2000 medi
um
3500 medi
um
3000 medi
um
5000 hi
gh
4000 medi
um
5000 hi
gh
1800 medi
um
1200 medi
um
2000 medi
um
2700 medi
um
2200 medi
um
3200 medi
um

DEFAULT

Def
aul
tcanbeconsi
der
edasasubst
it
utebehav
iorofnotnul
lconst
rai
ntwhenappl
iedt
onewr
owsbei
ngent
ered
i
ntot
het
abl
e.
Wheny
oudef
ineacol
umnwi
t hedef
ht aul
tkey
wor
dfol
lowedbyav
alue,
youar
eact
ual
lyt
ell
ingt
hedat
abaset
hat
,
oni
nser
tifar
owwasnotassi
gnedav
aluef
ort
hiscol
umn,
uset
hedef
aul
tval
uet
haty
ouhav
especi
fi
ed.
Def
aul
tisappl
iedonl
ydur
ingi
nser
ti
onofnewr
ows.

Ex:
SQL>cr
eat
etabl
est
udent
(nonumber
(2)def
aul
t11,
namev
archar
(2)
);
SQL>i
nser
tint
ost
udentv
alues(
1,'
a'
);
SQL>i
nser
tint
ost
udent
(name)v
alues(
'b'
);

SQL>sel
ect*f
rom st
udent
;

.
77

NO NAME
-
--
--
---
--
--
--
-
1a
11b

SQL>i
nser
tint
ost
udentv
alues(
nul
l
,‘c’
);

SQL>sel
ect*f
rom st
udent
;

NO NAME
-
--
--
---
--
--
--
-
1a
11b
C
-
-Def
aul
tcannotov
err
idenul
ls.

ABSTRACTDATATYPES

Somet
imesy
oumaywantt
ypewhi
chhol
dsal
lty
pesofdat
aincl
udi
ngnumber
s,char
sandspeci
alchar
act
ers
somet
hingl
iket
his.Youcannotachi
evet
hisusi
ngpr
e-def
inedt
ypes.
Youcandef
inecust
om t
ypeswhi
chhol
dsy
ourdesi
reddat
a.

Ex:
Supposei
nat
abl
ewehav
eaddr
esscol
umnwhi
chhol
dshnoandci
tyi
nfor
mat
ion.
Wewi
lldef
ineacust
om t
ypewhi
chhol
dsbot
hnumer
icaswel
laschardat
a.

CREATI
NGADT

SQL>cr
eat
ety
peaddrasobj
ect
(hnonumber
(3)
,ci
tyv
archar
(10)
);/

CREATI
NGTABLEBASEDONADT

SQL>cr
eat
etabl
est
udent
(nonumber
(2)
,namev
archar
(2)
,addr
essaddr
);

.
78

I
NSERTI
NGDATAI
NTOADTTABLES

SQL>i
nser
tint
ost
udentv
alues(
1,'
a'
,addr
(111,
'
hyd'
))
;
SQL>i
nser
tint
ost
udentv
alues(
2,'
b'
,addr
(222,
'
bang'
))
;
SQL>i
nser
tint
ost
udentv
alues(
3,'
c'
,addr
(333,
'
del
hi'
))
;

SELECTI
NGDATAFROM ADTTABLES

SQL>sel
ect*f
rom st
udent
;

NONAMEADDRESS(
HNO,
CITY)
-
---
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
1 a ADDR(
111,
'hy
d')
2 b ADDR(
222,
'bang'
)
3 c ADDR(
333,
'del
hi'
)

SQL>sel
ectno,
name,
s.addr
ess.
hno,
s.addr
ess.
cit
yfr
om st
udents;

NONAMEADDRESS.
HNOADDRESS.
CITY
-
--
---
--
--
---
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
1a 111 hy
d
2b 222 bang
3c 333 del
hi

UPDATEWI
THADTTABLES

SQL>updat
est
udentssets.
addr
ess.
cit
y='
bombay
'wher
es.
addr
ess.
hno=333;
SQL>sel
ectno,
name,
s.addr
ess.
hno,
s.addr
ess.
cit
yfr
om st
udents;

NONAMEADDRESS.
HNOADDRESS.
CITY
-
--
---
--
--
---
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
1a 111 hy
d
2b 222 bang
3c 333 bombay

DELETEWI
THADTTABLES

.
79

SQL>del
etest
udentswher
es.
addr
ess.
hno=111;
SQL>sel
ectno,
name,
s.addr
ess.
hno,
s.addr
ess.
cit
yfr
om st
udents;

NONAMEADDRESS.
HNOADDRESS.
CITY
-
--
---
--
--
---
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
2b 222 bang
3c 333 bombay

DROPPI
NGADT

SQL>dr
opt
ypeaddr
;

OBJECTVI
EWSANDMETHODS

OBJECTVI
EWS

I
fyouwantt
oimpl
ementobj
ect
swi
tht
heexi
sti
ngt
abl
e,obj
ectv
iewscomei
ntopi
ctur
e.
Youdef
inet
heobj
ectandcr
eat
eav
iewwhi
chr
elat
est
hisobj
ectt
otheexi
sti
ngt
abl
enot
hingbutobj
ectvi
ew.

Obj
ectv
iewsar
eusedt
orel
atet
heuserdef
inedobj
ect
stot
heexi
sti
ngt
abl
e.

Ex:
1)Assumet
hatt
het
abl
est
udenthasal
readybeencr
eat
edwi
tht
hef
oll
owi
ngcol
umns
SQL>cr
eat
etabl
est
udent
(nonumber
(2)
,namev
archar
(10)
,hnonumber
(3)
,ci
ty
v
archar
(10)
);
2)Cr
eat
ethef
oll
owi
ngt
ypes
SQL>cr
eat
ety
peaddrasobj
ect
(hnonumber
(2)
,ci
tyv
archar
(10)
);
/
SQL>cr
eat
ety
pest
udasobj
ect
(namev
archar
(10)
,addr
essaddr
);
/
3)Rel
atet
heobj
ect
stot
hest
udentt
abl
ebycr
eat
ingt
heobj
ectv
iew
SQL>cr
eat
evi
ewst
udent
_ov
(no,
stud_
inf
o)assel
ectno,
stud(
name,
addr
(hno,
ci
ty)
)
f
rom st
udent
;
4)Nowy
oucani
nser
tdat
aint
ost
udentt
abl
eint
woway
s

.
80

a)Byr
egul
ari
nser
t
SQL>I
nser
tint
ost
udentv
alues(
1,’
sudha’
,
111,

hyd’
);
b)Byusi
ngobj
ectv
iew
SQL>I
nser
tint
ost
udent
_ovv
alues(
1,st
ud(
‘sudha’
,
addr
(111,

hyd’
))
);

METHODS

Youcandef
inemet
hodswhi
char
enot
hingbutf
unct
ionsi
nty
pesandappl
yint
het
abl
eswhi
chhol
dst
het
ypes;

Ex:
1)Def
ini
ngmet
hodsi
nty
pes
SQL>Cr
eat
ety
pest
udasobj
ect
(namev
archar
(10)
,mar
ksnumber
(3)
,
Memberf
unct
ionmakr
s_f
(mar
ksi
nnumber
)ret
urnnumber
,
Pr
agmar
est
ri
ct_
ref
erences(
mar
ks_
f,wnds,
rnds,
wnps,
fnps)
);
/
2)Def
ini
ngt
ypebody
SQL>Cr
eat
ety
pebodyst
udas
Memberf
unct
ionmar
ks_
f(mar
ksi
nnumber
)ret
urnnumberi
s
Begi
n
Ret
urn(
mar
ks+100)
;
Endmar
ks_
f;
End;
/
3)Cr
eat
eat
abl
eusi
ngst
udt
ype
SQL>Cr
eat
etabl
est
udent
(nonumber
(2)
,i
nfost
ud)
;
4)I
nser
tsomedat
aint
ost
udentt
abl
e
SQL>I
nser
tint
ost
udentv
alues(
1,st
ud(
‘sudha’
,
100)
);
5)Usi
ngmet
hodi
nsel
ect
SQL>Sel
ects.
inf
o.mar
ks_
f(s.
inf
o.mar
ks)f
rom st
udents;
-
-Her
ewear
eusi
ngt
hepr
agmar
est
ri
ct_
ref
erencest
oav
oidt
hewr
it
est
othe
Dat
abase.

.
81

VARRAYSANDNESTEDTABLES

VARRAYS

Av
ary
ingar
rayal
lowsy
out
ost
orer
epeat
ingat
tri
but
esofar
ecor
dinasi
ngl
erowbutwi
thl
imi
t.

Ex:
1)Wecancr
eat
evar
ray
susi
ngor
acl
ety
pesaswel
lasuserdef
inedt
ypes.
a)Var
rayusi
ngpr
e-def
inedt
ypes
SQL>Cr
eat
ety
pev
aasv
arr
ay(
5)ofv
archar
(10)
;/
b)Var
ray
susi
nguserdef
inedt
ypes
SQL>Cr
eat
ety
peaddrasobj
ect
(hnonumber
(3)
,ci
tyv
archar
(10)
);
/
SQL>Cr
eat
ety
pev
aasv
arr
ay(
5)ofaddr
;/
2)Usi
ngv
arr
ayi
ntabl
e
SQL>Cr
eat
etabl
est
udent
(nonumber
(2)
,namev
archar
(10)
,addr
essv
a);
3)I
nser
ti
ngv
aluesi
ntov
arr
ayt
abl
e
SQL>I
nser
tint
ost
udentv
alues(
1,’
sudha’
,
va(
addr
(111,

hyd’
))
);
SQL>I
nser
tint
ost
udentv
alues(
2,’
j
agan’
,
va(
addr
(111,

hyd’
),
addr
(222,

bang’
))
);
4)Sel
ect
ingdat
afr
om v
arr
ayt
abl
e
SQL>Sel
ect*f
rom st
udent
;
-
-Thi
swi
lldi
spl
ayv
arr
aycol
umndat
aal
ongwi
thv
arr
ayandadt
;
SQL>Sel
ectno,
name,
s.*f
rom st
udents1,
tabl
e(s1.
addr
ess)s;
-
-Thi
swi
lldi
spl
ayi
ngener
alf
ormat
5)I
nst
eadofs.
*youcanspeci
fyt
hecol
umnsi
nvar
ray
SQL>Sel
ectno,
name,
s.hno,
s.ci
tyf
rom st
udents1,
tabl
e(s1.
addr
ess)s;

.
82

-
-Updat
eanddel
etenotpossi
blei
nvar
ray
s.
-
-Her
eweusedt
abl
efunct
ionwhi
chwi
llt
aket
hev
arr
aycol
umnasi
nputf
orpr
oduci
ng
out
putexcl
udi
ngv
arr
ayandt
ypes.

NESTEDTABLES

Anest
edt
abl
eis,asi
tsnamei
mpl
ies,at
abl
ewi
thi
nat
abl
e.I
nthi
scasei
tisat
abl
ethati
srepr
esent
edasa
col
umnwi
thi
nanot
hert
abl
e.
Nest
edt
abl
ehast
hesameef
fectofv
arr
aysbuthasnol
imi
t.

Ex:
1)Wecancr
eat
enest
edt
abl
esusi
ngor
acl
ety
pesanduserdef
inedt
ypeswhi
chhasno
l
imi
t.
a)Nest
edt
abl
esusi
ngpr
e-def
inedt
ypes
SQL>Cr
eat
ety
pentast
abl
eofv
archar
(10)
;/
b)Nest
edt
abl
esusi
nguserdef
inedt
ypes
SQL>Cr
eat
ety
peaddrasobj
ect
(hnonumber
(3)
,ci
tyv
archar
(10)
);
/
SQL>Cr
eat
ety
pentast
abl
eofaddr
;/
2)Usi
ngnest
edt
abl
eint
abl
e
SQL>Cr
eat
etabl
est
udent
(nonumber
(2)
,namev
archar
(10)
,addr
essnt
)nest
edt
abl
e
addr
essst
oreasst
udent
_temp;
3)I
nser
ti
ngv
aluesi
ntot
abl
ewhi
chhasnest
edt
abl
e
SQL>I
nser
tint
ost
udentv
alues(
1,’
sudha’
,
nt(
addr
(111,

hyd’
))
);
SQL>I
nser
tint
ost
udentv
alues(
2,’
j
agan’
,
nt(
addr
(111,

hyd’
),
addr
(222,

bang’
))
);
4)Sel
ect
ingdat
afr
om t
abl
ewhi
chhasnest
edt
abl
e
SQL>Sel
ect*f
rom st
udent
;
-
-Thi
swi
lldi
spl
aynest
edt
abl
ecol
umndat
aal
ongwi
thnest
edt
abl
eandadt
;
SQL>Sel
ectno,
name,
s.*f
rom st
udents1,
tabl
e(s1.
addr
ess)s;
-
-Thi
swi
lldi
spl
ayi
ngener
alf
ormat
5)I
nst
eadofs.
*youcanspeci
fyt
hecol
umnsi
nnest
edt
abl
e
SQL>Sel
ectno,
name,
s.hno,
s.ci
tyf
rom st
udents1,
tabl
e(s1.
addr
ess)s;
6)I
nser
ti
ngnest
edt
abl
edat
atot
heexi
sti
ngr
ow

.
83

SQL>I
nser
tint
otabl
e(sel
ectaddr
essf
rom st
udentwher
eno=1)
v
alues(
addr
(555,

chennai

));
7)Updat
einnest
edt
abl
es
SQL>Updat
etabl
e(sel
ectaddr
essf
rom st
udentwher
eno=2)ssets.
cit
y=’
bombay

wher
es.
hno=222;
8)Del
etei
nnest
edt
abl
e
SQL>Del
etet
abl
e(sel
ectaddr
essf
rom st
udentwher
eno=3)swher
es.
hno=333;

DATAMODEL

 ALL_
COLL_
TYPES
 ALL_
TYPES
 DBA_
COLL_
TYPES
 DBA_
TYPES
 USER_
COLL_
TYPES
 USER_
TYPES

.
84

FLASHBACKQUERY

Usedt
oret
ri
evet
hedat
awhi
chhasbeenal
readycommi
tt
edwi
thoutgoi
ngf
orr
ecov
ery
.

Fl
ashbacksar
eoft
wot
ypes
 Ti
mebasef
lashback
 SCNbasedf
lashback(
SCNst
andsf
orSy
stem ChangeNumber
)

Ex:

1)Usi
ngt
imebasedf
lashback
a)SQL>Sel
ect*
from st
udent
;
-
-Thi
swi
lldi
spl
ayal
lther
ows
b)SQL>Del
etest
udent
;
c)SQL>Commi
t; -
-thi
swi
llcommi
tthewor
k.
d)SQL>Sel
ect*
from st
udent
;
-
-Her
eitwi
lldi
spl
aynot
hing
e)Thenexecut
ethef
oll
owi
ngpr
ocedur
es
SQL>Execdbms_
flashback.
enabl
e_at
_ti
me(
sysdat
e-2/
1440)
f
)SQL>Sel
ect*
from st
udent
;
-
-Her
eitwi
lldi
spl
ayt
hel
ostdat
a
-
-Thel
ostdat
awi
llcomebutt
hecur
rentsy
stem t
imewasused
g)SQL>Execdbms_
flashback.
disabl
e
-
-Her
ewehav
etodi
sabl
ethef
lashbackt
oenabl
eitagai
n

2)Usi
ngSCNbasedf
lashback
a)Decl
areav
ari
abl
etost
oreSCN
SQL>Var
iabl
esnumber
b)Gett
heSCN
SQL>Exec:
s:=execdbms_
flashback.
get
_sy
stem_
change_
number

.
85

c)Toseet
heSCN
SQL>Pr
ints
d)Thenexecut
ethef
oll
owi
ngpr
ocedur
es
SQL>Execdbms_
flashback.
enabl
e_at
_sy
stem_
change_
number
(:
s)
SQL>Execdbms_
flashback.
disabl
e

EXTERNALTABLES

Youcanuserext
ernalt
abl
efeat
uret
oaccessext
ernalf
il
esasi
ftheyar
etabl
esi
nsi
det
hedat
abase.
Wheny
oucr
eat
eanext
ernalt
abl
e,y
oudef
inei
tsst
ruct
ureandl
ocat
ionwi
thi
nor
acl
e.
Wheny
ouquer
ythet
abl
e,or
acl
ereadst
heext
ernalt
abl
eandr
etur
nst
her
esul
tsj
ustasi
fthedat
ahadbeen
st
oredwi
thi
nthedat
abase.

ACCESSI
NGEXTERNALTABLEDATA

Toaccessext
ernalf
il
esf
rom wi
thi
nor
acl
e,y
oumustf
ir
stuset
hecr
eat
edi
rect
orycommandt
odef
inea
di
rect
oryobj
ectpoi
nti
ngt
otheext
ernalf
il
elocat
ion
User
swhowi
llaccesst
heext
ernalf
il
esmusthav
ether
eadandwr
it
epr
ivi
legeont
hedi
rect
ory
.

Ex:

CREATI
NGDI
RECTORYANDOSLEVELFI
LE

SQL>Sql
plussy
stem/
manager
SQL>Cr
eat
edi
rect
orysaket
h_di
ras‘
/Vi
sdb/
visdb/
9.2.
0/ext
ernal

;
SQL>Gr
antal
londi
rect
orysaket
h_di
rtosaket
h;
SQL>Connsaket
h/saket
h
SQL>Spool
dept
.l
st
SQL>Sel
ectdept
no|
|‘
,’
||dname|
|‘
,’
||l
ocf
rom dept
;
SQL>Spool
off

CREATI
NGEXTERNALTABLE

SQL>Cr
eat
etabl
edept
_ext
(
dept
nonumber
(2)
,
Dnamev
archar
(14)
,
Locv
archar
(13)
)
Or
gani
zat
ionext
ernal(t
ypeor
acl
e_l
oader

.
86

Def
aul
tdi
rect
orysaket
h_di
r
Accesspar
amet
ers
(r
ecor
dsdel
imi
tedbynewl
ine
Fi
eldst
ermi
nat
edby“
,”
(dept
nonumber
(2)
,
Dnamev
archar
(14)
,
Locv
archar
(13)
))
Locat
ion(
‘/
Visdb/
visdb/
9.2.
0/dept
.l
st’
))
;

SELECTI
NGDATAFROM EXTERNALTABLE

SQL>sel
ect*f
rom dept
_ext
;
Thi
swi
llr
eadf
rom dept
.l
stwhi
chi
saoper
ati
ngsy
stem l
evelf
il
e.

LI
MITATI
ONSONEXTERNALTABLES

a) Youcannotper
for
minser
t,updat
e,anddel
eteoper
ati
ons
a) I
ndexi
ngnotpossi
ble
b) Const
rai
ntsnotpossi
ble

BENEFI
TSOFEXTERNALTABLES

a) Quer
iesofext
ernalt
abl
escompl
etev
eryqui
ckl
yev
ent
houghaf
ullt
abl
escani
drequi
redwi
theach
access
b) Youcanj
oinext
ernalt
abl
est
oeachot
herort
ost
andar
dtabl
es

.
87

REFDEREFVALUE

REF

 Ther
eff
unct
ional
lowsr
efer
enci
ngofexi
sti
ngr
owobj
ect
s.
 Eachoft
her
owobj
ect
shasanobj
ecti
dval
ueassi
gnedt
oit
.
 Theobj
ecti
dassi
gnedcanbeseenbyusi
ngr
eff
unct
ion.

DEREF

 Theder
eff
unct
ionper
\opposi
teact
ion.
 I
ttakesar
efer
encev
alueofobj
ecti
dandr
etur
nst
hev
alueoft
her
owobj
ect
s.

VALUE

 Ev
ent
hought
hepr
imar
ytabl
eisobj
ectt
abl
e,st
il
litdi
spl
ayst
her
owsi
ngener
alf
ormat
.
 Todi
spl
ayt
heent
ir
est
ruct
ureoft
heobj
ect
,thi
swi
llbeused.

Ex:
1)cr
eat
evendot
_adtt
ype
SQL>Cr
eat
ety
pev
endor
_adtasobj
ect(
vendor
_codenumber
(2)
,vendor
_name
v
archar
(2)
,vendor
_addr
essv
archar
(10)
);
/
2)cr
eat
eobj
ectt
abl
esv
endor
sandv
endor
s1
SQL>Cr
eat
etabl
evendor
sofv
endor
_adt
;
SQL>Cr
eat
etabl
evendor
s1ofv
endor
_adt
;
3)i
nser
tthedat
aint
oobj
ectt
abl
es
SQL>i
nser
tint
ovendor
sval
ues(
1,‘
a’
,‘
hyd’
);
SQL>i
nser
tint
ovendor
sval
ues(
2,‘
b’
,‘
bang’
);
SQL>i
nser
tint
ovendor
s1v
alues(
3,‘
c’
,‘
del
hi’
);
SQL>i
nser
tint
ovendor
s1v
alues(
4,‘
d’
,‘
chennai

);
4)cr
eat
eanot
hert
abl
eor
der
swhi
chhol
dst
hev
endor
_adtt
ypeal
so.
SQL>Cr
eat
etabl
eor
der
s(or
der
_nonumber
(2)
,vendor
_inf
orefv
endor
_adt
);
Or

SQL>Cr
eat
etabl
eor
der
s(or
der
_nonumber
(2)
,vendor
_inf
orefv
endor
_adtwi
th

.
88

r
owi
d);
5)i
nser
tthedat
aint
oor
der
stabl
e
Thev
endor
_inf
ocol
umni
nthef
oll
owi
ngsy
ntaxeswi
llst
oreobj
ecti
dofanyt
abl
e
whi
chi
sref
erencedbyv
endor
_adtobj
ect(bot
hvendor
sandv
endor
s1)
.
SQL>i
nser
tint
oor
der
sval
ues(
11,
(sel
ectr
ef(
v)f
rom v
endor
svwher
evendor
_code
=1)
);
SQL>i
nser
tint
oor
der
sval
ues(
12,
(sel
ectr
ef(
v)f
rom v
endor
svwher
evendor
_code
=2)
);
SQL>i
nser
tint
oor
der
sval
ues(
13,
(sel
ectr
ef(
v1)f
rom v
endor
s1v
1wher
e
v
endor
_code=1)
);
SQL>i
nser
tint
oor
der
sval
ues(
14,
(sel
ectr
ef(
v1)f
rom v
endor
s1v
1wher
e
v
endor
_code=1)
);
6)Toseet
heobj
ecti
dsofv
endort
abl
e
SQL>Sel
ectr
ef(
V)f
rom v
endor
sv;
7)I
fyouseet
hev
endor
_inf
oofor
der
sitwi
llshowonl
ytheobj
ecti
dsnott
hev
alues,
t
oseet
hev
alues
SQL>Sel
ectder
ef(
o.v
endor
_inf
o)f
rom or
der
so;
8)Ev
ent
hought
hev
endor
stabl
eisobj
ectt
abl
eitwi
llnotshowt
headtal
ongwi
th
dat
a,t
oseet
hedat
aal
ongwi
tht
headt
SQL>Sel
ect*f
rom v
endor
s;
Thi
swi
ll
giv
ethedat
awi
thoutadt
.
SQL>Sel
ectv
alue(
v)f
rom v
endor
sv;
Thi
swi
ll
giv
ethecol
umnsdat
aal
ongwi
hthet
ype.

REFCONSTRAI
NTS

 Refcanal
soact
sasconst
rai
nt.
 Ev
ent
houghv
endor
s1al
sohol
dingv
endor
_adt
,theor
der
stabl
ewi
llst
oret
heobj
ecti
dsofv
endor
s
onl
ybecausei
tisconst
rai
nedt
othatt
abl
eonl
y.
 Thev
endor
_inf
ocol
umni
nthef
oll
owi
ngsy
ntaxeswi
llst
oreobj
ecti
dsofv
endor
sonl
y.

SQL>Cr
eat
etabl
eor
der
s(or
der
_nonumber
(2)
,vendor
_inf
orefv
endor
_adtscopei
s
v
endor
s);
Or

.
89

SQL>Cr
eat
etabl
eor
der
s(or
der
_nonumber
(2)
,vendor
_inf
orefv
endor
_adtconst
rai
ntf
k
r
efer
encesv
endor
s);

OBJECTVI
EWSWI
THREFERENCES

Toi
mpl
ementt
heobj
ect
sandt
her
efconst
rai
ntst
otheexi
sti
ngt
abl
es,whatwecando?Si
mpl
ydr
opt
hebot
h
t
abl
esandr
ecr
eat
ewi
thobj
ect
sandr
efconst
rai
ns.

.
90

Buty
oucanachi
evet
hiswi
thoutdr
oppi
ngt
het
abl
esandwi
thoutl
osi
ngt
hedat
abycr
eat
ingobj
ectv
iewswi
th
r
efer
ences.

Ex:
a)Cr
eat
ethef
oll
owi
ngt
abl
es
SQL>Cr
eat
etabl
est
udent
1(nonumber
(2)pr
imar
ykey
,namev
archar
(2)
,mar
ks
number
(3)
);
SQL>Cr
eat
etabl
est
udent
2(nonumber
(2)pr
imar
ykey
,hnonumber
(3)
,ci
ty
v
archar
(10)
,i
dnumber
(2)
,f
orei
gnKey
(id)r
efer
encesst
udent
1(no)
);
b)I
nser
tther
ecor
dsi
ntobot
htabl
es
SQL>i
nser
tint
ost
udent
1(1,

a’,
100)
;
SQL>i
nser
tint
ost
udent
1(2,

b’,
200)
;
SQL>i
nser
tint
ost
udent
2(11,
111,

hyd’
,
1);
SQL>i
nser
tint
ost
udent
2(12,
222,

bang’
,
2);
SQL>i
nser
tint
ost
udent
2(13,
333,

bombay
’,
1);
c)Cr
eat
ethet
ype
SQL>cr
eat
eorr
epl
acet
ypest
udasobj
ect
(nonumber
(2)
,namev
archar
(2)
,mar
ks
number
(3)
);
/
d)Gener
ati
ngOI
Ds
SQL>Cr
eat
eorr
epl
acev
iewst
udent
1_ovofst
udwi
thobj
ecti
dent
if
ier
(ori
d)(
no)as
Sel
ect*f
rom St
udent
1;
e)Gener
ati
ngr
efer
ences
SQL>Cr
eat
eorr
epl
acev
iewst
udent
2_ovassel
ectno,
hno,
ci
ty,
make_
ref
(st
udent
1_ov
,i
d)i
dfr
om St
udent
2;
d)Quer
ythef
oll
owi
ng
SQL>sel
ect*
from st
udent
1_ov
;
SQL>sel
ectr
ef(
s)f
rom st
udent
1_ovs;
SQL>sel
ectv
alues(
s)f
rom st
udent
1_ov
;
SQ>sel
ect*
from st
udent
2_ov
;
SQL>sel
ectder
ef(
s.i
d)f
rom st
udent
2_ovs;

PARTI
TIONS

Asi
ngl
elogi
calt
abl
ecanbespl
iti
ntoanumberofphy
sical
lysepar
atepi
ecesbasedonr
angesofkeyv
alues.
Eachoft
hepar
tsoft
het
abl
eiscal
ledapar
ti
ti
on.
Anon-
par
ti
ti
onedt
abl
ecannotbepar
ti
ti
onedl
ater
.

.
91

TYPES

 Rangepar
ti
ti
ons
 Li
stpar
ti
ti
ons
 Hashpar
ti
ti
ons
 Subpar
ti
ti
ons

ADVANTAGES

 Reduci
ngdownt
imef
orschedul
edmai
ntenance,whi
chal
lowsmai
ntenanceoper
ati
onst
obecar
ri
edout
onsel
ect
edpar
ti
ti
onswhi
leot
herpar
ti
ti
onsar
eav
ail
abl
etouser
s.
 Reduci
ngdownt
imeduet
odat
afai
lur
e,f
ail
ureofapar
ti
cul
arpar
ti
ti
onwi
llnowayaf
fectot
herpar
ti
ti
ons.
 Par
ti
ti
oni
ndependenceal
lowsf
orconcur
rentuseoft
hev
ari
ouspar
ti
ti
onsf
orv
ari
ouspur
poses.

ADVANTAGESOFPARTI
TIONSBYSTORI
NGTHEM I
NDI
FFERENTTABLESPACES

 Reducest
hepossi
bil
it
yofdat
acor
rupt
ioni
nmul
ti
plepar
ti
ti
ons.
 Backupandr
ecov
eryofeachpar
ti
ti
oncanbedonei
ndependent
ly.

DI
SADVANTAGES

 Par
ti
ti
onedt
abl
escannotcont
ainanycol
umnswi
thl
ongorl
ongr
aw dat
aty
pes,LOBt
ypesorobj
ect
t
ypes.

RANGEPARTI
TIONS

a)Cr
eat
ingr
angepar
ti
ti
onedt
abl
e
SQL>Cr
eat
etabl
est
udent
(nonumber
(2)
,namev
archar
(2)
)par
ti
ti
onbyr
ange(
no)
(
par
ti
ti
onp1v
aluesl
esst
han(
10)
,par
ti
ti
onp2v
aluesl
esst
han(
20)
,par
ti
ti
onp3
v
aluesl
esst
han(
30)
,par
ti
ti
onp4v
aluesl
esst
han(
maxv
alue)
);

*
*ify
ouar
eusi
ngmaxv
aluef
ort
hel
astpar
ti
ti
on,
youcannotaddapar
ti
ti
on.
b)I
nser
ti
ngr
ecor
dsi
ntor
angepar
ti
ti
onedt
abl
e
SQL>I
nser
tint
ost
udentv
alues(
1,’
a’
);-
-thi
swi
llgot
op1

.
92

SQL>I
nser
tint
ost
udentv
alues(
11,

b’)
; -
-thi
swi
ll
got
op2
SQL>I
nser
tint
ost
udentv
alues(
21,

c’)
; -
-thi
swi
ll
got
op3
SQL>I
nser
tint
ost
udentv
alues(
31,

d’)
; -
-thi
swi
ll
got
op4
c)Ret
ri
evi
ngr
ecor
dsf
rom r
angepar
ti
ti
onedt
abl
e
SQL>Sel
ect*
from st
udent
;
SQL>Sel
ect*
from st
udentpar
ti
ti
on(
p1)
;
d)Possi
bleoper
ati
onswi
thr
angepar
ti
ti
ons
 Add
 Dr
op
 Tr
uncat
e
 Rename
 Spl
it
 Mov
e
 Exchange
e)Addi
ngapar
ti
ti
on
SQL>Al
tert
abl
est
udentaddpar
ti
ti
onp5v
aluesl
esst
han(
40)
;
f
)Dr
oppi
ngapar
ti
ti
on
SQL>Al
tert
abl
est
udentdr
oppar
ti
ti
onp4;
g)Renami
ngapar
ti
ti
on
SQL>Al
tert
abl
est
udentr
enamepar
ti
ti
onp3t
op6;
h)Tr
uncat
eapar
ti
ti
on
SQL>Al
tert
abl
est
udentt
runcat
epar
ti
ti
onp6;
i
)Spl
it
ti
ngapar
ti
ti
on
SQL>Al
tert
abl
est
udentspl
itpar
ti
ti
onp2at
(15)i
nto(
par
ti
ti
onp21,
par
ti
ti
onp22)
;

j
)Exchangi
ngapar
ti
ti
on
SQL>Al
tert
abl
est
udentexchangepar
ti
ti
onp1wi
tht
abl
est
udent
2;
k)Mov
ingapar
ti
ti
on
SQL>Al
tert
abl
est
udentmov
epar
ti
ti
onp21t
abl
espacesaket
h_t
s;

LI
STPARTI
TIONS

a)Cr
eat
ingl
istpar
ti
ti
onedt
abl
e
SQL>Cr
eat
etabl
est
udent
(nonumber
(2)
,namev
archar
(2)
)par
ti
ti
onbyl
ist
(no)
(
par
ti
ti
onp1v
alues(
1,2,
3,
4,
5),
par
ti
ti
onp2v
alues(
6,7,
8,
9,
10)
,par
ti
ti
onp3
v
alues(
11,
12,
13,
14,
15)
,par
ti
ti
onp4v
alues(
16,
17,
18,
19,
20)
);
b)I
nser
ti
ngr
ecor
dsi
ntol
istpar
ti
ti
onedt
abl
e

.
93

SQL>I
nser
tint
ost
udentv
alues(
1,’
a’
);-
-thi
swi
llgot
op1
SQL>I
nser
tint
ost
udentv
alues(
6,’
b’
);-
-thi
swi
llgot
op2
SQL>I
nser
tint
ost
udentv
alues(
11,

c’)
; -
-thi
swi
ll
got
op3
SQL>I
nser
tint
ost
udentv
alues(
16,

d’)
; -
-thi
swi
ll
got
op4
c)Ret
ri
evi
ngr
ecor
dsf
rom l
istpar
ti
ti
onedt
abl
e
SQL>Sel
ect*
from st
udent
;
SQL>Sel
ect*
from st
udentpar
ti
ti
on(
p1)
;
d)Possi
bleoper
ati
onswi
thl
istpar
ti
ti
ons
 Add
 Dr
op
 Tr
uncat
e
 Rename
 Mov
e
 Exchange
e)Addi
ngapar
ti
ti
on
SQL>Al
tert
abl
est
udentaddpar
ti
ti
onp5v
alues(
21,
22,
23,
24,
25)
;
f
)Dr
oppi
ngapar
ti
ti
on
SQL>Al
tert
abl
est
udentdr
oppar
ti
ti
onp4;
g)Renami
ngapar
ti
ti
on
SQL>Al
tert
abl
est
udentr
enamepar
ti
ti
onp3t
op6;
h)Tr
uncat
eapar
ti
ti
on
SQL>Al
tert
abl
est
udentt
runcat
epar
ti
ti
onp6;

i
)Exchangi
ngapar
ti
ti
on
SQL>Al
tert
abl
est
udentexchangepar
ti
ti
onp1wi
tht
abl
est
udent
2;
j
)Mov
ingapar
ti
ti
on
SQL>Al
tert
abl
est
udentmov
epar
ti
ti
onp2t
abl
espacesaket
h_t
s;

HASHPARTI
TIONS

a)Cr
eat
inghashpar
ti
ti
onedt
abl
e
SQL>Cr
eat
etabl
est
udent
(nonumber
(2)
,namev
archar
(2)
)par
ti
ti
onbyhash(
no)
par
ti
ti
ons5;
Her
eor
acl
eaut
omat
ical
lygi
vespar
ti
ti
onnamesl
ike
SYS_
P1
SYS_
P2
SYS_
P3

.
94

SYS_
P4
SYS_
P5
b)I
nser
ti
ngr
ecor
dsi
ntohashpar
ti
ti
onedt
abl
e
i
twi
lli
nser
tther
ecor
dsbasedonhashf
unct
ioncal
cul
atedbyt
aki
ngt
hepar
ti
ti
onkey
SQL>I
nser
tint
ost
udentv
alues(
1,’
a’
);
SQL>I
nser
tint
ost
udentv
alues(
6,’
b’
);
SQL>I
nser
tint
ost
udentv
alues(
11,

c’)
;
SQL>I
nser
tint
ost
udentv
alues(
16,

d’)
;
c)Ret
ri
evi
ngr
ecor
dsf
rom hashpar
ti
ti
onedt
abl
e
SQL>Sel
ect*
from st
udent
;
SQL>Sel
ect*
from st
udentpar
ti
ti
on(
sys_
p1)
;
d)Possi
bleoper
ati
onswi
thhashpar
ti
ti
ons
 Add
 Tr
uncat
e
 Rename
 Mov
e
 Exchange
e)Addi
ngapar
ti
ti
on
SQL>Al
tert
abl
est
udentaddpar
ti
ti
onp6;
f
)Renami
ngapar
ti
ti
on
SQL>Al
tert
abl
est
udentr
enamepar
ti
ti
onp6t
op7;
g)Tr
uncat
eapar
ti
ti
on
SQL>Al
tert
abl
est
udentt
runcat
epar
ti
ti
onp7;
h)Exchangi
ngapar
ti
ti
on
SQL>Al
tert
abl
est
udentexchangepar
ti
ti
onsy
s_p1wi
tht
abl
est
udent
2;
i
)Mov
ingapar
ti
ti
on
SQL>Al
tert
abl
est
udentmov
epar
ti
ti
onsy
s_p2t
abl
espacesaket
h_t
s;

SUB-
PARTI
TIONSWI
THRANGEANDHASH

Subpar
ti
ti
onscl
ausei
susedbyhashonl
y.Wecannotcr
eat
esubpar
ti
ti
onswi
thl
istandhashpar
ti
ti
ons.

a)Cr
eat
ingsubpar
ti
ti
onedt
abl
e
SQL>Cr
eat
etabl
est
udent
(nonumber
(2)
,namev
archar
(2)
,mar
ksnumber
(3)
)
Par
ti
ti
onbyr
ange(
no)subpar
ti
ti
onbyhash(
name)subpar
ti
ti
ons3
(
Par
ti
ti
onp1v
aluesl
esst
han(
10)
,par
ti
ti
onp2v
aluesl
esst
han(
20)
);

.
95

Thi
swi
llcr
eat
etwopar
ti
ti
onsp1andp2wi
tht
hreesubpar
ti
ti
onsf
oreachpar
ti
ti
on
P1– SYS_
SUBP1
SYS_
SUBP2
SYS_
SUBP3
P2– SYS_
SUBP4
SYS_
SUBP5
SYS_
SUBP6
*
*ify
ouar
eusi
ngmaxv
aluef
ort
hel
astpar
ti
ti
on,
youcannotaddapar
ti
ti
on.
b)I
nser
ti
ngr
ecor
dsi
ntosubpar
ti
ti
onedt
abl
e
SQL>I
nser
tint
ost
udentv
alues(
1,’
a’
);-
-thi
swi
llgot
op1
SQL>I
nser
tint
ost
udentv
alues(
11,

b’)
; -
-thi
swi
ll
got
op2
c)Ret
ri
evi
ngr
ecor
dsf
rom subpar
ti
ti
onedt
abl
e
SQL>Sel
ect*
from st
udent
;
SQL>Sel
ect*
from st
udentpar
ti
ti
on(
p1)
;
SQL>Sel
ect*
from st
udentsubpar
ti
ti
on(
sys_
subp1)
;
d)Possi
bleoper
ati
onswi
thsubpar
ti
ti
ons
 Add
 Dr
op
 Tr
uncat
e
 Rename
 Spl
it
e)Addi
ngapar
ti
ti
on
SQL>Al
tert
abl
est
udentaddpar
ti
ti
onp3v
aluesl
esst
han(
30)
;
f
)Dr
oppi
ngapar
ti
ti
on
SQL>Al
tert
abl
est
udentdr
oppar
ti
ti
onp3;
g)Renami
ngapar
ti
ti
on
SQL>Al
tert
abl
est
udentr
enamepar
ti
ti
onp2t
op3;
h)Tr
uncat
eapar
ti
ti
on
SQL>Al
tert
abl
est
udentt
runcat
epar
ti
ti
onp1;
i
)Spl
it
ti
ngapar
ti
ti
on
SQL>Al
tert
abl
est
udentspl
itpar
ti
ti
onp3at
(15)i
nto(
par
ti
ti
onp31,
par
ti
ti
onp32)
;

DATAMODEL

 ALL_
IND_
PARTI
TIONS
 ALL_
IND_
SUBPARTI
TIONS
 ALL_
TAB_
PARTI
TIONS

.
96

 ALL_
TAB_
SUBPARTI
TIONS
 DBA_
IND_
PARTI
TIONS
 DBA_
IND_
SUBPARTI
TIONS
 DBA_
TAB_
PARTI
TIONS
 DBA_
TAB_
SUBPARTI
TIONS
 USER_
IND_
PARTI
TIONS
 USER_
IND_
SUBPARTI
TIONS
 USER_
TAB_
PARTI
TIONS
 USER_
TAB_
SUBPARTI
TIONS

GROUPBYANDHAVI
NG

GROUPBY

Usi
nggr
oupby
,wecancr
eat
egr
oupsofr
elat
edi
nfor
mat
ion.
Col
umnsusedi
nsel
ectmustbeusedwi
thgr
oupby
,ot
her
wisei
twasnotagr
oupbyexpr
essi
on.

Ex:
SQL>sel
ectdept
no,
sum(
sal
)fr
om empgr
oupbydept
no;

DEPTNO SUM(
SAL)
-
--
--
--
--
---
--
--
--
--
10 8750
20 10875
30 9400

SQL>sel
ectdept
no,
j
ob,
sum(
sal
)fr
om empgr
oupbydept
no,
j
ob;

DEPTNO JOB SUM(


SAL)

.
97

-
--
--
--
--
---
--
--
--
---
--
--
--
--
10 CLERK 1300
10 MANAGER 2450
10 PRESI
DENT 5000
20 ANALYST 6000
20 CLERK 1900
20 MANAGER 2975
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600

HAVI
NG

Thi
swi
llwor
kaswher
ecl
ausewhi
chcan beusedonl
ywi
thgr
oupbybecauseofabsenceofwher
ecl
ausei
n
gr
oupby
.
Ex:
SQL>sel
ectdept
no,
j
ob,
sum(
sal
)tsalf
rom empgr
oupbydept
no,
j
obhav
ingsum(
sal
)>
3000;

DEPTNO JOB TSAL


-
--
--
--
--
---
--
--
--
- -
--
--
--
--
-
10 PRESI
DENT 5000
20 ANALYST 6000
30 SALESMAN 5600

SQL>sel
ectdept
no,
j
ob,
sum(
sal
)tsalf
rom empgr
oupbydept
no,
j
obhav
ingsum(
sal
)>
3000or
derbyj
ob;

DEPTNO JOB TSAL


-
--
--
--
--
---
--
--
--
---
--
--
--
--
20 ANALYST 6000
10 PRESI
DENT 5000
30 SALESMAN 5600

ORDEROFEXECUTI
ON

 Gr
oupt
her
owst
oget
herbasedongr
oupbycl
ause.

.
98

 Cal
cul
atet
hegr
oupf
unct
ionsf
oreachgr
oup.
 Chooseandel
imi
nat
ethegr
oupsbasedont
hehav
ingcl
ause.
 Or
dert
hegr
oupsbasedont
hespeci
fi
edcol
umn.

ROLLUPGROUPI
NGCUBE

Thesear
etheenhancement
stot
hegr
oupbyf
eat
ure.

USI
NGROLLUP

Thi
swi
llgi
vet
hesal
ari
esi
neachdepar
tmenti
neachj
obcat
egor
yal
ongwi
hthet
otalsal
aryf
ori
ndi
vi
dual
depar
tment
sandt
het
otalsal
aryofal
lthedepar
tment
s.

SQL>Sel
ectdept
no,
j
ob,
sum(
sal
)fr
om empgr
oupbyr
oll
up(
dept
no,
j
ob)
;

DEPTNO JOB SUM(


SAL)
-
--
--
--
--
---
--
--
--
---
--
--
--
--
10 CLERK 1300
10 MANAGER 2450
10 PRESI
DENT 5000
10 8750
20 ANALYST 6000
20 CLERK 1900
20 MANAGER 2975
20 10875
30 CLERK 950
30 MANAGER 2850

.
99

30 SALESMAN 5600
30 9400
29025

USI
NGGROUPI
NG

I
ntheabov
equer
yitwi
llgi
vet
het
otalsal
aryoft
hei
ndi
vi
dual
depar
tment
sbutwi
tha
bl
anki
nthej
obcol
umnandgi
vest
het
otal
sal
aryofal
lthedepar
tment
swi
thbl
anksi
n
dept
noandj
obcol
umns.

Tor
epl
acet
hesebl
ankswi
thy
ourdesi
redst
ri
nggr
oupi
ngwi
llbeused

SQL>sel
ectdecode(
groupi
ng(
dept
no)
,1,
'
AllDept
s',
dept
no)
,decode(
groupi
ng(
job)
,1,
'
All
j
obs'
,
job)
,sum(
sal
)fr
om empgr
oupbyr
oll
up(
dept
no,
j
ob)
;

DECODE(
GROUPI
NG(
DEPTNO)
,1,
'
ALLDEPTS'
,
DEPDECODE(
GR SUM(
SAL)
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
10 CLERK 1300
10 MANAGER 2450
10 PRESI
DENT 5000
10 Al
ljobs 8750
20 ANALYST 6000
20 CLERK 1900
20 MANAGER 2975
20 Al
ljobs 10875
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600
30 Al
ljobs 9400
Al
lDept
s Al
ljobs 29025

Gr
oupi
ngwi
llr
etur
n1i
fthecol
umnwhi
chi
sspeci
fi
edi
nthegr
oupi
ngf
unct
ionhasbeen
usedi
nrol
lup.
Gr
oupi
ngwi
llbeusedi
nassoci
ati
onwi
thdecode.

USI
NGCUBE

.
100

Thi
swi
llgi
vet
hesal
ari
esi
neachdepar
tmenti
neachj
obcat
egor
y,t
het
otalsal
aryf
ori
ndi
vi
dualdepar
tment
s,t
he
t
otalsal
aryofal
lthedepar
tment
sandt
hesal
ari
esi
neachj
obcat
egor
y.

SQL>sel
ectdecode(
groupi
ng(
dept
no)
,1,

AllDept
s’,
dept
no)
,decode(
groupi
ng(
job)
,1,

All
Jobs’
,
job)
,sum(
sal
)fr
om empgr
oupbycube(
dept
no,
j
ob)
;

DECODE(
GROUPI
NG(
DEPTNO)
,1,
'
ALLDEPTS'
,
DEPDECODE(
GR SUM(
SAL)
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
10 CLERK 1300
10 MANAGER 2450
10 PRESI
DENT 5000
10 Al
lJobs 8750
20 ANALYST 6000
20 CLERK 1900
20 MANAGER 2975
20 Al
lJobs 10875
30 CLERK 950
30 MANAGER 2850
30 SALESMAN 5600
30 Al
lJobs 9400
Al
lDept
s ANALYST 6000
Al
lDept
s CLERK 4150
Al
lDept
s MANAGER 8275
Al
lDept
s PRESI
DENT 5000
Al
lDept
s SALESMAN 5600
Al
lDept
s Al
lJobs 29025

.
101

SETOPERATORS

TYPES

 Uni
on
 Uni
onal
l
 I
nter
sect
 Mi
nus

UNI
ON

Thi
swi
llcombi
net
her
ecor
dsofmul
ti
plet
abl
eshav
ingt
hesamest
ruct
ure.

Ex:
SQL>sel
ect*f
rom st
udent
1uni
onsel
ect*f
rom st
udent
2;

UNI
ONALL

Thi
swi
llcombi
net
her
ecor
dsofmul
ti
plet
abl
eshav
ingt
hesamest
ruct
urebuti
ncl
udi
ngdupl
icat
es.

Ex:
SQL>sel
ect*f
rom st
udent
1uni
onal
lsel
ect*f
rom st
udent
2;

I
NTERSECT

Thi
swi
llgi
vet
hecommonr
ecor
dsofmul
ti
plet
abl
eshav
ingt
hesamest
ruct
ure.

Ex:
SQL>sel
ect*f
rom st
udent
1int
ersectsel
ect*f
rom st
udent
2;

.
102

MI
NUS

Thi
swi
llgi
vet
her
ecor
dsofat
abl
ewhoser
ecor
dsar
enoti
not
hert
abl
eshav
ingt
hesamest
ruct
ure.

Ex:
SQL>sel
ect*f
rom st
udent
1mi
nussel
ect*f
rom st
udent
2;

.
103

VI
EWS

Av
iew i
sadat
abaseobj
ectt
hati
sal
ogi
calr
epr
esent
ati
onofat
abl
e.I
tisdel
iv
eredf
rom at
abl
ebuthasno
st
orageofi
tsownandof
tenmaybeusedi
nthesamemannerasat
abl
e.

Av
iewt
akest
heout
putoft
hequer
yandt
reat
sitasat
abl
e,t
her
efor
eav
iewcanbet
houghtofasast
oredquer
y
orav
irt
ualt
abl
e.

TYPES
 Si
mpl
evi
ew
 Compl
exv
iew

Si
mpl
evi
ewcanbecr
eat
edf
rom onet
abl
ewher
eascompl
exv
iewcanbecr
eat
edf
rom mul
ti
plet
abl
es.

WHYVI
EWS?

 Pr
ovi
desaddi
ti
onall
evelofsecur
it
ybyr
est
ri
cti
ngaccesst
oapr
edet
ermi
nedsetofr
owsand/
orcol
umns
ofat
abl
e.
 Hi
det
hedat
acompl
exi
ty.
 Si
mpl
i
fycommandsf
ort
heuser
.

VI
EWSWI
THOUTDML

 Readonl
yvi
ew
 Vi
ewwi
thgr
oupby
 Vi
ewwi
thaggr
egat
efunct
ions
 Vi
ewwi
thr
ownum
 Par
ti
ti
onv
iew
 Vi
ewwi
thdi
sti
nct

Ex:
SQL>Cr
eat
evi
ewdept
_vassel
ect*
from deptwi
thr
eadonl
y;
SQL>Cr
eat
evi
ewdept
_vassel
ectdept
no,
sum(
sal
)t_
salf
rom empgr
oupbydept
no;
SQL>Cr
eat
evi
ewst
udassel
ectr
ownum no,
name,
mar
ksf
rom st
udent
;

.
104

SQL>Cr
eat
evi
ewst
udentassel
ect*
from st
udent
1uni
onsel
ect*
from st
udent
2;
SQL>Cr
eat
evi
ewst
udassel
ectdi
sti
nctno,
namef
rom st
udent
;

VI
EWSWI
THDML

 Vi
ewwi
thnotnul
lcol
umn-
-inser
twi
thoutnotnul
lcol
umnnotpossi
ble
-
-updat
enotnul
lcol
umnt
onul
lisnotpossi
ble
-
-del
etepossi
ble
 Vi
ewwi
thoutnotnul
lcol
umnwhi
chwasi
nbaset
abl
e--i
nser
tnotpossi
ble
-
-updat
e,del
etepossi
ble
 Vi
ewwi
thexpr
essi
on-
-inser
t,updat
enotpossi
ble
-
-del
etepossi
ble
 Vi
ewwi
thf
unct
ions(
exceptaggr
egat
e)-
-inser
t,updat
enotpossi
ble
-
-del
etepossi
ble
 Vi
ew wascr
eat
edbutt
heunder
lyi
ngt
abl
ewasdr
oppedt
henwewi
llgett
hemessagel
ike“v
iew has
er
ror
s”.
 Vi
ewwascr
eat
edbutt
hebaset
abl
ehasbeenal
ter
edbutst
il
lthev
iewwaswi
tht
hei
nit
ialdef
ini
ti
on,we
hav
etor
epl
acet
hev
iewt
oaf
fectt
hechanges.
 Compl
exv
iew(
viewwi
thmor
ethanonet
abl
e)-
-inser
tnotpossi
ble
-
-updat
e,del
etepossi
ble(
notal
way
s)

CREATI
NGVI
EW WI
THOUTHAVI
NGTHEBASETABLE

SQL>Cr
eat
efor
cev
iewst
udassel
ect*
From st
udent
;
-
-Oncet
hebaset
abl
ewascr
eat
edt
hent
hev
iewi
sval
idat
ed.

VI
EW WI
THCHECKOPTI
ONCONSTRAI
NT

SQL>Cr
eat
evi
ewst
udassel
ect*
from st
udentwher
emar
ks=500wi
thcheckopt
ion
const
rai
ntCk;
-I
nser
tpossi
blewi
thmar
ksv
alueas500
-Updat
epossi
bleexcl
udi
ngmar
kscol
umn
-Del
etepossi
ble

DROPPI
NGVI
EWS

SQL>dr
opv
iewdept
_v;

.
105

DATAMODEL

ALL_
VIEW
DBA_
VIEW
USER_
VIEWS

SYNONYM ANDSEQUENCE

SYNONYM

.
106

Asy
nony
misadat
abaseobj
ect
,whi
chi
susedasanal
iasf
orat
abl
e,v
ieworsequence.

TYPES
 Pr
ivat
e
 Publ
ic
Pr
ivat
esy
nony
misav
ail
abl
etot
hepar
ti
cul
aruserwhocr
eat
es.
Publ
icsy
nony
miscr
eat
edbyDBAwhi
chi
sav
ail
abl
etoal
ltheuser
s.

ADVANTAGES

 Hi
det
henameandowneroft
heobj
ect
.
 Pr
ovi
desl
ocat
iont
ranspar
encyf
orr
emot
eobj
ect
sofadi
str
ibut
eddat
abase.

CREATEANDDROP

SQL>cr
eat
esy
nony
m s1f
oremp;
SQL>cr
eat
epubl
icsy
nony
m s2f
oremp;
SQL>dr
opsy
nony
m s1;

SEQUENCE

Asequencei
sadat
abaseobj
ect
,whi
chcangener
ateuni
que,
sequent
iali
ntegerv
alues.
I
tcanbeusedt
oaut
omat
ical
lygener
atepr
imar
ykeyoruni
quekeyv
alues.
Asequencecanbeei
theri
nanascendi
ngordescendi
ngor
der
.

Sy
ntax:
Cr esequence<seq_
eat name>[
incr
ementbt
yn][
star
twi
thn][
maxv
aluen]
[
minv
aluen][
cycl
e/nocy
cle][
cache/
nocache]
;

Bydef
alul
tthesequencest
art
swi
th1,
incr
ement
sby1wi
thmi
nval
ueof1andwi
thnocy
cle,nocache.
Cacheopt
ionpr
e-al
loocat
esasetofsequencenumber
sandr
etai
nst
hem i
nmemor
yforf
ast
eraccess.

Ex:
SQL>cr
eat
esequences;
SQL>cr
eat
esequencesi
ncr
ementby10st
artwi
th100mi
nval
ue5maxv
alue200cy
cle
cache20;

.
107

USI
NGSEQUENCE

SQL>cr
eat
etabl
est
udent
(nonumber
(2)
,namev
archar
(10)
);
SQL>i
nser
tint
ost
udentv
alues(
s.next
val
,‘
saket
h’)
;

 I
nit
ial
lycur
rval
isnotdef
inedandnext
vali
sst
art
ingv
alue.
 Af
tert
hatnext
valandcur
rvalar
eal
way
sequal
.

CREATI
NGALPHA-
NUMERI
CSEQUENCE

SQL>cr
eat
esequencesst
artwi
th111234;
SQL>I
nser
tint
ost
udentv
alues(
s.next
val|
|tr
ansl
ate
(
s.next
val
,
’1234567890’
,
’abcdef
ghi
j’
))
;

ALTERI
NGSEQUENCE

Wecanal
tert
hesequencet
oper
for
mthef
oll
owi
ng.
 Setorel
imi
nat
emi
nval
ueormaxv
alue.
 Changet
hei
ncr
ementv
alue.
 Changet
henumberofcachedsequencenumber
s.

Ex:
SQL>al
tersequencesmi
nval
ue5;
SQL>al
tersequencesi
ncr
ementby2;
SQL>al
tersequencescache10;

DROPPI
NGSEQUENCE

SQL>dr
opsequences;

JOI
NS

 Thepur
poseofaj
oini
stocombi
net
hedat
aacr
osst
abl
es.
 Aj
oini
sact
ual
lyper
for
medbyt
hewher
ecl
ausewhi
chcombi
nest
hespeci
fi
edr
owsoft
abl
es.
 I
faj
oini
nvol
vesi
nmor
ethant
wot
abl
est
henor
acl
ejoi
nsf
ir
stt
wot
abl
esbasedont
hej
oinscondi
ti
on
andt
hencompar
est
her
esul
twi
tht
henextt
abl
eandsoon.

TYPES
 Equij
oin

.
108

 Non-
equij
oin
 Sel
fjoi
n
 Nat
uralj
oin
 Cr
ossj
oin
 Out
erj
oin
 Lef
tout
er
 Ri
ghtout
er
 Ful
lout
er
 I
nnerj
oin
 Usi
ngcl
ause
 Oncl
ause

Assumet
hatwehav
ethef
oll
owi
ngt
abl
es.

SQL>sel
ect*f
rom dept
;

DEPTNODNAME LOC
-
--
--
---
--
--
--
---
--
--
--
--
-
10mkt hy
d
20f
in bang
30hr bombay

SQL>sel
ect*f
rom emp;

EMPNO ENAME JOB MGR DEPTNO


-
--
--
--
--
---
--
--
--
---
--
--
--
--
---
--
--
--
---
--
--
--
--
-
111 saket
h anal
yst 444 10
222 sudha cl
erk 333 20
333 j
agan manager 111 10
444 madhu engi
neer 222 40

EQUIJOI
N

.
109

Aj
oinwhi
chcont
ainsan‘
=’oper
atori
nthej
oinscondi
ti
on.

Ex:
SQL>sel
ectempno,
ename,
j
ob,
dname,
l
ocf
rom empe,
deptdwher
ee.
dept
no=d.
dept
no;

EMPNO ENAME JOB DNAME LOC


-
--
--
--
--
---
--
--
--
---
--
--
--
--
---
--
--
--
---
--
--
--
--
-
111 saket
h anal
yst mkt hy
d
333 j
agan managermkt hy
d
222 sudha cl
erk f
in bang

USI
NGCLAUSE

SQL>sel
ectempno,
ename,
j
ob,
dname,
l
ocf
rom empej
oindeptdusi
ng(
dept
no)
;

EMPNO ENAME JOB DNAME LOC


-
--
--
--
--
---
--
--
--
---
--
--
--
--
---
--
--
--
---
--
--
--
--
-
111 saket
h anal
yst mkt hy
d
333 j
agan managermkt hy
d
222 sudha cl
erk f
in bang

ONCLAUSE

SQL> sel
ectempno,
ename,
j
ob,
dname,
l
ocf
rom empej
oindeptdon(
e.dept
no=d.
dept
no)
;
EMPNO ENAME JOB DNAME LOC
-
--
--
--
--
---
--
--
--
---
--
--
--
--
---
--
--
--
---
--
--
--
--
-
111 saket
h anal
yst mkt hy
d
333 j
agan managermkt hy
d
222 sudha cl
erk f
in bang

NON-
EQUIJOI
N

Aj
oinwhi
chcont
ainsanoper
atorot
hert
han‘
=’i
nthej
oinscondi
ti
on.

Ex:
SQL>sel
ectempno,
ename,
j
ob,
dname,
l
ocf
rom empe,
deptdwher
ee.
dept
no>
d.
dept
no;

.
110

EMPNO ENAME JOB DNAME LOC


-
--
--
--
--
---
--
--
--
---
--
--
--
--
---
--
--
--
---
--
--
--
--
-
222 sudha cl
erk mkt hy
d
444 madhu engi
neer mkt hy
d
444 madhu engi
neer f
in bang
444 madhu engi
neer hr bombay

SELFJOI
N

Joi
ningt
het
abl
eit
sel
fiscal
ledsel
fjoi
n.

Ex:
SQL>sel
ecte1.
empno,
e2.
ename,
e1.
job,
e2.
dept
nof
rom empe1,
empe2wher
e
e1.
empno=e2.
mgr
;

EMPNO ENAME JOB DEPTNO


-
--
--
--
--
---
--
--
--
---
--
--
--
--
---
--
--
--
--
111 j
agan anal
yst 10
222 madhu cl
erk 40
333 sudha manager 20
444 saket
h engi
neer 10
NATURALJOI
N

Nat
uralj
oincompar
esal
lthecommoncol
umns.

Ex:
SQL>sel
ectempno,
ename,
j
ob,
dname,
l
ocf
rom empnat
uralj
oindept
;

EMPNO ENAME JOB DNAME LOC


-
--
--
--
--
---
--
--
--
---
--
--
--
--
---
--
--
--
---
--
--
--
--
-
111 saket
h anal
yst mkt hy
d
333 j
agan manager mkt hy
d
222 sudha cl
erk f
in bang

CROSSJOI
N

.
111

Thi
swi
llgi
vest
hecr
osspr
oduct
.

Ex:
SQL>sel
ectempno,
ename,
j
ob,
dname,
l
ocf
rom empcr
ossj
oindept
;

EMPNO ENAME JOB DNAME LOC


-
--
--
--
--
---
--
--
--
---
--
--
--
--
---
--
--
--
---
--
--
--
--
-
111 saket
h anal
yst mkt hy
d
222 sudha cl
erk mkt hy
d
333 j
agan manager mkt hy
d
444 madhu engi
neer mkt hy
d
111 saket
h anal
yst f
in bang
222 sudha cl
erk f
in bang
333 j
agan manager f
in bang
444 madhu engi
neer f
in bang
111 saket
h anal
yst hr bombay
222 sudha cl
erk hr bombay
333 j
agan manager hr bombay
444 madhu engi
neer hr bombay

OUTERJOI
N

Out
erj
oingi
vest
henon-
mat
chi
ngr
ecor
dsal
ongwi
thmat
chi
ngr
ecor
ds.

LEFTOUTERJOI
N

Thi
swi
lldi
spl
ayt
heal
lmat
chi
ngr
ecor
dsandt
her
ecor
dswhi
char
einl
efthandsi
det
abl
ethoset
hatar
enoti
n
r
ighthandsi
det
abl
e.

Ex:
SQL>sel
ectempno,
ename,
j
ob,
dname,
l
ocf
rom empel
eftout
erj
oindeptd
on(
e.dept
no=d.
dept
no)
;
Or
SQL>sel
ectempno,
ename,
j
ob,
dname,
l
ocf
rom empe,
deptdwher
e
e.
dept
no=d.
dept
no(
+);

.
112

EMPNO ENAME JOB DNAME LOC


-
--
--
--
--
---
--
--
--
---
--
--
--
--
---
--
--
--
---
--
--
--
--
-
111 saket
h anal
yst mkt hy
d
333 j
agan manager mkt hy
d
222 sudha cl
erk f
in bang
444 madhu engi
neer

RI
GHTOUTERJOI
N

Thi
swi
lldi
spl
ayt
heal
lmat
chi
ngr
ecor
dsandt
her
ecor
dswhi
char
einr
ighthandsi
det
abl
ethoset
hatar
enoti
n
l
efthandsi
det
abl
e.

Ex:
SQL>sel
ectempno,
ename,
j
ob,
dname,
l
ocf
rom emper
ightout
erj
oindeptd
on(
e.dept
no=d.
dept
no)
;
Or
SQL>sel
ectempno,
ename,
j
ob,
dname,
l
ocf
rom empe,
deptdwher
ee.
dept
no(
+)=
d.
dept
no;

EMPNO ENAME JOB DNAME LOC


-
--
--
--
--
---
--
--
--
---
--
--
--
--
---
--
--
--
---
--
--
--
--
-
111 saket
h anal
yst mkt hy
d
333 j
agan manager mkt hy
d
222 sudha cl
erk f
in bang
hr bombay

FULLOUTERJOI
N

Thi
swi
lldi
spl
ayt
heal
lmat
chi
ngr
ecor
dsandt
henon-
mat
chi
ngr
ecor
dsf
rom bot
htabl
es.

Ex:
SQL>sel
ectempno,
ename,
j
ob,
dname,
l
ocf
rom empef
ullout
erj
oindeptd
on(
e.dept
no=d.
dept
no)
;

EMPNO ENAME JOB DNAME LOC


-
--
--
--
--
---
--
--
--
---
--
--
--
--
---
--
--
--
---
--
--
--
--
-
333 j
agan manager mkt hy
d

.
113

111 saket
h anal
yst mkt hy
d
222 sudha cl
erk f
in bang
444 madhu engi
neer
hr bombay

I
NNERJOI
N

Thi
swi
lldi
spl
ayal
lther
ecor
dst
hathav
emat
ched.

Ex:
SQL>sel
ectempno,
ename,
j
ob,
dname,
l
ocf
rom empi
nnerj
oindeptusi
ng(
dept
no)
;

EMPNO ENAME JOB DNAME LOC


-
--
--
--
--
---
--
--
--
---
--
--
--
--
---
--
--
--
---
--
--
--
--
-
111 saket
h anal
yst mkt hy
d
333 j
agan manager mkt hy
d
222 sudha cl
erk f
in bang

SUBQUERI
ESANDEXI
STS

SUBQUERI
ES

 Nest
ingofquer
ies,
onewi
thi
ntheot
heri
ster
medasasubquer
y.
 Ast
atementcont
aini
ngasubquer
yiscal
ledapar
entquer
y.
 Subquer
iesar
eusedt
oret
ri
evedat
afr
om t
abl
est
hatdependont
hev
aluesi
nthet
abl
eit
sel
f.

TYPES

 Si
ngl
erowsubquer
ies
 Mul
tir
owsubquer
ies
 Mul
ti
plesubquer
ies
 Cor
rel
atedsubquer
ies

SI
NGLEROW SUBQUERI
ES

I
nsi
ngl
erowsubquer
y,i
twi
ll
ret
urnonev
alue.

.
114

Ex:
SQL>sel
ect*f
rom empwher
esal>(
sel
ectsalf
rom empwher
eempno=7566)
;

EMPNO ENAME JOB MGR HI


REDATE SAL COMM DEPTNO
-
--
--
--
--
---
--
--
--
---
--
--
--
---
--
--
--
--
---
--
--
--
--
---
--
--
---
--
--
--
--
---
--
--
--
--
7788 SCOTT ANALYST 7566 19-
APR-
87 3000 20
7839 KI
NG PRESI
DENT 17-
NOV-
815000 10
7902 FORD ANALYST 7566 03-
DEC-
81 3000 20

MULTIROW SUBQUERI
ES

I
nmul
tir
owsubquer
y,i
twi
llr
etur
nmor
ethanonev
alue.I
nsuchcasesweshoul
dincl
udeoper
ator
sli
keany
,al
l,
i
nornoti
nbet
weent
hecompar
isi
onoper
atorandt
hesubquer
y.

Ex:
SQL>sel
ect*f
rom empwher
esal>any(
sel
ectsalf
rom empwher
esalbet
ween2500
and4000)
;

EMPNO ENAME JOB MGR HI


REDATE SAL COMM DEPTNO
-
--
--
--
--
---
--
--
--
---
--
--
--
---
--
--
--
--
---
--
--
--
--
---
--
--
---
--
--
--
--
---
--
--
--
--
7566 JONES MANAGER 783902-
APR-
81 2975 20
7788 SCOTT ANALYST 756619-
APR-
87 3000 20
7839 KI
NG PRESI
DENT 17-
NOV-
81 5000 10
7902 FORD ANALYST 756603-
DEC-
81 3000 20

SQL>sel
ect*f
rom empwher
esal>al
l(sel
ectsalf
rom empwher
esalbet
ween2500
and4000)
;

EMPNO ENAME JOB MGR HI


REDATE SAL COMM DEPTNO
-
--
--
--
--
---
--
--
--
---
--
--
--
---
--
--
--
--
---
--
--
--
--
--
---
--
---
--
--
--
--
---
--
--
--
--
7839 KI
NG PRESI
DENT 17-
NOV-
815000 10

MULTI
PLESUBQUERI
ES

Ther
eisnol
imi
tont
henumberofsubquer
iesi
ncl
udedi
nawher
ecl
ause.I
tal
lowsnest
ingofaquer
ywi
thi
na
subquer
y.

.
115

Ex:
SQL>sel
ect*f
rom empwher
esal=(
sel
ectmax(
sal
)fr
om empwher
esal<(
sel
ect
max(
sal
)fr
om emp)
);

EMPNO ENAME JOB MGR HI


REDATE SAL COMM DEPTNO
-
--
--
--
--
---
--
--
--
---
--
--
--
---
--
--
--
--
---
--
--
--
--
---
--
--
---
--
--
--
--
---
--
--
--
--
7788 SCOTT ANALYST 7566 19-
APR-
87 3000 20
7902 FORD ANALYST 7566 03-
DEC-
81 3000 20

CORRELATEDSUBQUERI
ES

Asubquer
yisev
aluat
edoncef
ort
heent
ir
epar
entst
atementwher
easacor
rel
atedsubquer
yisev
aluat
edonce
f
orev
eryr
owpr
ocessedbyt
hepar
entst
atement
.
Ex:
SQL>sel
ectdi
sti
nctdept
nof
rom empewher
e5<=(
sel
ectcount
(ename)f
rom emp
wher
ee.
dept
no=dept
no)
;

DEPTNO
-
--
--
--
--
-
20
30

EXI
STS

Exi
stsf
unct
ioni
sat
estf
orexi
stence.Thi
sisal
ogi
calt
estf
ort
her
etur
nofr
owsf
rom aquer
y.

Ex:
Supposewewantt
odi
spl
ayt
hedepar
tmentnumber
swhi
chhasmor
ethan4
empl
oyees.

SQL>sel
ectdept
no,
count
(*)f
rom empgr
oupbydept
nohav
ingcount
(*)>4;

DEPTNO COUNT(
*)
-
--
--
--
---
--
--
--
--
-
20 5
30 6

.
116

Fr
om t
heabov
equer
ycany
ouwantt
odi
spl
ayt
henamesofempl
oyees?
SQL>sel
ectdept
no,
ename,
count
(*)f
rom empgr
oupbydept
no,
enamehav
ingcount
(*)
>4;

nor
owssel
ect
ed

Theabov
equer
yret
urnsnot
hingbecausecombi
nat
ionofdept
noandenamenev
er
r
etur
nmor
ethanonecount
.

Thesol
uti
oni
stouseexi
stswhi
chf
oll
ows.

SQL>sel
ectdept
no,
enamef
rom empe1wher
eexi
sts(
sel
ect*f
rom empe2
wher
ee1.
dept
no=e2.
dept
nogr
oupbye2.
dept
nohav
ingcount
(e2.
ename)>4)
or
derbydept
no,
ename;

DEPTNO ENAME
-
--
--
--
--
---
--
--
--
--
20ADAMS
20FORD
20JONES
20SCOTT
20SMI
TH
30ALLEN
30BLAKE
30JAMES
30MARTI
N
30TURNER
30WARD

NOTEXI
STS

SQL>sel
ectdept
no,
enamef
rom empe1wher
enotexi
sts(
sel
ect*f
rom empe2
wher
ee1.
dept
no=e2.
dept
nogr
oupbye2.
dept
nohav
ingcount
(e2.
ename)>4)or
der
bydept
no,
ename;

DEPTNOENAME

.
117

-
--
--
--
---
--
--
--
--
-
10 CLARK
10 KI
NG
10 MI
LLER

WALKUPTREESANDI
NLI
NEVI
EW

WALKUPTREES

Usi
nghi
erar
chi
calquer
ies,y
oucanr
etr
iev
edat
abasedonanat
uralhi
erar
chi
calr
elat
ionshi
pbet
weenr
owsi
na
t
abl
e.Howev
er,wher
eahi
erar
chi
calr
elat
ionshi
pexi
stsbet
weent
her
owsofat
abl
e,apr
ocesscal
ledt
ree
wal
kingenabl
est
hehi
erar
chyt
obeconst
ruct
ed.

Ex:
SQL>sel
ectename|
|'
==>'
||pr
iorename,
lev
elf
rom empst
artwi
thename='
KING'
connectbypr
iorempno=mgr
;

ENAME|
|'
==>'
|
|PRI
ORENAM LEVEL
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
KI
NG==> 1
JONES==>KI
NG 2
SCOTT==>JONES 3
ADAMS==>SCOTT 4
FORD==>JONES 3
SMI
TH==>FORD 4
BLAKE==>KI
NG 2
ALLEN==>BLAKE 3
WARD==>BLAKE 3
MARTI
N==>BLAKE 3
TURNER==>BLAKE 3

.
118

JAMES==>BLAKE 3
CLARK==>KI
NG 2
MI
LLER==>CLARK 3

I
ntheabov
e
St
artwi
thcl
ausespeci
fi
est
her
ootr
owoft
het
abl
e.
Lev
elpseudocol
umngi
vest
he1f
orr
oot,
2forchi
ldandsoon.
Connectbypr
iorcl
ausespeci
fi
est
hecol
umnswhi
chhaspar
ent
-chi
ldr
elat
ionshi
p.

I
NLI
NEVI
EW ORTOP-
NANALYSI
S

I
nthesel
ectst
atementi
nst
eadoft
abl
ename,
repl
aci
ngt
hesel
ectst
atementi
sknownasi
nli
nev
iew.

Ex:
SQL>Sel
ectename,
sal
,rownum r
ankf
rom (
sel
ect*
from empor
derbysal
);

ENAME SAL RANK


-
--
--
--
--
---
--
--
--
---
--
--
--
--
-
SMI
TH 800 1
JAMES 950 2
ADAMS 1100 3
WARD 1250 4
MARTI
N 1250 5
MI
LLER 1300 6
TURNER 1500 7
ALLEN 1600 8
CLARK 2450 9
BLAKE 2850 10
JONES 2975 11
SCOTT 3000 12
FORD 3000 13
KI
NG 5000 14

.
119

LOCKS

Locksar
ethemechani
smsusedt
opr
eventdest
ruct
ivei
nter
act
ionbet
weenuser
saccessi
ngsamer
esour
ce
si
mul
taneousl
y.Lockspr
ovi
deshi
ghdegr
eeofdat
aconcur
rency
.

TYPES
 Rowl
evell
ocks
 Tabl
elev
ell
ocks

ROW LEVELLOCKS

I
nther
owl
evell
ockar
owi
slockedexcl
usi
vel
ysot
hatot
hercannotmodi
fyt
her
owunt
ilt
het
ransact
ionhol
ding
t
hel
ocki
scommi
tt
edorr
oll
edback.Thi
scanbedonebyusi
ngsel
ect
..
forupdat
ecl
ause.

Ex:
SQL>sel
ect*f
rom empwher
esal>3000f
orupdat
eofcomm.
;

TABLELEVELLOCKS

At
abl
elev
ell
ockwi
llpr
otectt
abl
edat
ather
ebyguar
ant
eei
ngdat
aint
egr
it
ywhendat
aisbei
ngaccessed
concur
rent
lybymul
ti
pleuser
s.At
abl
elockcanbehel
dinsev
eralmodes.

 Shar
elock
 Shar
eupdat
elock
 Excl
usi
vel
ock

SHARELOCK

Ashar
elockl
ockst
het
abl
eal
lowi
ngot
heruser
stoonl
yquer
ybutnoti
nser
t,updat
eordel
eter
owsi
nat
abl
e.

.
120

Mul
ti
pleuser
scanpl
aceshar
elocksont
hesamer
esour
ceatt
hesamet
ime.

Ex:
SQL>l
ockt
abl
eempi
nshar
emode;
SHAREUPDATELOCK

I
tlocksr
owst
hatar
etobeupdat
edi
nat
abl
e.I
tper
mit
sot
heruser
stoconcur
rent
lyquer
y,i
nser
t,updat
eorev
en
l
ockot
herr
owsi
nthesamet
abl
e.I
tpr
event
stheot
heruser
sfr
om updat
ingt
her
owt
hathasbeenl
ocked.

Ex:
SQL>l
ockt
abl
eempi
nshar
eupdat
emode;

EXCLUSI
VELOCK

Excl
usi
vel
ocki
sthemostr
est
ri
cti
veoft
abl
esl
ocks.Wheni
ssuedbyanyuser
,ital
lowst
heot
herusert
oonl
y
quer
y.I
tissi
mil
art
oshar
elockbutonl
yoneusercanpl
aceexcl
usi
vel
ockonat
abl
eatat
ime.

Ex:
SQL>l
ockt
abl
eempi
nshar
eexcl
usi
vemode;

NOWAI
T

I
foneuserl
ockedt
het
abl
ewi
thoutnowai
tthenanot
herusert
ryi
ngt
olockt
hesamet
abl
ethenhehast
owai
t
unt
ilt
heuserwhohasi
nit
ial
lyl
ockedt
het
abl
eissuesacommi
torr
oll
backst
atement
.Thi
sdel
aycoul
dbe
av
oidedbyappendi
nganowai
tcl
ausei
nthel
ockt
abl
ecommand.

Ex:
SQL>l
ockt
abl
eempi
nexcl
usi
vemodenowai
t.

DEADLOCK

Adeadl
ockoccur
swhent
owuser
shav
eal
ockeachonsepar
ateobj
ect
,andt
heywantt
oacqui
real
ockont
he
eachot
her
’sobj
ect
.Whent
hishappens,
thef
ir
stuserhast
owai
tfort
hesecondusert
orel
easet
hel
ock,
butt
he
seconduserwi
llnotr
eleasei
tunt
ilt
hel
ockont
hef
ir
stuser
’sobj
ecti
sfr
eed.I
nsuchacase,or
acl
edet
ect
sthe
deadl
ockaut
omat
ical
lyandsol
vest
hepr
obl
em byabor
ti
ngoneoft
het
wot
ransact
ions.

I
NDEXES

.
121

I
ndexi
sty
pical
lyal
ist
ingofkey
wor
dsaccompani
edbyt
hel
ocat
ionofi
nfor
mat
iononasubj
ect
.Wecancr
eat
e
i
ndexesexpl
ici
tl
ytospeedupSQLst
atementexecut
iononat
abl
e.Thei
ndexpoi
ntsdi
rect
lyt
othel
ocat
ionoft
he
r
owscont
aini
ngt
hev
alue.

WHYI
NDEXES?

I
ndexesar
emostusef
ulonl
argert
abl
es,oncol
umnst
hatar
eli
kel
ytoappeari
nwher
ecl
ausesassi
mpl
e
equal
it
y.

TYPES

 Uni
quei
ndex
 Non-
uni
quei
ndex
 Bt
reei
ndex
 Bi
tmapi
ndex
 Composi
tei
ndex
 Rev
ersekeyi
ndex
 Funct
ion-
basedi
ndex
 Descendi
ngi
ndex
 Domai
nindex
 Obj
ecti
ndex
 Cl
ust
eri
ndex
 Texti
ndex
 I
ndexor
gani
zedt
abl
e
 Par
ti
ti
oni
ndex
 Locali
ndex
 Localpr
efi
xed
 Localnon-
pref
ixed
 Gl
obal
index
 Gl
obalpr
efi
xed
 Gl
obalnon-
pref
ixed

UNI
QUEI
NDEX

Uni
quei
ndexesguar
ant
eet
hatnot
wor
owsofat
abl
ehav
edupl
icat
eval
uesi
nthecol
umnst
hatdef
inet
hei
ndex.

.
122

Uni
quei
ndexi
saut
omat
ical
lycr
eat
edwhenpr
imar
ykeyoruni
queconst
rai
nti
scr
eat
ed.

Ex:
SQL>cr
eat
euni
quei
ndexst
ud_
indonst
udent
(sno)
;

NON-
UNI
QUEI
NDEX

Non-
Uni
quei
ndexesdonoti
mposet
heabov
erest
ri
cti
onont
hecol
umnv
alues.

Ex:
SQL>cr
eat
eindexst
ud_
indonst
udent
(sno)
;

BTREEI
NDEXorASCENDI
NGI
NDEX

Thedef
aul
tty
peofi
ndexusedi
nanor
acl
edat
abasei
sthebt
reei
ndex.Abt
reei
ndexi
sdesi
gnedt
opr
ovi
debot
h
r
api
daccesst
oindi
vi
dualr
owsandqui
ckaccesst
ogr
oupsofr
owswi
thi
nar
ange.Thebt
reei
ndexdoest
hisby
per
for
mingasuccessi
onofv
aluecompar
isons.Eachcompar
isonel
imi
nat
esmanyoft
her
ows.

Ex:
SQL>cr
eat
eindexst
ud_
indonst
udent
(sno)
;

BI
TMAPI
NDEX

Thi
scanbeusedf
orl
ow car
dinal
it
ycol
umns:t
hati
scol
umnsi
nwhi
cht
henumberofdi
sti
nctv
aluesi
ssnal
l
whencompar
edt
othenumberoft
her
owsi
nthet
abl
e.

Ex:
SQL>cr
eat
ebi
tmapi
ndexst
ud_
indonst
udent
(sex)
;

COMPOSI
TEI
NDEX

Acomposi
tei
ndexal
socal
ledaconcat
enat
edi
ndexi
sani
ndexcr
eat
edonmul
ti
plecol
umnsofat
abl
e.Col
umns
i
nacomposi
tei
ndexcanappeari
nanyor
derandneednotbeadj
acentcol
umnsoft
het
abl
e.

Ex:
SQL>cr
eat
ebi
tmapi
ndexst
ud_
indonst
udent
(sno,
sname)
;

.
123

REVERSEKEYI
NDEX

Ar
ever
sekeyi
ndexwhencompar
edt
ost
andar
dindex,r
ever
seseachby
teoft
hecol
umnbei
ngi
ndexedwhi
le
keepi
ngt
hecol
umnor
der
.Whent
hecol
umni
sindexedi
nrev
ersemodet
hent
hecol
umnv
alueswi
llbest
oredi
n
ani
ndexi
ndi
ff
erentbl
ocksast
hest
art
ingv
aluedi
ff
ers.Suchanar
rangementcanhel
pav
oidper
for
mance
degr
adat
ionsi
nindexeswher
emodi
fi
cat
ionst
othei
ndexar
econcent
rat
edonasmal
lsetofbl
ocks.

Ex:
SQL>cr
eat
eindexst
ud_
indonst
udent
(sno,
rev
erse)
;

Wecanr
ebui
ldar
ever
sekeyi
ndexi
ntonor
mali
ndexusi
ngt
henor
ever
sekey
wor
d.

Ex:
SQL>al
teri
ndexst
ud_
indr
ebui
ldnor
ever
se;

FUNCTI
ONBASEDI
NDEX

Thi
swi
lluser
esul
toft
hef
unct
ionaskeyi
nst
eadofusi
ngcol
umnast
hev
aluef
ort
hekey
.

Ex:
SQL>cr
eat
eindexst
ud_
indonst
udent
(upper
(sname)
);

DESCENDI
NGI
NDEX

Theor
derused byB-
tr
eei
ndexeshasbeen ascendi
ng or
der
.You can cat
egor
izedat
ain B-
tr
eei
ndexi
n
descendi
ngor
deraswel
l.Thi
sfeat
urecanbeusef
uli
nappl
icat
ionswher
esor
ti
ngoper
ati
onsar
erequi
red.

Ex:
SQL>cr
eat
eindexst
ud_
indonst
udent
(snodesc)
;

TEXTI
NDEX

Quer
yingt
exti
sdi
ff
erentf
rom quer
yingdat
abecausewor
dshav
eshadesofmeani
ng,r
elat
ionshi
pst
oot
her

.
124

wor
ds,andopposi
tes.Youmaywantt
osear
chf
orwor
dst
hatar
eneareachot
her
,orwor
dst
hatar
erel
atedt
o
t
her
s.Thesequer
ieswoul
dbeext
remel
ydi
ff
icul
tifal
lyouhadav
ail
abl
ewast
hest
andar
drel
ati
onaloper
ator
s.
Byext
endi
ngSQLt
oincl
udet
exti
ndexes,
oracl
etextper
mit
syout
oaskv
erycompl
exquest
ionsaboutt
het
ext
.

Touseor
acl
etext
,youneedt
ocr eat
eat exti
ndexont
hecol
umni
nwhi
cht
het
exti
sst
ored.Texti
ndexi
sa
col
lect
ionoft
abl
esandi
ndexest
hatst
orei
nfor
mat
ionaboutt
het
extst
oredi
nthecol
umn.

TYPES

Ther
ear
esev
eraldi
ff
erentt
ypesofi
ndexesav
ail
abl
einor
acl
e9i
.Thef
ir
st,CONTEXTi
ssuppor
tedi
nor
acl
e8ias
wel
lasor
acl
e9i
.Asofor
acl
e9i
,youcanuset
he CTXCAT t
exti
ndexf
ofur
therenhancey
ourt
exti
ndex
managementandquer
ycapabi
li
ti
es.

 CONTEXT
 CTXCAT
 CTXRULE

TheCTXCAT i
ndext
ypesuppor
tst
het
ransact
ionalsy
nchr
oni
zat
ionofdat
abet
weent
hebaset
abl
eandi
tst
ext
i
ndex.Wi
thCONTEXT i
ndexes,y
ouneedt
omanual
lyt
ellor
acl
etoupdat
ethev
aluesi
nthet
exti
ndexaf
terdat
a
changesi
nbaset
abl
e.CTXCATi
ndext
ypesdonotgener
atescor
eval
uesdur
ingt
het
extquer
ies.

HOW TOCREATETEXTI
NDEX?

Youcancr
eat
eat
exti
ndexv
iaaspeci
alv
ersi
onoft
hecr
eat
eindexcomman.Forcont
exti
ndex,speci
fyt
he
ct
xsy
s.cont
exti
ndext
ypeandf
orct
xcati
ndex,
speci
fyt
hect
xsy
s.ct
xcati
ndext
ype.

Ex:
Supposey
ouhav
eat
abl
ecal
ledBOOKSwi
tht
hef
oll
owi
ngcol
umns
Ti
tl
e,Aut
hor
,Inf
o.

SQL>cr
eat
eindexbook_
indexonbooks(
inf
o)i
ndext
ypei
sct
xsy
s.cont
ext
;
SQL>cr
eat
eindexbook_
indexonbooks(
inf
o)i
ndext
ypei
sct
xsy
s.ct
xcat
;

TEXTQUERI
ES

Onceat
exti
ndexi
scr
eat
edont
hei
nfocol
umnofBOOKSt
abl
e,t
ext
-sear
chi
ngcapabi
li
ti
esi
ncr
easedy
nami
cal
ly
.

CONTAI
NS&CATSEARCH

.
125

NSf
CONTAI unct
iont
akest
wopar
amet
ers–t
hecol
umnnameandt
hesear
chst
ri
ng.

Sy
ntax:
Cont
ai i
ns(ndexed_
col
umn,
sear
ch_
str
);

I
fyoucr
eat
eaCTXCATi
ndex,uset
heCATSEARCH f
unct
ioni
npl
aceofCONTAI
NS.CATSEARCH t
akest
hreepar
amet
ers
–t
hecol
umnname,
thesear
chst
ri
ngandt
hei
ndexset
.

Sy
ntax:
Cont
ai i
ns(ndexed_
col
umn,
sear
ch_
str
,index_
set
);

HOW ATEXTQEURYWORKS?

Whenaf
unct
ionsuchasCONTAI
NS orCATSEARCH i
susedi
nquer
y,t
het
extpor
ti
onoft
hequer
yispr
ocessedby
or
acl
etext
.Ther
emai
nderoft
hequer
yispr
ocessedj
ustl
ikear
egul
arquer
ywi
thi
nthedat
abase.Ther
esul
tof
t
het
extquer
ypr
ocessi
ngandt
her
egul
arquer
ypr
ocessi
ngar
emer
gedt
oret
urnasi
ngl
esetofr
ecor
dst
othe
user
.
SEARCHI
NGFORANEXACTMATCHOFAWORD

Thef
oll
owi
ngquer
ieswi
llsear
chf
orawor
dcal
led‘
prper
ty’
whosescor
eisgr
eat
ert
hanzer
o.

SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
proper
ty’
)>0;
SQL>sel
ect*f
rom bookswher
ecat
sear
ch(
inf
o,‘
proper
ty’
,nul
l)>0;

Supposei
fyouwantt
oknow t
hescor
eoft
he‘
proper
ty’i
neachbook,i
fscor
eval
uesf
ori
ndi
vi
dualsear
ches
r
angef
rom 0t
o10f
oreachoccur
renceoft
hest
ri
ngwi
thi
nthet
extt
henuset
hescor
efunct
ion.

SQL>sel
ectt
it
le,
scor
e(10)f
rom bookswher
econt
ains(
inf
o,‘
proper
ty’
,10)>0;

SEARCHI
NGFORANEXACTMATCHOFMULTI
PLEWORDS

Thef
oll
owi
ngquer
ieswi
llsear
chf
ort
wowor
ds.

SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
proper
tyANDhar
vest
s’)>0;
SQL>sel
ect*f
rom bookswher
ecat
sear
ch(
inf
o,‘
proper
tyANDhar
vest
s’,
nul
l)>0;

I
nst
eadofusi
ngAND y
oucoul
dhaeusedanamper
sand(
&).Bef
oreusi
ngt
hismet
hod,setdef
ineof
fsot
he&

.
126

char
act
erwi
llnotbeseenaspar
tofav
ari
abl
ename.

SQL>setdef
ineof
f
SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
proper
ty&har
vest
s’)>0;
SQL>sel
ect*f
rom bookswher
ecat
sear
ch(
inf
o,‘
proper
tyhar
vest
s’,
nul
l)>0;

Thef
oll
owi
ngquer
ieswi
llsear
chf
ormor
ethant
wowor
ds.

SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
proper
tyANDhar
vest
sANDwor
ker
s’)>0;
SQL>sel
ect*f
rom bookswher
ecat
sear
ch(
inf
o,‘
proper
tyhar
vest
swor
ker
s’,
nul
l)>0;

Thef
oll
owi
ngquer
ieswi
llsear
chf
orei
theroft
het
wowor
ds.

SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
proper
tyORhar
vest
s’)>0;

I
nst
eadofORy
oucanuseav
ert
icall
ine(
|)
.

SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
proper
ty|har
vest
s’)>0;
SQL>sel
ect*f
rom bookswher
ecat
sear
ch(
inf
o,‘
proper
ty|har
vest
s’,
nul
l)>0;

I
nthef
oll
owi
ngquer
iest
heACCUM(
accumul
ate)oper
atoraddst
oget
hert
hescor
esoft
hei
ndi
vi
dualsear
chesand
compar
est
heaccumul
atedscor
etot
het
hreshol
dval
ue.

SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
proper
tyACCUM har
vest
s’)>0;
SQL>sel
ect*f
rom bookswher
ecat
sear
ch(
inf
o,‘
proper
tyACCUM har
vest
s’,
nul
l)>0;

I
nst
eadofORy
oucanuseacomma(
,)
.

SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
proper
ty,
har
vest
s’)>0;
SQL>sel
ect*f
rom bookswher
ecat
sear
ch(
inf
o,‘
proper
ty,
har
vest
s’,
nul
l)>0;

I
nthef
oll
owi
ngquer
iest
heMI
NUSoper
atorsubt
ract
sthescor
eoft
hesecondt
erm’
ssear
chf
rom t
hescor
eoft
he
f
ir
stt
erm’
ssear
ch.

SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
proper
tyMI
NUShar
vest
s’)>0;
SQL>sel
ect*f
rom bookswher
ecat
sear
ch(
inf
o,‘
proper
tyNOThar
vest
s’,
nul
l)>0;

.
127

I
nst
eadofMI
NUSy
oucanuse–andi
nst
eadofNOTy
oucanuse~.

SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
proper
ty-har
vest
s’)>0;
SQL>sel
ect*f
rom bookswher
ecat
sear
ch(
inf
o,‘
proper
ty~har
vest
s’,
nul
l)>0;

SEARCHI
NGFORANEXACTMATCHOFAPHRASE

Thef
oll
owi
ngquer
ieswi
llsear
chf
ort
hephr
ase.I
fthesear
chphr
asei
ncl
udesar
eser
vedwor
dwi
thi
nor
acl
etext
,
t
hey
oumustusecur
lybr
aces(
{})t
oencl
oset
ext
.

SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
tr
ansact
ions{
and}f
inances’
)>0;
SQL>sel
ect*f
rom bookswher
ecat
sear
ch(
inf
o,‘
tr
ansact
ions{
and}f
inances’
,nul
l)>0;

Youcanencl
oset
heent
ir
ephr
asewi
thi
ncur
lybr
aces,
inwhi
chcaseanyr
eser
vedwor
dswi
thi
nthephr
asewi
llbe
t
reat
edaspar
toft
hesear
chcr
it
eri
a.

SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
{t
ransact
ionsandf
inances}
’)>0;
SQL>sel
ect*f
rom bookswher
ecat
sear
ch(
inf
o,‘
{t
ransact
ionsandf
inances}
’,nul
l)>0;

SEARCHI
NGFORWORDSTHATARENEAREACHOTHER

Thef
oll
owi
ngquer
ieswi
llsear
chf
ort
hewor
dst
hatar
einbet
weent
hesear
cht
erms.

SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
wor
ker
sNEARhar
vest
s’)>0;

I
nst
eadofNEARy
oucanuse;
.

SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
wor
ker
s;har
vest
s’)>0;

I
nCONTEXTi
ndexquer
ies,
youcanspeci
fyt
hemaxi
mum numberofwor
dsbet
weent
hesear
cht
erms.

SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
NEAR(
(wor
ker
s,har
vest
s),
10)
’>0;

USI
NGWI
LDCARDSDURI
NGSEARCHES

Youcanusewi
ldcar
dst
oexpandt
hel
istofv
ali
dsear
cht
ermsuseddur
ingy
ourquer
y.Justasi
nregul
art
ext
-
st
ri
ngwi
ldcar
dpr
ocessi
ng,
twowi
ldcar
dsar
eav
ail
abl
e.

.
128

% - per
centsi
gn;
mul
ti
ple-
char
act
erwi
ldcar
d
_ - under
scor
e;si
ngl
e-char
act
erwi
ldcar
d

SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
wor
ker
%’)>0;
SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
wor
k__
_’)>0;

SEARCHI
NGFORWORDSTHATSHARETHESAMESTEM

Rat
hert
hanusi
ngwi
ldcar
ds,
youcanusest
em-
expansi
oncapabi
li
ti
est
oexpandt
hel
istoft
extst
ri
ngs.Gi
vent
he

stem’ofawor
d,or
acl
ewi
llexpandt
hel
istofwor
dst
osear
chf
ort
oincl
udeal
lwor
dshav
ingt
hesamest
em.
Sampl
eexpansi
onsar
eshowher
e.
Pl
ay - pl
ayspl
ayi
ngpl
ayedpl
ayf
ul

SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
$manage’
)>0;

SEARCHI
NGFORFUZZYMATCHES

Af
uzzymat
chexpandst
hespeci
fi
edsear
cht
ermt
oincl
udewor
dst
hatar
espel
ledsi
mil
arl
ybutt
hatdonot
necessar
il
yhav
ethesamewor
dst
em.Fuzzymat
chesar
emosthel
pfulwhent
het
extcont
ainsmi
sspel
li
ngs.The
mi
sspel
li
ngscanbeei
theri
nthesear
chedt
extori
nthesear
chst
ri
ngspeci
fi
edbyt
heuserdur
ingt
hequer
y.

Thef
oll
owi
ngquer
ieswi
llnotr
etur
nany
thi
ngbecausei
tssear
chdoesnotcont
aint
hewor
d‘har
dest
’.

SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
har
dest
’)>0;

I
tdoes,howev
er,cont
ainst
hewor
d‘har
vest
’.Af
uzzymat
chwi
llr
etur
nthebookscont
aini
ngt
hewor
d‘har
vest

ev
ent
hough‘
har
vest
’hasadi
ff
erentwor
dst
em t
hantt
hewor
dusedast
hesear
cht
erm.

Touseaf
uzzymat
ch,pr
ecedet
hesear
cht
erm wi
thaquest
ionmar
k,wi
thnospacebet
weent
hequest
ionmar
k
andt
hebegi
nni
ngoft
hesear
cht
erm.

SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
?har
dest
’)>0;

SEARCHI
NGFORWORDSTHATSOUNDLI
KEOTHERWORDS

SOUNDEX,expandssear
cht
ermsbasedonhow t
hewor
dsounds.TheSOUNDEXexpansi
onmet
hodusest
he
samet
ext
-mat
chi
ngl
ogi
cav
ail
abl
evi
atheSOUNDEXf
unct
ioni
nSQL.

.
129

Touset
heSOUNDEXopt
ion,
youmustpr
ecedet
hesear
cht
erm wi
thanexcl
amat
ionmar
k(!
).

SQL>sel
ect*f
rom bookswher
econt
ains(
inf
o,‘
!
grat
e’)>0;

I
NDEXSYNCHRONI
ZATI
ON

Whenusi
ngCONTEXTi
ndexes,
youneedt
omanaget
het
exti
ndexcont
ent
s;t
het
exti
ndexesar
enotupdat
edwhen
t
hebaset
abl
eisupdat
ed.Whent
het
abl
ewasupdat
ed,
itst
exti
ndexi
soutofsy
ncwi
tht
hebaset
abl
e.Tosy
nc
oft
hei
ndex,
execut
etheSYNC_I
NDEXpr
ocedur
eoft
heCTX_DDLpackage.

SQL>execCTX_DDL.
SYNC_
INDEX(
‘book_
index’
);

I
NDEXSETS

Hi
stor
ical
ly
,pr
obl
emswi
thquer
iesoft
exti
ndexeshav
eoccur
redwhenot
hercr
it
eri
aar
eusedal
ongsi
det
ext
sear
chesaspar
toft
hewher
ecl
ause.Toi
mpr
ovet
hemi
xedquer
ycapabi
li
ty,or
acl
efeat
uresi
ndexset
s.The
i
ndexeswi
thi
nthei
ndexsetmaybest
ruct
uredr
elat
ionalcol
umnsoront
extcol
umns.

Tocr
eat
eani
ndexset
,uset
heCTX_DDLpackaget
ocr
eat
ethei
ndexsetandaddi
ndexest
oit
.Wheny
oucr
eat
ea
t
exti
ndex,
youcant
henspeci
fyt
hei
ndexseti
tbel
ongst
o.

SQL>execCTX_DDL.
CREATE_
I SET(
NDEX_ ‘books_
index_
set
’)
;

Theaddnon-
texti
ndexes.

SQL>execCTX_DDL.
ADD_
INDEX(
‘books_
index_
set
’,‘
ti
tl
e_i
ndex’
);

Nowcr
eat
eaCTXCATt
exti
ndex.Speci
fyct
xsy
s.ct
xcatast
hei
ndext
ype,andl
istt
hei
ndexseti
nthepar
amet
ers
cl
ause.

SQL>cr
eat
eindexbook_
indexonbooks(
inf
o)i
ndext
ypei
sct
xsy
s.ct
xcat
par
amet
ers(
‘i
ndexsetbooks_
index_
set
’)
;

I
NDEX-
ORGANI
ZEDTABLE

Ani
ndex-
organi
zedt
abl
ekeepsi
tsdat
asor
tedaccor
dingt
othepr
imar
ykeycol
umnv
aluesf
ort
het
abl
e.I
ndex-
or
gani
zedt
abl
esst
oret
hei
rdat
aasi
ftheent
ir
etabl
ewasst
oredi
nani
ndex.

.
130

Ani
ndex-
organi
zedt
abl
eal
lowsy
out
ost
oret
heent
ir
etabl
e’sdat
ainani
ndex.
Ex:
SQL>cr
eat
etabl
est
udent(
snonumber
(2)
,snamev
archar
(10)
,smar
ksnumber
(3)
const
rai
ntpkpr
imar
ykey
(sno)or
gani
zat
ioni
ndex;

PARTI
TIONI
NDEX

Si
mil
art
opar
ti
ti
oni
ngt
abl
es,or
acl
eal
lowsy
out
opar
ti
ti
oni
ndexest
oo.Li
ket
abl
epar
ti
ti
ons,i
ndexpar
ti
ti
ons
coul
dbei
ndi
ff
erentt
abl
espaces.

LOCALI
NDEXES

 Localkey
wor
dtel
lsor
acl
etocr
eat
easepar
tei
ndexf
oreachpar
ti
ti
on.
 I
nthel
ocalpr
efi
xedi
ndext
hepar
ti
ti
onkeyi
sspeci
fi
edont
hel
eftpr
efi
x.Whent
heunder
lyi
ngt
abl
eis
par
ti
ti
onedbaeson,
sayt
wocol
umnst
hent
hei
ndexcanbepr
efi
xedont
hef
ir
stcol
umnspeci
fi
ed.
 Localpr
efi
xedi
ndexescanbeuni
queornonuni
que.
 Locali
ndexesmaybeeasi
ert
omanaget
hangl
obali
ndexes.

Ex:
SQL>cr
eat
eindexst
ud_
indexonst
udent
(sno)l
ocal
;

GLOBALI
NDEXES

 Agl
obali
ndexmaycont
ainv
aluesf
rom mul
ti
plepar
ti
ti
ons.
 Ani
ndexi
sgl
obalpr
efi
xedi
fiti
spar
ti
ti
onedont
hel
eftpr
efi
xoft
hei
ndexcol
umns.
 Thegl
obalcl
auseal
lowsy
out
ocr
eat
eanon-
par
ti
ti
onedi
ndex.
 Gl
obali
ndexesmayper
for
m uni
quenesschecksf
ast
ert
hanl
ocal(
par
ti
ti
oned)i
ndexes.
 Youcannotcr
eat
egl
obali
ndexesf
orhashpar
ti
ti
onsorsubpar
ti
ti
ons.

Ex:
SQL>cr
eat
eindexst
ud_
indexonst
udent
(sno)gl
obal
;

Si
mil
art
otabl
epar
ti
ti
ons,i
tispossi
blet
omov
ethem f
rom onedev
icet
oanot
her
.Butunl
iket
abl
epar
ti
ti
ons,
mov
ementofi
ndexpar
ti
ti
onsr
equi
resi
ndi
vi
dualr
econst
ruct
ionoft
hei
ndexoreachpar
ti
ti
on(
onl
yint
hecaseof
gl
obali
ndex)
.

Ex:

.
131

SQL>al
teri
ndexst
ud_
indr
ebui
ldpar
ti
ti
onp2

 I
ndexpar
ti
ti
onscannotbedr
oppedmanual
ly
.
 Theyar
edr
oppedi
mpl
ici
tl
ywhent
hedat
atheyr
efert
oisdr
oppedf
rom t
hepar
ti
ti
onedt
abl
e.

MONI
TORI
NGUSEOFI
NDEXES

Oncey
out
urnedont
hemoni
tor
ingt
heuseofi
ndexes,
thenwecancheckwhet
hert
het
abl
eishi
tt
ingt
hei
ndexor
not
.

Tomoni
tort
heuseofi
ndexuset
hef
oll
wingsy
ntax.

Sy
ntax:
al
t ndexi
eri ndex_
namemoni
tor
ingusage;

t
hencheckf
ort
hedet
ail
sinV$OBJECT_
USAGEv
iew.

I
fyouwantt
ost
opmoni
tor
inguset
hef
oll
owi
ng.

Sy
ntax:
al
t ndexi
eri ndex_
namenomoni
tor
ingusage;

DATAMODEL

 ALL_
INDEXES
 DBA_
INDEXES
 USER_
INDEXES
 ALL_
IND-
COLUMNS
 DBA-
IND_
COLUMNS
 USER_
IND_
COLUMNS
 ALL_
PART_
INDEXES
 DBA_
PART_
INDEXES
 USER_
PART_
INDEXES
 V$OBJECT_
USAGE

.
132

SQL*
PLUSCOMMNANDS

Thesecommandsdoesnotr
equi
rest
atementt
ermi
nat
orandappl
icabl
etot
hesessi
ons,
thosewi
llbe
aut
omat
ical
lycl
ear
edwhensessi
onwascl
osed.

BREAK

Thi
swi
llbeusedt
obr
eakupt
hedat
adependi
ngont
hegr
oupi
ng.

.
133

Sy
ntax:
Br
eakorbr
e[ umn_
on<col name>onr
epor
t]

COMPUTE

Thi
swi
llbeusedt
oper
for
m gr
oupf
unct
ionsont
hedat
a.

Sy
ntax:
Comput
eorcomp[
group_
funct
ionofcol
umn_
nameonbr
eaki
ng_
col
umn_
nameor
r
epor
t]

TTI
TLE

Thi
swi
llgi
vet
het
opt
it
lef
ory
ourr
epor
t.Youcanonorof
fthet
ti
tl
e.

Sy
ntax:
Tt
it
leort
ti
t[l
eft|cent
er|r
i ]t
ght i
tl
e_name ski
pnot
her
_char
act
ers
Tt
it
leort
ti
t[onorof
f]

BTI
TLE

Thi
swi
llgi
vet
hebot
tom t
it
lef
ory
ourr
epor
t.Youcanonorof
fthebt
it
le.

Sy
ntax:
Bt
it
leorbt
it[
lef
t|cent
er|r
i ]t
ght i
tl
e_name ski
pnot
her
_char
act
ers
Bt
it
leorbt
it[
onorof
f]
Ex:
SQL>br
eondept
noski
p1onr
epor
t
SQL>compsum ofsalondept
no
SQL>compsum ofsalonr
epor
t
SQL>t
ti
tl
ecent
er'
EMPLOYEEDETAI
LS'
ski
p1cent
er'
-
---
--
--
--
--
--
--
'
SQL>bt
it
lecent
er'
**THANKQ*
*'
SQL>sel
ect*f
rom empor
derbydept
no;

Out
put
:

.
134

EMPLOYEEDETAI
LS
-
--
--
--
--
--
--
--
--
--
--
--

EMPNO ENAME JOB MGR HI


REDATE SAL COMM DEPTNO
-
--
--
--
--
---
--
--
--
---
--
--
--
-- -
--
--
---
--
--
--
--
--
--
---
--
--
---
--
--
--
--
---
--
--
--
--
7782 CLARK MANAGER 7839 09-
JUN-
81 2450 10
7839 KI
NG PRESI
DENT 17-
NOV-
81 5000
7934 MI
LLERCLERK 7782 23-
JAN-
82 1300
-
--
--
--
--
- *
***
***
***
8750 sum

7369 SMI
TH CLERK 7902 17-
DEC-
80 800 20
7876 ADAMSCLERK 7788 23-
MAY-
87 1100
7902 FORD ANALYST 7566 03-
DEC-
81 3000
7788 SCOTT ANALYST 7566 19-
APR-
87 3000
7566 JONESMANAGER 7839 02-
APR-
81 2975
-
--
--
--
--
- *
***
***
***
10875 sum

7499 ALLEN SALESMAN 7698 20-


FEB-
81 1600 300 30
7698 BLAKE MANAGER 7839 01-
MAY-
81 2850
7654 MARTI
NSALESMAN 7698 28-
SEP-
81 1250 1400
7900 JAMES CLERK 7698 03-
DEC-
81 950
7844 TURNERSALESMAN 7698 08-
SEP-
81 1500 0
7521 WARD SALESMAN 7698 22-
FEB-
81 1250 500
-
--
--
--
--
- *
***
***
***
9400 sum
-
--
--
--
--
-
sum 29025

*
*THANKQ*
*

CLEAR

Thi
swi
llcl
eart
heexi
sti
ngbuf
fer
sorbr
eakorcomput
ati
onsorcol
umnsf
ormat
ti
ng.

Sy
ntax:

.
135

Cl
earorcl
ebuf
fer|br
e|comp|col
;

Ex:
SQL>cl
earbuf
fer
Buf
fercl
ear
ed
SQL>cl
earbr
e
Br
eakscl
ear
ed
SQL>cl
earcomp
Comput
escl
ear
ed
SQL>cl
earcol
Col
umnscl
ear
ed

CHANGE

Thi
swi
llbeusedt
orepl
aceanyst
ri
ngsi
nSQLst
atement
s.

Sy
ntax:
ol
Changeorc/ d_
str
ing/
new_
str
ing

I
ftheol
d_st
ri
ngr
epeat
smanyt
i hennew_
mest str
ingr
epl
acest
hef
ir
stst
ri
ngonl
y.

Ex:
SQL>sel
ect*f
rom det
;
sel
ect*f
rom det
*
ERRORatl
ine1:
ORA-
00942:t
abl
eorv
iewdoesnotexi
st

SQL>c/
det
/dept
1*sel
ect*f
rom dept
SQL>/

DEPTNO DNAME LOC


-
--
--
--
--
---
--
--
--
--
--
--
---
--
--
--
--
--
10 ACCOUNTI
NG NEW YORK
20 RESEARCH ALLAS
30 SALES CHI
CAGO

.
136

40 OPERATI
ONS BOSTON

COLUMN

Thi
swi
llbeusedt
oincr
easeordecr
easet
hewi
dthoft
het
abl
ecol
umns.

Sy
ntax:
Col <col
umnorcol umn_
name>f
ormat<num_
for
mat
|t
ext
_for
mat
>

Ex:
SQL>coldept
nof
ormat999
SQL>coldnamef
ormata10

SAVE

Thi
swi
llbeusedt
osav
eyourcur
rentSQLst
atementasSQLScr
iptf
il
e.

Sy
ntax:
eorsav<f
Sav il
e_name>.
[ext
ensi
on]r
epl
aceorr
ep

I
fyouwantt
osav
ethef
il
enamewi
thexi
sti
ngf
il
enamet
hey
ouhav
etouser
epl
aceopt
ion.
Bydef
aul
titwi
l akesql
lt ast
heext
ensi
on.

Ex:
SQL>sav
ess
Cr
eat
edf
il
ess.
sql
SQL>sav
essr
epl
ace
Wr
otef
il
ess.
sql

EXECUTE

Thi
swi
llbeusedt
oexecut
est
oredsubpr
ogr
amsorpackagedsubpr
ogr
ams.

Sy
ntax:
eorexec<subpr
Execut ogr
am_
name>

Ex:

.
137

SQL>execsampl
e_pr
oc

SPOOL

Thi
swi
llr
ecor
dthedat
awheny
ouspool
on,
upt
owheny
ousayspoolof
f.Bydef
aul
titwi
llgi
vel
stasext
ensi
on.

Sy
ntax:
f|out|<f
Spoolon|of il
e_name>.
[Ext
ensi
on]

Ex:
SQL>spool
on
SQL>sel
ect*f
rom dept
;

DEPTNODNAME LOC
-
--
--
--
---
--
--
--
--
--
--
---
--
--
--
--
10 ACCOUNTI
NG NEW YORK
20 RESEARCH DALLAS
30 SALES CHI
CAGO
40 OPERATI
ONS BOSTON

SQL>spool
off
SQL>edon.
lst

SQL>sel
ect*f
rom dept
;

DEPTNODNAME LOC
-
--
--
--
---
--
--
--
--
--
--
---
--
--
--
--
10 ACCOUNTI
NG NEW YORK
20 RESEARCH DALLAS
30 SALES CHI
CAGO
40 OPERATI
ONS BOSTON

SQL>spool
off

LI
ST

Thi
swi
llgi
vet
hecur
rentSQLst
atement
.

.
138

Sy
ntax:
Li
storl
i[st
art
_li
ne_
number
][end_
li
ne_
number
]

Ex:
SQL>sel
ect
2*
3f
rom
4dept
;
SQL>l
ist
1sel
ect
2*
3f
rom
4*dept
SQL>l
ist1
1*sel
ect
SQL>l
ist3
3*f
rom

SQL>l
ist13
1sel
ect
2*
3*f
rom

I
NPUT

Thi
swi
lli
nser
tthenewl
inet
othecur
rentSQLst
atement
.

Sy
ntax:
I n<st
nputori ri
ng>

Ex:
SQL>sel
ect*
SQL>l
ist
1*sel
ect*
SQL>i
nputf
rom dept
SQL>l
ist

.
139

1sel
ect*
2*f
rom dept

APPEND

Thi
swi
lladdsanewst
ri
ngt
otheexi
sti
ngst
ri
ngi
ntheSQLst
atementwi
thoutanyspace.

Sy
ntax:
Appendorapp<st
ri
ng>

Ex:
SQL>sel
ect*
SQL>l
ist
1*sel
ect*
SQL>appendf
rom dept
1*sel
ect*f
rom dept
SQL>l
ist
1*sel
ect*f
rom dept

DELETE

Thi
swi
lldel
etet
hecur
rentSQLst
atementl
ines.

Sy
ntax:
Del
eteordel<st
art
_li
ne_
number
>[<end_
li
ne_
number
>]

Ex:
SQL>sel
ect
2*
3f
rom
4dept
5wher
e
6dept
no
7>10;
SQL>l
ist
1sel
ect
2*

.
140

3f
rom
4dept
5wher
e
6dept
no
7*>10
SQL>del1
SQL>l
ist
1*
2f
rom
3dept
4wher
e
5dept
no
6*>10
SQL>del2
SQL>l
ist
1*
2dept
3wher
e
4dept
no
5*>10
SQL>del24
SQL>l
ist
1*
2*>10
SQL>del
SQL>l
ist
1*

VARI
ABLE

Thi
swi
llbeusedt
odecl
areav
ari
abl
e.

Sy
ntax:
Var
iabl ar<v
eorv ari
abl
e_name><var
iabl
e_t
ype>

Ex:
SQL>v
ardept
_namev
archar
(15)

.
141

SQL>sel
ectdnamei
ntodept
_namef
rom deptwher
edept
no=10;

PRI
NT

Thi
swi
llbeusedt
opr
intt
heout
putoft
hev
ari
abl
est
hatwi
llbedecl
aredatSQLl
evel
.

Sy
ntax:
Pr
int<v
ari
abl
e_name>

Ex:
SQL>pr
intdept
_name

DEPT_
NAME
-
--
--
--
--
--
--
-
ACCOUNTI
NG

START

Thi
swi
llbeusedt
oexecut
eSQLscr
ipt
s.

Sy
ntax:
st
art<f
iename_
l name>.
sql

Ex:
SQL>st
artss.
sql
SQL>@ss.
sql -
-thi
swi
llexecut
esqlscr
iptf
il
esonl
y.

HOST

Thi
swi
llbeusedt
oint
eractwi
tht
heOSl
evelf
rom SQL.

Sy
ntax:
oper
Host[ ati
on]

Ex:
SQL>host
SQL>hostdi
r

.
142

SHOW

Usi
ngt
his,
youcanseesev
eral
commandst
hatuset
hesetcommandandst
atus.

Sy
ntax:
l|<set
Showal _command>

Ex:
SQL>showal
l
appi
nfoi
sOFFandsett
o"SQL*
Plus"
ar
ray
size15
aut
ocommi
tOFF
aut
opr
intOFF
aut
orecov
eryOFF
aut
otr
aceOFF
bl
ockt
ermi
nat
or"
."(
hex2e)
bt
it
leOFFandi
sthef
ir
stf
ewchar
act
ersoft
henextSELECTst
atement
cmdsepOFF
col
sep""
compat
ibi
li
tyv
ersi
onNATI
VE
concat"
."(
hex2e)
copy
commi
t0
COPYTYPECHECKi
sON
def
ine"
&"(
hex26)
descr
ibeDEPTH1LI
NENUM OFFI
NDENTON
echoOFF
edi
tf
il
e"af
iedt
.buf
"
embeddedOFF
escapeOFF
FEEDBACKONf
or6ormor
erows
f
laggerOFF
f
lushON

SQL>shov
eri
fy
v
eri
fyOFF

.
143

RUN

Thi
swi
llr
unst
hecommandi
nthebuf
fer
.

Sy
ntax:
Run|/

Ex:
SQL>r
un
SQL>/

STORE

Thi
swi
llsav
eal
lthesetcommandst
atusesi
naf
il
e.

Sy
ntax:
St
oreset<f
il
ename>.
[ext
ensi
on][
creat
e]|[
repl
ace]|[
append]

Ex:
SQL>st
oresetmy
_set
ti
ngs.
scmd
Cr
eat
edf
il
emy
_set
ti
ngs.
scmd
SQL>st
oresetmy
_set
ti
ngs.
cmdr
epl
ace
Wr
otef
il
emy
_set
ti
ngs.
cmd
SQL>st
oresetmy
_set
ti
ngs.
cmdappend
Appendedf
il
etomy
_set
ti
ngs.
cmd

FOLD_
AFTER

Thi
swi
llf
oldt
hecol
umnsoneaf
tert
heot
her
.

Sy
ntax:
umn<col
Col umn_
name>f
old_
aft no_
er[ of_
li
nes]

Ex:
SQL>coldept
nof
old_
aft
er1
SQL>coldnamef
old_
aft
er1
SQL>coll
ocf
old_
aft
er1
SQL>setheadi
ngof
f

.
144

SQL>sel
ect*f
rom dept
;

10
ACCOUNTI
NG
NEW YORK

20
RESEARCH
DALLAS
30
SALES
CHI
CAGO

40
OPERATI
ONS
BOSTON

FOLD_
BEFORE

Thi
swi
llf
oldt
hecol
umnsonebef
oret
heot
her
.

Sy
ntax:
umn<col
Col umn_
name>f
old_
bef
or no_
e[ of_
li
nes]

DEFI
NE

Thi
swi
llgi
vet
hel
istofal
lthev
ari
abl
escur
rent
lydef
ined.

Sy
ntax:
Def
ine[
var
iabl
e_name]

Ex:
SQL>def
ine
DEFI
NE_
DATE ="
16-
MAY-
07"(
CHAR)
DEFI
NE_
CONNECT_
IDENTI
FIER="
oracl
e"(
CHAR)
DEFI
NE_
USER ="
SCOTT"(
CHAR)
DEFI
NE_
PRI
VILEGE ="
"(CHAR)

.
145

DEFI
NE_
SQLPLUS_
RELEASE="
1001000200"(
CHAR)
DEFI
NE_
EDI
TOR ="
Not
epad"(
CHAR)
DEFI
NE_
O_VERSI
ON ="
Oracl
eDat
abase10gEnt
erpr
iseEdi
ti
onRel
ease
10.
1.0.
2.0–Pr
oduct
ionWi
tht
hePar
ti
ti
oni
ng,
OLAPand
Dat
aMi
ningopt
ions"(
CHAR)
DEFI
NE_
O_RELEASE ="
1001000200"(
CHAR)

SETCOMMANDS

Thesecommandsdoesnotr
equi
rest
atementt
ermi
nat
orandappl
icabl
etot
hesessi
ons,
thosewi
llbe
aut
omat
ical
lycl
ear
edwhensessi
onwascl
osed.

LI
NESI
ZE

Thi
swi
llbeusedt
osett
hel
inesi
ze.Def
aul
tli
nesi
zei
s80.

Sy
ntax:
Setl
i ze<v
nesi alue>

Ex:
SQL>setl
inesi
ze100

PAGESI
ZE

Thi
swi
llbeusedt
osett
hepagesi
ze.Def
aul
tpagesi
zei
s14.

Sy
ntax:
ze<v
Setpagesi alue>

Ex:
SQL>setpagesi
ze30

DESCRI
BE

Thi
swi
llbeusedt
oseet
heobj
ect
’sst
ruct
ure.

.
146

Sy
ntax:
Descr
ibeordesc<obj
ect
_name>

Ex:
SQL>descdept

Name Nul
l? Ty
pe
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
DEPTNO NOTNULLNUMBER(
2)
DNAME VARCHAR2(
14)
LOC VARCHAR2(
13)

PAUSE

Whent
hedi
spl
ayeddat
acont
ainshundr
edsort
housandsofl
ines,wheny
ousel
ecti
ttheni
twi
llaut
omat
ical
ly
scr
oll
sanddi
spl
ayst
hel
astpagedat
a.Topr
eventt
hisy
oucanuset
hispauseopt
ion.Byusi
ngt
hisi
twi
lldi
spl
ay
t
hedat
acor
respoi
ndi
ngt
othepagesi
zewi
thabr
eakwhi
chwi
llcont
inuebyhi
tt
ingt
her
etur
nkey
.Bydef
aul
tthi
s
wi
llbeof
f.

Sy
ntax:
Setpauseon|of
f

Ex:
SQL>setpauseon

FEEDBACK

Thi
swi
llgi
vet
hei
nfor
mat
ionr
egar
ding howmanyr
owsy
ousel
ect
ed t
heobj
ect
.Bydef
aul
tthef
eedback
messagewi
llbedi
spl
ayed,
onl
ywhent
heobj
ectcont
ainsmor
ethan5r
ows.

Sy
ntax:
eedback<v
Setf alue>

Ex:
SQL>setf
eedback4
SQL>sel
ect*f
rom dept
;

.
147

DEPTNO DNAME LOC


-
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
--
--
10 ACCOUNTI
NG NEW YORK
20 RESEARCH DALLAS
30 SALES CHI
CAGO
40 OPERATI
ONS BOSTON

4r
owssel
ect
ed.

HEADI
NG

I
fyouwantt
odi
spl
aydat
awi
thoutheadi
ngs,
theny
oucanachi
evewi
tht
his.Bydef
aul
theadi
ngi
son.

Sy
ntax:
Setheadi
ngon|of
f

Ex:
SQL>setheadi
ngof
f
SQL>sel
ect*f
rom dept
;

10 ACCOUNTI
NG NEW YORK
20 RESEARCH DALLAS
30 SALES CHI
CAGO
40 OPERATI
ONS BOSTON

SERVEROUTPUT

Thi
swi
llbeusedt
odi
spl
ayt
heout
putoft
hePL/
SQLpr
ogr
ams.Bydef
aul
tthi
swi
llbeof
f.

Sy
ntax:
Setser
ver
out
puton|of
f

Ex:
SQL>setser
ver
out
puton

.
148

TI
ME

Thi
swi
llbeusedt
odi
spl
ayt
het
ime.Bydef
aul
tthi
swi
llbeof
f.

Sy
ntax:
Sett
imeon|of
f

Ex:
SQL>sett
imeon
19:
56:
33SQL>

TI
MING

Thi
swi
llgi
vet
het
imet
akent
oexecut
ethecur
rentSQLst
atement
.Bydef
aul
tthi
swi
llbeof
f.

Sy
ntax:
Sett
imi
ngon|of
f

Ex:
SQL>sett
imi
ngon
SQL>sel
ect*f
rom dept
;

DEPTNO DNAME LOC


-
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
--
--
10 ACCOUNTI
NG NEW YORK
20 RESEARCH DALLAS
30 SALES CHI
CAGO
40 OPERATI
ONS BOSTON

El
apsed:00:
00:
00.
06

SQLPROMPT

Thi
swi
llbeusedt
ochanget
heSQLpr
ompt
.
Sy
ntax:
Setsql
prompt<pr
ompt
>

.
149

Ex:
SQL>setsql
prompt'
ORACLE>'
ORACLE>

SQLCASE

Thi
swi
llbeusedt
ochanget
hecaseoft
heSQLst
atement
s.Bydef
aul
tthecasei
smi
xed.

Sy
ntax:
Setsql
caseupper|mi
xed|l
ower

Ex:
SQL>setsql
caseupper

SQLTERMI
NATOR

Thi
swi
llbeusedt
ochanget
het
ermi
nat
oroft
heSQLst
atement
s.Bydef
aul
tthet
ermi
nat
ori
s;.

Sy
ntax:
Setsql
ter
mi or<t
nat ermi
nat
ion_
char
act
er>

Ex:
SQL>setsql
ter
minat
or:
SQL>sel
ect*f
rom dept
:

DEFI
NE

Bydef
aul
tift
he&char
act
erf
indst
heni
twi
llt
reatasbi
ndv
ari
abl
eandaskf
ort
hei
nput
.Supposey
ourwantt
o
t
reati
tasanor
malchar
act
erwhi
lei
nser
ti
ngdat
a,t
heny
oucanpr
eventt
hisbyusi
ngt
hedef
ineopt
ion.Bydef
aul
t
t
hiswi
llbeon

Sy
ntax:
Setdef
ineon|of
f

Ex:
SQL>i
nser
tint
odeptv
alues(
50,
'
R&D'
,
'HYD'
);

.
150

Ent
erv
aluef
ord:
ol
d 1:i
nser
tint
odeptv
alues(
50,
'
R&D'
,
'HYD'
)
new 1:I
NSERTI
NTODEPTVALUES(
50,
'
R',
'
HYD'
)

SQL>setdef
ineof
f
SQL>i
nser
tint
odeptv
alues(
50,
'
R&D'
,
'HYD'
); -
-her
eitwon’
taskf
orv
alue

NEWPAGE

Thi
swi
llshowshowmanybl
ankl
ineswi
llbel
eftbef
oret
her
epor
t.Bydef
aul
titwi
lll
eav
eonebl
ankl
i
ne.

Sy
ntax:
Setnewpage<v
alue>

Ex:
SQL>setnewpage10

Thezer
oval
uef
ornewpagedoesnotpr
oducezer
obl
ankl
inesi
nst
eadi
tswi
tchest
oaspeci
alpr
oper
tywhi
ch
pr
oducesat
op-
of-
for
m char
act
er(
hex13)j
ustbef
oret
hedat
eoneachpage.Mostmoder
npr
int
ersr
espondt
o
t
hisbymov
ingi
mmedi
atel
ytot
het
opoft
henextpage,
wher
ethepr
it
ingoft
her
epor
twi
llbegi
n.

HEADSEP

Thi
sal
lowy
out
oindi
cat
ewher
eyouwantt
obr
eakapaget
it
leoracol
umnheadi
ngt
hatr
unsl
ongert
hanonel
ine.
Thedef
aul
theadi
ngsepar
atori
sver
ti
calbar(
|)
.

Sy
ntax:
Setheadsep<separ
ati
on_
char
>
Ex:
SQL>sel
ect*f
rom dept
;

DEPTNO DNAME LOC


-
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
--
--
10 ACCOUNTI
NG NEW YORK
20 RESEARCH DALLAS
30 SALES CHI
CAGO
40 OPERATI
ONS BOSTON

.
151

SQL>setheadset
p!
SQL>coldnameheadi
ng'
DEPARTMENT!NAME'
SQL>/

DEPARTMENT
DEPTNO NAME LOC
-
--
--
--
--
---
--
--
--
--
--
--
--
---
--
--
--
--
10 ACCOUNTI
NG NEW YORK
20RESEARCH DALLAS
30SALES CHI
CAGO
40OPERATI
ONS BOSTON

ECHO

Whenusi
ngabi
ndv
ari
abl
e,t
heSQLst
atementi
smai
ntai
nedbyecho.Bydef
aul
tthi
sisof
f.

Sy
ntax:
Setechoon|of
f

VERI
FY

Whenusi
ngabi
ndv
ari
abl
e,t
heol
dandnewst
atement
swi
llbemai
ntai
nedbyv
eri
fy.Bydef
aul
tthi
sison.

Sy
ntax:
Setv
eri
fyon|of
f

Ex:
SQL>sel
ect*f
rom deptwher
edept
no=&dno;
Ent
erv
aluef
ordno:10
ol
d 1:sel
ect*f
rom deptwher
edept
no=&dno
new 1:sel
ect*f
rom deptwher
edept
no=10

DEPTNO DNAME LOC


-
--
--
--
--
---
--
--
--
--
--
--
---
--
--
--
--
--
10 ACCOUNTI
NG NEW YORK

.
152

SQL>setv
eri
fyof
f
SQL>sel
ect*f
rom deptwher
edept
no=&dno;
Ent
erv
aluef
ordno:20

DEPTNO DNAME LOC


-
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
-
20 RESEARCH DALLAS

PNO

Thi
swi
llgi
vedi
spl
ayst
hepagenumber
s.Bydef
aul
tthev
aluewoul
dbezer
o.

Ex:
SQL>colhi
redat
enew_
val
uext
odaynopr
intf
ormata1t
runc
SQL>t
ti
tl
elef
txt
odayr
ight'
page'
sql
.pno
SQL>sel
ect*f
rom empwher
edept
no=10;

09-
JUN-
81 page 1

EMPNO ENAME JOB MGR SALCOMM DEPTNO


-
--
--
--
--
---
--
--
--
---
--
--
--
--
--
--
---
--
--
--
---
--
---
--
--
--
--
---
--
--
--
--
7782CLARK MANAGER 7839 2450 10
7839KI
NG PRESI
DENT 5000 10
7934MI
LLER CLERK 7782 1300 10

I
ntheabov
enopr
intt
ell
sSQLPLUSnott
odi
spl
ayt
hiscol
umnwheni
tpr
int
sther
esul
tsoft
heSQLst
atement
.Dat
es
t
hathav
ebeenr
efor
mat
tedbyTO_CHARgetadef
aul
twi
dthofabout100char
act
ers.Bychangi
ngt
hef
ormatt
oa1
t
runc,y
oumi
nimi
zet
hisef
fect
.NEW_
VALUEi
nser
tscont
ent
soft
hecol
umnr
etr
iev
edbyt
heSQLst
atementi
nto
av
ari
abl
ecal
ledxt
oday
.

.
153

SPECI
ALFI
LES

LOGI
N.sql

I
fyouwoul
dli
keSQLPLUS t
odef
iney
ourownenv
ironment
alset
ti
ngs,putal
lther
equi
redcommandsi
naf
il
e
namedl
ogi
n.sql
.Thi
sisaspeci
alf
il
enamet
hatSQLPLUSal
way
slooksf
orwhenev
eri
tst
art
sup.I
fitf
indsl
ogi
n.sql
,
i
texecut
esanycommandsi
nitasi
fyouhadent
eredt
henbyhand.Youcanputanycommandi
nlogi
n.sqlt
hat
y
oucanusei
nSQLPLUS,i
ncl
udi
ngSQLPLUScommandsandSQLst
atement
s.Al
lott
hem execut
edbef
oreSQLPLUS
gi
vesy
out
heSQL>pr
ompt
.

GLOGI
N.sql

Thi
sisusedi
nthesameway
sasLOGI
N.sqlbutt
oest
abl
ishdef
aul
tSQLPLUSset
ti
ngsf
oral
luser
sofadat
abase.

.
154

I
MPORTANTQUERI
ES

1) Tof
indt
hent
hrowofat
abl
e

SQL>Sel
ect*
from empwher
erowi
d=(
sel
ectmax(
rowi
d)f
rom empwher
erownum
<=4)
;
Or
SQL>Sel
ect*
from empwher
erownum <=4mi
nussel
ect*
from empwher
erownum
<=3;

2) Tof
inddupl
icat
erows

SQL>Sel
ect*
from empwher
erowi
din(
sel
ectmax(
rowi
d)f
rom empgr
oupby
empno,
ename,
mgr
,job,
hir
edat
e,comm,
dept
no,
sal
);
Or
SQL>Sel
ectempno,
ename,
sal
,
job,
hir
edat
e,comm ,
count
(*)f
rom empgr
oupby
empno,
ename,
sal
,
job,
hir
edat
e,comm hav
ingcount
(*)>=1;

3) Todel
etedupl
icat
erows

.
155

SQL>Del
eteempwher
erowi
din(
sel
ectmax(
rowi
d)f
rom empgr
oupby
empno,
ename,
mgr
,j
ob,
hir
edat
e,sal
,
comm,
dept
no)
;

4) Tof
indt
hecountofdupl
icat
erows

SQL>Sel
ectename,
count
(*)f
rom empgr
oupbyenamehav
ingcount
(*)>=1;

5) Howt
odi
spl
ayal
ter
nat
iver
owsi
nat
abl
e?

SQL>sel
ect*
from empwher
e(r
owi
d,0)i
n(sel
ectr
owi
d,mod(
rownum,
2)f
rom emp)
;

6) Get
ti
ngempl
oyeedet
ail
sofeachdepar
tmentwhoi
sdr
awi
ngmaxi
mum sal
?

SQL>sel
ect*
from empwher
e(dept
no,
sal
)in
(sel
ectdept
no,
max(
sal
)fr
om empgr
oupbydept
no)
;
7) Howt
ogetnumberofempl
oyeesi
neachdepar
tment,
inwhi
chdepar
tmenti
shav
ingmor
ethan2500
empl
oyees?

SQL>Sel
ectdept
no,
count
(*)f
rom empgr
oupbydept
nohav
ingcount
(*)>2500;

8) Tor
esett
het
imet
othebegi
nni
ngoft
heday

SQL>Sel
ectt
o_char
(tr
unc(
sysdat
e),

dd-
mon-
yyy
yhh:
mi:
ssam’
)fr
om dual
;

9) Tof
indnt
hmaxi
mum sal

SQL>Sel
ect*
from empwher
esali
n(sel
ectmax(
sal
)fr
om (
sel
ect*
from empor
der
bysal
)wher
erownum <=5)
;

.
156

I
NTRODUCTI
ON

CHARACTERSTI
CS

 Hi
ghl
yst
ruct
ured,
readabl
eandaccessi
blel
anguage.
 St
andar
dandPr
otabl
elanguage.
 Embeddedl
anguage.
 I
mpr
ovedexecut
ionaut
hor
it
y.

10gFEATURES

 Opt
imi
zedcompi
ler
.
To change t
he opt
imi
zer set
ti
ngs f
or t
he ent
ir
e dat
abase, set t
he dat
abase par
amet
er
PLSQL_
OPTI
MI LEVEL.
ZE_ Val
idset
ti
ngsar
easf
oll
ows
0 - Noopt
imi
zat
ion
1 - Moder
ateopt
imi
zat
ion
2 - Aggr
essi
veopt
imi
zat
ion

Theseset
ti
ngsar
eal
somodi
fi
abl
efort
hecur
rentsessi
on.
SQL>al
tersessi
onsetpl
sql
_opt
imze_
lev
el=2;

Or
acl
eret
ainsopt
imi
zerset
ti
ngsonamodul
e-by
-modul
ebasi
s.Wheny
our
ecompi
leapar
ti
cul
armodul
e

.
157

wi
thnondef
aul
tset
ti
ngs,
theset
ti
ngswi
llst
ickal
lowi
ngy
out
orecompi
lel
ateronusi
ngREUSESETTI
NGS.

SQL>Al
terpr
ocedur
epr
occompi
lepl
sql
_opt
imi
ze_
lev
el=1;
SQL>Al
terpr
ocedur
epr
occompi
ler
euseset
ti
ngs;

 Compi
le-
ti
mewar
nings.

St
art
ingwi
thor
acl
edat
abase10gr
elease1y
oucanenabl
eaddi
ti
onalcompi
le-
ti
mewar
ningst
ohel
p
makey
ourpr
ogr
amsmor
erobust
.Thecompi
lercandet
ectpot
ent
ialr
unt
imepr
obl
emswi
thy
ourcode,
suchasi
dent
if
yingl
inesofcodet
hatwi
llnev
erber
un.Thi
spr
ocess,
alsoknownasl
intchecki
ng.
Toenabl
ethesewar
ningsf
otheent
ir
edat
abase,sett
hedat
abasepar
amet
erPLSQL_WARNI
NGS.These
set
ti
ngsar
eal
somodi
fi
abl
efort
hecur
rentsessi
on.

SQL>al
tersessi
onsetpl
sql
_war
nings=‘
enabl
e:al
l’
;
Theabov
ecanbeachi
evedusi
ngt
hebui
lt
-i
npackageDBMS_WARNI
NG.

 Condi
ti
onalcompi
lat
ion.

Condi
ti
onalcompi
lat
ional
lowst
hecompi
lert
oal
low t
ocompi
lesel
ect
edpar
tsofapr
ogr
am basedon
condi
ti
onsy
oupr
ovi
dewi
tht
he$I
Fdi
rect
ive.

 Suppor
tfornon-
sequent
ialcol
lect
ionsi
nFORALL.
 I
mpr
oveddat
aty
pesuppor
t.

 Backt
raceanexcept
iont
oit
sli
nenumber
.

Whenhandl
inganer
ror
,howcany
ouf
indt
hel
inenumberonwhi
cht
heer
rorwasor
igi
nal
lyr
aised?

I
near
li
err
elease,
theonl
ywayt
odot
hiswasal
lowy
ouexcept
iont
ogounhandl
edandt
henv
iewt
hef
ull
er
rort
racest
ack.

Now y
oucancal
lDBMS_UTI
LITY.
FORMAT_ BACKTRACE f
ERROR_ unct
iont
oobt
aint
hatst
ackandmani
pul
atei
t
pr
ogr
ammat
ical
lywi
thi
nyourpr
ogr
am.

 Setoper
ator
sfornest
edt
abl
es.

 Suppor
tforr
egul
arexpr
essi
ons.

.
158

Or
acl
edat
abase10gsuppor
tst
heuseofr
egul
arexpr
essi
onsi
nsi
dePL/SQL codev
iaf
ournew bui
lt
-i
n
f
unct
ions.

 REGEXP_
LIKE
 REGEXP_
INSTR
 REGEXP_
SUBSTR
 REGEXP_
REPLACE
 Pr
ogr
ammer
-def
inedquot
ingmechani
sm.

St
art
ingwi
thor
acl
edat
abase10gr
elease1,y
oucandef
iney
ourownquot
ingmechani
sm f
orst
ri
ng
l
it
eral
sinbot
hSQLandPL/SQL.

Uset
hechar
act
ersq’
(qf
oll
owedbyasi
ngl
equot
e)t
onot
ethepr
ogr
ammer
-def
ineddel
iemet
erf
ory
ou
st
ri
ngl
it
eral
.

Ex:
DECLARE
vv
archar
(10):
='comput
er'
;
BEGI
N
dbms_
out
put
.put
_li
ne(
q'*
v=*
'||v
);
dbms_
out
put
.put
_li
ne(
q'$v=$'
||v
);
END;

Out
put
:
v=comput
er
v=comput
er

 Manynewbui
lt
-i
npackages.

DBMS_
SCHEDULER
Repr
esent
samaj
orupdat
eto DBMS_JOB.DBMS_SCHEDULER pr
ovi
desmuch i
mpr
oved f
unct
ional
it
yfor
schedul
ingandexecut
ingj
obsdef
inedv
iast
oredpr
ocedur
es.

DBMS_
CRYPTO
Of
fer
stheabi
li
tyt
oencr
yptanddecr
yptcommonor
acl
edat
aty
pe,i
ncl
udi
ngRAWs,BLOBs,andCLOBs.I
t
al
sopr
ovi
desgl
obal
izat
ionsuppor
tforencr
ypt
ingdat
aacr
ossdi
ff
erentchar
act
erset
s.

.
159

DBMS_
MONI
TOR
Pr
ovi
desanAPIt
ocont
roladdi
ti
onalt
raci
ngandst
ati
sti
csgat
her
ingofsessi
ons.

DBMS_
WARNI
NG
Pr
ovi
desanAPIi
ntot
hePL/SQLcompi
lerwar
ningsmodul
e,al
lowi
ngy
out
oreadandchangeset
ti
ngst
hat
cont
rolwhi
chwar
ningsar
esuppr
essed,
displ
ayed,
ort
reat
edaser
ror
s.

STANDARDPACKAGE

Or
acl
ehasdef
inedi
nthi
sspeci
alpackage.Or
acl
edef
inesqui
teaf
ewi
dent
if
ier
sint
hispackage,i
ncl
udi
ngbui
lt
-
i
nexcept
ions,
funct
ionsandsubt
ypes.
Youcanr
efer
encet
hebui
lt
-i
nfor
m bypr
efi
xingi
twi
thSTANDARD.

Thebasi
cuni
tinanyPL/SQL pr
ogr
am i
sbl
ock.Al
lPL/SQL pr
ogr
amsar
ecomposedofbl
ockswhi
chcanoccur
sequent
ial
lyornest
ed.

BLOCKSTRUCTURE

Decl
are
-
-decl
arat
ivesect
ion
Begi
n
-
-execut
abl
esect
ion
Except
ion
-
-except
ionsect
ion
End;

I
ntheabov
edecl
arat
iveandexcept
ionasect
ionsar
eopt
ional
.

BLOCKTYPES

 Anony
mousbl
ocks
 Namedbl
ocks
 Label
edbl
ocks
 Subpr
ogr
ams
 Tr
igger
s

.
160

ANONYMOUSBLOCKS

Anony
mousbl
ocksi
mpl
iesbasi
cbl
ockst
ruct
ure.

Ex:
BEGI
N
Dbms_
out
put
.put
_li
ne(
‘Myf
ir
stpr
ogr
am’
):
END;

LABELEDBLOCKS

Label
edbl
ocksar
eanony
mousbl
ockswi
thal
abelwhi
chgi
vesanamet
othebl
ock.

Ex:
<<my
_bl
oock>>
BEGI
N
Dbms_
out
put
.put
_li
ne(
‘Myf
ir
stpr
ogr
am’
):
END;

SUBPROGRAMS

Subpr
ogr
amsar
epr
ocedur
esandf
unct
ions.Theycanbest
oredi
nthedat
abaseasst
and-
aloneobj
ect
s,aspar
t
ofpackageorasmet
hodsofanobj
ectt
ype.

TRI
GGERS

Tr
igger
sconsi
stsofaPL/SQLbl
ockt
hati
sassoci
atedwi
thanev
entt
hatoccuri
nthedat
abase.

NESTEDBLOCKS

Abl
ockcanbenest
edwi
thi
ntheexecut
abl
eorexcept
ionsect
ionofanout
erbl
ock.

I
DENTI
FIERS

I
dent
if
ier
sar
eusedt
onamePL/SQL obj
ect
s,suchasv
ari
abl
es,cur
sor
s,t
ypesandsubpr
ogr
ams.I
dent
if
ier
s
consi
stsofal
ett
er,opt
ional
l
yfol
lowedbyanysequenceofchar
act
ers,i
ncl
udi
ngl
ett
ers,number
s,dol
larsi
gns,

.
161

under
scor
es,
andpoundsi
gnsonl
y.Themaxi
mum l
engt
hforani
dent
if
ieri
s30char
act
ers.

QUOTEDI
DENTI
FIERS

I
fyouwantt
omakeani
dent
if
iercasesensi
ti
ve,i
ncl
udechar
act
erssuchasspacesorusear
eser
vedwor
d,y
ou
canencl
oset
hei
dent
if
ieri
ndoubl
equot
ati
onmar
ks.

Ex:
DECLARE
"
a"number:
=5;
"
A"number:
=6;
BEGI
N
dbms_
out
put
.put
_li
ne(
'a='
||a)
;
dbms_
out
put
.put
_li
ne(
'A='
||A)
;
END;
Out
put
:
a=6
A=6

COMMENTS

Comment
simpr
over
eadabi
li
tyandmakey
ourpr
ogr
am mor
eunder
standabl
e.Theyar
eignor
edbyt
hePL/SQL
compi
ler
.Ther
ear
etwot
ypesofcomment
sav
ail
abl
e.

 Si
ngl
eli
necomment
s
 Mul
ti
li
necomment
s

SI
NGLELI
NECOMMENTS

Asi
ngl
e-l
inecommentcanst
artanypoi
ntonal
inewi
tht
wodashesandcont
inuesunt
ilt
heendoft
hel
ine.

Ex:
BEGI
N
Dbms_
out
put
.put
_li
ne(
‘hel
lo’
); -
-sampl
epr
ogr
am
END;
MULTI
LINECOMMENTS

Mul
ti
li
necomment
sst
artwi
tht
he/
*del
imi
terandendswi
th*
/del
imi
ter
.

.
162

Ex:
BEGI
N
Dbms_
out
put
.put
_li
ne(
‘hel
lo’
); /
*sampl
epr
ogr
am *
/
END;

VARI
ABLEDECLERATI
ONS

Var
iabl
escanbedecl
aredi
ndecl
arat
ivesect
ionoft
hebl
ock;

Ex:
DECLARE
anumber
;
bnumber:
=5;
cnumberdef
aul
t6;

CONSTANTDECLERATI
ONS

Todecl
areaconst
ant
,youi
ncl
udet
heCONSTANTkey
wor
d,andy
oumustsuppl
yadef
aul
tval
ue.

Ex:
DECLARE
bconst
antnumber:
=5;
cconst
antnumberdef
aul
t6;

NOTNULLCLAUSE

Youcanal
sospeci
fyt
hatt
hev
ari
abl
emustbenotnul
l
.

Ex:
DECLARE
bconst
antnumbernotnul
l
:=5;
cnumbernotnul
ldef
aul
t6;
ANCHOREDDECLERATI
ONS

SQLo
PL/ f
fer
stwoki
ndsofachor
ing.
 Scal
aranchor
ing
 Recor
danchor
ing

.
163

SCALARANCHORI
NG

Uset
he%TYPEat
tri
but
etodef
iney
ourv
ari
abl
ebasedont
abl
e’scol
umnofsomeot
herPL/SQLscal
arv
ari
abl
e.

Ex:
DECLARE
dnodept
.dept
no%t
ype;
Subt
ypet
_numberi
snumber
;
at
_number
;
Subt
ypet
_snoi
sst
udent
.sno%t
ype;
V_
snot
_sno;

RECORDANCHORI
NG

Uset
he%ROWTYPEat
tri
but
etodef
iney
ourr
ecor
dst
ruct
urebasedonat
abl
e.

Ex:
D̀ECLARE

V_
deptdept
%rowt
ype;

BENEFI
TSOFANCHOREDDECLARATI
ONS

 Sy
nchr
oni
zat
ionwi
thdat
abasecol
umns.
 Nor
mal
izat
ionofl
ocal
var
iabl
es.

PROGRAMMER-
DEFI
NEDTYPES

Wi
tht
heSUBTYPEst
atement
, SQLa
PL/ l
lowsy
out
odef
iney
ourownsubt
ypesoral
iasesofpr
edef
ineddat
aty
pes,
somet
imesr
efer
redt
oasabst
ractdat
aty
pes.
Ther
ear
etwoki
ndsofsubt
ypes.
 Const
rai
ned
 Unconst
rai
ned

CONSTRAI
NEDSUBTYPE

Asubt
ypet
hatr
est
ri
ctsorconst
rai
nst
hev
aluesnor
mal
lyal
lowdbyt
hedat
aty
pei
tsel
f.

.
164

Ex:
Subt
ypeposi
ti
vei
sbi
nar
y_i
ntegerr
ange1.
.2147483647;

I
ntheabov
edecl
arat
ionav
ari
abl
ethati
sdecl
aredasposi
ti
vecanst
oreonl
yingegergr
eat
ert
hanzer
oev
en
t
houghbi
nar
y_i
ntegerr
angesf
rom -
2147483647.
.+2147483647.

UNCONSTRAI
NEDSUBTYPE

Asubt
ypet
hatdoesnotr
est
ri
ctt
hev
aluesoft
heor
igi
nal
dat
aty
pei
nvar
iabl
esdecl
aredwi
tht
hesubt
ype.

Ex:
Subt
ypef
loati
snumber
;

DATATYPECONVERSI
ONS

SQLc
PL/ anhandl
econv
ersi
onsbet
weendi
ff
erentf
ami
li
esamongt
hedat
aty
pes.
Conv
ersi
oncanbedonei
ntwoway
s.

 Expl
ici
tconv
ersi
on
 I
mpl
ici
tconv
ersi
on

EXPLI
CITCONVERSI
ON

Thi
scanbedoneusi
ngt
hebui
lt
-i
nfunct
ionsav
ail
abl
e.

I
MPLI
CITCONVERSI
ON

PL/
SQLwi
llaut
omat
ical
lyconv
ertbet
weendat
aty
pef
ami
li
eswhenpossi
ble.
Ex:
DECLARE
av
archar
(10)
;
BEGI
N
sel
ectdept
noi
ntoaf
rom deptwher
edname='
ACCOUNTI ;
NG'
END;

I
ntheabov
evar
iabl
eai
schart
ypeanddept
noi
snumbert
ypeev
ent
hough,
oracl
ewi
llaut
omat
ical
lyconv
ert
sthe
numer
icdat
aint
ochart
ypeassi
gnst
othev
ari
abl
e.
SQLc
PL/ anaut
omat
ical
lyconv
ertbet
ween

.
165

 Char
act
ersandnumber
s
 Char
act
ersanddat
es

VARI
ABLESCOPEANDVI
SIBI
LITY

Thescopeofav
ari
abl
eist
hepor
ti
onoft
hepr
ogr
am i
nwhi
cht
hev
ari
abl
ecanbeaccessed.ForPL/SQLv
ari
abl
es,
t
hisi
sfr
om t
hev
ari
abl
edecl
arat
ionunt
ilt
heendoft
hebl
ock.Whenav
ari
abl
egoesoutofscope,t
hePL/SQL
engi
newi
llf
reet
hememor
yusedt
ost
oret
hev
ari
abl
e.

Thev
isi
bil
it
yofav
ari
abl
eist
hepor
ti
onoft
hepr
ogr
am wher
ethev
ari
abl
ecanbeaccessedwi
thouthav
ingt
o
qual
if
yther
efer
ence.Thev
isi
bil
it
yisal
way
swi
thi
nthescope.I
fiti
soutofscope,
iti
snotv
isi
ble.

Ex1:
DECLARE
anumber
;--scopeofa
BEGI
N
-
--
--
--
-
DECLARE
bnumber
;--scopeofb
BEGI
N
-
--
--
END;
-
--
--
-
END;
Ex2:
DECLARE
anumber
;
bnumber
;
BEGI
N
-
-a,
bav
ail
abl
eher
e
DECLARE
bchar
(10)
;
BEGI
N
-
-aandchart
ypebi
sav
ail
abl
eher
e
END;
-
--
--
END;

.
166

Ex3:
<<my
_bl
ock>>
DECLARE
anumber
;
bnumber
;
BEGI
N
-
-a,
bav
ail
abl
eher
e
DECLARE
bchar
(10)
;
BEGI
N
-
-aandchart
ypebi
sav
ail
abl
eher
e
-
-numbert
ypebi
sav
ail
abl
eusi
ng<<my
_bl
ock>>.
b
END;
-
--
--
-
END;

PL/
SQLCONTROLSTRUCTURES

SQLh
PL/ asav
ari
etyofcont
rolst
ruct
urest
hatal
low y
out
ocont
rolt
hebehav
iouroft
hebl
ockasi
truns.These
st
ruct
uresi
ncl
udecondi
ti
onalst
atement
sandl
oops.

 I
f-
then-
else
 Case
 Casewi
thnoel
se
 Label
edcase
 Sear
chedcase
 Si
mpl
eloop
 Whi
lel
oop
 Forl
oop
 Got
oandLabel
s

I
F-THEN-
ELSE

Sy
ntax:
f<condi
I ti
on1>t
hen
Sequenceofst
atement
s;
El
sif<condi
ti
on1>t
hen
Sequenceofst
atement
s;

.
167

……
El
se
Sequenceofst
atement
s;
Endi
f;

Ex:
DECLARE
dnonumber
(2)
;
BEGI
N
sel
ectdept
noi
ntodnof
rom deptwher
edname=' NG'
ACCOUNTI ;
i
fdno=10t
hen
dbms_
out
put
.put
_li
ne(
'Locat
ioni
sNEW YORK'
);
el
sifdno=20t
hen
dbms_
out
put
.put
_li
ne(
'Locat
ioni
sDALLAS'
);
el
sifdno=30t
hen
dbms_
out
put
.put
_li
ne(
'Locat
ioni
sCHI
CAGO'
);
el
se
dbms_
out
put
.put
_li
ne(
'Locat
ioni
sBOSTON'
);
endi
f;
END;

Out
put
:
Locat
ioni
sNEW YORK

CASE

Sy
ntax:
Caset
est
-var
iabl
e
Whenv
alue1t
hensequenceofst
atement
s;
Whenv
alue2t
hensequenceofst
atement
s;
……
Whenv
aluent
hensequenceofst
atement
s;
sesequenceofst
El atement
s;
Endcase;

Ex:

.
168

DECLARE
dnonumber
(2)
;
BEGI
N
sel
ectdept
noi
ntodnof
rom deptwher
edname=' NG'
ACCOUNTI ;
casedno
when10t
hen
dbms_
out
put
.put
_li
ne(
'Locat
ioni
sNEW YORK'
);
when20t
hen
dbms_
out
put
.put
_li
ne(
'Locat
ioni
sDALLAS'
);
when30t
hen
dbms_
out
put
.put
_li
ne(
'Locat
ioni
sCHI
CAGO'
);
el
se
dbms_
out
put
.put
_li
ne(
'Locat
ioni
sBOSTON'
);
endcase;
END;

Out
put
:
Locat
ioni
sNEW YORK

CASEWI
THOUTELSE

Sy
ntax:
Caset
est
-var
iabl
e
Whenv
alue1t
hensequenceofst
atement
s;
Whenv
alue2t
hensequenceofst
atement
s;
……
Whenv
aluent
hensequenceofst
atement
s;
Endcase;

Ex:
DECLARE
dnonumber
(2)
;
BEGI
N
sel
ectdept
noi
ntodnof
rom deptwher
edname=' NG'
ACCOUNTI ;
casedno
when10t
hen

.
169

dbms_
out
put
.put
_li
ne(
'Locat
ioni
sNEW YORK'
);
when20t
hen
dbms_
out
put
.put
_li
ne(
'Locat
ioni
sDALLAS'
);
when30t
hen
dbms_
out
put
.put
_li
ne(
'Locat
ioni
sCHI
CAGO'
);
when40t
hen
dbms_
out
put
.put
_li
ne(
'Locat
ioni
sBOSTON'
);
endcase;
END;

Out
put
:
Locat
ioni
sNEW YORK

LABELEDCASE

Sy
ntax:
<<l
abel
>>
Caset
est
-var
iabl
e
Whenv
alue1t
hensequenceofst
atement
s;
Whenv
alue2t
hensequenceofst
atement
s;
……
Whenv
aluent
hensequenceofst
atement
s;
Endcase;

Ex:
DECLARE
dnonumber
(2)
;
BEGI
N
sel
ectdept
noi
ntodnof
rom deptwher
edname=' NG'
ACCOUNTI ;
<<my
_case>>
casedno
when10t
hen
dbms_
out
put
.put
_li
ne(
'Locat
ioni
sNEW YORK'
);
when20t
hen
dbms_
out
put
.put
_li
ne(
'Locat
ioni
sDALLAS'
);
when30t
hen
dbms_
out
put
.put
_li
ne(
'Locat
ioni
sCHI
CAGO'
);

.
170

when40t
hen
dbms_
out
put
.put
_li
ne(
'Locat
ioni
sBOSTON'
);
endcasemy
_case;
END;

Out
put
:
Locat
ioni
sNEW YORK

SEARCHEDCASE

Sy
ntax:
Case
When<condi
ti
on1>t
hensequenceofst
atement
s;
When<condi
ti
on2>t
hensequenceofst
atement
s;
……
When<condi
ti
onn>t
hensequenceofst
atement
s;
Endcase;

Ex:
DECLARE
dnonumber
(2)
;
BEGI
N
sel
ectdept
noi
ntodnof
rom deptwher
edname=' NG'
ACCOUNTI ;
casedno
whendno=10t
hen
dbms_
out
put
.put
_li
ne(
'Locat
ioni
sNEW YORK'
);
whendno=20t
hen
dbms_
out
put
.put
_li
ne(
'Locat
ioni
sDALLAS'
);
whendno=30t
hen
dbms_
out
put
.put
_li
ne(
'Locat
ioni
sCHI
CAGO'
);
whendno=40t
hen
dbms_
out
put
.put
_li
ne(
'Locat
ioni
sBOSTON'
);
endcase;
END;

Out
put
:
Locat
ioni
sNEW YORK

.
171

SI
MPLELOOP

Sy
ntax:
Loop
Sequenceofst
atement
s;
twhen<condi
Exi ti
on>;
Endl
oop;

I
nthesy
nt twhen<condi
axexi ti
on>i
sequi
val
entt
o
f<condi
I ti
on>t
hen
Exi
t;
Endi
f;

Ex:
DECLARE
inumber:
=1;
BEGI
N
l
oop
dbms_
out
put
.put
_li
ne(
'i='
||i
);
i:
=i+1;
exi
twheni>5;
endl
oop;
END;

Out
put
:
i=1
i=2
i=3
i=4
i=5

WHI
LELOOP

Sy
ntax:
Whi
le<condi
ti
on>l
oop
Sequenceofst
atement
s;

.
172

Endl
oop;

Ex:
DECLARE
inumber:
=1;
BEGI
N
Whi
lei<=5l
oop
dbms_
out
put
.put
_li
ne(
'i='
||i
);
i:
=i+1;
endl
oop;
END;

Out
put
:
i=1
i=2
i=3
i=4
i=5

FORLOOP

Sy
ntax:
For<l
oop_
count
er_
var
iabl
e>i
nlow_
bound.
.hi
gh_
boundl
oop
Sequenceofst
atement
s;
Endl
oop;

Ex1:
BEGI
N
Forii
n1.
.5l
oop
dbms_
out
put
.put
_li
ne(
'i='
||i
);
endl
oop;
END;
Out
put
:
i=1
i=2
i=3
i=4
i=5

.
173

Ex2:
BEGI
N
Forii
nrev
erse1.
.5l
oop
dbms_
out
put
.put
_li
ne(
'i='
||i
);
endl
oop;
END;
Out
put
:
i=5
i=4
i=3
i=2
i=1

NULLSTATEMENT

Usual
lywheny
ouwr
it
east
atementi
napr
ogr
am,y
ouwanti
ttodosomet
hing.Ther
ear
ecases,howev
er,when
y
ouwantt
otel
lPL/SQLt
odoabsol
utel
ynot
hing,
andt
hati
swher
etheNULLcomes.

TheNULLst
atementdeosnot
hingexceptpasscont
rolt
othenextexecut
abl
est
atement
.
YoucanuseNULLst
atementi
nthef
oll
owi
ngsi
tuat
ions.

 I
mpr
ovi
ngpr
ogr
am r
eadabi
li
ty.
Somet
imes,i
tishel
pfult
oav
oidanyambi
gui
tyi
nher
enti
nanI
Fst
atementt
hatdoesn’
tcov
eral
lpossi
ble
cases.Forexampl
e,wheny
ouwr
it
eanI
Fst
atement
,youdonothav
etoi
ncl
udeanELSEcl
ause.

 Nul
li
fyi
ngar
aisedexcept
ion.
Wheny
oudon’
twantt
owr
it
eanyspeci
alcodet
ohandl
eanexcept
ion,y
oucanuset
heNULLst
atementt
o
makesur
ethatar
aisedexcept
ionhal
tsexecut
ionoft
hecur
rentPL/SQLbl
ockbutdoesnotpr
opagat
eany
except
ionst
oencl
osi
ngbl
ocks.

 Usi
ngnul
laf
teral
abel
.
I
nsomecases,y
oucanpai
rNULLwi
thGOTO t
oav
oidhav
ingt
oexecut
eaddi
ti
onalst
atement
s.Forexampl
e,I
useaGOTO st
atementt
oqui
ckl
ymov
etot
heendofmypr
ogr
am i
fthest
ateofmydat
aindi
cat
est
hatno
f
urt
herpr
ocessi
ngi
srequi
red.BecauseIdonothav
etodoany
thi
ngatt
het
ermi
nat
ionoft
hepr
ogr
am,I
pl
aceaNULL st
atementaf
tert
hel
abelbecauseatl
eastoneexecut
abl
est
atementi
srequi
redt
her
e.Ev
en
t
houghNULLdeosnot
hing,
iti
sst
il
lanexecut
abl
est
atement
.

.
174

GOTOANDLABELS

Sy
ntax:
ol
Got abel
;

el
Wher abeli
sal
abeldef
inedi
nthePL/SQLbl
ock.Label
sar
eencl
osedi
ndoubl
eangl
ebr
acket
s.Whenagot
o
st
atementi
sev
aluat
ed,
cont
roli
mmedi
atel
ypassest
othest
atementi
dent
if
iedbyt
hel
abel
.
Ex:
BEGI
N
Forii
n1.
.5l
oop
dbms_
out
put
.put
_li
ne(
'i='
||i
);
i
fi=4t
hen
got
oexi
t_l
oop;
endi
f;
endl
oop;
<<exi
t_l
oop>>
Nul
l;
END;

Out
put
:
i=1
i=2
i=3
i=4

RESTRI
CTI
ONSONGOTO

 I
tisi
ll
egalt
obr
anchi
ntoani
nnerbl
ock,
loop.
 Atl
eastoneexecut
abl
est
atementmustf
oll
ow.
 I
tisi
ll
egalt
obr
anchi
ntoani
fst
atement
.
 I
tisi
ll
egalt
obr
anchf
rom onei
fst
atementt
oanot
heri
fst
atement
.
 I
tisi
ll
egalt
obr
anchf
rom except
ionbl
ockt
othecur
rentbl
ock.

PRAGMAS

Pr
agmasar
ecompi
lerdi
rect
ives.Theyser
veasi
nst
ruct
ionst
othePL/SQLcompi
ler
.Thecompi
lerwi
l
lactont
he

.
175

pr
agmadur
ingt
hecompi
lat
ionoft
hebl
ock.

Sy
ntax:
PRGAMAi
nst
ruct
ion_
to_
compi
ler
.

SQLo
PL/ f
fer
ssev
eralpr
agmas:

 AUTONOMOUS_
TRANSACTI
ON
 EXCEPTI
ON_
INI
T
 RESTRI
CT_
REFERENCES
 SERI
ALLY_
REUSABLE

.
176

SUBPROGRAMS

PROCEDURES

Apr
ocedur
eisamodul
ethatper
for
msoneormor
eact
ions.

Sy
ntax:
Pr
ocedur
e[schema.
]name[
(par
amet
er1[
par
, amet
er2…]
)]
[
aut
hiddef
iner|cur
rent
_user
]is
-
-[decl
arat
ions]
Begi
n
-
-execut
abl
est
atement
s
[
Except
ion
-
-except
ionhandl
ers]
End[
name]
;

I
nt eaut
heabov hidcl
ausedef
ineswhet
hert
hepr
ocedur
ewi
llexecut
eundert
heaut
hor
it
yoft
hedef
ineroft
he
pr
ocedur
eorundert
heaut
hor
it
yoft
hecur
rentuser
.

FUNCTI
ONS

Af
unct
ioni
samodul
ethatr
etur
nsav
alue.

Sy
ntax:
Funct
ion[
schema.
]name[
(par
amet
er1[
par
, amet
er2…]
)]
Ret
urnr
etur
n_dat
aty
pe
[
aut
hiddef
iner|cur
rent
_user
]
[
det
ermi
nist
ic]
[
par
all
el_
enabl
e]i
s

.
177

-
-[decl
arat
ions]
Begi
n
-
-execut
abl
est
atement
s
[
Except
ion
-
-except
ionhandl
ers]
End[
name]
;
I
nt eaut
heabov hidcl
ausedef
ineswhet
hert
hepr
ocedur
ewi
llexecut
eundert
heaut
hor
it
yoft
hedef
ineroft
he
pr
ocedur
eorundert
heaut
hor
it
yoft
hecur
rentuser
.

Det
ermi
nist
iccl
ausedef
ines,
anopt
imi
zat
ionhi
ntt
hatl
etst
hesy
stem useasav
edcopyoft
hef
unct
ion’
sret
urn
r
esul
t,i
fav
ail
abl
e.Thequet
yopt
imi
zercanchoosewhet
hert
ouset
hesav
edcopyorr
e-cal
lthef
unct
ion.

Par
all
el_
enabl
ecl
ausedef
ines,
anopt
imi
zat
ionhi
ntt
hatenabl
est
hef
unct
iont
obeexecut
edi
npar
all
elwhen
cal
ledf
rom wi
thi
nSELECTst
atement
.

PARAMETERMODES

 I
n(Def
aul
t)
 Out
 I
nout

I
N

I
npar
amet
erwi
llactaspl
/sqlconst
ant
.

OUT

 Outpar
amet
erwi
llactasuni
nti
ali
zedvar
iabl
e.
 Youcannotpr
ovi
deadef
aul
tval oanoutpar
uet amet
er.
 Anyassi
gnment ooutpar
smadet amet
erar
erol
ledbackwhenanexcept
ioni
srai
sedi
nthepr
ogr
am.
 Anact
ualpar
amet
ercor
respondi oanoutf
ngt ormalpar
amet
ermustbeav
ari
abl
e.

I
NOUT

 I
noutpar
amet
erwi
llactasi
nit
ial
izedvar
iabl
e.
 Anact
ualpar
amet
ercor
respondi oani
ngt noutf
ormalpar
amet
ermustbeav
ari
abl
e.

.
178

DEFAULTPARAMETERS

Def
aul
tPar
amet
erswi
llnotal
lowi
nthebegi
nni
ngandmi
ddl
e.
OutandI
nOutpar
amet
erscannothav
edef
aul
tval
ues.

Ex:
pr
ocedur
ep(
ainnumberdef
aul
t5,
binnumberdef
aul
t6,
cinnumberdef
aul
t7)–v
ali
d
pr
ocedur
ep(
ainnumber
,bi
nnumberdef
aul
t6,
cinnumberdef
aul
t7)–v
ali
ld
pr
ocedur
ep(
ainnumber
,bi
nnumber
,ci
nnumberdef
aul
t7)–v
ali
ld
pr
ocedur
ep(
ainnumber
,bi
nnumberdef
aul
t6,
cinnumber
)–i
nval
il
d
pr
ocedur
ep(
ainnumberdef
aul
t5,
binnumberdef
aul
t6,
cinnumber
)–i
nval
il
d
pr
ocedur
ep(
ainnumberdef
aul
t5,
binnumber
,ci
nnumber
)–i
nval
il
d

NOTATI
ONS

Not
ati
onsar
eoft
wot
ypes.

 Posi
ti
onal
not
ati
on
 Namenot
ati
on

Wecancombi
neposi
ti
onalandnamenot
ati
onbutposi
ti
onalnot
ati
oncannotbef
oll
owedbyt
henamenot
ati
on.

Ex:
Supposewehav
eapr
ocedur
epr
oc(
anumber
,bnumber
,cnumber
)andwehav
eone
anony
mousbl
ockwhi
chcont
ainsv
1,v
2,andv
3;

SQL>execpr
oc(
v1,
v2,
v3) -
-Posi
ti
onalnot
ati
on
SQL>execpr
oc(
a=>v
1,b=>v
2,c=>v
3)-
-Namednot
ati
on

FORMALANDACTUALPARAMETERS

 Par
amet
eswhi
char
eincal
li
ngsubpr
ogr eact
am ar ualpar
amet
ers.
 Par
amet
eswhi
char
eincal
ledsubpr
ogr ef
am ar or
malpar
amet
ers.
 I
fanysubpr
ogr
am wascal
led,
oncet
hecal
lwascompl
etedt
hent
hev
aluesoff
ormal
par
amet
ersar
ecopi
edt
otheact
ualpar
amet
ers.

.
179

Ex1:
CREATEORREPLACEPROCEDURESAMPLE(
ainnumber
,boutnumber
,ci
nout
number
)is
BEGI
N
dbms_
out
put
.put
_li
ne(
'Af
tercal
l'
);
dbms_
out
put
.put
_li
ne(
'a='
||a|
|'
b='
||b|
|'c='
||c)
;
b:
=10;
c:
=20;
dbms_
out
put
.put
_li
ne(
'Af
terassi
gnment
')
;
dbms_
out
put
.put
_li
ne(
'a='
||a|
|'
b='
||b|
|'c='
||c)
;
ENDSAMPLE;

DECLARE
v
1number:
=4;
v
2number:
=5;
v
3number:
=6;
BEGI
N
dbms_
out
put
.put
_li
ne(
'Bef
orecal
l'
);
dbms_
out
put
.put
_li
ne(
'v1='
||v
1||'
v2='
||v
2||'
v3='
||v
3);
sampl
e(v
1,v
2,v
3);
dbms_
out
put
.put
_li
ne(
'Af
tercompl
eti
onofcal
l'
);
dbms_
out
put
.put
_li
ne(
'v1='
||v
1||'
v2='
||v
2||'
v3='
||v
3);
END;

Out
put
:
Bef
orecal
l
v
1=4v
2=5v
3=6
Af
tercal
l
a=4b=c=6
Af
terassi
gnment
a=4b=10c=20
Af
tercompl
eti
onofcal
l
v
1=4v
2=10v
3=20
Ex2:
CREATEORREPLACEFUN(
ainnumber
,boutnumber
,ci
noutnumber
)ret
urn
numberI
S
BEGI
N

.
180

dbms_
out
put
.put
_li
ne(
'Af
tercal
l'
);
dbms_
out
put
.put
_li
ne(
'a='
||a|
|'b='
||b|
|'c='
||c)
;
dbms_
out
put
.put
_li
ne(
'Bef
oreassi
gnementResul
t='
||(
a*nv
l(b,
1)*
c))
;
b:
=5;
c:
=7;
dbms_
out
put
.put
_li
ne(
'Af
terassi
gnment
')
;
dbms_
out
put
.put
_li
ne(
'a='
||a|
|'b='
||b|
|'c='
||c)
;
r
etur
n(a*
b*c)
;
ENDFUN;

DECLARE
v
1number:
=1;
v
2number:
=2;
v
3number:
=3;
vnumber
;
BEGI
N
dbms_
out
put
.put
_li
ne(
'Bef
orecal
l'
);
dbms_
out
put
.put
_li
ne(
'v1='
||v
1||'
v2='
||v
2||'
v3='
||v
3);
v:
=fun(
v1,
v2,
v3)
;
dbms_
out
put
.put
_li
ne(
'Af
tercal
lcompl
eted'
);
dbms_
out
put
.put
_li
ne(
'v1='
||v
1||'
v2='
||v
2||'
v3='
||v
3);
dbms_
out
put
.put
_li
ne(
'Resul
t='
||v
);
END;

Out
put
:
Bef
orecal
l
v
1=1v
2=2v
3=3
Af
tercal
l
a=1b=c=3
Bef
oreassi
gnementResul
t=3
Af
terassi
gnment
a=1b=5c=7
Af
tercal
lcompl
eted
v
1=1v
2=5v
3=7
Resul
t=35

RESTRI
CTI
ONSONFORMALPARAMETERS

.
181

 Bydecl
ari
ngwi
thspeci
fi
edsi
zei
nact
ualpar
amet
ers.
 Bydecl
ari
ngf
ormalpar
amet
erswi
th%t
ypespeci
fi
er.

USI
NGNOCOPY

 Nocopyi
sahi
nt,notacommand.Thi
smeanst
hatt
hecompi
lermi
ghtsi
lent
lydeci
det
hati
tcan’
tful
fi
ll
y
ourr oranocopypar
equestf amet
er.
 Thecopy
ingf
rom f
ormalt
oact
ualcanber
est
ri
ctedbyi ngnocopyqual
ssui if
ier
.
 Topasst
heoutandi
noutpar
amet
ersbyr
efer
enceusenocopyqual
if
ier
.

Ex:
CREATEORREPLACEPROCEDUREPROC(
ainoutnocopynumber
)IS
BEGI
N
-
--
-
ENDPROC;

CALLANDEXEC

Cal
lisaSQLst
atement
,whi
chcanbeusedt
oexecut
esubpr
ogr
amsl
ikeexec.

Sy
ntax:
lsubpr
Cal ogr
am_
name(
[ar
gument
_li
st]
)[i
ntohost
_var
iabl
e];

 Thepar
ant
hesesar
eal
way
srequi
red,
eveni
fthesubpr
ogr
am t
akesnoar
gument
s.
 Wecannotusecal
lwi
thoutandi
noutpar
amet
ers.
 Cal
lisaSQLst
atement
,iti
snotv
ali
dinsi
deaPL/SQLbl
ock;
 TheI
NTOcl
ausei
susedf
ort
heout
putv
ari
abl
esoff
unct
ionsonl
y.
 Wecannotuse‘
exec’
wihoutori
t noutpar
amet
ers.
 Execi
snotv
ali
dinsi
deaPL/SQLbl
ock;

Ex1:
CREATEORREPLACEPROCI
S
BEGI
N
dbms_
out
put
.put
_li
ne(
'hel
lowor
ld'
);
ENDPROC;

Out
put
:

.
182

SQL>cal
lpr
oc(
);
hel
lowor
ld

Ex2:
CREATEORREPLACEPROC(
ainnumber
,bi
nnumber
)IS
BEGI
N
dbms_
out
put
.put
_li
ne(
'a='
||a|
|'b='
||b)
;
ENDPROC;

Out
put
:
SQL>cal
lpr
oc(
5,6)
;
a=5b=6

Ex3:
CREATEORREPLACEFUNCTI
ONFUNRETURNVARCHARI
S
BEGI
N
r
etur
n'hel
lowor
ld'
;
ENDFUN;

Out
put
:
SQL>v
ari
abl
evv
archar
(20)
SQL>cal
lfun(
)int
o:v
;
SQL>pr
intv
hel
lowor
ld

CALLBYREFERENCEANDCALLBYVALUE

 I
npar
amet
ersbydef tcal
aul lbyr
efer
encewher noutcal
easoutandi lbyval
ue.
 Whenpar
amet
erpassedbyr
efer
ence,apoi
ntert
otheact
ualpar
amet
eri
spassedt
othecor
respondi
ng
f
ormalpar
amet
er.
 Whenpar
amet
erpassedbyv
aluei
tcopi
est
hev
alueoft
heact
ualpar
amet
ert
othef
ormalpar
amet
er.
 Cal
lbyr
efer
encei
sfast
ert
hant
hecal
lbyv
aluebecausei
tav
oidst
hecopy
ing.

SUBPROGRAMSOVERLOADI
NG

 Possi
blewi
thdi
ff
erentnumberofpar
amet
ers.
 Possi
blewi
thdi
ff
erentt
ypesofdat
a.
 Possi
blewi
thsamet
ypewi
thobj
ect
s.

.
183

 Cannotbepossi
blewi
thdi
ff
erentt
ypesofmodes.
 Wecanov
erl
oadl
ocal
subpr
ogr
amsal
so.

Ex:
SQL>cr
eat
eorr
epl
acet
ypet
1asobj
ect
(anumber
);
/
SQL>cr
eat
eorr
epl
acet
ypet
1asobj
ect
(anumber
);
/

DECLARE
it
1:=t
1(5)
;
jt
2:=t
2(5)
;
PROCEDUREP(
mt1)I
S
BEGI
N
dbms_
out
put
.put
_li
ne(
'a='
||m.
a);
ENDP;
PROCEDUREP(
nt2)I
S
BEGI
N
dbms_
out
put
.put
_li
ne(
'b='
||n.
b);
ENDP;
PROCEDUREPRODUCT(
anumber
,bnumber
)IS
BEGI
N
dbms_
out
put
.put
_li
ne(
'Pr
oductofa,
b='
||a*b)
;
ENDPRODUCT;
PROCEDUREPRODUCT(
anumber
,bnumber
,cnumber
)IS
BEGI
N
dbms_
out
put
.put
_li
ne(
'Pr
oductofa,
b='
||a*b*c)
;
ENDPRODUCT;
BEGI
N
p(
i)
;
p(
j)
;
pr
oduct
(4,
5);
pr
oduct
(4,
5,
6);
END;

Out
put
:
a=5
b=5
Pr
oductofa,
b=20
Pr
oductofa,
b=120

BENEFI
TSOFOVERLOADI
NG

.
184

 Suppor
ti
ngmanydat
acombi
nat
ions
 Fi
tt
ingt
hepr
ogr
am t
otheuser
.

RESTRI
CTI
ONSONOVERLOADI
NG

 Ov
erl
oadedpr
ogr
amswi
thpar
amet
erl
ist
sthatdi
ff
eronl
ybynamemustbecal
ledusi
ngnamednot
ati
on.
 Thepar
amet
erl
istofov
erl
oadedpr
ogr
amsmustdi
ff
erbymor
ethanpar
amet
ermode.
 Al
loft
heov
erl
oadedpr
ogr
amsmustbedef
inedwi
thi
nthesamePL/SQLscopeorbl
ock.
 Ov
erl
oadedf
unct
ionsmustdi
ff
erbymor
ethant
hei
rret
urnt
ype.

I
MPORTANTPOI
NTSABOUTSUBPROGRAMS

 Whenast
oredsubpr
ogr
am i
scr
eat
ed,
iti
sst
oredi
nthedat
adi
cti
onar
y.
 Thesubpr
ogr
am i
sst
oredi
ncompi
lef
orm whi sknownasp-
chi codei
naddi
ti
ont
othesour
cet
ext
.
 Thep-
codehasal
loft
her
efer
encesi
nthesubpr
ogr
am ev
aluat
ed,
andt
hesour
cecodei
str
ansl
atedi
nto
af
ormt
hati
seasi
lyr
eadabl
ebyPL/SQLengi
ne.
 Whent
hesubpr
ogr
am i
scal
led,
thep-
codei
sreadf
rom t
hedi
sk,
ifnecessar
y,andexecut
ed.
 Oncei
treadsf
rom t
hedi
sk,t
hep-
codei
sst
oredi
ntheshar
edpoolpor
ti
onoft
hesy
stem gl
obalar
ea
( ,wh
SGA) er
eitcanbeaccessedbymul
ti
pleuser
sasneeded.
 Li
keal
loft
hecont
ent
soft
heshar
edpool
,p-
codei
sagedoutoft
heshar
edpoolaccor
dingt
oal
east
r
ecent
lyused(
LRU)al
gor
it
hm.
 Subpr amscanbel
ogr ocal
.
 Localsubpr
ogr
amsmustbedecl
aredi
nthedecl
arat
ivesect
ionofPL/SQL bl
ockandcal
l
edf
rom t
he
execut
abl
esect
ion.
 Subpr
ogr
amscannothav
ethedecl
arat
ivesect
ionsepar
atel
y.
 St
oredsubpr
ogr
amscanhav
elocalsubpr
ogr
ams;
 Localsubpr
ogr
amsal
socanhav
elocalsubpr
ogr
ams.
 I
fthesubpr
ogr
am cont
ainsav
ari
abl
ewi
tht
hesamenameast
hecol
umnnameoft
het
abl
ethenuset
he
dotmet
hodt
odi
ff
erent
iat
e(subpr
ogr
am_
name.
sal
).
 Subpr
ogr
amscanbei
nval
idat
ed.

PROCEDURESVFUNCTI
ONS

 Pr
ocedur
esmayr
etur
nthr
oughoutandi
noutpar
amet
erswher
easf
unct
ionmustr
etur
n.
 Pr
ocedur
escannothav
eret
urncl
ausewher
easf
unct
ionsmust
.
 Wecanusecal
lst
atementdi
rect
lyf
orexecut
ingpr
ocedur
ewher
easweneedt
odecl
areav
ari
abl
ein

.
185

caseoff
unct
ions.
 Funct
ionscanusei
nsel
ectst
atement
swher
easpr
ocedur
escannot
.
 Funct
ionscancal
lfr
om r
epor
tsenv
ironmentwher
easpr
ocedur
escannot
.
 Wecanuseexecf
orexecut
ingpr
ocedur
eswher
easf
unct
ionscannot
.
 Funct
ioncanbeusedi
ndbms_
out
putwher
easpr
ocedur
ecannot
.
 Pr
ocedur
ecal
lisast
andal
oneexecut
abl
est
atementwher
easf
unct
ioncal
lisapar
tofanexecut
abl
e
st
atement
.

STOREDVLOCALSUBPROGRAMS

 Thest
oredsubpr
ogr
am i
sst
oredi
ncompi
ledp-
codei
nthedat
abase,whent
hepr
ocedur
eiscal
ledi
t
doesnothav
etobecompi
led.
Thel
ocalsubpr
ogr
am i
scompi
ledaspar
tofi
tscont
aini
ngbl
ock.I
fthecont
aini
ng
bl
ocki
sanony
mousandi
srunmul
ti
plet
imes,
thesubpr
ogr
am hast
obecompi
led
eacht
ime.

 St
oredsubpr
ogr
amscanbecal
ledf
rom anybl
ocksubmi
tt
edbyauserwhohasexecut
epr
ivi
legesont
he
subpr
ogr
am.
Localsubpr
ogr
amscanbecal
ledonl
yfr
om t
hebl
ockcont
aini
ngt
hesubpr
ogr
am.
 Bykeepi
ngt
hest
oredsubpr
ogr
am codesepar
atef
rom t
hecal
li
ngbl
ock,t
hecal
li
ngbl
ocki
sshor
terand
easi
ert
ounder
stand.
Thel
ocalsubpr
ogr
am andt
hecal
li
ngbl
ockar
eoneandt
hesame,
whi
chcanl
eadt
o
par
tconf
usi
on.I
fachanget
othecal
l
ingbl
ocki
smade,
thesubpr
ogr
am wi
llbe
r
ecompi
ledasoft
her
ecompi
lat
ionoft
hecont
aini
ngbl
ock.
 Thecompi
ledp-
codecanbepi
nnedi
ntheshar
edpoolusi
ngt
heDBMS_
SHARED_
POOLPackage.Thi
s
cani
mpr
oveper
for
mance.
Localsubpr
ogr
amscannotbepi
nnedi
ntheshar
edpoolbyt
hemsel
ves.
 St
andal
onest
oredsubpr
ogr
amscannotbeov
erl
oaded,butpackagedsubpr
ogr
amscanbeov
erl
oaded
wi
thi
nthesamepackage.
 Localsubpr
ogr
amscanbeov
erl
oadedwi
thi
nthesamebl
ock.

Ex1:
CREATEORREPLACEPROCEDUREPI
S
BEGI
N
dbms_
out
put
.put
_li
ne(
'St
oredsubpr
ogr
am'
);
END;

.
186

Out
put
:
SQL>execp
St
oredsubpr
ogr
am

Ex2:
DECLARE
PROCEDUREPI
S
BEGI
N
dbms_
out
put
.put
_li
ne(
'Local
subpr
ogr
am'
);
END;
BEGI
N
p;
END;

Out
put
:
Localsubpr
ogr
am
COMPI
LINGSUBPROGRAMS

 SQL>Al
terpr
ocedur
eP1compi
le;
 SQL>Al
terf
unct
ionF1compi
le;

SUBPROGRAMSDEPENDECI
ES

 Ast
oredsubpr
ogr
am i
smar
kedasi
nval
idi
nthedat
adi
cti
onar
yifi
thascompi
leer
ror
s.
 Ast
oredsubpr
ogr
am canal
sobecomei
nval
idi
faDDLoper
ati
oni
sper
for
medononeofi
tsdependent
obj
ect
s.
 I
fasubpr
ogr
am i
sinv
ali
dat
ed,
thePL/SQLengi
newi
llaut
omat
ical
lyat
temptt
orecompi
lei
nthenextt
ime
i
tiscal
led.
 I
fwehav
etwopr
ocedur
esl
ikeP1andP2i
nwhi
chP1dependsonP2.I
fwecompi
leP2t
henP1i
s
i
nval
idat
ed.

SUBPROGRAMSDEPENDENCI
ESI
NREMOTEDATABASES

 Wewi
llcal
lremot
esubpr
ogr
am usi
ngconnectst
ri
ngl
ikeP1@ORACLE;
 I
fwehav
etwopr
ocedur
esl
ikeP1andP2i
nwhi
chP1dependsonP2butP2wasi
nremot
edat
abase.I
f
wecompi
leP2i
twi
l
lnoti
nval
idat
eP1i
mmedi
atel
ybecauset
hedat
adi
cti
onar
ydoesnott
rackr
emot
e
dependenci
es.
 I
nst
eadt
hev
ali
dit
yofr
emot
eobj
ect
sischeckedatr
unt
ime.WhenP1i
scal
led,t
her
emot
edat
a

.
187

di
cti
onar
yisquer
iedt
odet
ermi
net
hest
atusofP2.
 P1andP2ar
ecompar
edt
oseei
tP1needst
ober
ecompi
led,t
her
ear
etwodi
ff
erentmet
hodsof
compar
isi
on
 Ti
mest
ampModel
 Si
gnat
ureModel

TI
MESTAMPMODEL

 Thi
sist
hedef
aul
tmodelusedbyor
acl
e.
 Wi
tht
hismodel
,thet
imest
ampsoft
hel
astmodi
fi
cat
ionsoft
het
woobj
ect
sar
e
compar
ed.
 Thel
ast
_ddl
_ti
mef
i dofuser
el _obj
ect
scont
ainst
het
imest
amp.
 I
fthebaseobj
ecthasanewert
imest
ampt
hant
hedependentobj
ect
,the
dependentobj
ectwi
llber
ecompi
led.

I
SSUESWI
THTHI
SMODEL

 I
ftheobj
ect
sar
eindi
ff
erentt
imezones,
thecompar
isoni
sinv
ali
d.
 WhenP1i
sinacl
ientsi
dePL/SQL engi
nesuchasor
acl
efor
ms,i
nthi
scasei
tmaynotpossi
blet
o
r
ecompi
leP1,
becauset
hesour
cef
ori
tmaynotbei
ncl
udedwi
tht
hef
orms.

SI
GNATUREMODEL

 Whenapr
ocedur
eiscr
eat
ed,
asi
gnat
urei
sst
oredi
nthedat
adi
cti
onar
yinaddi
ti
ont
othep-
code.
 Thesi
gnat
ureencodest
het
ypesandor
deroft
hepar
amet
es.
 WhenP1i
scompi
ledt
hef
ir
stt
ime,t
hesi
gnat
ureofP2i
sincl
uded.Thus,P1onl
yneedst
orecompi
led
whent
hesi
gnat
ureofP2changes.
 I
nor
dert
ouset
hesi
gnat
uremodel
,thepar
amet
erREMOTE_DEPENDENCI MODEmu
ES_ stbesett
oSI
GNATURE.

Thi
sisapar
amet
eri
nthedat
abasei
nit
ial
izat
ionf
il
e.

THREEWAYSOFSETTI
NGTHI
SMODE

 Addt
hel
ineREMOTE_DEPENDENCI
ES_ GNATURE t
MODE=SI othedat
abasei
nit
ial
izat
ionf
il
e.Thenextt
imet
he
dat
abasei
sst
art
ed,
themodewi
llbesett
oSI
GNATUREf
oral
lsessi
ons.
 Al
tersy
stem setr
emot
e_dependenci
es_
mode=si
gnat
ure;
Thi
swi
l
laf
fectt
heent
ir
edat
abase(
allsessi
ons)f
rom t
het
imet
hest
atementi
s
i
ssued.Youmusthav
etheALTERSYSTEM pr
ivi
leget
oissuet
hiscommand.

.
188

 Al
tersessi
onsetr
emot
e_dependenci
es_
mode=si
gnat
ure;
Thi
swi
l
lonl
yaf
fecty
oursessi
on

I
SSUESWI
THTHI
SMODEL

 Si
gnat
uresdon’
tgetmodi
fi
edi
fthedef
aul
tval
uesoff
ormalpar
amet
ersar
e
changed.

 SupposeP2hasadef
aul
tval
uef
oroneofi
tspar
amet
ers,
andP1i
susi
ngt
his
def
aul
tval
ue.I
fthedef
aul
tint
hespeci
fi
cat
ionf
orP2i
schanged,
P1wi
llnotbe
r
ecompi
l
edbydef
aul
t.Theol
dval
uef
ort
hedef
aul
tpar
amet
erwi
llst
il
lbeusedunt
il
P1i
smanual
lyr
ecompi
led.
 I
fP1i
scal
li
ngapackagedpr
ocedur
eP2,andanew ov
erl
oadedv
ersi
onofP2i
saddedt
other
emot
e
package,
thesi
gnat
urei
snotchanged.P1wi
llst
il
luset
heol
dver
sion(
nott
henewov
erl
oadedone)unt
il
P1i
srecompi
ledmanual
ly
.

FORWARDDECLERATI
ON

Bef
oregoi
ngt
ouset
hepr
ocedur
einanyot
hersubpr
ogr
am orot
herbl
ock,
youmustdecl
aret
hepr
otot
ypeoft
he
pr
ocedur
eindecl
arat
ivesect
ion.

Ex1:
DECLARE
PROCEDUREP1I
S
BEGI
N
dbms_
out
put
.put
_li
ne(
'Fr
om pr
ocedur
ep1'
);
p2;
ENDP1;
PROCEDUREP2I
S
BEGI
N
dbms_
out
put
.put
_li
ne(
'Fr
om pr
ocedur
ep2'
);
p3;
ENDP2;
PROCEDUREP3I
S
BEGI
N
dbms_
out
put
.put
_li
ne(
'Fr
om pr
ocedur
ep3'
);
ENDP3;
BEGI
N
p1;

.
189

END;

Out
put
:
p2;
*
ERRORatl
ine5:
ORA-
06550:l
ine5,
col
umn1:
PLS-
00313:'
P2'
notdecl
aredi
nthi
sscope
ORA-
06550:l
ine5,
col
umn1:
PL/
SQL:St
atementi
gnor
ed
ORA-
06550:l
ine10,
col
umn1:
PLS-
00313:'
P3'
notdecl
aredi
nthi
sscope
ORA-
06550:l
ine10,
col
umn1:
PL/
SQL:St
atementi
gnor
ed

Ex2:
DECLARE
PROCEDUREP2; -
-for
war
ddecl
arat
ion
PROCEDUREP3;
PROCEDUREP1I
S
BEGI
N
dbms_
out
put
.put
_li
ne(
'Fr
om pr
ocedur
ep1'
);
p2;
ENDP1;
PROCEDUREP2I
S
BEGI
N
dbms_
out
put
.put
_li
ne(
'Fr
om pr
ocedur
ep2'
);
p3;
ENDP2;
PROCEDUREP3I
S
BEGI
N
dbms_
out
put
.put
_li
ne(
'Fr
om pr
ocedur
ep3'
);
ENDP3;
BEGI
N
p1;
END;

Out
put
:
Fr
om pr
ocedur
ep1

.
190

Fr
om pr
ocedur
ep2
Fr
om pr
ocedur
ep3

PRI
VILEGESANDSTOREDSUBPROGRAMS

EXECUTEPREVI
LEGE

 Forst
oredsubpr
ogr
amsandpackagest
her
elev
antpr
ivi
legei
sEXECUTE.
 I
fuserAhadt
hepr
ocedur
ecal
ledemp_
proct
henuserAgr
ant
sexecut
epr
ivi
legeonpr
ocedur
etouser
Bwi
tht
hef
oll
owi
ngcommand.
SQL>Gr
antexecut
eonemp_
proct
ouserB.
 ThenuserBcanr
unt
hepr
ocedur
ebyi
ssui
ng
SQL>ExecuserA.
emp_
proc

user
Acr
eat
edt
hef
oll
owi
ngpr
ocedur
e

CREATEORREPLACEPROCEDUREPI
S
cur
sori
ssel
ect*
from st
udent
1;
BEGI
N
f
orvi
ncl
oop
i
nser
tint
ost
udent
2val
ues(
v.no,
v.
name,
v.
mar
ks)
;
endl
oop;
ENDP;

user
Agr
ant
edexecut
epr
ivi
leget
ouser
Busi
ng
SQL>gr
antexecut
eonpt
ouser
B

Thenuser
Bexecut
edt
hepr
ocedur
e
SQL>Execuser
A.p

I
fsupposeuser
Bal
sohav
ingst
udent
2tabl
ethenwhi
cht
abl
ewi
llpopul
atewhet
heruser
A’soruser
B’s.

Theansweri
suser
A’sst
udent
2tabl
eonl
ybecausebydef
aul
tthepr
ocedur
ewi
llexecut
eundert
hepr
ivl
igesetof
i
tsowner
.
Theabov
epr
ocedur
eisknownasdef
iner
’spr
ocedur
e.

.
191

HOW TOPOPULATEUSERB’
sTABLE

 Or
acl
eint
roducesI
nvoker
’sandDef
iner
’sr
ight
s.
 Bydef
aul
titwi
lluset
hedef
iner
’sr
ight
s.
 Ani
nvoker
’sr
ight
srout
inecanbecr
eat
edbyusi
ngAUTHI
Dcl
auset
opopul
atet
he
user
B’st
abl
e.
 I
tisv
ali
dforst
and-
alonesubpr
ogr
ams,
packagespeci
fi
cat
ions,
andobj
ectt
ype
speci
fi
cat
ionsonl
y.

user
Acr
eat
edt
hef
oll
owi
ngpr
ocedur
e

CREATEORREPLACEPROCEDUREP
AUTHI
DCURRENT_
USERI
S
cur
sori
ssel
ect*
from st
udent
1;
BEGI
N
f
orvi
ncl
oop
i
nser
tint
ost
udent
2val
ues(
v.no,
v.
name,
v.
mar
ks)
;
endl
oop;
ENDP;

Thengr
antexecut
epr
ivi
legeonpt
ouser
B.
Execut
ingt
hepr
ocedur
ebyuser
B,whi
chpopul
atesuser
B’st
abl
e.

Theabov
epr
ocedur
eiscal
ledi
nvoker
’spr
ocedur
e.
I
nst
eadofcur
rent
_userofaut
hidcl
ause,
ify
ouusedef
inert
heni
twi
llbecal
leddef
iner
’pr
ocedur
e.

STOREDSUBPROGRAMSANDROLES

wehav
etwouser
ssaket
handsudhai
nwhi
chsaket
hhasst
udentt
abl
eandsudhadoesnot
.
Sudhai
sgoi
ngt
ocr
eat
eapr
ocedur
ebasedonst
udentt
abl
eownedbysaket
h.Bef
oredoi
ngt
hissaket
hmust
gr
antt
heper
missi
onsont
hist
abl
etosudha.

SQL>connsaket
h/saket
h
SQL>gr
antal
lonst
udentt
osudha;
t
hensudhacancr
eat
epr
ocedur
e
SQL>connsudha/
sudha

.
192

CREATEORREPLACEPROCEDUREPI
S
cur
sorci
ssel
ect*
from saket
h.st
udent
;
BEGI
N
f
orvi
ncl
oop
dbms_
out
put
.put
_li
ne(
‘No=‘
||v
.no)
;
endl
oop;
ENDP;

her
epr
ocedur
ewi
llbecr
eat
ed.

I
fthesamepr
ivi
legewasgr
ant
edt
hroughar
olei
twontcr
eat
ethepr
ocedur
e.
Exami
net
hef
oll
owi
ngcode

SQL>connsaket
h/saket
h
SQL>cr
eat
erol
esaket
h_r
ole;
SQL>gr
antal
lonst
udentt
osaket
h_r
ole;
SQL>gr
antsaket
h_r
olet
osudha;
t
henconnsudha/
sudha

CREATEORREPLACEPROCEDUREPI
S
cur
sorci
ssel
ect*
from saket
h.st
udent
;
BEGI
N
f
orvi
ncl
oop
dbms_
out
put
.put
_li
ne(
‘No=‘
||v
.no)
;
endl
oop;
ENDP;

Theabov
ecodewi
llr
aiseer
rori
nst
eadofcr
eat
ingpr
ocedur
e.
Thi
sisbecauseofear
lybi
ndi
ngwhi
chPL/SQLusesbydef
aul
tinwhi
chr
efer
encesar
eev
aluat
edi
ncompi
let
ime
butwheny
ouar
eusi
ngar
olet
hiswi
llaf
fecti
mmedi
atel
y.

I
SSUESWI
THI
NVOKER’
SRI
GHTS

 I
nani
nvoker
’sr
ight
srout
ine,ext
ernalr
efer
encesi
nSQLst
atement
swi
llber
esol
vedusi
ngt
hecal
ler
’s
pr
ivi
legeset
.
 Butr
efer
encesi
nPL/SQLst
atement
sar
est
il
lresol
vedundert
heowner
’spr
ivi
legeset
.

TRI
GGERS,
VIEWSANDI
NVOKER’
SRI
GHTS

.
193

 Adat
abaset
ri
ggerwi
llal
way
sbeexecut
edwi
thdef
iner
’sr
ight
sandwi
llexecut
eundert
hepr
ivi
legesetof
t
heschemat
hatownst
het
ri
gger
ingt
abl
e.
 Thi
sisal
sot
ruef
orPL/SQLf
unct
iont
hati
scal
ledf
rom av
iew.I
nthi
scase,
thef
unct
ionwi
llexecut
eunder
t
hepr
ivi
legesetoft
hev
iew’
sowner
.

PACKAGES

Apackagei
sacont
ainerf
orr
elat
edobj
ect
s.I
thasspeci
fi
cat
ionandbody
.Eachoft
hem i
sst
oredsepar
atel
y

.
194

i
ndat
adi
cti
onar
y.

PACKAGESYNTAX

Cr
eat
eorr acepackage<package_
epl name>i
s
-
-packagespeci
fi
cat
ioni
ncl
udessubpr
ogr
amssi
gnat
ures,
cur
sor
sandgl
obalor
publ
icv
ari
abl
es.
End<package_
name>;

Cr
eat
eorr acepackagebody<package_
epl name>i
s
-
-packagebodyi
ncl
udesbodyf
oral
lthesubpr
ogr
amsdecl
aredi
nthespec,
pri
vat
e
Var
iabl
esandcur
sor
s.
Begi
n
-
-ini
ti
ali
zat
ionsect
ion
Except
ion
-
-Except
ionhandl
ingseci
ton
End<package_
name>;

I
MPORTANTPOI
NGSABOUTPACKAGES

 Thef
ir
stt
imeapackagedsubpr
ogr
am i
scal
ledoranyr
efer
encet
oapackagedv
ari
abl
eort
ypei
smade,
t
hepackagei
sinst
ant
iat
ed.
 Eachsessi
onwi
llhav
eit
sowncopyofpackagedv
ari
abl
es,ensur
ingt
hatt
wosessi
onsexecut
ing
subpr
ogr
amsi
nthesamepackageusedi
ff
erentmemor
ylocat
ions.
 I
nmanycasesi
nit
ial
izat
ionneedst
ober
unt
hef
ir
stt
imet
hepackagei
sinst
ant
iat
edwi
thi
nasessi
on.
Thi
scanbedonebyaddi
ngi
nit
ial
izat
ionsect
iont
othepackagebodyaf
teral
ltheobj
ect
s.
 Packagesar
est
oredi
nthedat
adi
cti
onar
yandcannotbel
ocal
.
 Packagedsubpr
ogr
amshasanadv
ant
ageov
erst
andal
onesubpr
ogr
am.
 Whenev
eranyr
efer
encet
opackage,
thewhol
epackagep-
codewasst
oredi
nshar
edpoolofSGA.
 Packagemayhav
elocalsubpr
ogr
ams.
 Youcani
ncl
udeaut
hidcl
ausei
nsi
det
hepackagespecnoti
nthebody
.
 Theexecut
ionsect
ionofapackagei
sknowasi
nit
ial
izat
ionsect
ion.
 Youcanhav
eanexcept
ionsect
ionatt
hebot
tom ofapackagebody
.
 Packagessubpr
ogr
amsar
enoti
nval
idat
ed.

COMPI
LINGPACKAGES

.
195

 SQL>Al
terpackagePKGcompi
le;
 SQL>Al
terpackagePKGcompi
lespeci
fi
cat
ion;
 SQL>Al
terpackagePKGcompi
lebody
;

PACKAGEDEPENDENCI
ES

 Thepackagebodydependsont
hesomeobj
ect
sandt
hepackageheader
.
 Thepackageheaderdoesnotdependont
hepackagebody
,whi
chi
sanadv
ant
ageofpackages.
 Wecanchanget
hepackagebodywi
thoutchangi
ngt
heheader
.

PACKAGERUNTI
MESTATE

Packager
unt
imest
atei
sdi
ff
erf
ort
hef
oll
owi
ngpackages.
 Ser
ial
lyr
eusabl
epackages
 Nonser
ial
lyr
eusabl
epackages

SERI
ALLYREUSABLEPACKAGES

Tof
orcet
heor
acl
etouseser
ial
lyr
eusabl
ever
siont
heni
ncl
udePRAGMASERI REUSABLEi
ALLY_ nbot
hpackagespec
andbody
,Exami
net
hef
oll
owi
ngpackage.

CREATEORREPLACEPACKAGEPKGI
S
pr
agmaser
ial
ly
_reusabl
e;
pr
ocedur
eemp_
proc;
ENDPKG;

CREATEORREPLACEPACKAGEBODYPKGI
S
pr
agmaser
ial
ly
_reusabl
e;
cur
sorci
ssel
ectenamef
rom emp;
PROCEDUREEMP_
PROCI
S
v
_enameemp.
ename%t
ype;
v
_fl
agbool
ean:
=tr
ue;
v
_numr
owsnumber:
=0;
BEGI
N
i
fnotc%i
sopent
hen
openc;
endi
f;

.
196

whi
lev
_fl
agl
oop
f
etchci
ntov
_ename;
v
_numr
ows:
=v_
numr
ows+1;
i
fv_
numr
ows=5t
hen
v
_fl
ag:
=fal
se;
endi
f;
dbms_
out
put
.put
_li
ne(
'Ename='
||v
_ename)
;
endl
oop;
ENDEMP_
PROC;
ENDPKG;

SQL>execpkg.
emp_
proc
Ename=SMI
TH

Ename=ALLEN
Ename=WARD
Ename=JONES
Ename=MARTI
N

SQL>execpkg.
emp_
proc
Ename=SMI
TH

Ename=ALLEN
Ename=WARD
Ename=JONES
Ename=MARTI
N

 Theabov
epackagedi
spl
ayst
hesameout
putf
oreachexecut
ionev
ent
hought
hecur
sori
snotcl
osed.
 Becauset
heser
ial
lyr
eusabl
ever
sionr
eset
sthest
ateoft
hecur
soreacht
imei
twascal
led.

NONSERI
ALLYREUSABLEPACKAGES

Thi
sist
hedef
aul
tver
sionusedbyt
heor
acl
e,exami
net
hef
oll
owi
ngpackage.

CREATEORREPLACEPACKAGEPKGI
S
pr
ocedur
eemp_
proc;
ENDPKG;

CREATEORREPLACEPACKAGEBODYPKGI
S

.
197

cur
sorci
ssel
ectenamef
rom emp;
PROCEDUREEMP_
PROCI
S
v
_enameemp.
ename%t
ype;
v
_fl
agbool
ean:
=tr
ue;
v
_numr
owsnumber:
=0;
BEGI
N
i
fnotc%i
sopent
hen
openc;
endi
f;
whi
lev
_fl
agl
oop
f
etchci
ntov
_ename;
v
_numr
ows:
=v_
numr
ows+1;
i
fv_
numr
ows=5t
hen
v
_fl
ag:
=fal
se;
endi
f;
dbms_
out
put
.put
_li
ne(
'Ename='
||v
_ename)
;
endl
oop;
ENDEMP_
PROC;
ENDPKG;

SQL>execpkg.
emp_
proc
Ename=SMI
TH

Ename=ALLEN
Ename=WARD
Ename=JONES
Ename=MARTI
N

SQL>execpkg.
emp_
proc

Ename=BLAKE
Ename=CLARK
Ename=SCOTT
Ename=KI
NG

Ename=TURNER

 Theabov
epackagedi
spl
ayst
hedi
ff
erentout
putf
oreachexecut
ionev
ent
hought
hecur
sori
snotcl
osed.
 Becauset
henon-
ser
ial
lyr
eusabl
ever
sionr
emai
nst
hest
ateoft
hecur
sorov
erdat
abasecal
l
s.

.
198

DEPENDENCI
ESOFPACKAGERUNTI
MESTATE

Dependenci
escanexi
stsbet
weenpackagest
ateandanony
mousbl
ocks.
Exami
net
hef
oll
owi
ngpr
ogr
am

Cr
eat
ethi
spackagei
nfi
rstsessi
on
CREATEORREPLACEPACKAGEPKGI
S
vnumber:
=5;
pr
ocedur
ep;
ENDPKG;

CREATEORREPLACEPACKAGEBODYPKGI
S
PROCEDUREPI
S
BEGI
N
dbms_
out
put
.put
_li
ne(
'v='
||v
);
v:
=10;
dbms_
out
put
.put
_li
ne(
'v='
||v
);
ENDP;
ENDPKG;

Connectt
osecondsessi
on,
runt
hef
oll
owi
ngcode.

BEGI
N
pkg.
p;
END;

Theabov
ecodewi
lwor
k.

Gobackt
ofi
rstsessi
onandr
ecr
eat
ethepackageusi
ngcr
eat
e.
Thenconnectt
osecondsessi
onandr
unt
hef
oll
owi
ngcodeagai
n.

BEGI
N
pkg.
p;
END;

Thi
sabov
ecodewi
llnotwor
kbecauseoft
hef
oll
owi
ng.

 Theanony
mousbl
ockdependsonpkg.Thi
siscompi
let
imedependency
.

.
199

 Ther
eisal
soar
unt
imedependencyont
hepackagedv
ari
abl
es,si
nceeachsessi
onhasi
tsowncopyof
packagedv
ari
abl
es.
 Thuswhenpkgi
srecompi
ledt
her
unt
imedependencyi
sfol
lowed,
whi
chi
nval
idat
est
hebl
ockandr
aises
t
heor
acl
eer
ror
.
 Runt
imedependenci
esexi
stonl
yonpackagest
ate.Thi
sincl
udesv
ari
abl
esandcur
sor
sdecl
aredi
na
package.
 I
fthepackagehadnogl
obalv
ari
abl
es,t
hesecondexecut
ionoft
heanony
mousbl
ockwoul
dhav
e
succeeded.

PURI
TYLEVELS

I
ngener
al,
cal
lst
osubpr
ogr
amsar
epr
ocedur
al,
theycannotbecal
ledf
rom SQLst
atement
s.Howev
er,
ifast
and-
al
oneorpackagedf
unct
ionmeet
scer
tai
nrest
ri
cti
ons,
itcanbecal
leddur
ingexecut
ionofaSQLst
atement
.

User
-def
inedf
unct
ionsar
ecal
ledt
hesamewayasbui
lt
-i
nfunct
ionsbuti
tmustmeetdi
ff
erentr
est
ri
cti
ons.
Theser
est
ri
cti
onsar
edef
inedi
nter
msofpur
it
ylev
els.

Ther
ear
efourt
ypesofpur
it
ylev
els.
WNDS -
- Wr
it
esNoDat
abaseSt
ate
RNDS -
- ReadsNoDat
abaseSt
ate
WNPS -
- Wr
it
esNoPackageSt
ate
RNPS -
- ReadsNoPackageSt
ate

I
naddi
ti
ont
othepr
ecedi
ngr
est
ri
cti
ons,
auser
-def
inedf
unct
ionmustal
someett
hef
oll
owi
ngr
equi
rement
stobe
cal
ledf
rom aSQLst
atement
.

 Thef
unct
ionhast
obest
oredi
nthedat
abase,
eit
herst
and-
aloneoraspar
tofa
package.
 Thef
unct
ioncant
akeonl
yinpar
amet
es.
 Thef
ormal
par
amet
ersmustuseonl
ydat
abaset
ypes,
notPL/SQLt
ypessuchas
bool
eanorr
ecor
d.
 Ther
etur
nty
peoft
hef
unct
ionmustal
sobeadat
abaset
ype.
 Thef
unct
ionmustnotendt
hecur
rentt
ransact
ionwi
thcommi
torr
oll
back,
or
r
oll
backt
oasav
epoi
ntpr
iort
othef
unct
ionexecut
ion.
 I
tal
somustnoti
ssueanyal
tersessi
onoral
tersy
stem commands.

RESTRI
CT_
REFERENCES

.
200

Forpackagedf
unct
ions,howev
er,t
heRESTRI REFERENCES p
CT_ r
agmai
srequi
redt
ospeci
fyt
hepur
it
ylev
elofa
gi
venf
unct
ion.

Sy
ntax:
PRAGMARESTRI subpr
REFERENCES(
CT_ ogr
am_
nameorpackage_
name,
WNDS[
,
WNPS]

[
,
RNDS][
,
RNPS]
);
Ex:
CREATEORREPLACEPACKAGEPKGI
S
f
unct
ionf
un1r
etur
nvar
char
;
pr
agmar
est
ri
ct_
ref
erences(
fun1,
wnds)
;
f
unct
ionf
un2r
etur
nvar
char
;
pr
agmar
est
ri
ct_
ref
erences(
fun2,
wnds)
;
ENDPKG;

CREATEORREPLACEPACKAGEBODYPKGI
S
ONFUN1r
FUNCTI etur
nvar
charI
S
BEGI
N
updat
edeptsetdept
no=11;
r
etur
n'hel
lo'
;
ENDFUN1;
ONFUN2r
FUNCTI etur
nvar
charI
S
BEGI
N
updat
edeptsetdname='
aa'
;
r
etur
n'hel
lo'
;
ENDFUN2;
ENDPKG;

Theabov
epackagebodywi
llnotcr
eat
ed,
itwi
llgi
vet
hef
oll
owi
nger
ros.

PLS-
00452:Subpr
ogr
am '
FUN1'
viol
atesi
tsassoci
atedpr
agma
PLS-
00452:Subpr
ogr
am '
FUN2'
viol
atesi
tsassoci
atedpr
agma

CREATEORREPLACEPACKAGEBODYPKGI
S
ONFUN1r
FUNCTI etur
nvar
charI
S
BEGI
N
r
etur
n'hel
lo'
;
ENDFUN1;
ONFUN2r
FUNCTI etur
nvar
charI
S

.
201

BEGI
N
r
etur
n'hel
lo'
;
ENDFUN2;
ENDPKG;

Nowt
hepackagebodywi
llbecr
eat
ed.

DEFAULT

I
fther
eisnoRESTRI REFERENCESp
CT_ r
agmaassoci
atedwi
thagi
venpackagedf
unct
ion,i
twi
llnothav
eanypur
it
y
l
evelasser
ted.Howev
er,y
oucanchanget
hedef
aul
tpur
it
ylev
elf
orapackage.TheDEFAULT key
wor
disused
i
nst
eadoft
hesubpr
ogr
am namei
nthepr
agma.

Ex:
CREATEORREPLACEPACKAGEPKGI
S
pr
agmar
est
ri
ct_
ref
erences(
def
aul
t,
wnds)
;
f
unct
ionf
un1r
etur
nvar
char
;
f
unct
ionf
un2r
etur
nvar
char
;
ENDPKG;

CREATEORREPLACEPACKAGEBODYPKGI
S
ONFUN1r
FUNCTI etur
nvar
charI
S
BEGI
N
updat
edeptsetdept
no=11;
r
etur
n'hel
lo'
;
ENDFUN1;
ONFUN2r
FUNCTI etur
nvar
charI
S
BEGI
N
updat
edeptsetdname='
aa'
;
r
etur
n'hel
lo'
;
ENDFUN2;
ENDPKG;

Theabov
epackagebodywi
l
lnotcr
eat
ed,i
twi
llgi
vet
hef
oll
owi
nger
rosbecauset
hepr
agmawi
llappl
ytoal
lthe
f
unct
ions.

PLS-
00452:Subpr
ogr
am '
FUN1'
viol
atesi
tsassoci
atedpr
agma
PLS-
00452:Subpr
ogr
am '
FUN2'
viol
atesi
tsassoci
atedpr
agma

CREATEORREPLACEPACKAGEBODYPKGI
S

.
202

ONFUN1r
FUNCTI etur
nvar
charI
S
BEGI
N
r
etur
n'hel
lo'
;
ENDFUN1;
ONFUN2r
FUNCTI etur
nvar
charI
S
BEGI
N
r
etur
n'hel
lo'
;
ENDFUN2;
ENDPKG;

Nowt
hepackagebodywi
llbecr
eat
ed.

TRUST

I
ftheTRUSTkey
wor
dispr
esent
,ther
est
ri
cti
onsl
ist
edi
nthepr
agmaar
enotenf
orced.Rat
her
,theyar
etr
ust
edt
o
bet
rue.

Ex:
CREATEORREPLACEPACKAGEPKGI
S
f
unct
ionf
un1r
etur
nvar
char
;
pr
agmar
est
ri
ct_
ref
erences(
fun1,
wnds,
tr
ust
);
f
unct
ionf
un2r
etur
nvar
char
;
pr
agmar
est
ri
ct_
ref
erences(
fun2,
wnds,
tr
ust
);
ENDPKG;

CREATEORREPLACEPACKAGEBODYPKGI
S
ONFUN1r
FUNCTI etur
nvar
charI
S
BEGI
N
updat
edeptsetdept
no=11;
r
etur
n'hel
lo'
;
ENDFUN1;
ONFUN2r
FUNCTI etur
nvar
charI
S
BEGI
N
updat
edeptsetdname='
aa'
;
r
etur
n'hel
lo'
;
ENDFUN2;
ENDPKG;

.
203

Theabov
epackagewi
llbecr
eat
edsuccessf
ull
y.

I
MPORTANTPOI
NTSABOUTRESTRI
CT_
REFERENCES

 Thi
spr
agmacanappearany
wher
eint
hepackagespeci
fi
cat
ion,
aft
ert
hef
unct
ion
decl
arat
ion.
 I
tcanappl
ytoonl
yonef
unct
iondef
ini
ti
on.
 Forov
erl
oadf
unct
ions,
thepr
agmaappl
iest
othenear
estdef
ini
ti
onpr
iort
othe
Pr
agma.
 Thi
spr
agmai
srequi
redonl
yforpackagesf
unct
ionsnotf
orst
and-
alonef
unct
ions.
 ThePr
agmacanbedecl
aredonl
yinsi
det
hepackagespeci
fi
cat
ion.
 Thepr
agmai
scheckedatcompi
let
ime,
notr
unt
ime.
 I
tispossi
blet
ospeci
fywi
thoutanypur
it
ylev
elswhent
rustorcombi
nat
ionof
def
aul
tandt
rustkey
wor
dsar
epr
esent
.

PI
NNI
NGI
NTHESHAREDPOOL

The shar
ed pooli
sthe por
ti
on oft
he SGS t
hatcont
ains,among ot
hert
hings,t
he p-
code ofcompi
led
subpr
ogr
amsast
heyar
erun.Thef
ir
stt
imeast
oredast
oresubpr
ogr
am i
scal
led,
thep-
codei
sloadedf
rom di
sk
i
ntot
heshar
edpool
.Oncet
heobj
ecti
snol
ongerr
efer
enced,
iti
sfr
eet
obeagedout
.Obj
ect
sar
eagedoutoft
he
shar
edpoolusi
nganLRU(
LeastRecent
lyUsed)al
gor
it
hm.

TheDBMS_SHARED_POOLpackageal
lowsy
out
opi
nobj
ect
sint
heshar
edpool
.Whenanobj
ecti
spi
nned,i
twi
ll
nev
erbeagedoutunt
ily
our
equesti
t,nomat
terhowf
ullt
hepoolget
sorhowof
tent
heobj
ecti
saccessed.Thi
s
cani
mpr
oveper
for
mance,
asi
ttakest
imet
orel
oadapackagef
rom di
sk.

DBMS_ POOLh
SHARED_ asf
ourpr
ocedur
es

 KEEP
 UNKEEP
 SI
ZES
 ABORTED_
REQUEST_
THRESHOLD

KEEP

TheDBMS_SHARED_POOL.
KEEPpr
ocedur
eisusedt
opi
nobj
ect
sint
hepool
.

.
204

Sy
ntax:
obj
PROCEDUREKEEP( ect
_namevar
char
2,f
lagchardef
aul
t‘P’
);

Her
ethef
lagr
epr
esent
sdi
ff
erentt
ypesoff
lagv
aluesf
ordi
ff
erentt
ypesofobj
ect
s.

P -
- Package,
funct
ionorpr
ocedur
e
Q -
- Sequence
R -
- Tr
igger
C -
- SQLCur
sor
T -
- Obj
ectt
ype
JS -
- Jav
asour
ce
JC -
- Jav
acl
ass
JR -
- Jav
aresour
ce
JD -
- Jav
ashar
eddat
a

UNKEEP

UNKEEP i
stheonl
ywayt
oremov
eakeptobj
ectf
rom t
heshar
edpool
,wi
thoutr
est
art
ingt
hedat
abase.Kept
obj
ect
sar
enev
eragedoutaut
omat
ical
ly
.

Sy
ntax:
obj
PROCEDUREUNKEEP( ect
_namevar
char
2,f
lagchardef
aul
t‘P’
);

SI
ZES

ZESwi
SI llechot
hecont
ent
soft
heshar
edpoolt
othescr
een.

Sy
ntax:
mi
ZES(
PROCEDURESI nsi
zenumber
);
Obj
ect
swi
thgr
eat
ert hemi
hant nsi
zewi
llber
etur
ned.SI
ZESusesDBMS_OUTPUTt
oret
urnt
hedat
a.

ABORTED_
REQUEST_
THRESHOLD

Whent
hedat
abasedet
ermi
nest
hatt
her
eisnotenoughmemor
yint
heshar
edpoolt
osat
isf
yagi
venr
equest
,it
wi
llbegi
nagi
ngobj
ect
soutunt
ilt
her
eisenoughmemor
y.I
tenoughobj
ect
sar
eagedout
,thi
scanhav
ea
per
for
mancei
mpactonot
herdat
abasesessi
ons.TheABORTED_REQUEST_THRESHOLDcanbeusedt
oremedyt
his.

.
205

Sy
ntax:
PROCEDUREABORTED_ t
THRESHOLD(
REQUEST_ hr
eshol
d_si
zenumber
);

Oncet
hispr
ocedur
eiscal
led,or
acl
ewi
llnotst
artagi
ngobj
ect
sfr
om t
hepoolunl eastt
essatl hreshol
d_si
ze
by
tesi
sneeded.
DATAMODELFORSUBPROGRAMSANDPACKAGES

 USER_
OBJECTS
 USER_
SOURCE
 USER_
ERRORS
 DBA_
OBJECTS
 DBA_
SOURCE
 DBA_
ERRORS
 ALL_
OBJECTS
 ALL_
SOURCE
 ALL_
ERRORS

.
206

CURSORS

Cur
sori
sapoi
ntert
omemor
ylocat
ionwhi
chi
scal
ledascont
extar
eawhi
chcont
ainst
hei
nfor
mat
ionnecessar
y
f
orpr
ocessi
ng,
incl
udi
ngt
henumberofr
owspr
ocessedbyt
hest
atement
,apoi
ntert
othepar
sedr
epr
esent
ati
on
oft
hest
atement
, heact
andt ivesetwhi
chi
sthesetofr
owsr
etur
nedbyt
hequer
y.

Cur
sorcont
ainst
wopar
ts

 Header
 Body

Headeri
ncl
udescur
sorname,
anypar
amet
ersandt
het
ypeofdat
abei
ngl
oaded.
Bodyi
ncl
udest
hesel
ectst
atement
.

Ex:
Cur
sorc(
dnoi
nnumber
)ret
urndept
%rowt
ypei
ssel
ect*
from dept
;
I
ntheabov
e
Header–cur
sorc(
dnoi
nnumber
)ret
urndept
%rowt
ype
Body–sel
ect*
from dept

CURSORTYPES

 I
mpl
ici
t(SQL)
 Expl
ici
t
 Par
amet
eri
zedcur
sor
s
 REFcur
sor
s

CURSORSTAGES

 Open
 Fet
ch
 Cl
ose

.
207

CURSORATTRI
BUTES

 %f
ound
 %not
found
 %r
owcount
 %i
sopen
 %bul
k_r
owcount
 %bul
k_except
ions

CURSORDECLERATI
ON

Sy
ntax:
sor<cur
Cur sor
_name>i
ssel
ectst
atement
;

Ex:
Cur
sorci
ssel
ect*
from dept
;

CURSORLOOPS

 Si
mpl
eloop
 Whi
lel
oop
 Forl
oop

SI
MPLELOOP

Sy
ntax:
Loop
ch<cur
Fet sor
_name>i
nto<r
ecor
d_var
iabl
e>;
twhen<cur
Exi sor
_name>%not
found;
<st
atement
s>;
Endl
oop;

Ex:
DECLARE
cur
sorci
ssel
ect*f
rom st
udent
;

.
208

v
_st
udst
udent
%rowt
ype;
BEGI
N
openc;
l
oop
f
etchci
ntov
_st
ud;
exi
twhenc%not
found;
dbms_
out
put
.put
_li
ne(
'Name='
||v
_st
ud.
name)
;
endl
oop;
cl
osec;
END;

Out
put
:
Name=saket
h
Name=sr
inu
Name=sat
ish
Name=sudha

WHI
LELOOP

Sy
ntax:
Whi
le<cur
sor
_name>%f
oundl
oop
ch<cur
Fet sor
_name>nt
o<r
ecor
d_var
iabl
e>;
<st
atement
s>;
Endl
oop;

Ex:
DECLARE
cur
sorci
ssel
ect*f
rom st
udent
;
v
_st
udst
udent
%rowt
ype;
BEGI
N
openc;
f
etchci
ntov
_st
ud;
whi
lec%f
oundl
oop
f
etchci
ntov
_st
ud;
dbms_
out
put
.put
_li
ne(
'Name='
||v
_st
ud.
name)
;
endl
oop;
cl
osec;
END;

.
209

Out
put
:

Name=saket
h
Name=sr
inu
Name=sat
ish
Name=sudha

FORLOOP

Sy
ntax:
or<r
f ecor
d_var
iabl
e>i
n<cur
sor
_name>l
oop
<st
atement
s>;
Endl
oop;

Ex:
DECLARE
cur
sorci
ssel
ect*f
rom st
udent
;
BEGI
N
f
orv
_st
udi
ncl
oop
dbms_
out
put
.put
_li
ne(
'Name='
||v
_st
ud.
name)
;
endl
oop;
END;

Out
put
:

Name=saket
h
Name=sr
inu
Name=sat
ish
Name=sudha

PARAMETARI
ZEDCURSORS

 Thi
swasusedwheny
ouar
egoi
ngt
ouset
hecur
sori
nmor
ethanonepl
acewi
thdi
ff
erentv
aluesf
ort
he
samewher
ecl
ause.
 Cur
sorpar
amet
ersmustbei
nmode.
 Cur
sorpar
amet
ersmayhav
edef
aul
tval
ues.
 Thescopeofcur
sorpar
amet
eri
swi
thi
nthesel
ectst
atement
.

.
210

Ex:
DECLARE
cur
sorc(
dnoi
nnumber
)issel
ect*f
rom deptwher
edept
no=dno;
v
_deptdept
%rowt
ype;
BEGI
N
openc(
20)
;
l
oop
f
etchci
ntov
_dept
;
exi
twhenc%not
found;
dbms_
out
put
.put
_li
ne(
'Dname='
||v
_dept
.dname|
|'Loc='
||v
_dept
.l
oc)
;
endl
oop;
cl
osec;
END;

Out
put
:

Dname=RESEARCHLoc=DALLAS

PACKAGEDCURSORSWI
THHEADERI
NSPECANDBODYI
NPACKAGEBODY

 cur
sor
sdecl
aredi
npackageswi
llnotcl
oseaut
omat
ical
ly
.
 I
npackagedcur
sor
syoucanmodi
fyt
hesel
ectst
atementwi
thoutmaki
nganychangest
othecur
sor
headeri
nthepackagespeci
fi
cat
ion.
 Packagedcur
sor
swi
thmustbedef
inedi
nthepackagebodyi
tsel
f,andt
henusei
tasgl
obalf
ort
he
package.
 Youcannotdef
inet
hepackagedcur
sori
nanysubpr
ogr
ams.
 Cur
sordecl
arat
ioni
npackagewi
thoutbodyneedst
her
etur
ncl
ause.

Ex1:
CREATEORREPLACEPACKAGEPKGI
S
cur
sorcr
etur
ndept
%rowt
ypei
ssel
ect*f
rom dept
;
pr
ocedur
epr
oci
s
ENDPKG;

CREATEORREPLACEPAKCAGEBODYPKGI
S
cur
sorcr
etur
ndept
%rowt
ypei
ssel
ect*f
rom dept
;
PROCEDUREPROCI
S

.
211

BEGI
N
f
orvi
ncl
oop
dbms_
out
put
.put
_li
ne(
'Dept
no='
||v
.dept
no|
|'Dname='
||
v
.dname|
|'Loc='
||v
.loc)
;
endl
oop;
ENDPROC;
ENDPKG;

Out
put
:
SQL>execpkg.
proc
Dept
no=10Dname=ACCOUNTI
NGLoc=NEW YORK
Dept
no=20Dname=RESEARCHLoc=DALLAS
Dept
no=30Dname=SALESLoc=CHI
CAGO

Dept
no=40Dname=OPERATI
ONSLoc=BOSTON

Ex2:
CREATEORREPLACEPAKCAGEBODYPKGI
S
cur
sorcr
etur
ndept
%rowt
ypei
ssel
ect*f
rom deptwher
edept
no>20;
PROCEDUREPROCI
S
BEGI
N
f
orvi
ncl
oop
dbms_
out
put
.put
_li
ne(
'Dept
no='
||v
.dept
no|
|'Dname='
||
v
.dname|
|'Loc='
||v
.loc)
;
endl
oop;
ENDPROC;
ENDPKG;

Out
put
:

SQL>execpkg.
proc
Dept
no=30Dname=SALESLoc=CHI
CAGO

Dept
no=40Dname=OPERATI
ONSLoc=BOSTON

REFCURSORSANDCURSORVARI
ABLES

 Thi
sisunconst
rai
nedcur
sorwhi
chwi
llr
etur
ndi
ff
erentt
ypesdependsupont
heuseri
nput
.

.
212

 Refcur
sor
scannotbecl
osedi
mpl
ici
tl
y.
 Refcur
sorwi
thr
etur
nty
pei
scal
ledst
rongcur
sor
.
 Refcur
sorwi
thoutr
etur
nty
pei
scal
ledweakcur
sor
.
 Youcandecl
arer
efcur
sort
ypei
npackagespecaswel
lasbody
.
 Youcandecl
arer
efcur
sort
ypesi
nlocalsubpr
ogr
amsoranony
mousbl
ocks.
 Cur
sorv
ari
abl
escanbeassi
gnedf
rom onet
oanot
her
.
 Youcandecl
areacur
sorv
ari
abl
einonescopeandassi
gnanot
hercur
sorv
ari
abl
ewi
thdi
ff
erentscope,
t
heny
oucanuset
hecur
sorv
ari
abl
eev
ent
hought
heassi
gnedcur
sorv
ari
abl
egoesoutofscope.
 Cur
sorv
ari
abl
escanbepassedasapar
amet
erst
othesubpr
ogr
ams.
 Cur
sorv
ari
abl
esmodesar
einoroutori
nout
.
 Cur
sorv
ari
abl
escannotbedecl
aredi
npackagespecandpackagebody(
excl
udi
ngsubpr
ogr
ams)
.
 Youcannotuserr
emot
epr
ocedur
ecal
lst
opasscur
sorv
ari
abl
esf
rom oneser
vert
oanot
her
.
 Cur
sorv
ari
abl
escannotusef
orupdat
ecl
ause.
 Youcannotassi
gnnul
lst
ocur
sorv
ari
abl
es.
 Youcannotcompar
ecur
sorv
ari
abl
esf
orequal
it
y,i
nequal
it
yandnul
li
ty.

Ex:
CREATEORREPLACEPROCEDUREREF_
CURSOR(
TABLE_
NAMEI
NVARCHAR)I
S
t
ypeti
srefcur
sor
;
ct
;
v
_deptdept
%rowt
ype;
t
yperi
srecor
d(enameemp.
ename%t
ype,
j
obemp.
job%t
ype,
salemp.
sal
%ty
pe)
;
v
_empr
;
v
_st
udst
udent
.name%t
ype;
BEGI
N
i
ftabl
e_name=' t
DEPT'hen
opencf
orsel
ect*f
rom dept
;
el
sift
abl
e_name=' t
EMP'hen
opencf
orsel
ectename,
j
ob,
salf
rom emp;
el
sift
abl
e_name=' t
STUDENT'hen
opencf
orsel
ectnamef
rom st
udent
;
endi
f;
l
oop
i
ftabl
e_name=' t
DEPT'hen
f
etchci
ntov
_dept
;
exi
twhenc%not
found;
dbms_
out
put
.put
_li
ne(
'Dept
no='
||v
_dept
.dept
no|
|'Dname='
||

.
213

v
_dept
.dname |
|'Loc='
||v
_dept
.l
oc)
;
el
sift
abl
e_name=' t
EMP'hen
f
etchci
ntov
_emp;
exi
twhenc%not
found;
dbms_
out
put
.put
_li
ne(
'Ename='
||v
_emp.
ename|
|'Job='
||v
_emp.
job
|
|'Sal='
||v
_emp.
sal
);
el
sift
abl
e_name=' t
STUDENT'hen
f
etchci
ntov
_st
ud;
exi
twhenc%not
found;
dbms_
out
put
.put
_li
ne(
'Name='
||v
_st
ud)
;
endi
f;
endl
oop;
cl
osec;
END;

Out
put
:
SQL>execr
ef_
cur
sor
(' )
DEPT'

Dept
no=10Dname=ACCOUNTI
NGLoc=NEW YORK
Dept
no=20Dname=RESEARCHLoc=DALLAS
Dept
no=30Dname=SALESLoc=CHI
CAGO

Dept
no=40Dname=OPERATI
ONSLoc=BOSTON

SQL>execr
ef_
cur
sor
(' )
EMP'

Ename=SMI
TH Job=CLERKSal=800
Ename=ALLENJob=SALESMANSal=1600
Ename=WARDJob=SALESMANSal=1250
Ename=JONESJob=MANAGERSal=2975
Ename=MARTI
NJob=SALESMANSal=1250
Ename=BLAKEJob=MANAGERSal=2850
Ename=CLARKJob=MANAGERSal=2450
Ename=SCOTTJob=ANALYSTSal=3000
Ename=KI
NGJob=PRESI
DENTSal=5000
Ename=TURNERJob=SALESMANSal=1500
Ename=ADAMSJob=CLERKSal=1100
Ename=JAMESJob=CLERKSal=950

.
214

Ename=FORDJob=ANALYSTSal=3000
Ename=MI
LLERJob=CLERKSal=1300

SQL>execr
ef_
cur
sor
(' )
STUDENT'

Name=saket
h
Name=sr
inu
Name=sat
ish
Name=sudha

CURSOREXPRESSI
ONS

 Youcanusecur
sorexpr
essi
onsi
nexpl
ici
tcur
sor
s.
 Youcanusecur
sorexpr
essi
onsi
ndy
nami
cSQL.
 Youcanusecur
sorexpr
essi
onsi
nREFcur
sordecl
arat
ionsandv
ari
abl
es.
 Youcannotusecur
sorexpr
essi
onsi
nimpl
ici
tcur
sor
s.
 Or
acl
eopenst
henest
edcur
sordef
inedbyacur
sorexpr
essi
oni
mpl
ici
tl
yassoonasi
tfet
chest
hedat
a
cont
aini
ngt
hecur
sorexpr
essi
onf
rom t
hepar
entorout
ercur
sor
.
 Nest
edcur
sorcl
osesi
fyoucl
oseexpl
ici
tl
y.
 Nest
edcur
sorcl
oseswhenev
ert
heout
erorpar
entcur
sori
sexecut
edagai
norcl
osedorcancel
ed.
 Nest
edcur
sorcl
oseswhenev
eranexcept
ioni
srai
sedwhi
lef
etchi
ngdat
afr
om apar
entcur
sor
.
 Cur
sorexpr
essi
onscannotbeusedwhendecl
ari
ngav
iew.
 Cur
sorexpr
essi
onscanbeusedasanar
gumentt
otabl
efunct
ion.
 You can notper
for
m bi
nd and execut
e oper
ati
ons on cur
sorexpr
essi
ons when usi
ng t
he cur
sor
expr
essi
onsi
ndy
nami
cSQL.

USI
NGNESTEDCURSORSORCURSOREXPRESSI
ONS

Ex:
DECLARE
cur
sorci
ssel
ectename,
cur
sor
(sel
ectdnamef
rom deptdwher
ee.
empno=d.
dept
no)f
rom empe;
t
ypeti
srefcur
sor
;
c1t
;
c2t
;
v
1emp.
ename%t
ype;
v
2dept
.dname%t
ype;
BEGI
N

.
215

openc;
l
oop
f
etchc1i
ntov
1;
exi
twhenc1%not
found;
f
etchc2i
ntov
2;
exi
twhenc2%not
found;
dbms_
out
put
.put
_li
ne(
'Ename='
||v
1||'
Dname='
||v
2);
endl
oop;
endl
oop;
cl
osec;
END;

CURSORCLAUSES

 Ret
urn
 Forupdat
e
 Wher
ecur
rentof
 Bul
kcol
lect

RETURN

Cur
sorcr
etur
ndept
%rowt
ypei
ssel
ect*
from dept
;
Or
Cur
sorc1i
ssel
ect*
from dept
;
Cur
sorcr
etur
nc1%r
owt
ypei
ssel
ect*
from dept
;
Or
Ty
peti
srecor
d(dept
nodept
.dept
no%t
ype,
dnamedept
.dname%t
ype)
;
Cur
sorcr
etur
nti
ssel
ectdept
no,
dnamef
rom dept
;

FORUPDATEANDWHERECURRENTOF

Nor
mal
ly
,asel
ectoper
ati
onwi
llnott
akeanyl
ocksont
her
owsbei
ngaccessed.Thi
swi
llal
lowot
hersessi
ons
connect
edt
othedat
abaset
ochanget
hedat
abei
ngsel
ect
ed.Ther
esul
tseti
sst
il
lconsi
stent
.Atopent
ime,
whent
heact
iveseti
sdet
ermi
ned,or
acl
etakesasnapshotoft
het
abl
e.Anychangest
hathav
ebeencommi
tt
ed
pr
iort
othi
spoi
ntar
eref
lect
edi
ntheact
iveset
.Anychangesmadeaf
tert
hispoi
nt,ev
eni
ftheyar
ecommi
tt
ed,

.
216

ar
enotr
efl
ect
edunl
esst
hecur
sori
sreopened,
whi
chwi
ll
eval
uat
etheact
ivesetagai
n.

Howev
er,
ift
heFORUPDATEcal
usei
spesent
,excl
usi
ver
owl
ocksar
etakenont
her
owsi
ntheact
ivesetbef
oret
he
openr
etur
ns.Thesel
ockspr
eventot
hersessi
onsf
rom changi
ngt
her
owsi
ntheact
ivesetunt
ilt
het
ransact
ion
i
scommi
tt
edorr
oll
edback.I
fanot
hersessi
onal
readyhasl
ocksont
her
owsi
ntheact
iveset
,thenSELECT…FOR
UPDATE oper
ati
onwi
llwai
tfort
hesel
ockst
ober
eleasedbyt
heot
hersessi
on.Ther
eisnot
ime-
outf
ort
his
wai
ti
ngper
iod.TheSELECT…FORUPDATEwi
llhangunt
ilt
heot
hersessi
onr
eleasest
hel
ock.Tohandl
ethi
ssi
tuat
ion,
t
heNOWAI
Tcl
ausei
sav
ail
abl
e.

Sy
ntax:
Sel
ect…f
rom …f
orupdat
eofcol
umn_
name[
wai
tn]
;

I
fthecur
sori
sdecl
aredwi
tht
heFORUPDATEcl
ause,t
heWHERECURRENTOFcl
ausecanbeusedi
nanupdat
e
ordel
etest
atement
.

Sy
ntax:
Wher
ecur
rentofcur
sor
;

Ex:
DECLARE
cur
sorci
ssel
ect*f
rom deptf
orupdat
eofdname;
BEGI
N
f
orvi
ncl
oop
updat
edeptsetdname='
aa'
wher
ecur
rentofc;
commi
t;
endl
oop;
END;

BULKCOLLECT

 Thi
sisusedf
orar
rayf
etches
 Wi
tht
hisy
oucanr
etr
iev
emul
ti
pler
owsofdat
awi
thasi
ngl
eroundt
ri
p.
 Thi
sreducest
henumberofcont
extswi
tchesbet
weent
hepl
/sqlandsqlengi
nes.
 Reducest
heov
erheadofr
etr
iev
ingdat
a.
 Youcanusebul
kcol
lecti
nbot
hdy
nami
candst
ati
csql
.
 Youcanusebul
kcol
lecti
nsel
ect
,fet
chi
ntoandr
etur
ningi
ntocl
auses.
 SQLengi
neaut
omat
ical
lyi
nit
ial
izesandext
endst
hecol
lect
ionsy
our
efer
encei
nthebul
kcol
l
ectcl
ause.

.
217

 Bul
kcol
lectoper
ati
onempt
iest
hecol
lect
ionr
efer
encedi
nthei
ntocl
ausebef
oreexecut
ingt
hequer
y.
 Youcanuset
hel
imi
tcl
auseofbul
kcol
lectt
orest
ri
ctt
henoofr
owsr
etr
iev
ed.
 Youcanf
etchi
ntomul
ti
blecol
lect
ionswi
thonecol
umneach.
 Usi
ngt
her
etur
ningcl
ausewecanr
etur
ndat
atot
heanot
hercol
lect
ion.

BULKCOLLECTI
NFETCH

Ex:
DECLARE
Ty
peti
stabl
eofdept
%rowt
ype;
ntt
;
Cur
sorci
ssel
ect*
from dept
;
BEGI
N
Openc;
Fet
chcbul
kcol
lecti
ntont
;
Cl
osec;
Forii
nnt
.f
ir
st.
.nt
.l
astl
oop
dbms_
out
put
.put
_li
ne(
'Dname='
||nt
(i
).
dname|
|'Loc='
||
nt
(i
).
loc)
;
endl
oop;
END;

Out
put
:
Dname=ACCOUNTI
NGLoc=NEW YORK
Dname=RESEARCHLoc=DALLAS
Dname=SALESLoc=CHI
CAGO

Dname=OPERATI
ONSLoc=BOSTON

BULKCOLLECTI
NSELECT

Ex:
DECLARE
Ty
peti
stabl
eofdept
%rowt
ype;
Ntt
;
BEGI
N
Sel
ect*bul
kcol
lecti
ntontf
rom dept
;
f
orii
nnt
.f
ir
st.
.nt
.l
astl
oop
dbms_
out
put
.put
_li
ne(
'Dname='
||nt
(i
).
dname|
|'Loc='
||

.
218

nt
(i
).
loc)
;
endl
oop;
END;

Out
put
:
Dname=ACCOUNTI
NGLoc=NEW YORK
Dname=RESEARCHLoc=DALLAS
Dname=SALESLoc=CHI
CAGO

Dname=OPERATI
ONSLoc=BOSTON

LI
MITI
NBULKCOLLECT

Youcanuset
hist
oli
mitt
henumberofr
owst
obef
etched.

Ex:
DECLARE
Ty
peti
stabl
eofdept
%rowt
ype;
ntt
;
Cur
sorci
ssel
ect*
from dept
;
BEGI
N
Openc;
Fet
chcbul
kcol
lecti
ntontl
imi
t2;
Cl
osec;
Forii
nnt
.f
ir
st.
.nt
.l
astl
oop
dbms_
out
put
.put
_li
ne(
'Dname='
||nt
(i
).
dname|
|'Loc='
||nt
(i
).
loc)
;
endl
oop;
END;

Out
put
:
Dname=ACCOUNTI
NGLoc=NEW YORK
Dname=RESEARCHLoc=DALLAS

MULTI
PLEFETCHESI
NINTOCLAUSE

Ex1:
DECLARE
Ty
peti
stabl
eofdept
.dname%t
ype;

.
219

ntt
;
Ty
pet
1ist
abl
eofdept
.l
oc%t
ype;
nt
1t;
Cur
sorci
ssel
ectdname,
l
ocf
rom dept
;
BEGI
N
Openc;
Fet
chcbul
kcol
lecti
ntont
,nt
1;
Cl
osec;
Forii
nnt
.f
ir
st.
.nt
.l
astl
oop
dbms_
out
put
.put
_li
ne(
'Dname='
||nt
(i
))
;
endl
oop;
Forii
nnt
1.f
ir
st.
.nt
1.l
astl
oop
dbms_
out
put
.put
_li
ne(
'Loc='
||nt
1(i
))
;
endl
oop;
END;

Out
put
:
Dname=ACCOUNTI
NG

Dname=RESEARCH
Dname=SALES
Dname=OPERATI
ONS

Loc=NEW YORK
Loc=DALLAS
Loc=CHI
CAGO

Loc=BOSTON

Ex2:
DECLARE
t
ypeti
stabl
eofdept
.dname%t
ype;
t
ypet
1ist
abl
eofdept
.l
oc%t
ype;
ntt
;
nt
1t1;
BEGI
N
Sel
ectdname,
l
ocbul
kcol
lecti
ntont
,nt
1fr
om dept
;
f
orii
nnt
.f
ir
st.
.nt
.l
astl
oop
dbms_
out
put
.put
_li
ne(
'Dname='
||nt
(i
))
;
endl
oop;

.
220

f
orii
nnt
1.f
ir
st.
.nt
1.l
astl
oop
dbms_
out
put
.put
_li
ne(
'Loc='
||nt
1(i
))
;
endl
oop;
END;

Out
put
:
Dname=ACCOUNTI
NG

Dname=RESEARCH
Dname=SALES
Dname=OPERATI
ONS

Loc=NEW YORK
Loc=DALLAS
Loc=CHI
CAGO

Loc=BOSTON

RETURNI
NGCLAUSEI
NBULKCOLLECT

Youcanuset
hist
oret
urnt
hepr
ocesseddat
atot
heouputv
ari
abl
esort
ypedv
ari
abl
es.

Ex:
DECLARE
t
ypeti
stabl
eofnumber
(2)
;
ntt:
=t(
1,2,
3,
4);
t
ypet
1ist
abl
eofv
archar
(2)
;
nt
1t1;
t
ypet
2ist
abl
eofst
udent
%rowt
ype;
nt
2t2;
BEGI
N
sel
ectnamebul
kcol
lecti
ntont
1fr
om st
udent
;
f
oral
lvi
nnt
1.f
ir
st.
.nt
1.l
ast
updat
est
udentsetno=nt
(v)wher
ename=nt
1(v
)ret
urni
ng
no,
name,
mar
ksbul
kcol
lecti
ntont
2;
f
orvi
nnt
2.f
ir
st.
.nt
2.l
astl
oop
dbms_
out
put
.put
_li
ne(
'Mar
ks='
||nt
2(v
));
endl
oop;
END;

Out
put
:

.
221

Mar
ks=100
Mar
ks=200
Mar
ks=300
Mar
ks=400

POI
NTSTOREMEMBER

 Cur
sornamecanbeupt
o30char
act
ersi
nlengt
h.
 Cur
sor
sdecl
aredi
nanony
mousbl
ocksorsubpr
ogr
amscl
osesaut
omat
ical
lywhent
hatbl
ockt
ermi
nat
es
execut
ion.
 %bul
k_r
owcountand%bul
k_except
ionscanbeusedonl
ywi
thf
oral
lconst
ruct
.
 Cur
sordecl
arat
ionsmayhav
eexpr
essi
onswi
thcol
umnal
iases.
 Theseexpr
essi
onsar
ecal
ledv
irt
ualcol
umnsorcal
cul
atedcol
umns.

.
222

SQLI
NPL/
SQL

Theonl
yst
atement
sal
loweddi
rect
lyi
npl
/sqlar
eDMLandTCL.

BI
NDI
NG

Bi
ndi
ngav
ari
abl
eist
hepr
ocessofi
dent
if
yingt
hest
oragel
ocat
ionassoci
atedwi
thani
dent
if
ieri
nthepr
ogr
am.

Ty
pesofbi
ndi
ng

 Ear
lybi
ndi
ng
 Lat
ebi
ndi
ng

 Bi
ndi
ngdur
ingt
hecompi
ledphasei
sear
lybi
ndi
ng.
 Bi
ndi
ngdur
ingt
her
unt
imephasei
slat
ebi
ndi
ng.
 I
near
lybi
ndi
ngcompi
lephasewi
llt
akel
ongerbecauseofbi
ndi
ngwor
kbutt
he
execut
ioni
sfast
er.
 I
nlat
ebi
ndi
ngi
twi
llshor
tent
hecompi
lephasebutl
engt
henst
heexecut
iont
ime.
 SQLb
PL/ ydef
aul
tusesear
lybi
ndi
ng.
 Bi
ndi
ngal
soi
nvol
veschecki
ngt
hedat
abasef
orper
missi
onst
oaccesst
heobj
ect
Ref
erenced.

DYNAMI
CSQL

 I
fyouuseDDLi
npl
/sqli
tval
idat
est
heper
missi
onsandexi
stencei
frequi
resdur
ingcompi
l
eti
mewhi
ch
makesi
nval
id.
 Wecanav
oidt
hisbyusi
ngDy
nami
cSQL.
 Dy
nami
cSQLal
lowsy
out
ocr
eat
eaSQLst
atementdy
nami
cal
lyatr
unt
ime.

.
223

Twot
echni
quesar
eav
ail
abl
eforDy
nami
cSQL.

 Nat
iveDy
nami
cSQL
 DBMS_
SQLpackage

USI
NGNATI
VEDYNAMI
CSQL

USI
NGEXECUTEI
MMEDI
ATE

Ex:
BEGI
N
Execut
eimmedi
ate‘
creat
etabl
est
udent
(nonumber
(2)
,namev
archar
(10)
)’
;
or
Execut
eimmedi
ate(
‘cr
eat
etabl
est
udent
(nonumber
(2)
,namev
archar
(10)
)’
);
END;

USI
NGEXECUTEI
MMEDI
ATEWI
THPL/
SQLVARI
ABLES

Ex:
DECLARE
vv
archar
(100)
;
BEGI
N
v:
='cr
eat
etabl
est
udent
(nonumber
(2)
,namev
archar
(10)
)'
;
execut
eimmedi
atev
;
END;

USI
NGEXECUTEI
MMEDI
ATEWI
THBI
NDVARI
ABLESANDUSI
NGCLAUSE

Ex:
DECLARE
vv
archar
(100)
;
BEGI
N
v:
='i
nser
tint
ost
udentv
alues(
:v1,
:
v2,
:
v3)
';
execut
eimmedi
atevusi
ng6,
'
f'
,600;
END;

EXECUTI
NGQUERI
ESWI
THOPENFORANDUSI
NGCLAUSE

.
224

Ex:
CREATEORREPLACEPROCEDUREP(
smar
ksi
nnumber
)IS

sv
archar
(100):
='sel
ect*
from st
udentwher
emar
ks>:
m';
t
ypeti
srefcur
sor
;
ct
;
vst
udent
%rowt
ype;
BEGI
N
opencf
orsusi
ngsmar
ks;
l
oop
f
etchci
ntov
;
exi
twhenc%not
found;
dbms_
out
put
.put
_li
ne(
'St
udentMar
ks='
||v
.mar
ks)
;
endl
oop;
cl
osec;
END;

Out
put
:
SQL>execp(
100)

St
udentMar
ks=200
St
udentMar
ks=300
St
udentMar
ks=400

QUERI
ESWI
THEXECUTEI
MMEDI
ATE

Ex:
DECLARE
d_
namedept
.dname%t
ype;
l
cdept
.l
oc%t
ype;
vv
archar
(100)
;
BEGI
N
v:
='sel
ectdnamef
rom deptwher
edept
no=10'
;
execut
eimmedi
atevi
ntod_
name;
dbms_
out
put
.put
_li
ne(
'Dname='
|
|d_
name)
;
v:
='sel
ectl
ocf
rom deptwher
edname=:
dn'
;
execut
eimmedi
atevi
ntol
cusi
ngd_
name;
dbms_
out
put
.put
_li
ne(
'Loc='
||l
c);
END;

.
225

Out
put
:
Dname=ACCOUNTI
NG
Loc=NEW YORK
VARI
ABLENAMES

Ex:
DECLARE
Mar
ksnumber
(3):
=100;
BEGI
N
Del
etest
udentwher
emar
ks=mar
ks; -
-thi
swi
lldel
eteal
lther
owsi
nthe
-
-st
udentt
abl
e
END;

Thi
scanbeav
oidedbyusi
ngt
hel
abel
edbl
ocks.

<<my
_bl
ock>>
DECLARE
Mar
ksnumber
(3):
=100;
BEGI
N
Del
etest
udentwher
emar
ks=my
_bl
ock.
mar
ks; -
-del
eter
owswhi
chhas
-
-amar
ksof100
END;

GETTI
NGDATAI
NTOPL/
SQLVARI
ABLES

Ex:
DECLARE
V1number
;
V2v
archar
(2)
;
BEGI
N
Sel
ectno,
namei
ntov
1,v
2fr
om st
udentwher
emar
ks=100;
END;

DMLANDRECORDS

Ex:
CREATEORREPLACEPROCEDUREP(
srowi
nst
udent
%rowt
ype)I
S
BEGI
N

.
226

i
nser
tint
ost
udentv
aluessr
ow;
ENDP;

DECLARE
sst
udent
%rowt
ype;
BEGI
N
s.
no:
=11;
s.
name:
='aa'
;
s.
mar
ks:
=100;
p(
s);
END;

RECORDBASEDI
NSERTS

Ex:
DECLARE
sr
owst
udent
%rowt
ype;
BEGI
N
sr
ow.
no:
=7;
sr
ow.
name:
='cc'
;
sr
ow.
mar
ks:
=500;
i
nser
tint
ost
udentv
aluessr
ow;
END;

RECORDBASEDUPDATES

Ex:
DECLARE
sr
owst
udent
%rowt
ype;
BEGI
N
sr
ow.
no:
=6;
sr
ow.
name:
='cc'
;
sr
ow.
mar
ks:
=500;
updat
est
udentsetr
ow=sr
owwher
eno=sr
ow.
no;
END;

USI
NGRECORDSWI
THRETURNI
NGCLAUSE

Ex:
DECLARE

.
227

sr
owst
udent
%rowt
ype;
sr
etur
nst
udent
%rowt
ype;
BEGI
N
sr
ow.
no:
=8;
sr
ow.
name:
='dd'
;
sr
ow.
mar
ks:
=500;
i
nser
tint
ost
udentv
aluessr
owr
etur
ningno,
name,
mar
ksi
ntosr
etur
n;
dbms_
out
put
.put
_li
ne(
'No='
||sr
etur
n.no)
;
dbms_
out
put
.put
_li
ne(
'No='
||sr
etur
n.name)
;
dbms_
out
put
.put
_li
ne(
'No='
||sr
etur
n.mar
ks)
;
END;

Out
put
:
No=8
No=dd
No=500

USI
NGDBMS_
SQLPACKAGE

DBMS_
SQLi
susedt
oexecut
edy
nami
cSQLf
rom wi
thi
nPL/
SQL.Unl
ikenat
ivedy
nami
cSQL,i
tisnotbui
lt
di
rect
lyi
ntot
hel
anguage,
andt
husi
slessef
fi
cient
.TheDBMS_
SQLpackageal
lowsy
out
odi
rect
lycont
rolt
he
pr
ocessi
ngofast
atementwi
thi
nacur
sor
,wi
thoper
ati
onssuchasopeni
ngandcl
osi
ngacur
sor
,par
singa
st
atement
,bi
ndi
ngi
nputv
ari
abl
e,anddef
ini
ngout
putv
ari
abl
es.

Ex1:

DECLARE
cur
sor
_idnumber
;
f
lagnumber
;
v
_st
mtv
archar
(50)
;
BEGI
N
cur
sor
_id:
=dbms_
sql
.open_
cur
sor
;
v
_st
mt:
='cr
eat
etabl
est
ud(
snonumber
(2)
,snamev
archar
(10)
)'
;
dbms_
sql
.par
se(
cur
sor
_id,
v_st
mt,
dbms_
sql
.nat
ive)
;
f
lag:
=dbms_
sql
.execut
e(cur
sor
_id)
;
dbms_
sql
.cl
ose_
cur
sor
(cur
sor
_id)
;
dbms_
out
put
.put
_li
ne(
'Tabl
ecr
eat
ed'
);
END;

.
228

Out
put
:

Tabl
ecr
eat
ed

SQL>descst
ud
Name Nul
l? Ty
pe
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
---
--
--
--
---
--
--
--
SNO NUMBER(
2)
SNAME VARCHAR2(
10)

Ex2:

CREATEORREPLACEPROCEDUREDBMS_ PROC(
SQL_ v1st
udent
.no%t
ype,
v
2st
udent
.mar
ks%t
ype)i
s
cur
sor
_idnumber
;
f
lagnumber
;
v
_updat
evar
char
(50)
;
BEGI
N
cur
sor
_id:
=dbms_
sql
.open_
cur
sor
;
v
_updat
e:='
updat
est
udentsetmar
ks=:
smar
kswher
eno=:
sno'
;
dbms_
sql
.par
se(
cur
sor
_id,
v_updat
e,dbms_
sql
.nat
ive)
;
dbms_
sql
.bi
nd_
var
iabl
e(cur
sor
_id,
'
:sno'
,
v1)
;
dbms_
sql
.bi
nd_
var
iabl
e(cur
sor
_id,
'
:smar
ks'
,
v2)
;
f
lag:
=dbms_
sql
.execut
e(cur
sor
_id)
;
dbms_
sql
.cl
ose_
cur
sor
(cur
sor
_id)
;
ENDDBMS_
SQL_
PROC;

Out
put
:

SQL>sel
ect*f
rom st
udent
; -
-bef
oreexecut
ion

NO NA MARKS
-
--
---
--
---
---
--
--
--
--
1a 100
2b 200
3c 300

.
229

SQL>execdbms_
sql
_pr
oc(
2,222)

SQL>sel
ect*f
rom st
udent
; -
-af
terexecut
ion

NO NA MARKS
-
--
---
--
---
---
--
--
--
--
1a 100
2b 222
3c 300

FORALLSTATEMENT

Thi
s canbeusedt
ogett
hedat
afr
om t
hedat
abaseatoncebyr
educt
ingt
henumberofcont
extswi
tches
whi
chi
sat
ransf
erofcont
rolbet
weenPL/SQLandSQLengi
ne.

Sy
ntax:
For
alli
ndex_
vari
n
[Lower
_bound.
.upper
_bound|
I cesofi
ndi ndexi
ng_
col
lect
ion|
uesofi
Val ndexi
ng_
col
lect
ion]
SQLst
atement
;

FORALLWI
THNON-
SEQUENTI
ALARRAYS

Ex:
DECLARE
t
ypeti
stabl
eofst
udent
.no%t
ypei
ndexbybi
nar
y_i
nteger
;
i
btt
;
BEGI
N
i
bt(
1):
=1;
i
bt(
10):
=2;
f
oral
lii
nibt
.f
ir
st.
.i
bt.
last
updat
est
udentsetmar
ks=900wher
eno=i
bt(
i)
;
END;

Theabov
epr
ogr
am wi
llgi
veer
rorl
ike‘
elementati
ndex[
2]doesnotexi
sts.

.
230

Youcanr
ect
if
yiti
noneoft
het
wof
oll
owi
ngway
s.

USGAGEOFI
NDI
CESOFTOAVOI
DTHEABOVEBEHAVI
OUR

Thi
swi
llbeusedwheny
ouhav
eacol
lect
ionwhosedef
inedr
owsspeci
fywhi
chr
owsi
nthebi
ndi
ngar
rayy
ou
woul
dli
ket
opr
ocessed.

Ex:
DECLARE
t
ypeti
stabl
eofst
udent
.no%t
ypei
ndexbybi
nar
y_i
nteger
;
i
btt
;
t
ypet
1ist
abl
eofbool
eani
ndexbybi
nar
y_i
nteger
;
i
bt1t
1;
BEGI
N
i
bt(
1):
=1;
i
bt(
10):
=2;
i
bt(
100):
=3;
i
bt1(
1):
=tr
ue;
i
bt1(
10):
=tr
ue;
i
bt1(
100):
=tr
ue;
f
oral
lii
nindi
cesofi
bt1
updat
est
udentsetmar
ks=900wher
eno=i
bt(
i)
;
END;

Ouput
:

SQL>sel
ect*f
rom st
udent-
-bef
oreexecut
ion

NO NA MARKS
-
--
--
--
--
---
--
--
--
--
--
1 a 100
2 b 200
3 c 300
SQL>sel
ect*f
rom st
udent-
-af
terexecut
ion

NO NA MARKS
-
--
--
--
--
---
--
--
--
--
--
1 a 900

.
231

2 b 900
3 c 900

USGAGEOFVALUESOFTOAVOI
DTHEABOVEBEHAVI
OUR

Thi
swi
llbeusedwheny
ouhav
eacol
lect
ionofi
nteger
swhosecont
enti
dent
if
iest
heposi
ti
oni
nthebi
ndi
ng
ar
rayt
haty
ouwantt
obepr
ocessedbyt
heFORALLst
atement
.

Ex:
DECLARE
t
ypeti
stabl
eofst
udent
.no%t
ypei
ndexbybi
nar
y_i
nteger
;
i
btt
;
t
ypet
1ist
abl
eofpl
s_i
ntegeri
ndexbybi
nar
y_i
nteger
;
i
bt1t
1;
BEGI
N
i
bt(
1):
=1;
i
bt(
10):
=2;
i
bt(
100):
=3;
i
bt1(
11):
=1;
i
bt1(
15):
=10;
i
bt1(
18):
=100;
f
oral
lii
nval
uesofi
bt1
updat
est
udentsetmar
ks=567wher
eno=i
bt(
i)
;
END;

Ouput
:

SQL>sel
ect*f
rom st
udent-
-bef
oreexecut
ion

NO NA MARKS
-
--
--
--
--
---
--
--
--
--
--
1 a 100
2 b 200
3 c 300

SQL>sel
ect*f
rom st
udent-
-af
terexecut
ion

NO NA MARKS

.
232

-
--
--
--
--
---
--
--
--
--
--
1 a 900
2 b 900
3 c 900

POI
NTSABOUTBULKBI
NDS

 Passi
ngt
heent
ir
ePL/SQLt
abl
etot
heSQLengi
nei
nonest
epi
sknownasbul
kbi
nd.
 Bul
kbi
ndsar
edoneusi
ngt
hef
oral
lst
atement
.
 I
fther
eisaner
rorpr
ocessi
ngoneoft
her
owsi
nbul
kDMLoper
ati
on,
onl
ythatr
owi
srol
ledback.

POI
NTSABOUTRETURI
NGCLAUSE

 Thi
swi
llbeusedonl
ywi
thDMLst
atement
stor
etur
ndat
aint
oPL/SQLv
ari
abl
es.
 Thi
swi
llbeusef
uli
nsi
tuat
ionsl
ike,whenper
for
mingi
nser
torupdat
eordel
etei
fyouwantt
oknowt
he
dat
aoft
het
abl
ewhi
chhasbeenef
fect
edbyt
heDML.
 Wi
thoutgoi
ngf
oranot
herSELECTusi
ngRETURNI
NG cl
ausewewi
llgett
hedat
awhi
chwi
llav
oidacal
lto
RDBMSker
nel
.

COLLECTI
ONS

Col
lect
ionsar
eal
socomposi
tet
ypes,i
nthatt
heyal
low y
out
otr
eatsev
eralv
ari
abl
esasauni
t.Acol
lect
ion
combi
nesv
ari
abl
esoft
hesamet
ype.

TYPES
 Var
ray
s
 Nest
edt
abl
es
 I
ndex-byt
abl
es(
Associ
atear
ray
s)

.
233

VARRAYS

Av
arr
ayi
sdat
aty
pev
erysi
mil
art
oanar
ray
.A v
arr
ayhasaf
ixedl
imi
toni
tssi
ze,speci
fi
edaspar
toft
he
decl
arat
ion.El
ement
sar
einser
tedi
ntov
arr
ayst
art
ingati
ndex1,upt
omaxi
mum l
ent
hdecl
aredi
nthev
arr
ay
t
ype.Themaxi
mum si
zeoft
hev
arr
ayi
s2gi
gaby
tes.

Sy
ntax:
pe<t
Ty ype_
name>i
svar
ray|v
ary
ingar
r <l
ay( i
mit
>)of<el
ement
_ty
pe>;

Ex1:
DECLARE
t
ypeti
svar
ray
(10)ofv
archar
(2)
;
v
at:
=t(
'a'
,
'b'
,
'c'
,
'd'
);
f
lagbool
ean;
BEGI
N
dbms_
out
put
.put
_li
ne(
'Li
mit='
||v
a.l
imi
t)
;
dbms_
out
put
.put
_li
ne(
'Count='
||v
a.count
);
dbms_
out
put
.put
_li
ne(
'Fi
rstI
ndex='
||v
a.f
ir
st)
;
dbms_
out
put
.put
_li
ne(
'LastI
ndex='
||v
a.l
ast
);
dbms_
out
put
.put
_li
ne(
'NextI
ndex='
||v
a.next
(2)
);
dbms_
out
put
.put
_li
ne(
'Pr
evi
ousI
ndex='
||v
a.pr
ior
(3)
);
dbms_
out
put
.put
_li
ne(
' )
VARRAYELEMENTS';
f
orii
nva.
fi
rst
..
va.
lastl
oop
dbms_
out
put
.put
_li
ne(
'va[
'|
|i|
|'
]='
||v
a(i
))
;
endl
oop;
f
lag:
=va.
exi
sts(
3);
i
ffl
ag=t
ruet
hen
dbms_
out
put
.put
_li
ne(
'I
ndex3exi
stswi
thanel
ement'
||v
a(3)
);
el
se
dbms_
out
put
.put
_li
ne(
'I
ndex3doesnotexi
sts'
);
endi
f;
v
a.ext
end;
dbms_
out
put
.put
_li
ne(
'Af
terext
endofonei
ndex,
Count='
||v
a.count
);
f
lag:
=va.
exi
sts(
5);
i
ffl
ag=t
ruet
hen
dbms_
out
put
.put
_li
ne(
'I
ndex5exi
stswi
thanel
ement'
||v
a(5)
);

.
234

el
se
dbms_
out
put
.put
_li
ne(
'I
ndex5doesnotexi
sts'
);
endi
f;
f
lag:
=va.
exi
sts(
6);
i
ffl
ag=t
ruet
hen
dbms_
out
put
.put
_li
ne(
'I
ndex6exi
stswi
thanel
ement'
||v
a(6)
);
el
se
dbms_
out
put
.put
_li
ne(
'I
ndex6doesnotexi
sts'
);
endi
f;
v
a.ext
end(
2);
dbms_
out
put
.put
_li
ne(
'Af
terext
endoft
woi
ndexes,
Count='
||v
a.count
);
dbms_
out
put
.put
_li
ne(
' )
VARRAYELEMENTS';
f
orii
nva.
fi
rst
..
va.
lastl
oop
dbms_
out
put
.put
_li
ne(
'va[
'|
|i|
|'
]='
||v
a(i
))
;
endl
oop;
v
a(5):
='e'
;
v
a(6):
='f
';
v
a(7):
='g'
;
dbms_
out
put
.put
_li
ne(
'
AFTERASSI NGVALUESTOEXTENDEDELEMENTS,
NGNI

VARRAYELEMENTS'
);
f
orii
nva.
fi
rst
..
va.
lastl
oop
dbms_
out
put
.put
_li
ne(
'va[
'|
|i|
|'
]='
||v
a(i
))
;
endl
oop;
v
a.ext
end(
3,2)
;
dbms_
out
put
.put
_li
ne(
'Af
terext
endoft
hreei
ndexes,
Count='
||v
a.count
);
dbms_
out
put
.put
_li
ne(
' )
VARRAYELEMENTS';
f
orii
nva.
fi
rst
..
va.
lastl
oop
dbms_
out
put
.put
_li
ne(
'va[
'|
|i|
|'
]='
||v
a(i
))
;
endl
oop;
v
a.t
ri
m;
dbms_
out
put
.put
_li
ne(
'Af
tert
ri
m ofonei
ndex,
Count='
||v
a.count
);
v
a.t
ri
m(3)
;
dbms_
out
put
.put
_li
ne(
'Af
tert
ri
m oft
hreei
ndexs,
Count='
||v
a.count
);
dbms_
out
put
.put
_li
ne(
'
AFTERTRI
M, )
VARRAYELEMENTS';
f
orii
nva.
fi
rst
..
va.
lastl
oop
dbms_
out
put
.put
_li
ne(
'va[
'|
|i|
|'
]='
||v
a(i
))
;
endl
oop;

.
235

v
a.del
ete;
dbms_
out
put
.put
_li
ne(
'Af
terdel
eteofent
ir
evar
ray
,Count='
||v
a.count
);
END;

Out
put
:
Li
mit=10
Count=4
Fi
rstI
ndex=1
LastI
ndex=4
NextI
ndex=3
Pr
evi
ousI
ndex=2
VARRAYELEMENTS
v
a[1]=a
v
a[2]=b
v
a[3]=c
v
a[4]=d
I
ndex3exi
stswi
thanel
ementc
Af
terext
endofonei
ndex,
Count=5
I
ndex5exi
stswi
thanel
ement
I
ndex6doesnotexi
sts
Af
terext
endoft
woi
ndexes,
Count=7
VARRAYELEMENTS
v
a[1]=a
v
a[2]=b
v
a[3]=c
v
a[4]=d
v
a[5]=
v
a[6]=
v
a[7]=
AFTERASSI
NGNI
NGVALUESTOEXTENDEDELEMENTS,
VARRAYELEMENTS
v
a[1]=a
v
a[2]=b
v
a[3]=c
v
a[4]=d
v
a[5]=e
v
a[6]=f
v
a[7]=g

.
236

Af
terext
endoft
hreei
ndexes,
Count=10
VARRAYELEMENTS
v
a[1]=a
v
a[2]=b
v
a[3]=c
v
a[4]=d
v
a[5]=e
v
a[6]=f
v
a[7]=g
v
a[8]=b
v
a[9]=b
v
a[10]=b
Af
tert
ri
m ofonei
ndex,
Count=9
Af
tert
ri
m oft
hreei
ndexs,
Count=6
AFTERTRI
M,VARRAYELEMENTS
v
a[1]=a
v
a[2]=b
v
a[3]=c
v
a[4]=d
v
a[5]=e
v
a[6]=f
Af
terdel
eteofent
ir
evar
ray
,Count=0

Ex2:
DECLARE
t
ypeti
svar
ray
(4)ofst
udent
%rowt
ype;
v
at:
=t(
nul
l,
nul
l,
nul
l,
nul
l
);
BEGI
N
f
orii
n1.
.va.
countl
oop
sel
ect*i
ntov
a(i
)fr
om st
udentwher
esno=i
;
dbms_
out
put
.put
_li
ne(
'Sno='
||v
a(i
).
sno|
|'Sname='
||v
a(i
).
sname)
;
endl
oop;
END;

Out
put
:
Sno=1Sname=saket
h
Sno=2Sname=sr
inu

.
237

Sno=3Sname=di
vya
Sno=4Sname=manogni

Ex3:
DECLARE
t
ypeti
svar
ray
(4)ofst
udent
.smar
ks%t
ype;
v
at:
=t(
nul
l,
nul
l,
nul
l,
nul
l
);
BEGI
N
f
orii
n1.
.va.
countl
oop
sel
ectsmar
ksi
ntov
a(i
)fr
om st
udentwher
esno=i
;
dbms_
out
put
.put
_li
ne(
'Smar
ks='
||v
a(i
))
;
endl
oop;
END;

Out
put
:
Smar
ks=100
Smar
ks=200
Smar
ks=300
Smar
ks=400

Ex4:
DECLARE
t
yperi
srecor
d(c1st
udent
.sname%t
ype,
c2st
udent
.smar
ks%t
ype)
;
t
ypeti
svar
ray
(4)ofr
;
v
at:
=t(
nul
l,
nul
l,
nul
l,
nul
l
);
BEGI
N
f
orii
n1.
.va.
countl
oop
sel
ectsname,
smar
ksi
ntov
a(i
)fr
om st
udentwher
esno=i
;
dbms_
out
put
.put
_li
ne(
'Sname='
||v
a(i
).
c1|
|'Smar
ks='
||v
a(i
).
c2)
;
endl
oop;
END;

Out
put
:
Sname=saket
hSmar
ks=100
Sname=sr
inuSmar
ks=200
Sname=di
vyaSmar
ks=300
Sname=manogniSmar
ks=400

.
238

Ex5:
DECLARE
t
ypeti
svar
ray
(1)ofaddr
;
v
at:
=t(
nul
l)
;
cur
sorci
ssel
ect*f
rom empl
oy;
inumber:
=1;
BEGI
N
f
orvi
ncl
oop
sel
ectaddr
essi
ntov
a(i
)fr
om empl
oywher
eename=v
.ename;
dbms_
out
put
.put
_li
ne(
'Hno='
||v
a(i
).
hno|
|'Ci
ty='
||v
a(i
).
cit
y);
endl
oop;
END;

Out
put
:
Hno=11Ci
ty=hy
d
Hno=22Ci
ty=bang
Hno=33Ci
ty=kochi

Ex6:
DECLARE
t
ypeti
svar
ray
(5)ofv
archar
(2)
;
v
a1t
;
v
a2t:
=t(
);
BEGI
N
i
fva1i
snul
lthen
dbms_
out
put
.put
_li
ne(
'va1i
snul
l
');
el
se
dbms_
out
put
.put
_li
ne(
'va1i
snotnul
l
');
endi
f;
i
fva2i
snul
lthen
dbms_
out
put
.put
_li
ne(
'va2i
snul
l
');
el
se
dbms_
out
put
.put
_li
ne(
'va2i
snotnul
l
');
endi
f;
END;

Out
put
:

.
239

v
a1i
snul
l
v
a2i
snotnul
l

NESTEDTABLES

Anest
edt
abl
eist
houghtofadat
abaset
abl
ewhi
chhasnol
imi
toni
tssi
ze.El
ement
sar
einser
tedi
ntonest
ed
t
abl
est
art
ingati
ndex1.Themaxi
mum si
zeoft
hev
arr
ayi
s2gi
gaby
tes.

Sy
ntax:
pe<t
Ty ype_
name>i
st eof<t
abl abl
e_t
ype>;

Ex1:
DECLARE
t
ypeti
stabl
eofv
archar
(2)
;
ntt:
=t(
'a'
,
'b'
,
'c'
,
'd'
);
f
lagbool
ean;
BEGI
N
i
fnt
.l
imi
tisnul
lthen
dbms_
out
put
.put
_li
ne(
'Nol
imi
ttoNest
edTabl
es'
);
el
se
dbms_
out
put
.put
_li
ne(
'Li
mit='
||nt
.l
imi
t)
;
endi
f;
dbms_
out
put
.put
_li
ne(
'Count='
||nt
.count
);
dbms_
out
put
.put
_li
ne(
'Fi
rstI
ndex='
||nt
.f
ir
st)
;
dbms_
out
put
.put
_li
ne(
'LastI
ndex='
||nt
.l
ast
);
dbms_
out
put
.put
_li
ne(
'NextI
ndex='
||nt
.next
(2)
);
dbms_
out
put
.put
_li
ne(
'Pr
evi
ousI
ndex='
||nt
.pr
ior
(3)
);
dbms_
out
put
.put
_li
ne(
' )
NESTEDTABLEELEMENTS';
f
orii
n1.
.nt
.countl
oop
dbms_
out
put
.put
_li
ne(
'nt
['|
|i|
|'
]='
||nt
(i
))
;
endl
oop;
f
lag:
=nt
.exi
sts(
3);
i
ffl
ag=t
ruet
hen
dbms_
out
put
.put
_li
ne(
'I
ndex3exi
stswi
thanel
ement'
||nt
(3)
);
el
se
dbms_
out
put
.put
_li
ne(
'I
ndex3doesnotexi
sts'
);

.
240

endi
f;
nt
.ext
end;
dbms_
out
put
.put
_li
ne(
'Af
terext
endofonei
ndex,
Count='
||nt
.count
);
f
lag:
=nt
.exi
sts(
5);
i
ffl
ag=t
ruet
hen
dbms_
out
put
.put
_li
ne(
'I
ndex5exi
stswi
thanel
ement'
||nt
(5)
);
el
se
dbms_
out
put
.put
_li
ne(
'I
ndex5doesnotexi
sts'
);
endi
f;
f
lag:
=nt
.exi
sts(
6);
i
ffl
ag=t
ruet
hen
dbms_
out
put
.put
_li
ne(
'I
ndex6exi
stswi
thanel
ement'
||nt
(6)
);
el
se
dbms_
out
put
.put
_li
ne(
'I
ndex6doesnotexi
sts'
);
endi
f;
nt
.ext
end(
2);
dbms_
out
put
.put
_li
ne(
'Af
terext
endoft
woi
ndexes,
Count='
||nt
.count
);
dbms_
out
put
.put
_li
ne(
' )
NESTEDTABLEELEMENTS';
f
orii
n1.
.nt
.countl
oop
dbms_
out
put
.put
_li
ne(
'nt
['|
|i|
|'
]='
||nt
(i
))
;
endl
oop;
nt
(5):
='e'
;
nt
(6):
='f
';
nt
(7):
='g'
;
dbms_
out
put
.put
_li
ne(
'
AFTERASSI
NGNI
NGVALUESTOEXTENDEDELEMENTS,
NESTED
TABLEELEMENTS'
);
f
orii
n1.
.nt
.countl
oop
dbms_
out
put
.put
_li
ne(
'nt
['|
|i|
|'
]='
||nt
(i
))
;
endl
oop;
nt
.ext
end(
5,2)
;
dbms_
out
put
.put
_li
ne(
'Af
terext
endoff
ivei
ndexes,
Count='
||nt
.count
);
dbms_
out
put
.put
_li
ne(
' )
NESTEDTABLEELEMENTS';
f
orii
n1.
.nt
.countl
oop
dbms_
out
put
.put
_li
ne(
'nt
['|
|i|
|'
]='
||nt
(i
))
;
endl
oop;
nt
.t
ri
m;
dbms_
out
put
.put
_li
ne(
'Af
tert
ri
m ofonei
ndex,
Count='
||nt
.count
);

.
241

nt
.t
ri
m(3)
;
dbms_
out
put
.put
_li
ne(
'Af
tert
ri
m oft
hreei
ndexs,
Count='
||nt
.count
);
dbms_
out
put
.put
_li
ne(
'
AFTERTRI
M,NESTEDTABLEELEMENTS'
);
f
orii
n1.
.nt
.countl
oop
dbms_
out
put
.put
_li
ne(
'nt
['|
|i|
|'
]='
||nt
(i
))
;
endl
oop;
nt
.del
ete(
1);
dbms_
out
put
.put
_li
ne(
'Af
terdel
eteoff
ir
sti
ndex,
Count='
||nt
.count
);
dbms_
out
put
.put
_li
ne(
NESTEDTABLEELEMENTS'
' );
f
orii
n2.
.nt
.count
+1l
oop
dbms_
out
put
.put
_li
ne(
'nt
['|
|i|
|'
]='
||nt
(i
))
;
endl
oop;
nt
.del
ete(
4);
dbms_
out
put
.put
_li
ne(
'Af
terdel
eteoff
our
thi
ndex,
Count='
||nt
.count
);
dbms_
out
put
.put
_li
ne(
NESTEDTABLEELEMENTS'
' );
f
orii
n2.
.3l
oop
dbms_
out
put
.put
_li
ne(
'nt
['|
|i|
|'
]='
||nt
(i
))
;
endl
oop;
f
orii
n5.
.nt
.count
+2l
oop
dbms_
out
put
.put
_li
ne(
'nt
['|
|i|
|'
]='
||nt
(i
))
;
endl
oop;
nt
.del
ete;
dbms_
out
put
.put
_li
ne(
'Af
terdel
eteofent
ir
enest
edt
abl
e,Count='
||
nt
.count
);
END;

Out
put
:
Nol
imi
ttoNest
edTabl
es
Count=4
Fi
rstI
ndex=1
LastI
ndex=4
NextI
ndex=3
Pr
evi
ousI
ndex=2
NESTEDTABLEELEMENTS
nt
[1]=a
nt
[2]=b
nt
[3]=c

.
242

nt
[4]=d
I
ndex3exi
stswi
thanel
ementc
Af
terext
endofonei
ndex,
Count=5
I
ndex5exi
stswi
thanel
ement
I
ndex6doesnotexi
sts
Af
terext
endoft
woi
ndexes,
Count=7
NESTEDTABLEELEMENTS
nt
[1]=a
nt
[2]=b
nt
[3]=c
nt
[4]=d
nt
[5]=
nt
[6]=
nt
[7]=
AFTERASSI
NGNI
NGVALUESTOEXTENDEDELEMENTS,
NESTEDTABLE ELEMENTS
nt
[1]=a
nt
[2]=b
nt
[3]=c
nt
[4]=d
nt
[5]=e
nt
[6]=f
nt
[7]=g
Af
terext
endoff
ivei
ndexes,
Count=12
NESTEDTABLEELEMENTS
nt
[1]=a
nt
[2]=b
nt
[3]=c
nt
[4]=d
nt
[5]=e
nt
[6]=f
nt
[7]=g
nt
[8]=b
nt
[9]=b
nt
[10]=b
nt
[11]=b
nt
[12]=b
Af
tert
ri
m ofonei
ndex,
Count=11

.
243

Af
tert
ri
m oft
hreei
ndexs,
Count=8
AFTERTRI
M,NESTEDTABLEELEMENTS
nt
[1]=a
nt
[2]=b
nt
[3]=c
nt
[4]=d
nt
[5]=e
nt
[6]=f
nt
[7]=g
nt
[8]=b
Af
terdel
eteoff
ir
sti
ndex,
Count=7
NESTEDTABLEELEMENTS
nt
[2]=b
nt
[3]=c
nt
[4]=d
nt
[5]=e
nt
[6]=f
nt
[7]=g
nt
[8]=b
Af
terdel
eteoff
our
thi
ndex,
Count=6
NESTEDTABLEELEMENTS
nt
[2]=b
nt
[3]=c
nt
[5]=e
nt
[6]=f
nt
[7]=g
nt
[8]=b
Af
terdel
eteofent
ir
enest
edt
abl
e,Count=0

Ex2:
DECLARE
t
ypeti
stabl
eofst
udent
%rowt
ype;
ntt:
=t(
nul
l,
nul
l,
nul
l,
nul
l)
;
BEGI
N
f
orii
n1.
.nt
.countl
oop
sel
ect*i
ntont
(i
)fr
om st
udentwher
esno=i
;
dbms_
out
put
.put
_li
ne(
'Sno='
||nt
(i
).
sno|
|'Sname='
||nt
(i
).
sname)
;

.
244

endl
oop;
END;

Out
put
:
Sno=1Sname=saket
h
Sno=2Sname=sr
inu
Sno=3Sname=di
vya
Sno=4Sname=manogni

Ex3:
DECLARE
t
ypeti
stabl
eofst
udent
.smar
ks%t
ype;
ntt:
=t(
nul
l,
nul
l,
nul
l,
nul
l
);
BEGI
N
f
orii
n1.
.nt
.countl
oop
sel
ectsmar
ksi
ntont
(i
)fr
om st
udentwher
esno=i
;
dbms_
out
put
.put
_li
ne(
'Smar
ks='
||nt
(i
))
;
endl
oop;
END;

Out
put
:
Smar
ks=100
Smar
ks=200
Smar
ks=300
Smar
ks=400

Ex4:
DECLARE
t
yperi
srecor
d(c1st
udent
.sname%t
ype,
c2st
udent
.smar
ks%t
ype)
;
t
ypeti
stabl
eofr
;
ntt:
=t(
nul
l,
nul
l,
nul
l,
nul
l
);
BEGI
N
f
orii
n1.
.nt
.countl
oop
sel
ectsname,
smar
ksi
ntont
(i
)fr
om st
udentwher
esno=i
;
dbms_
out
put
.put
_li
ne(
'Sname='
||nt
(i
).
c1|
|'Smar
ks='
||nt
(i
).
c2)
;
endl
oop;
END;

.
245

Out
put
:
Sname=saket
hSmar
ks=100
Sname=sr
inuSmar
ks=200
Sname=di
vyaSmar
ks=300
Sname=manogniSmar
ks=400

Ex5:
DECLARE
t
ypeti
stabl
eofaddr
;
ntt:
=t(
nul
l)
;
cur
sorci
ssel
ect*f
rom empl
oy;
inumber:
=1;
BEGI
N
f
orvi
ncl
oop
sel
ectaddr
essi
ntont
(i
)fr
om empl
oywher
eename=v
.ename;
dbms_
out
put
.put
_li
ne(
'Hno='
||nt
(i
).
hno|
|'Ci
ty='
||nt
(i
).
cit
y);
endl
oop;
END;

Out
put
:
Hno=11Ci
ty=hy
d
Hno=22Ci
ty=bang
Hno=33Ci
ty=kochi

Ex6:
DECLARE
t
ypeti
svar
ray
(5)ofv
archar
(2)
;
nt
1t;
nt
2t:
=t(
);
BEGI
N
i
fnt
1isnul
lthen
dbms_
out
put
.put
_li
ne(
'nt
1isnul
l
');
el
se
dbms_
out
put
.put
_li
ne(
'nt
1isnotnul
l'
);
endi
f;
i
fnt
2isnul
lthen

.
246

dbms_
out
put
.put
_li
ne(
'nt
2isnul
l
');
el
se
dbms_
out
put
.put
_li
ne(
'nt
2isnotnul
l'
);
endi
f;
END;

Out
put
:
nt
1isnul
l
nt
2isnotnul
l

SETOPERATI
ONSI
NNESTEDTABLES

Youcanper
for
m setoper
ati
onsi
nthenest
edt
abl
es.Youcanal
soper
for
m equal
it
ycompar
isi
onsbet
ween
nest
edt
abl
es.

Possi
bleoper
ati
onsar
e

 UNI
ON
 UNI
ONDI
STI
NCT
 I
NTERSECT
 EXCEPT(actl
ikeMI
NUS)

Ex:
DECLARE
t
ypeti
stabl
eofv
archar
(2)
;
nt
1t:
=t(
'a'
,
'b'
,
'c'
);
nt
2t:
=t(
'c'
,
'b'
,
'a'
);
nt
3t:
=t(
'b'
,
'c'
,
'a'
,
'c'
);
nt
4t:
=t(
'a'
,
'b'
,
'd'
);
nt
5t;
BEGI
N
nt
5:=set
(nt
1);
dbms_
out
put
.put
_li
ne(
'NESTEDTABLEELEMENTS'
);
f
orii
nnt
5.f
ir
st.
.nt
5.l
astl
oop
dbms_
out
put
.put
_li
ne(
'nt
5['
||i|
|']='
||nt
5(i
))
;
endl
oop;
nt
5:=set
(nt
3);

.
247

dbms_
out
put
.put
_li
ne(
'NESTEDTABLEELEMENTS'
);
f
orii
nnt
5.f
ir
st.
.nt
5.l
astl
oop
dbms_
out
put
.put
_li
ne(
'nt
5['
||i|
|']='
||nt
5(i
))
;
endl
oop;
nt
5:=nt
1mul
ti
setuni
onnt
4;
dbms_
out
put
.put
_li
ne(
'NESTEDTABLEELEMENTS'
);
f
orii
nnt
5.f
ir
st.
.nt
5.l
astl
oop
dbms_
out
put
.put
_li
ne(
'nt
5['
||i|
|']='
||nt
5(i
))
;
endl
oop;
nt
5:=nt
1mul
ti
setuni
onnt
3;
dbms_
out
put
.put
_li
ne(
'NESTEDTABLEELEMENTS'
);
f
orii
nnt
5.f
ir
st.
.nt
5.l
astl
oop
dbms_
out
put
.put
_li
ne(
'nt
5['
||i|
|']='
||nt
5(i
))
;
endl
oop;
nt
5:=nt
1mul
ti
setuni
ondi
sti
nctnt
3;
dbms_
out
put
.put
_li
ne(
'NESTEDTABLEELEMENTS'
);
f
orii
nnt
5.f
ir
st.
.nt
5.l
astl
oop
dbms_
out
put
.put
_li
ne(
'nt
5['
||i|
|']='
||nt
5(i
))
;
endl
oop;
nt
5:=nt
1mul
ti
setexceptnt
4;
dbms_
out
put
.put
_li
ne(
'NESTEDTABLEELEMENTS'
);
f
orii
nnt
5.f
ir
st.
.nt
5.l
astl
oop
dbms_
out
put
.put
_li
ne(
'nt
5['
||i|
|']='
||nt
5(i
))
;
endl
oop;
nt
5:=nt
4mul
ti
setexceptnt
1;
dbms_
out
put
.put
_li
ne(
'NESTEDTABLEELEMENTS'
);
f
orii
nnt
5.f
ir
st.
.nt
5.l
astl
oop
dbms_
out
put
.put
_li
ne(
'nt
5['
||i|
|']='
||nt
5(i
))
;
endl
oop;
END;

Out
put
:

NESTEDTABLEELEMENTS
nt
5[1]=a
nt
5[2]=b
nt
5[3]=c

.
248

NESTEDTABLEELEMENTS
nt
5[1]=b
nt
5[2]=c
nt
5[3]=a
NESTEDTABLEELEMENTS
nt
5[1]=a
nt
5[2]=b
nt
5[3]=c
nt
5[4]=a
nt
5[5]=b
nt
5[6]=d
NESTEDTABLEELEMENTS
nt
5[1]=a
nt
5[2]=b
nt
5[3]=c
nt
5[4]=b
nt
5[5]=c
nt
5[6]=a
nt
5[7]=c
NESTEDTABLEELEMENTS
nt
5[1]=a
nt
5[2]=b
nt
5[3]=c
NESTEDTABLEELEMENTS
nt
5[1]=c
NESTEDTABLEELEMENTS
nt
5[1]=d

I
NDEX-
BYTABLES

Ani
ndex-
byt
abl
ehasnol
imi
toni
tssi
ze.El
ement
sar
einser
tedi
ntoi
ndex-
byt
abl
ewhosei
ndexmayst
artnon-
sequent
ial
lyi
ncl
udi
ngnegat
ivei
nteger
s.

Sy
ntax:
pe<t
Ty ype_
name>i
st eof<t
abl abl
e_t
ype>i
ndexbybi
nar
y_i
nteger
;

Ex:

.
249

DECLARE
t
ypeti
stabl
eofv
archar
(2)i
ndexbybi
nar
y_i
nteger
;
i
btt
;
f
lagbool
ean;
BEGI
N
i
bt(
1):
='a'
;
i
bt(
-20):
='b'
;
i
bt(
30):
='c'
;
i
bt(
100):
='d'
;
i
fibt
.l
imi
tisnul
lthen
dbms_
out
put
.put
_li
ne(
'Nol
imi
ttoI
ndexbyTabl
es'
);
el
se
dbms_
out
put
.put
_li
ne(
'Li
mit='
||i
bt.
li
mit
);
endi
f;
dbms_
out
put
.put
_li
ne(
'Count='
||i
bt.
count
);
dbms_
out
put
.put
_li
ne(
'Fi
rstI
ndex='
||i
bt.
fi
rst
);
dbms_
out
put
.put
_li
ne(
'LastI
ndex='
||i
bt.
last
);
dbms_
out
put
.put
_li
ne(
'NextI
ndex='
||i
bt.
next
(2)
);
dbms_
out
put
.put
_li
ne(
'Pr
evi
ousI
ndex='
||i
bt.
pri
or(
3))
;
dbms_
out
put
.put
_li
ne(
'
I )
NDEXBYTABLEELEMENTS';
dbms_
out
put
.put
_li
ne(
'i
bt[
-20]='
||i
bt(
-20)
);
dbms_
out
put
.put
_li
ne(
'i
bt[
1]='
||i
bt(
1))
;
dbms_
out
put
.put
_li
ne(
'i
bt[
30]='
||i
bt(
30)
);
dbms_
out
put
.put
_li
ne(
'i
bt[
100]='
||i
bt(
100)
);
f
lag:
=ibt
.exi
sts(
30)
;
i
ffl
ag=t
ruet
hen
dbms_
out
put
.put
_li
ne(
'I
ndex30exi
stswi
thanel
ement'
||i
bt(
30)
);
el
se
dbms_
out
put
.put
_li
ne(
'I
ndex30doesnotexi
sts'
);
endi
f;
f
lag:
=ibt
.exi
sts(
50)
;
i
ffl
ag=t
ruet
hen
dbms_
out
put
.put
_li
ne(
'I
ndex50exi
stswi
thanel
ement'
||i
bt(
30)
);
el
se
dbms_
out
put
.put
_li
ne(
'I
ndex50doesnotexi
sts'
);
endi
f;
i
bt.
del
ete(
1);

.
250

dbms_
out
put
.put
_li
ne(
'Af
terdel
eteoff
ir
sti
ndex,
Count='
||i
bt.
count
);
i
bt.
del
ete(
30)
;
dbms_
out
put
.put
_li
ne(
'Af
terdel
eteofi
ndext
hir
ty,
Count='
||i
bt.
count
);
dbms_
out
put
.put
_li
ne(
'
I )
NDEXBYTABLEELEMENTS';
dbms_
out
put
.put
_li
ne(
'i
bt[
-20]='
||i
bt(
-20)
);
dbms_
out
put
.put
_li
ne(
'i
bt[
100]='
||i
bt(
100)
);
i
bt.
del
ete;
dbms_
out
put
.put
_li
ne(
'Af
terdel
eteofent
ir
eindex-
byt
abl
e,Count='
||
i
bt.
count
);
END;

Out
put
:

Nol
imi
ttoI
ndexbyTabl
es
Count=4
Fi
rstI
ndex=-
20
LastI
ndex=100
NextI
ndex=30
Pr
evi
ousI
ndex=1
I
NDEXBYTABLEELEMENTS
i
bt[
-20]=b
i
bt[
1]=a
i
bt[
30]=c
i
bt[
100]=d
I
ndex30exi
stswi
thanel
ementc
I
ndex50doesnotexi
sts
Af
terdel
eteoff
ir
sti
ndex,
Count=3
Af
terdel
eteofi
ndext
hir
ty,
Count=2
I
NDEXBYTABLEELEMENTS
i
bt[
-20]=b
i
bt[
100]=d
Af
terdel
eteofent
ir
eindex-
byt
abl
e,Count=0

DI
FFERENCESAMONGCOLLECTI
ONS

 Var
ray
shasl
imi
t,nest
edt
abl
esandi
ndex-
byt
abl
eshasnol
imi
t.
 Var
ray
sandnest
edt
abl
esmustbei
nit
ial
izedbef
oreassi
gnmentofel
ement
s,i
nindex-
byt
abl
eswecan

.
251

di
rect
lyassi
gnel
ement
s.
 Var
ray
sandnest
edt
abl
esst
oredi
ndat
abase,
buti
ndex-
byt
abl
escannot
.
 Nest
edt
abl
esandi
ndex-
byt
abl
esar
ePL/SQLt
abl
es,
butv
arr
ayscannot
.
 Key
smustbeposi
ti
vei
ncaseofnest
edt
abl
esandv
arr
ays,i
ncaseofi
ndex-
byt
abl
eskey
scanbe
posi
ti
veornegat
ive.
 Ref
erenci
ngnonexi
stentel
ement
srai
sesSUBSCRI
PT_ COUNTi
BEYOND_ nbot
hnest
edt
abl
esandv
arr
ays,but
i
ncaseofi
ndex-
byt
abl
esNO_DATA_FOUNDr
aises.
 Key
sar
esequent
ial
inbot
hnest
edt
abl
esandv
arr
ays,
non-
sequent
iali
nindex-
byt
abl
es.
 I
ndi
vi
duali
ndexescanbedel
etedi
nbot
hnest
edt
abl
esandi
ndex-
byt
abl
es,
buti
nvar
ray
scannot
.
 I
ndi
vi
duali
ndexescanbet
ri
mmedi
nbot
hnest
edt
abl
esandv
arr
ays,
buti
nindex-
byt
abl
escannot
.
 I
ndi
vi
duali
ndexescanbeext
endedi
nbot
hnest
edt
abl
esandv
arr
ays,
buti
nindex-
byt
abl
escannot
.

MULTI
LEVELCOLLECTI
ONS

Col
lect
ionsofmor
ethanonedi
mensi
onwhi
chi
sacol
lect
ionofcol
lect
ions,
knownasmul
ti
lev
elcol
lect
ions.

Sy
ntax:
pe<t
Ty ype_
name1>i
st eof<t
abl abl
e_t
ype>i
ndexbybi
nar
y_i
nteger
;
pe<t
Ty ype_
name2>i
svar
ray
(<l
imi
t>)|t e|of<t
abl ype_
name1>|i
ndexby
bi
nar
y_i
nteger
;

Ex1:
DECLARE
t
ypet
1ist
abl
eofv
archar
(2)i
ndexbybi
nar
y_i
nteger
;
t
ypet
2isv
arr
ay(
5)oft
1;
v
at2:
=t2(
);
cnumber:
=97;
f
lagbool
ean;
BEGI
N
v
a.ext
end(
4);
dbms_
out
put
.put
_li
ne(
'Count='
||v
a.count
);
dbms_
out
put
.put
_li
ne(
'Li
mit='
||v
a.l
imi
t)
;
f
orii
n1.
.va.
countl
oop
f
orji
n1.
.va.
countl
oop
v
a(i
)(
j):
=chr
(c)
;
c:
=c+1;
endl
oop;

.
252

endl
oop;
dbms_
out
put
.put
_li
ne(
' )
VARRAYELEMENTS';
f
orii
n1.
.va.
countl
oop
f
orji
n1.
.va.
countl
oop
dbms_
out
put
.put
_li
ne(
'va[
'|
|i|
|'
][
'|
|j|
|'
]='
||v
a(i
)(
j)
);
endl
oop;
endl
oop;
dbms_
out
put
.put
_li
ne(
'Fi
rsti
ndex='
||v
a.f
ir
st)
;
dbms_
out
put
.put
_li
ne(
'Lasti
ndex='
||v
a.l
ast
);
dbms_
out
put
.put
_li
ne(
'Nexti
ndex='
||v
a.next
(2)
);
dbms_
out
put
.put
_li
ne(
'Pr
evi
ousi
ndex='
||v
a.pr
ior
(3)
);
f
lag:
=va.
exi
sts(
2);
i
ffl
ag=t
ruet
hen
dbms_
out
put
.put
_li
ne(
'I
ndex2exi
sts'
);
el
se
dbms_
out
put
.put
_li
ne(
'I
ndex2exi
sts'
);
endi
f;
v
a.ext
end;
v
a(1)
(5):
='q'
;
v
a(2)
(5):
='r
';
v
a(3)
(5):
='s'
;
v
a(4)
(5):
='t
';
v
a(5)
(1):
='u'
;
v
a(5)
(2):
='v
';
v
a(5)
(3):
='w'
;
v
a(5)
(4):
='x'
;
v
a(5)
(5):
='y
';
dbms_
out
put
.put
_li
ne(
'Af
terext
endofonei
ndex,
Count='
||v
a.count
);
dbms_
out
put
.put
_li
ne(
' )
VARRAYELEMENTS';
f
orii
n1.
.va.
countl
oop
f
orji
n1.
.va.
countl
oop
dbms_
out
put
.put
_li
ne(
'va[
'|
|i|
|'
][
'|
|j|
|'
]='
||v
a(i
)(
j)
);
endl
oop;
endl
oop;
v
a.t
ri
m;
dbms_
out
put
.put
_li
ne(
'Af
tert
ri
m ofonei
ndex,
Count='
||v
a.count
);
v
a.t
ri
m(2)
;

.
253

dbms_
out
put
.put
_li
ne(
'Af
tert
ri
m oft
woi
ndexes,
Count='
||v
a.count
);
dbms_
out
put
.put
_li
ne(
' )
VARRAYELEMENTS';
f
orii
n1.
.va.
countl
oop
f
orji
n1.
.va.
countl
oop
dbms_
out
put
.put
_li
ne(
'va[
'|
|i|
|'
][
'|
|j|
|'
]='
||v
a(i
)(
j)
);
endl
oop;
endl
oop;
v
a.del
ete;
dbms_
out
put
.put
_li
ne(
'Af
terdel
eteofent
ir
evar
ray
,Count='
||v
a.count
);
END;

Out
put
:

Count=4
Li
mit=5
VARRAYELEMENTS
v
a[1]
[1]=a
v
a[1]
[2]=b
v
a[1]
[3]=c
v
a[1]
[4]=d
v
a[2]
[1]=e
v
a[2]
[2]=f
v
a[2]
[3]=g
v
a[2]
[4]=h
v
a[3]
[1]=i
v
a[3]
[2]=j
v
a[3]
[3]=k
v
a[3]
[4]=l
v
a[4]
[1]=m
v
a[4]
[2]=n
v
a[4]
[3]=o
v
a[4]
[4]=p
Fi
rsti
ndex=1
Lasti
ndex=4
Nexti
ndex=3
Pr
evi
ousi
ndex=2
I
ndex2exi
sts

.
254

Af
terext
endofonei
ndex,
Count=5
VARRAYELEMENTS
v
a[1]
[1]=a
v
a[1]
[2]=b
v
a[1]
[3]=c
v
a[1]
[4]=d
v
a[1]
[5]=q
v
a[2]
[1]=e
v
a[2]
[2]=f
v
a[2]
[3]=g
v
a[2]
[4]=h
v
a[2]
[5]=r
v
a[3]
[1]=i
v
a[3]
[2]=j
v
a[3]
[3]=k
v
a[3]
[4]=l
v
a[3]
[5]=s
v
a[4]
[1]=m
v
a[4]
[2]=n
v
a[4]
[3]=o
v
a[4]
[4]=p
v
a[4]
[5]=t
v
a[5]
[1]=u
v
a[5]
[2]=v
v
a[5]
[3]=w
v
a[5]
[4]=x
v
a[5]
[5]=y
Af
tert
ri
m ofonei
ndex,
Count=4
Af
tert
ri
m oft
woi
ndexes,
Count=2
VARRAYELEMENTS
v
a[1]
[1]=a
v
a[1]
[2]=b
v
a[2]
[1]=e
v
a[2]
[2]=f
Af
terdel
eteofent
ir
evar
ray
,Count=0

Ex2:

.
255

DECLARE
t
ypet
1ist
abl
eofv
archar
(2)i
ndexbybi
nar
y_i
nteger
;
t
ypet
2ist
abl
eoft
1;
ntt
2:=t
2()
;
cnumber:
=65;
vnumber:
=1;
f
lagbool
ean;
BEGI
N
nt
.ext
end(
4);
dbms_
out
put
.put
_li
ne(
'Count='
||nt
.count
);
i
fnt
.l
imi
tisnul
lthen
dbms_
out
put
.put
_li
ne(
'Nol
imi
ttoNest
edTabl
es'
);
el
se
dbms_
out
put
.put
_li
ne(
'Li
mit='
||nt
.l
imi
t)
;
endi
f;
f
orii
n1.
.nt
.countl
oop
f
orji
n1.
.nt
.countl
oop
nt
(i
)(
j):
=chr
(c)
;
c:
=c+1;
i
fc=91t
hen
c:
=97;
endi
f;
endl
oop;
endl
oop;
dbms_
out
put
.put
_li
ne(
' )
NESTEDTABLEELEMENTS';
f
orii
n1.
.nt
.countl
oop
f
orji
n1.
.nt
.countl
oop
dbms_
out
put
.put
_li
ne(
'nt
['|
|i|
|'
][
'|
|j|
|'
]='
||nt
(i
)(
j)
);
endl
oop;
endl
oop;
dbms_
out
put
.put
_li
ne(
'Fi
rsti
ndex='
||nt
.f
ir
st)
;
dbms_
out
put
.put
_li
ne(
'Lasti
ndex='
||nt
.l
ast
);
dbms_
out
put
.put
_li
ne(
'Nexti
ndex='
||nt
.next
(2)
);
dbms_
out
put
.put
_li
ne(
'Pr
evi
ousi
ndex='
||nt
.pr
ior
(3)
);
f
lag:
=nt
.exi
sts(
2);
i
ffl
ag=t
ruet
hen
dbms_
out
put
.put
_li
ne(
'I
ndex2exi
sts'
);

.
256

el
se
dbms_
out
put
.put
_li
ne(
'I
ndex2exi
sts'
);
endi
f;
nt
.ext
end(
2);
nt
(1)
(5):
='Q'
;
nt
(1)
(6):
='R'
;
nt
(2)
(5):
='S'
;
nt
(2)
(6):
='T'
;
nt
(3)
(5):
='U'
;
nt
(3)
(6):
='V'
;
nt
(4)
(5):
='W'
;
nt
(4)
(6):
='X'
;
nt
(5)
(1):
='Y'
;
nt
(5)
(2):
='Z'
;
nt
(5)
(3):
='a'
;
nt
(5)
(4):
='b'
;
nt
(5)
(5):
='c'
;
nt
(5)
(6):
='d'
;
nt
(6)
(1):
='e'
;
nt
(6)
(2):
='f
';
nt
(6)
(3):
='g'
;
nt
(6)
(4):
='h'
;
nt
(6)
(5):
='i
'
;
nt
(6)
(6):
='j
'
;
dbms_
out
put
.put
_li
ne(
'Af
terext
endofonei
ndex,
Count='
||nt
.count
);
dbms_
out
put
.put
_li
ne(
' )
NESTEDTABLEELEMENTS';
f
orii
n1.
.nt
.countl
oop
f
orji
n1.
.nt
.countl
oop
dbms_
out
put
.put
_li
ne(
'nt
['|
|i|
|'
][
'|
|j|
|'
]='
||nt
(i
)(
j)
);
endl
oop;
endl
oop;
nt
.t
ri
m;
dbms_
out
put
.put
_li
ne(
'Af
tert
ri
m ofonei
ndexe,
Count='
||nt
.count
);
nt
.t
ri
m(2)
;
dbms_
out
put
.put
_li
ne(
'Af
tert
ri
m oft
woi
ndexes,
Count='
||nt
.count
);
dbms_
out
put
.put
_li
ne(
' )
NESTEDTABLEELEMENTS';
f
orii
n1.
.nt
.countl
oop

.
257

f
orji
n1.
.nt
.countl
oop
dbms_
out
put
.put
_li
ne(
'nt
['|
|i|
|'
][
'|
|j|
|'
]='
||nt
(i
)(
j)
);
endl
oop;
endl
oop;
nt
.del
ete(
2);
dbms_
out
put
.put
_li
ne(
'Af
terdel
eteofsecondi
ndex,
Count='
||nt
.count
);
dbms_
out
put
.put
_li
ne(
' )
NESTEDTABLEELEMENTS';
l
oop
exi
twhenv=4;
f
orji
n1.
.nt
.count
+1l
oop
dbms_
out
put
.put
_li
ne(
'nt
['|
|v|
|'
][
'|
|j|
|'
]='
||nt
(v)
(j
))
;
endl
oop;
v:
=v+1;
i
fv=2t
hen
v:
=3;
endi
f;
endl
oop;
nt
.del
ete;
dbms_
out
put
.put
_li
ne(
'Af
terdel
eteofent
ir
enest
edt
abl
e,Count='
||
nt
.count
);
END;

Out
put
:
Count=4
Nol
imi
ttoNest
edTabl
es
NESTEDTABLEELEMENTS
nt
[1]
[1]=A
nt
[1]
[2]=B
nt
[1]
[3]=C
nt
[1]
[4]=D
nt
[2]
[1]=E
nt
[2]
[2]=F
nt
[2]
[3]=G
nt
[2]
[4]=H
nt
[3]
[1]=I

.
258

nt
[3]
[2]=J
nt
[3]
[3]=K
nt
[3]
[4]=L
nt
[4]
[1]=M
nt
[4]
[2]=N
nt
[4]
[3]=O
nt
[4]
[4]=P
Fi
rsti
ndex=1
Lasti
ndex=4
Nexti
ndex=3
Pr
evi
ousi
ndex=2
I
ndex2exi
sts
Af
terext
endofonei
ndex,
Count=6
NESTEDTABLEELEMENTS
nt
[1]
[1]=A
nt
[1]
[2]=B
nt
[1]
[3]=C
nt
[1]
[4]=D
nt
[1]
[5]=Q
nt
[1]
[6]=R
nt
[2]
[1]=E
nt
[2]
[2]=F
nt
[2]
[3]=G
nt
[2]
[4]=H
nt
[2]
[5]=S
nt
[2]
[6]=T
nt
[3]
[1]=I
nt
[3]
[2]=J
nt
[3]
[3]=K
nt
[3]
[4]=L
nt
[3]
[5]=U
nt
[3]
[6]=V
nt
[4]
[1]=M
nt
[4]
[2]=N
nt
[4]
[3]=O
nt
[4]
[4]=P
nt
[4]
[5]=W

.
259

nt
[4]
[6]=X
nt
[5]
[1]=Y
nt
[5]
[2]=Z
nt
[5]
[3]=a
nt
[5]
[4]=b
nt
[5]
[5]=c
nt
[5]
[6]=d
nt
[6]
[1]=e
nt
[6]
[2]=f
nt
[6]
[3]=g
nt
[6]
[4]=h
nt
[6]
[5]=i
nt
[6]
[6]=j
Af
tert
ri
m ofonei
ndexe,
Count=5
Af
tert
ri
m oft
woi
ndexes,
Count=3
NESTEDTABLEELEMENTS
nt
[1]
[1]=A
nt
[1]
[2]=B
nt
[1]
[3]=C
nt
[2]
[1]=E
nt
[2]
[2]=F
nt
[2]
[3]=G
nt
[3]
[1]=I
nt
[3]
[2]=J
nt
[3]
[3]=K
Af
terdel
eteofsecondi
ndex,
Count=2
NESTEDTABLEELEMENTS
nt
[1]
[1]=A
nt
[1]
[2]=B
nt
[1]
[3]=C
nt
[3]
[1]=I
nt
[3]
[2]=J
nt
[3]
[3]=K
Af
terdel
eteofent
ir
enest
edt
abl
e,Count=0

Ex3:
DECLARE

.
260

t
ypet
1ist
abl
eofv
archar
(2)i
ndexbybi
nar
y_i
nteger
;
t
ypet
2ist
abl
eoft
1indexbybi
nar
y_i
nteger
;
i
btt
2;
f
lagbool
ean;
BEGI
N
dbms_
out
put
.put
_li
ne(
'Count='
||i
bt.
count
);
i
fibt
.l
imi
tisnul
lthen
dbms_
out
put
.put
_li
ne(
'Nol
imi
ttoI
ndex-
byTabl
es'
);
el
se
dbms_
out
put
.put
_li
ne(
'Li
mit='
||i
bt.
li
mit
);
endi
f;
i
bt(
1)(
1):
='a'
;
i
bt(
4)(
5):
='b'
;
i
bt(
5)(
1):
='c'
;
i
bt(
6)(
2):
='d'
;
i
bt(
8)(
3):
='e'
;
i
bt(
3)(
4):
='f
';
dbms_
out
put
.put
_li
ne(
'
INDEX- )
BYTABLEELEMENTS';
dbms_
out
put
.put
_li
ne(
'i
bt(
[1]
[1]='
||i
bt(
1)(
1))
;
dbms_
out
put
.put
_li
ne(
'i
bt(
[4]
[5]='
||i
bt(
4)(
5))
;
dbms_
out
put
.put
_li
ne(
'i
bt(
[5]
[1]='
||i
bt(
5)(
1))
;
dbms_
out
put
.put
_li
ne(
'i
bt(
[6]
[2]='
||i
bt(
6)(
2))
;
dbms_
out
put
.put
_li
ne(
'i
bt(
[8]
[3]='
||i
bt(
8)(
3))
;
dbms_
out
put
.put
_li
ne(
'i
bt(
[3]
[4]='
||i
bt(
3)(
4))
;
dbms_
out
put
.put
_li
ne(
'Fi
rstI
ndex='
||i
bt.
fi
rst
);
dbms_
out
put
.put
_li
ne(
'LastI
ndex='
||i
bt.
last
);
dbms_
out
put
.put
_li
ne(
'NextI
ndex='
||i
bt.
next
(3)
);
dbms_
out
put
.put
_li
ne(
'Pr
iorI
ndex='
||i
bt.
pri
or(
8))
;
i
bt(
1)(
2):
='g'
;
i
bt(
1)(
3):
='h'
;
i
bt(
1)(
4):
='i
'
;
i
bt(
1)(
5):
='k'
;
i
bt(
1)(
6):
='l
'
;
i
bt(
1)(
7):
='m'
;
i
bt(
1)(
8):
='n'
;
dbms_
out
put
.put
_li
ne(
'Count='
||i
bt.
count
);
dbms_
out
put
.put
_li
ne(
'
INDEX- )
BYTABLEELEMENTS';

.
261

f
orii
n1.
.8l
oop
dbms_
out
put
.put
_li
ne(
'i
bt[
1][
'|
|i|
|'
]='
||i
bt(
1)(
i)
);
endl
oop;
dbms_
out
put
.put
_li
ne(
'i
bt(
[4]
[5]='
||i
bt(
4)(
5))
;
dbms_
out
put
.put
_li
ne(
'i
bt(
[5]
[1]='
||i
bt(
5)(
1))
;
dbms_
out
put
.put
_li
ne(
'i
bt(
[6]
[2]='
||i
bt(
6)(
2))
;
dbms_
out
put
.put
_li
ne(
'i
bt(
[8]
[3]='
||i
bt(
8)(
3))
;
dbms_
out
put
.put
_li
ne(
'i
bt(
[3]
[4]='
||i
bt(
3)(
4))
;
f
lag:
=ibt
.exi
sts(
3);
i
ffl
ag=t
ruet
hen
dbms_
out
put
.put
_li
ne(
'I
ndex3exi
sts'
);
el
se
dbms_
out
put
.put
_li
ne(
'I
ndex3exi
sts'
);
endi
f;
i
bt.
del
ete(
1);
dbms_
out
put
.put
_li
ne(
'Af
terdel
eteoff
ir
sti
ndex,
Count='
||i
bt.
count
);
i
bt.
del
ete(
4);
dbms_
out
put
.put
_li
ne(
'Af
terdel
eteoff
our
thi
ndex,
Count='
||i
bt.
count
);
dbms_
out
put
.put
_li
ne(
'
INDEX- )
BYTABLEELEMENTS';
dbms_
out
put
.put
_li
ne(
'i
bt(
[5]
[1]='
||i
bt(
5)(
1))
;
dbms_
out
put
.put
_li
ne(
'i
bt(
[6]
[2]='
||i
bt(
6)(
2))
;
dbms_
out
put
.put
_li
ne(
'i
bt(
[8]
[3]='
||i
bt(
8)(
3))
;
dbms_
out
put
.put
_li
ne(
'i
bt(
[3]
[4]='
||i
bt(
3)(
4))
;
i
bt.
del
ete;
dbms_
out
put
.put
_li
ne(
'Af
terdel
eteofent
ir
eindex-
byt
abl
e,Count='
||
i
bt.
count
);
END;

Out
put
:
Count=0
Nol
imi
ttoI
ndex-
byTabl
es
I
NDEX-
BYTABLEELEMENTS
i
bt(
[1]
[1]=a
i
bt(
[4]
[5]=b
i
bt(
[5]
[1]=c
i
bt(
[6]
[2]=d
i
bt(
[8]
[3]=e

.
262

i
bt(
[3]
[4]=f
Fi
rstI
ndex=1
LastI
ndex=8
NextI
ndex=4
Pr
iorI
ndex=6
Count=6
I
NDEX-
BYTABLEELEMENTS
i
bt[
1][
1]=a
i
bt[
1][
2]=g
i
bt[
1][
3]=h
i
bt[
1][
4]=i
i
bt[
1][
5]=k
i
bt[
1][
6]=l
i
bt[
1][
7]=m
i
bt[
1][
8]=n
i
bt(
[4]
[5]=b
i
bt(
[5]
[1]=c
i
bt(
[6]
[2]=d
i
bt(
[8]
[3]=e
i
bt(
[3]
[4]=f
I
ndex3exi
sts
Af
terdel
eteoff
ir
sti
ndex,
Count=5
Af
terdel
eteoff
our
thi
ndex,
Count=4
I
NDEX-
BYTABLEELEMENTS
i
bt(
[5]
[1]=c
i
bt(
[6]
[2]=d
i
bt(
[8]
[3]=e
i
bt(
[3]
[4]=f
Af
terdel
eteofent
ir
eindex-
byt
abl
e,Count=0

Ex4:
DECLARE
t
ypet
1ist
abl
eofv
archar
(2)i
ndexbybi
nar
y_i
nteger
;
t
ypet
2ist
abl
eoft
1indexbybi
nar
y_i
nteger
;
t
ypet
3ist
abl
eoft
2;
ntt
3:=t
3()
;
cnumber:
=65;

.
263

BEGI
N
nt
.ext
end(
2);
dbms_
out
put
.put
_li
ne(
'Count='
||nt
.count
);
f
orii
n1.
.nt
.countl
oop
f
orji
n1.
.nt
.countl
oop
f
orki
n1.
.nt
.countl
oop
nt
(i
)(
j)
(k):
=chr
(c)
;
c:
=c+1;
endl
oop;
endl
oop;
endl
oop;
dbms_
out
put
.put
_li
ne(
' )
NESTEDTABLEELEMENTS';
f
orii
n1.
.nt
.countl
oop
f
orji
n1.
.nt
.countl
oop
f
orki
n1.
.nt
.countl
oop
dbms_
out
put
.put
_li
ne(
'nt
['|
|i|
|'
][
'|
|j|
|'
][
'|
|k|
|'
]='
||
nt
(i
)(
j)
(k)
);
endl
oop;
endl
oop;
endl
oop;
END;

Out
put
:
Count=2
NESTEDTABLEELEMENTS
nt
[1]
[1]
[1]=A
nt
[1]
[1]
[2]=B
nt
[1]
[2]
[1]=C
nt
[1]
[2]
[2]=D
nt
[2]
[1]
[1]=E
nt
[2]
[1]
[2]=F
nt
[2]
[2]
[1]=G
nt
[2]
[2]
[2]=H

OBJECTSUSEDI
NTHEEXAMPLES

.
264

SQL>sel
ect*f
rom st
udent
;

SNO SNAME SMARKS


-
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
-
1 saket
h 100
2 sr
inu 200
3 di
vya 300
4 manogni 400

SQL>cr
eat
eorr
epl
acet
ypeaddrasobj
ect
(hnonumber
(2)
,ci
tyv
archar
(10)
);
/

SQL>sel
ect*f
rom empl
oy;

ENAME JOB ADDRESS(


HNO,
CITY)
-
--
--
--
--
---
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
Ranj
it cl
erk ADDR(
11,
'hy
d')
Sat
ish manager ADDR(
22,
'bang'
)
Sr
inu engi
neer ADDR(
33,
'kochi
'
)

.
265

ERRORHANDLI
NG

PL/
SQLi
mpl
ement
ser
rorhandl
ingwi
thexcept
ionsandexcept
ionhandl
ers.Except
ionscanbeassoci
atedwi
th
or
acl
eer
ror
sorwi
thy
ourownuser
-def
ineder
ror
s.Byusi
ngexcept
ionsandexcept
ionhandl
ers,y
oucanmake
y
ourPL/
SQLpr
ogr
amsr
obustandabl
etodealwi
thbot
hunexpect
edandexpect
eder
ror
sdur
ingexecut
ion.

ERRORTYPES
 Compi
le-
ti
meer
ror
s
 Runt
imeer
ror
s

Er
ror
sthatoccurdur
ingt
hecompi
lat
ionphasear
edet
ect
edbyt
hePL/
SQLengi
neandr
epor
tedbackt
otheuser
,
wehav
etocor
rectt
hem.

Runt
imeer
ror
sar
edet
ect
edbyt
hePL/
SQLr
unt
imeengi
newhi
chcanpr
ogr
ammat
ical
lyr
aiseandcaughtby
except
ionhandl
ers.
Except
ionsar
edesi
gnedf
orr
un-
ti
meer
rorhandl
ing,
rat
hert
hancompi
le-
ti
meer
rorhandl
ing.

HANDLI
NGEXCEPTI
ONS

Whenexcept
ioni
srai
sed,cont
rolpassest
otheexcept
ionsect
ionoft
hebl
ock.Theexcept
ionsect
ionconsi
sts
ofhandl
ersf
orsomeoral
loft
heexcept
ions.Anexcept
ionhandl
ercont
ainst
hecodet
hati
sexecut
edwhent
he
er
rorassoci
atedwi
tht
heexcept
ionoccur
s,andt
heexcept
ioni
srai
sed.

Sy
ntax:
EXCEPTI
ON

.
266

Whenexcept
ion_
namet
hen
Sequence_
of_
stat
ement
s;
Whenexcept
ion_
namet
hen
Sequence_
of_
stat
ement
s;
Whenot
her
sthen
Sequence_
of_
stat
ement
s;
END;
EXCEPTI
ONTYPES

 Pr
edef
inedexcept
ions
 User
-def
inedexcept
ions

PREDEFI
NEDEXCEPTI
ONS

Or
acl
e has pr
edef
ined sev
eralexcept
ions t
hatcor
responds t
othe mostcommon or
acl
e er
ror
s.Li
ke t
he
pr
edef
inedt
ypes,t
hei
dent
if
ier
soft
heseexcept
ionsar
edef
inedi
ntheSTANDARD package.Becauseoft
his,t
hey
ar
eal
readyav
ail
abl
etot
hepr
ogr
am,
iti
snotnecessar
ytodecl
aret
hem i
nthedecl
arat
iveseci
on.

Ex1:
DECLARE
anumber
;
bv
archar
(2)
;
v
_mar
ksnumber
;
cur
sorci
ssel
ect*f
rom st
udent
;
t
ypeti
svar
ray
(3)ofv
archar
(2)
;
v
at:
=t(
'a'
,
'b'
);
v
a1t
;
BEGI
N
-
-NO_
DATA_
FOUND
BEGI
N
sel
ectsmar
ksi
ntov
_mar
ksf
rom st
udentwher
esno=50;
EXCEPTI
ON
whenno_
dat
a_f
oundt
hen
dbms_
out
put
.put
_li
ne(
'I
nval
idst
udentnumber
')
;
END;
-
-CURSOR_
ALREADY_
OPEN
BEGI
N
openc;

.
267

openc;
EXCEPTI
ON
whencur
sor
_al
ready
_opent
hen
dbms_
out
put
.put
_li
ne(
'Cur
sori
sal
readyopened'
);
END;

-
-INVALI
D_CURSOR
BEGI
N
cl
osec;
openc;
cl
osec;
cl
osec;
EXCEPTI
ON
wheni
nval
id_
cur
sort
hen
dbms_
out
put
.put
_li
ne(
'Cur
sori
sal
readycl
osed'
);
END;
-
-TOO_
MANY_
ROWS
BEGI
N
sel
ectsmar
ksi
ntov
_mar
ksf
rom st
udentwher
esno>1;
EXCEPTI
ON
whent
oo_
many
_rowst
hen
dbms_
out
put
.put
_li
ne(
'Toomanyv
aluesar
ecomi
ngt
omar
ks
v
ari
abl
e'
);
END;
-
-ZERO_
DIVI
DE
BEGI
N
a:
=5/
0;
EXCEPTI
ON
whenzer
o_di
vi
det
hen
dbms_
out
put
.put
_li
ne(
'Di
vi
dedbyzer
o-i
nval
idoper
ati
on'
);
END;
-
-VALUE_
ERROR
BEGI
N
b:
='saket
h';
EXCEPTI
ON
whenv
alue_
err
ort
hen
dbms_
out
put
.put
_li
ne(
'I
nval
idst
ri
ngl
engt
h')
;
END;
-
-INVALI
D_NUMBER
BEGI
N
i
nser
tint
ost
udentv
alues(
'a'
,
'sr
inu'
,
100)
;

.
268

EXCEPTI
ON
wheni
nval
id_
numbert
hen
dbms_
out
put
.put
_li
ne(
'I
nval
idnumber
')
;
END;

-
-SUBSCRI
PT_
OUTSI
DE_
LIMI
T
BEGI
N
v
a(4):
='c'
;
EXCEPTI
ON
whensubscr
ipt
_out
side_
li
mitt
hen
dbms_
out
put
.put
_li
ne(
'I
ndexi
sgr
eat
ert
hant
hel
imi
t'
);
END;
-
-SUBSCRI
PT_
BEYOND_
COUNT
BEGI
N
v
a(3):
='c'
;
EXCEPTI
ON
whensubscr
ipt
_bey
ond_
countt
hen
dbms_
out
put
.put
_li
ne(
'I
ndexi
sgr
eat
ert
hant
hecount
')
;
END;
-
-COLLECTI
ON_
IS_
NULL
BEGI
N
v
a1(
1):
='a'
;
EXCEPTI
ON
whencol
lect
ion_
is_
nul
lthen
dbms_
out
put
.put
_li
ne(
'Col
lect
ioni
sempt
y'
);
END;
-
-
END;

Out
put
:
I
nval
idst
udentnumber
Cur
sori
sal
readyopened
Cur
sori
sal
readycl
osed
Toomanyv
aluesar
ecomi
ngt
omar
ksv
ari
abl
e
Di
vi
dedbyzer
o-i
nval
idoper
ati
on
I
nval
idst
ri
ngl
engt
h
I
nval
idnumber
I
ndexi
sgr
eat
ert
hant
hel
imi
t
I
ndexi
sgr
eat
ert
hant
hecount
Col
lect
ioni
sempt
y

.
269

Ex2:
DECLARE
cnumber
;
BEGI
N
c:
=5/
0;
EXCEPTI
ON
whenzer
o_di
vi
det
hen
dbms_
out
put
.put
_li
ne(
'I
nval
idOper
ati
on'
);
whenot
her
sthen
dbms_
out
put
.put
_li
ne(
'Fr
om OTHERShandl
er:I
nval
id
Oper
ati
on'
);
END;

Out
put
:
I
nval
idOper
ati
on

USER-
DEFI
NEDEXCEPTI
ONS

Auser
-def
inedexcept
ioni
saner
rort
hati
sdef
inedbyt
hepr
ogr
ammer
.User
-def
inedexcept
ionsar
edecl
aredi
n
t
hedecl
arat
iveseci
onofaPL/
SQLbl
ock.Justl
ikev
ari
abl
es,
exept
ionshav
eat
ypeEXCEPTI
ON andscope.

RAI
SINGEXCEPTI
ONS

User
-def
inedexcept
ionsar
erai
sedexpl
ici
tl
yvi
atheRAI
SEst
atement
.

Ex:
DECLARE
eexcept
ion;
BEGI
N
r
aisee;
EXCEPTI
ON
whenet
hen
dbms_
out
put
.put
_li
ne(
'ei
srai
sed'
);
END;
Out
put
:

.
270

ei
srai
sed
BULI
T-I
NERRORFUNCTI
ONS

SQLCODEANDSQLERRM

 SQLCODEr
etur
nst
hecur
renter
rorcode,
andSQLERRM r
etur
nst
hecur
renter
rormessaget
ext
;
 Foruser
-def
inedexcept
ionSQLCODEr
etur
ns1andSQLERRM r
etur
ns“
user
-dei
fnedexcept
ion”
.
 SQLERRM wi
ilt
akeonl
ynegat
ivev
alueexcept100.I
fanyposi
ti
vev
alueot
hert
han100r
etur
nsnon-
or
acl
eexcept
ion.

Ex1:
DECLARE
eexcept
ion;
v
_dnamev
archar
(10)
;
BEGI
N
-
-USER-
DEFI
NEDEXCEPTI
ON
BEGI
N
r
aisee;
EXCEPTI
ON
whenet
hen
dbms_
out
put
.put
_li
ne(
SQLCODE|
|''
||SQLERRM)
;
END;

-
-PREDEFI
NEDEXCEPTI
ON
BEGI
N
sel
ectdnamei
ntov
_dnamef
rom deptwher
edept
no=50;
EXCEPTI
ON
whenno_
dat
a_f
oundt
hen
dbms_
out
put
.put
_li
ne(
SQLCODE|
|''
||SQLERRM)
;
END;
END;

Out
put
:
1User
-Def
inedExcept
ion
100ORA-
01403:nodat
afound

Ex2:
BEGI
N
dbms_
out
put
.put
_li
ne(
SQLERRM(
100)
);

.
271

dbms_
out
put
.put
_li
ne(
SQLERRM(
0))
;
dbms_
out
put
.put
_li
ne(
SQLERRM(
1))
;
dbms_
out
put
.put
_li
ne(
SQLERRM(
-100)
);
dbms_
out
put
.put
_li
ne(
SQLERRM(
-500)
);
dbms_
out
put
.put
_li
ne(
SQLERRM(
200)
);
dbms_
out
put
.put
_li
ne(
SQLERRM(
-900)
);
END;
Out
put
:
ORA-
01403:nodat
afound
ORA-
0000:nor
mal
,successf
ulcompl
eti
on
User
-Def
inedExcept
ion
ORA-
00100:nodat
afound
ORA-
00500:Message500notf
ound;pr
oduct
=RDBMS;
faci
li
ty=ORA
-
200:non-
ORACLEexcept
ion
ORA-
00900:i
nval
idSQLst
atement

DBMS_
UTI
LITY.
FORMAT_
ERROR_
STACK

 Thebui
lt
-i
nfunct
ion,
li
keSQLERRM,
ret
urnst
hemessageassoci
atedwi
tht
hecur
renter
ror
.
 I
tdi
ff
ersf
rom SQLERRM i
ntwoway
s:
 I
tsl
engt
hisnotr
est
ri
cted;
itwi
llr
etur
nthef
ull
err
ormessagest
ri
ng.
 Youcannotpassaner
rorcodenumbert
othi
sfunct
ion;i
tcannotbeusedt
oret
urnt
hemessagef
ora
r
andom er
rorcode.

Ex:
DECLARE
vnumber:
='ab'
;
BEGI
N
nul
l;
EXCEPTI
ON
whenot
her
sthen
dbms_
out
put
.put
_li
ne(
dbms_
uti
li
ty.
for
mat
_er
ror
_st
ack)
;
END;
Out
put
:
decl
are
*
ERRORatl
ine1:
ORA-
06502:PL/
SQL:numer
icorv
alueer
ror
:char
act
ert
onumberconv
ersi
oner
ror

.
272

ORA-
06512:atl
ine2

DBMS_
UTI
LITY.
FORMAT_
CALL_
STACK

Thi
sfunct
ionr
etur
nsaf
ormat
tedst
ri
ngshowi
ngt
heexecut
ioncal
lst
acki
nsi
dey
ourPL/SQL appl
icat
ion.I
ts
usef
ulnessi
snotr
est
ri
ctedt
oer
rormanagement
;youwi
llal
sof
indi
tshandyf
ort
raci
ngt
heexect
uti
onofy
our
code.Youmaynotuset
hisf
unct
ioni
nexcept
ionbl
ock.

Ex:
BEGI
N
dbms_
out
put
.put
_li
ne(
dbms_
uti
li
ty.
for
mat
_cal
l_
stack)
;
END;

Out
put
:
-
--
--PL/
SQLCal
lSt
ack-
--
--
Obj
ect
_handl
e l
ine_
numberobj
ect
_name
69760478 2 anony
mousbl
ock

DBMS_
UTI
LITY.
FORMAT_
ERROR_
BACKTRACE

I
tdi
spl
ayst
heexecut
ionst
ackatt
hepoi
ntwher
eanexcept
ionwasr
aised.Thus,
youcancal
lthi
sfunct
ionwi
th
anexcept
ionsect
ionatt
het
opl
evelofy
ourst
ackandst
il
lfi
ndoutwher
etheer
rorwasr
aiseddeepwi
thi
nthe
cal
lst
ack.

Ex:
CREATEORREPLACEPROCEDUREP1I
S
BEGI
N
dbms_
out
put
.put
_li
ne(
'f
rom pr
ocedur
e1'
);
r
aisev
alue_
err
or;
ENDP1;
CREATEORREPLACEPROCEDUREP2I
S
BEGI
N
dbms_
out
put
.put
_li
ne(
'f
rom pr
ocedur
e2'
);
p1;
ENDP2;

CREATEORREPLACEPROCEDUREP3I
S
BEGI
N
dbms_
out
put
.put
_li
ne(
'f
rom pr
ocedur
e3'
);

.
273

p2;
EXCEPTI
ON
whenot
her
sthen
dbms_
out
put
.put
_li
ne(
dbms_
uti
li
ty.
for
mat
_er
ror
_backt
race)
;
ENDP3;

Out
put
:
SQL>execp3

f
rom pr
ocedur
e3
f
rom pr
ocedur
e2
f
rom pr
ocedur
e1
ORA-
06512:at"
SAKETH.
P1"
,li
ne4
ORA-
06512:at"
SAKETH.
P2"
,li
ne4
ORA-
06512:at"
SAKETH.
P3"
,li
ne4

EXCEPTI
ON_
INI
TPRAGMA

Usi
ngt
hisy
oucanassoci
ateanamedexcept
ionwi
thapar
ti
cul
aror
acl
eer
ror
.Thi
sgi
vesy
out
heabi
li
tyt
otr
ap
t
hiser
rorspeci
fi
cal
ly
,rat
hert
hanv
iaanOTHERShandl
er.

Sy
ntax:
PRAGMAEXCEPTI
ON_
I T(
NI except
ion_
name,
oracl
e_er
ror
_number
);
Ex:
DECLARE
eexcept
ion;
pr
agmaexcept
ion_
ini
t(
e,-
1476)
;
cnumber
;
BEGI
N
c:
=5/
0;
EXCEPTI
ON
whenet
hen
dbms_
out
put
.put
_li
ne(
'I
nval
idOper
ati
on'
);
END;

Out
put
:
I
nval
idOper
ati
on

.
274

RAI
SE_
APPLI
CATI
ON_
ERROR

Youcanuset
hisbui
lt
-i
nfunct
iont
ocr
eat
eyourowner
rormessages,
whi
chcanbemor
edescr
ipt
ivet
hannamed
except
ions.

Sy
ntax:
RAI
SE_
APPLI
CATI er
ERROR(
ON_ r
or_
number
,er
ror
_message,
,keep_
[ err
ors_
flag]
);

TheBool
eanpar erkeep_
amet err
ors_
flagi
sopt
ional
.Ifi
tisTRUE,t
henew er
rori
saddedt
othel
istofer
ror
s
al
readyr
aised.I
fiti
sFALSE,
whi
chi
sdef
aul
t,t
henewer
rorwi
llr
epl
acet
hecur
rentl
istofer
ror
s.

Ex:
DECLARE
cnumber
;
BEGI
N
c:
=5/
0;

EXCEPTI
ON
whenzer
o_di
vi
det
hen
r
aise_
appl
icat
ion_
err
or(
-20222,
'
Inv
ali
dOper
ati
on'
);
END;

Out
put
:
DECLARE
*
ERRORatl
ine1:
ORA-
20222:I
nval
idOper
ati
on
ORA-
06512:atl
ine7

EXCEPTI
ONPROPAGATI
ON

Except
ionscanoccuri
nthedecl
arat
ive,
theexecut
abl
e,ort
heexcept
ionsect
ionofaPL/
SQLbl
ock.

EXCEPTI
ONRAI
SEDI
NTHEEXECUATABLESECTI
ON

Except
ionsr
aisedi
nexecuat
abl
esect
ioncanbehandl
edi
ncur
rentbl
ockorout
erbl
ock.

Ex1:

.
275

DECLARE
eexcept
ion;
BEGI
N
BEGI
N
r
aisee;
END;
EXCEPTI
ON
whenet
hen
dbms_
out
put
.put
_li
ne(
'ei
srai
sed'
);
END;

Out
put
:
ei
srai
sed

Ex2:
DECLARE
eexcept
ion;
BEGI
N
BEGI
N
r
aisee;
END;
END;

Out
put
:
ERRORatl
ine1:
ORA-
06510:PL/
SQL:unhandl
eduser
-def
inedexcept
ion
ORA-
06512:atl
ine5

EXCEPTI
ONRAI
SEDI
NTHEDECLARATI
VESECTI
ON

Except
ionsr
aisedi
nthedecl
arat
iveseci
onmustbehandl
edi
ntheout
erbl
ock.

Ex1:
DECLARE
cnumber
(3):
='abcd'
;
BEGI
N
dbms_
out
put
.put
_li
ne(
'Hel
lo'
);
EXCEPTI
ON
whenot
her
sthen
dbms_
out
put
.put
_li
ne(
'I
nval
idst
ri
ngl
engt
h')
;

.
276

END;

Out
put
:
ERRORatl
ine1:
ORA-
06502:PL/
SQL:numer
icorv
alueer
ror
:char
act
ert
onumberconv
ersi
oner
ror
ORA-
06512:atl
ine2

Ex2:
BEGI
N
DECLARE
cnumber
(3):
='abcd'
;
BEGI
N
dbms_
out
put
.put
_li
ne(
'Hel
lo'
);
EXCEPTI
ON
whenot
her
sthen
dbms_
out
put
.put
_li
ne(
'I
nval
idst
ri
ngl
engt
h')
;
END;
EXCEPTI
ON
whenot
her
sthen
dbms_
out
put
.put
_li
ne(
'Fr
om out
erbl
ock:I
nval
idst
ri
ngl
engt
h')
;
END;

Out
put
:
Fr
om out
erbl
ock:I
nval
idst
ri
ngl
engt
h

EXCEPTI
ONRAI
SEDI
NTHEEXCEPTI
ONSECTI
ON

Except
ionsr
aisedi
nthedecl
arat
iveseci
onmustbehandl
edi
ntheout
erbl
ock.

Ex1:
DECLARE
e1except
ion;
e2except
ion;
BEGI
N
r
aisee1;
EXCEPTI
ON
whene1t
hen
dbms_
out
put
.put
_li
ne(
'e1i
srai
sed'
);
r
aisee2;

.
277

whene2t
hen
dbms_
out
put
.put
_li
ne(
'e2i
srai
sed'
);
END;

Out
put
:
e1i
srai
sed
DECLARE
*
ERRORatl
ine1:
ORA-
06510:PL/
SQL:unhandl
eduser
-def
inedexcept
ion
ORA-
06512:atl
ine9
ORA-
06510:PL/
SQL:unhandl
eduser
-def
inedexcept
ion

Ex2:
DECLARE
e1except
ion;
e2except
ion;
BEGI
N
BEGI
N
r
aisee1;
EXCEPTI
ON
whene1t
hen
dbms_
out
put
.put
_li
ne(
'e1i
srai
sed'
);
r
aisee2;
whene2t
hen
dbms_
out
put
.put
_li
ne(
'e2i
srai
sed'
);
END;
EXCEPTI
ON
whene2t
hen
dbms_
out
put
.put
_li
ne(
'Fr
om out
erbl
ock:e2i
srai
sed'
);
END;

Out
put
:
e1i
srai
sed
Fr
om out
erbl
ock:e2i
srai
sed

Ex3:
DECLARE

.
278

eexcept
ion;
BEGI
N
r
aisee;
EXCEPTI
ON
whenet
hen
dbms_
out
put
.put
_li
ne(
'ei
srai
sed'
);
r
aisee;
END;

Out
put
:
ei
srai
sed
DECLARE
*
ERRORatl
ine1:
ORA-
06510:PL/
SQL:unhandl
eduser
-def
inedexcept
ion
ORA-
06512:atl
ine8
ORA-
06510:PL/
SQL:unhandl
eduser
-def
inedexcept
ion

RESTRI
CTI
ONS

Youcannotpassexcept
ionasanar
gumentt
oasubpr
ogr
am.

.
279

DATABASETRI
GGERS

Tr
igger
s ar
e si
mil
art
o pr
ocedur
es orf
unct
ions i
nthatt
heyar
e named PL/
SQL bl
ocks wi
th decl
arat
ive,
execut
abl
e,andexcept
ionhandl
ingsect
ions.A t
ri
ggeri
sexecut
edi
mpl
ici
tl
ywhenev
ert
het
ri
gger
ingev
ent
happens.Theactofexecut
ingat
ri
ggeri
sknownasf
ir
ingt
het
ri
gger
.

RESTRI
CTI
ONSONTRI
GGERES

 Li
kepackages,t
ri
gger
smustbest
oredasst
and-
aloneobj
ect
sint
hedat
abaseandcannotbel
ocalt
oa
bl
ockorpackage.
 At
ri
ggerdoesnotacceptar
gument
s.

USEOFTRI
GGERS

 Mai
ntai
ningcompl
exi
ntegr
it
yconst
rai
ntsnotpossi
blet
hroughdecl
arat
iveconst
rai
ntsenabl
eatt
abl
e
cr
eat
ion.
 Audi
ti
ngi
nfor
mat
ioni
nat
abl
ebyr
ecor
dingt
hechangesmadeandwhomadet
hem.
 Aut
omat
ical
lysi
gnal
ingot
herpr
ogr
amst
hatact
ionneedst
otakepl
acewhenchagesar
emadet
oat
abl
e.
 Per
for
mval
idat
iononchangesbei
ngmadet
otabl
es.

.
280

 Aut
omat
emai
ntenanceoft
hedat
abase.

TYPESOFTRI
GGERS

 DMLTr
igger
s
 I
nst
eadofTr
igger
s
 DDLTr
igger
s
 Sy
stem Tr
igger
s
 SuspendTr
igger
s

CATEGORI
ES

Ti
ming-
- Bef
oreorAf
ter
Lev
el -
- RoworSt
atement

Rowl
evelt
ri
ggerf
ir
esoncef
oreachr
owaf
fect
edbyt
het
ri
gger
ingst
atement
.Rowl
evelt
ri
ggeri
sident
if
iedby
t
heFOREACHROW cl
ause.

St
atementl
evelt
ri
ggerf
ir
esonceei
therbef
oreoraf
tert
hest
atement
.

DMLTRI
GGERSYNTAX

Cr
eat
eorr
epl
acet
rgger<t
i ri
gger
_name>
{
Bef
ore|af
ter
}{i
nser
torupdat
eordel
ete}on<t
abl
e_name>
[
Foreachr
ow]
[
When(
…)]
[
Decl
are]
-
-decl
arat
ion
Begi
n
-
-tr
iggerbody
[
Except
ion]
-
-except
ionsect
ion
End<t
ri
gger
_name>;

.
281

DMLTRI
GGERS

ADMLt
ri
ggeri
sfi
redonanI
NSERT,
UPDATE,
orDELETEoper
ati
ononadat
abaset
abl
e.I
tcanbef
ir
edei
therbef
oreor
af
tert
hest
atementexecut
es,
andcanbef
ir
edonceperaf
fect
edr
ow,
oronceperst
atement
.

Thecombi
nat
ionoft
hesef
act
orsdet
ermi
nest
het
ypesoft
het
ri
gger
s.Thesear
eat
otalof12possi
blet
ypes(
3
st
atement
s*2t
imi
ng*2l
evel
s).

STATEMENTLEVEL

St
atementl
evelt
ri
ggerf
ir
esonl
yonce.
Ex:

SQL>cr
eat
etabl
est
atement
_lev
el(
countv
archar
(50)
);

CREATEORREPLACETRI
GGERSTATEMENT_
LEVEL_
TRI
GGER
af
terupdat
eonst
udent
BEGI
N
i
nser
tint
ost
atement
_lev
elv
alues(
'St
atementl
evelf
ir
ed'
);
ENDSTATEMENT_
LEVEL_
TRI
GGER;

Out
put
:

SQL>updat
est
udentsetsmar
ks=500;

3r
owsupdat
ed.

SQL>sel
ect*f
rom st
atement
_lev
el;

COUNT
-
--
--
--
--
--
--
--
--
--
--
--
--
--
-
St
atementl
evelf
ir
ed

ROW LEVEL

Rowl
evelt
ri
ggerf
ir
esoncef
oreachr
owaf
fect
edbyt
het
ri
gger
ingst
atement
.

Ex:

.
282

SQL>cr
eat
etabl
erow_
lev
el(
countv
archar
(50)
);

CREATEORREPLACETRI
GGERROW_
LEVEL_
TRI
GGER
af
terupdat
eonst
udent
BEGI
N
i
nser
tint
orow_
lev
elv
alues(
'Rowl
evelf
ir
ed'
);
ENDROW_
LEVEL_
TRI
GGER;

Out
put
:

SQL>updat
est
udentsetsmar
ks=500;

3r
owsupdat
ed.

SQL>sel
ect*f
rom st
atement
_lev
el;

COUNT
-
--
--
--
--
--
--
--
--
--
--
--
--
--
-
Rowl
evelf
ir
ed
Rowl
evelf
ir
ed
Rowl
evelf
ir
ed

ORDEROFDMLTRI
GGERFI
RING

 Bef
orest
atementl
evel
 Bef
orer
owl
evel
 Af
terr
owl
evel
 Af
terst
atementl
evel

Ex:
Supposewehav
eaf
oll
wingt
abl
e.

SQL>sel
ect*f
rom st
udent
;

NONAME MARKS
-
--
---
--
--
---
--
--
--
--
-

.
283

1 a 100
2 b 200
3 c 300
4 d 400

SQL>cr
eat
etabl
efi
ri
ng_
order
(or
derv
archar
(50)
);

CREATEORREPLACETRI
GGERBEFORE_
STATEMENT
bef
orei
nser
tonst
udent
BEGI
N
i
nser
tint
ofi
ri
ng_
orderv
alues(
'Bef
oreSt
atementLev
el'
);
ENDBEFORE_
STATEMENT;

CREATEORREPLACETRI
GGERBEFORE_
ROW
bef
orei
nser
tonst
udent
f
oreachr
ow
BEGI
N
i
nser
tint
ofi
ri
ng_
orderv
alues(
'Bef
oreRowLev
el'
);
ENDBEFORE_
ROW;

CREATEORREPLACETRI
GGERAFTER_
STATEMENT
af
teri
nser
tonst
udent
BEGI
N
i
nser
tint
ofi
ri
ng_
orderv
alues(
'Af
terSt
atementLev
el'
);
ENDAFTER_
STATEMENT;

CREATEORREPLACETRI
GGERAFTER_
ROW
af
teri
nser
tonst
udent
f
oreachr
ow
BEGI
N
i
nser
tint
ofi
ri
ng_
orderv
alues(
'Af
terRowLev
el'
);
ENDAFTER_
ROW;

Out
put
:
SQL>sel
ect*f
rom f
ir
ing_
order
;

nor
owssel
ect
ed

SQL>i
nser
tint
ost
udentv
alues(
5,'
e'
,500)
;

.
284

1r
owcr
eat
ed.

SQL>sel
ect*f
rom f
ir
ing_
order
;

ORDER
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
Bef
oreSt
atementLev
el
Bef
oreRowLev
el
Af
terRowLev
el
Af
terSt
atementLev
el

SQL>sel
ect*f
rom st
udent
;

NO NAME MARKS
-
--
---
--
--
---
--
--
--
--
-
1 a 100
2 b 200
3 c 300
4 d 400
5 e 500

CORRELATI
ONI
DENTI
FIERSI
NROW-
LEVELTRI
GGERS

I
nsi
det
het
ri
gger
,youcanaccesst
hedat
aint
her
ow t
hati
scur
rent
lybei
ngpr
ocessed.Thi
sisaccompl
ished
t
hrought
wocor
rel
ati
oni
dent
if
ier
s-:
oldand:
new.

Acor
rel
ati
oni
dent
if
ieri
saspeci
alki
ndofPL/SQLbi
ndv
ari
abl
e.Thecol
oni
nfr
ontofeachi
ndi
cat
est
hatt
heyar
e
bi
ndv
ari
abl
es,i
nthesenseofhostv
ari
abl
esusedi
nembeddedPL/SQL,andi
ndi
cat
est
hatt
heyar
enotr
egul
ar
SQLv
PL/ ari
abl
es.ThePL/SQLcompi
lerwi
llt
reatt
hem asr
ecor
dsoft
ype

Tr
igger
ing_
tabl
e%ROWTYPE.

Al
thoughsy
ntact
ical
lyt
heyar
etr
eat
edasr
ecor
ds,i
nreal
it
ytheyar
enot
.:ol
dand:
new ar
eal
soknownas
pseudor
ecor
ds,
fort
hisr
eason.

.
285

TRI
GGERI
NGSTATEMENT :
OLD :
NEW
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
- -
--
--
--
--
--
--
--
--
--
--
--
--
--
- -
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
I
NSERT al
lfi
eldsar
eNULL. v
aluest
hatwi
llbei
nser
ted
Whent
hest
atementi
scompl
eted.
UPDATE or
igi
nalv
aluesf
or newv
aluest
hatwi
llbeupdat
ed
t
her
owbef
oret
he whent
hest
atementi
scompl
eted.
updat
e.
DELETE or
igi
nalv
aluesbef
ore al
lfi
eldsar
eNULL.
t
her
owi
sdel
eted.

Ex:
SQL>cr
eat
etabl
emar
ks(
nonumber
(2)ol
d_mar
ksnumber
(3)
,new_
mar
ks
number
(3)
);

CREATEORREPLACETRI
GGEROLD_
NEW
bef
orei
nser
torupdat
eordel
eteonst
udent
f
oreachr
ow
BEGI
N
i
nser
tint
omar
ksv
alues(
:ol
d.no,
:
old.
mar
ks,
:
new.
mar
ks)
;
ENDOLD_
NEW;

Out
put
:
SQL>sel
ect*f
rom st
udent
;

NO NAMEMARKS
-
--
---
--
--
---
--
--
--
--
-
1 a 100
2 b 200
3 c 300
4 d 400
5 e 500

SQL>sel
ect*f
rom mar
ks;

nor
owssel
ect
ed

SQL>i
nser
tint
ost
udentv
alues(
6,'
f'
,
600)
;

.
286

1r
owcr
eat
ed.

SQL>sel
ect*f
rom st
udent
;

NO NAME MARKS
-
--
---
--
--
---
--
--
--
--
-
1 a 100
2 b 200
3 c 300
4 d 400
5 e 500
6 f 600

SQL>sel
ect*f
rom mar
ks;

NO OLD_
MARKSNEW_
MARKS
-
--
---
--
--
--
--
--
--
---
--
--
--
--
--
--
-
600

SQL>updat
est
udentsetmar
ks=555wher
eno=5;

1r
owupdat
ed.

SQL>sel
ect*f
rom st
udent
;

NO NAMEMARKS
-
--
---
--
--
---
--
--
--
--
-
1 a 100
2 b 200
3 c 300
4 d 400
5 e 555
6 f 600

.
287

SQL>sel
ect*f
rom mar
ks;

NO OLD_
MARKS NEW_
MARKS
-
--
--
---
--
--
--
--
--
--
---
--
--
--
--
--
--
--
600
5 500 555

SQL>del
etest
udentwher
eno=2;

1r
owdel
eted.

SQL>sel
ect*f
rom st
udent
;

NO NAME MARKS
-
--
---
--
--
---
--
--
--
--
-
1 a 100
3 c 300
4 d 400
5 e 555
6 f 600

SQL>sel
ect*f
rom mar
ks;

NO OLD_
MARKSNEW_
MARKS
-
--
---
--
--
--
--
--
--
---
--
--
--
--
--
--
--
600
5 500 555
2 200

REFERENCI
NGCLAUSE

I
fdesi
red,
youcanuset
heREFERENCI
NG cl
auset
ospeci
fyadi
ff
erentnamef
or:
oldane:
new.Thi
scl
ausei
sfound
af
tert
het
ri
gger
ingev
ent
,bef
oret
heWHENcl
ause.

Sy
ntax:
NG[
REFERENCI oldasol
d_name][
newasnew_
name]

.
288

Ex:
CREATEORREPLACETRI
GGERREFERENCE_
TRI
GGER
bef
orei
nser
torupdat
eordel
eteonst
udent
r
efer
enci
ngol
dasol
d_st
udentnewasnew_
student
f
oreachr
ow
BEGI
N
i
nser
tint
omar
ks
v
alues(
:ol
d_st
udent
.no,
:
old_
student
.mar
ks,
:
new_
student
.mar
ks)
;
ENDREFERENCE_
TRI
GGER;

WHENCLAUSE

WHEN cl
ausei
sval
idf
orr
ow-
lev
elt
ri
gger
sonl
y.I
fpr
esent
,thet
ri
ggerbodywi
llbeexecut
edonl
yfort
hoser
ows
t
hatmeett
hecondi
ti
onspeci
fi
edbyt
heWHENcl
ause.

Sy
ntax:
WHEN t
ri
gger
_condi
ti
on;

et
Wher r
igger
_condi
ti
oni
saBool
eanexpr
essi
on.I
twi
llbeev
aluat
edf ow.The:
oreachr newand:
oldr
ecor
dscan
ber
efer
encedi det
nsi ri
gger
_condi
ti
onaswel
l,
butl
ikeREFERENCI
NG,
thecol
oni
snotusedt
her
e.Thecol
oni
sonl
y
v
ali
dint
het
ri
ggerbody
.

Ex:
CREATEORREPLACETRI
GGERWHEN_
TRI
GGER
bef
orei
nser
torupdat
eordel
eteonst
udent
r
efer
enci
ngol
dasol
d_st
udentnewasnew_
student
f
oreachr
ow
when(
new_
student
.mar
ks>500)
BEGI
N
i
nser
tint
omar
ks
v
alues(
:ol
d_st
udent
.no,
:
old_
student
.mar
ks,
:
new_
student
.mar
ks)
;
ENDWHEN_
TRI
GGER;

TRI
GGERPREDI
CATES

Ther
ear
ethr
eeBool
eanf
unct
ionst
haty
oucanuset
odet
ermi
newhatt
heoper
ati
oni
s.
Thepr
edi
cat
esar
e

 I
NSERTI
NG

.
289

 UPDATI
NG
 DELETI
NG

Ex:

SQL>cr
eat
etabl
epr
edi
cat
es(
oper
ati
onv
archar
(20)
);

CREATEORREPLACETRI
GGERPREDI
CATE_
TRI
GGER
bef
orei
nser
torupdat
eordel
eteonst
udent
BEGI
N
i
finser
ti
ngt
hen
i
nser
tint
opr
edi
cat
esv
alues(
'I
nser
t'
);
el
sifupdat
ingt
hen
i
nser
tint
opr
edi
cat
esv
alues(
'Updat
e')
;
el
sifdel
eti
ngt
hen
i
nser
tint
opr
edi
cat
esv
alues(
'Del
ete'
);
endi
f;
ENDPREDI
CATE_
TRI
GGER;

Out
put
:
SQL>del
etest
udentwher
eno=1;

1r
owdel
eted.

SQL>sel
ect*f
rom pr
edi
cat
es;

MSG
-
--
--
--
--
--
--
--
Del
ete

SQL>i
nser
tint
ost
udentv
alues(
7,'
g'
,700)
;

1r
owcr
eat
ed.

SQL>sel
ect*f
rom pr
edi
cat
es;

MSG
-
--
--
--
--
--
--
--

.
290

Del
ete
I
nser
t

SQL>updat
est
udentsetmar
ks=777wher
eno=7;

1r
owupdat
ed.

SQL>sel
ect*f
rom pr
edi
cat
es;

MSG
-
--
--
--
--
--
--
--
Del
ete
I
nser
t
Updat
e

I
NSTEAD-
OFTRI
GGERS

I
nst
ead-
oft
ri
gger
sfi
rei
nst
eadofaDMLoper
ati
on.Al
so,
inst
ead-
oft
ri
gger
scanbedef
inedonl
yonv
iews.I
nst
ead-
oft
ri
gger
sar
eusedi
ntwocases:

 Toal
lowav
iewt
hatwoul
dot
her
wisenotbemodi
fi
abl
etobemodi
fi
ed.
 Tomodi
fyt
hecol
umnsofanest
edt
abl
ecol
umni
nav
iew.

Ex:
SQL>cr
eat
evi
ewemp_
deptassel
ectempno,
ename,
j
ob,
dname,
l
oc,
sal
,
e.dept
nof
rom
empe,
deptdwher
ee.
dept
no=d.
dept
no;

CREATEORREPLACETRI
GGERI
NSTEAD_
OF_
TRI
GGER
i
nst
eadofi
nser
tonemp_
dept
BEGI
N
i
nser
tint
odept
1val
ues(
50,
'
rd'
,
'bang'
);
i
nser
tint
o
emp1(
empno,
ename,
j
ob,
sal
,
dept
no)
val
ues(
2222,
'
saket
h',
'
doct
or'
,
8000,
50)
;
ENDI
NSTEAD_
OF_
TRI
GGER;
Out
put
:

SQL> i
nser
tint
oemp_
deptv
alues(
2222,
'
saket
h',
'
doct
or'
,
8000,
'
rd'
,
'bang'
,
50)
;

.
291

SQL>sel
ect*f
rom emp_
dept
;

EMPNO ENAME JOB SAL DNAME LOC DEPTNO


-
--
--
--
--
---
--
--
--
---
--
--
--
--
--
---
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
7369 SMI
TH CLERK 800 RESEARCH DALLAS 20
7499 ALLEN SALESMAN 1600 SALES CHI
CAGO 30
7521 WARD SALESMAN 1250 SALES CHI
CAGO 30
7566 JONES MANAGER 2975 RESEARCH DALLAS 20
7654 MARTI
N SALESMAN 1250 SALES CHI
CAGO 30
7698 BLAKE MANAGER 2850 SALES CHI
CAGO 30
7782 CLARK MANAGER 2450 ACCOUNTI
NG NEW YORK 10
7788 SCOTT ANALYST 3000 RESEARCH DALLAS 20
7839 KI
NG PRESI
DENT 5000 ACCOUNTI
NG NEW YORK 10
7844 TURNER SALESMAN 1500 SALES CHI
CAGO 30
7876 ADAMS CLERK 1100 RESEARCH DALLAS 20
7900 JAMES CLERK 950 SALES CHI
CAGO 30
7902 FORD ANALYST 3000 RESEARCH DALLAS 20
7934 MI
LLER CLERK 1300 ACCOUNTI
NG NEW YORK 10
2222 saket
h doct
or 8000 r
d bang 50

SQL>sel
ect*f
rom dept
;

DEPTNO DNAME LOC


-
--
--
--
--
---
--
--
--
--
--
--
---
--
--
--
--
--
10 ACCOUNTI
NG NEW YORK
20 RESEARCH DALLAS
30 SALES CHI
CAGO
40 OPERATI
ONS BOSTON
50 r
d bang

SQL>sel
ect*f
rom emp;

EMPNO ENAME JOB MGR HI


REDATE SAL COMM DEPTNO
-
--
--
--
--
---
--
--
--
---
--
--
--
--
--
--
---
--
--
--
---
--
--
--
--
--
---
--
--
--
---
--
--
--
---
--
--
--
--
-

.
292

7369 SMI
TH CLERK 7902 17-
DEC-
80 800 20
7499 ALLEN SALESMAN 7698 20-
FEB-
81 1600 300 30
7521 WARD SALESMAN 7698 22-
FEB-
81 1250 500 30
7566 JONES MANAGER 7839 02-
APR-
81 2975 20
7654 MARTI
N SALESMAN 7698 28-
SEP-
81 1250 1400 30
7698 BLAKE MANAGER 7839 01-
MAY-
81 2850 30
7782 CLARK MANAGER 7839 09-
JUN-
81 2450 10
7788 SCOTT ANALYST 7566 19-
APR-
87 3000 20
7839 KI
NG PRESI
DENT 17-
NOV-
81 5000 10
7844 TURNER SALESMAN 7698 08-
SEP-
81 1500 0 30
7876 ADAMS CLERK 7788 23-
MAY-
87 1100 20
7900 JAMES CLERK 7698 03-
DEC-
81 950 30
7902 FORD ANALYST 7566 03-
DEC-
81 3000 20
7934 MI
LLER CLERK 7782 23-
JAN-
82 1300 10
2222 saket
h doct
or 8000 50

DDLTRI
GGERS

Or
acl
eal
lowsy
out
odef
inet
ri
gger
sthatwi
l
lfi
rewhenDat
aDef
ini
ti
onLanguagest
atement
sar
eexecut
ed.

Sy
ntax:

Cr
eat
eorr
epl
acet
rgger<t
i ri
gger
_name>
{
Bef
ore|af
ter
}{DDLevent
}on{
dat
abase|schema}
[
When(
…)]
[
Decl
are]
-
-decl
arat
ion
Begi
n
-
-tr
iggerbody
[
Except
ion]
-
-except
ionsect
ion
End<t
ri
gger
_name>;

Ex:

SQL>cr
eat
etabl
emy
_obj
ect
s(obj
_namev
archar
(10)
,obj
_ty
pev
archar
(10)
,obj
_owner
v
archar
(10)
,obj
_ti
medat
e);

.
293

CREATEORREPLACETRI
GGERCREATE_
TRI
GGER
af
tercr
eat
eondat
abase
BEGI
N
i
nser
tint
omy
_obj
ect
sval
ues(
sys.
dict
ionar
y_obj
_name,
sys.
dict
ionar
y_obj
_ty
pe,
sy
s.di
cti
onar
y_obj
_owner
,sy
sdat
e);
ENDCREATE_
TRI
GGER;

Out
put
:

SQL>sel
ect*f
rom my
_obj
ect
s;

nor
owssel
ect
ed

SQL>cr
eat
etabl
est
ud1(
nonumber
(2)
);

SQL>sel
ect*f
rom my
_obj
ect
s;

OBJ_
NAME OBJ_
TYPE OBJ_
OWNER OBJ_
TIME
-
--
--
--
--
--
---
--
--
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
--
-
STUD1 TABLE SYS 21-
JUL-
07

SQL>cr
eat
esequencess;

SQL>cr
eat
evi
ewst
ud_
viewassel
ect*f
rom st
ud1;

SQL>sel
ect*f
rom my
_obj
ect
s;

OBJ_
NAME OBJ_
TYPE OBJ_
OWNER OBJ_
TIME
-
--
--
--
--
--
--
---
--
--
--
--
--
---
--
--
--
--
--
--
---
--
--
--
--
--
--
STUD1 TABLE SYS 21-
JUL-
07
SS SEQUENCE SYS 21-
JUL-
07
STUD_
VIEW VI
EW SYS 21-
JUL-
07

WHENCLAUSE

I
fWHEN pr
esent
,thet
ri
ggerbodywi
llbeexecut
edonl
yfort
hoset
hatmeett
hecondi
ti
onspeci
fi
edbyt
heWHEN

.
294

cl
ause.

Ex:
CREATEORREPLACETRI
GGERCREATE_
TRI
GGER
af
tercr
eat
eondat
abase
when(
sys.
dict
ionar
y_obj
_ty
pe=‘
TABLE’
)
BEGI
N
i
nser
tint
omy
_obj
ect
sval
ues(
sys.
dict
ionar
y_obj
_name,
sys.
dict
ionar
y_obj
_ty
pe,
sy
s.di
cti
onar
y_obj
_owner
,sy
sdat
e);
ENDCREATE_
TRI
GGER;

SYSTEM TRI
GGERS

Sy
stem t
ri
gger
swi
llf
ir
ewhenev
erdat
abase-
wideev
entoccur
s.Thef
oll
owi
ngar
ethedat
abaseev
entt
ri
gger
s.To
cr
eat
esy
stem t
ri
ggery
ouneedADMI
NI GGERp
STERDATABASETRI r
ivi
lege.

 STARTUP
 SHUTDOWN
 LOGON
 LOGOFF
 SERVERERROR

Sy
ntax:

Cr
eat
eorr
epl
acet
rgger<t
i ri
gger
_name>
{
Bef
ore|af
ter
}{Dat
abaseev
ent
}on{
dat
abase|schema}
[
When(
…)]
[
Decl
are]
-
-decl
arat
ionsect
ion
Begi
n
-
-tr
iggerbody
[
Except
ion]
-
-except
ionsect
ion
End<t
ri
gger
_name>;

Ex:

SQL>cr
eat
etabl
euser
_logs(
u_namev
archar
(10)
,l
og_
timet
imest
amp)
;

.
295

CREATEORREPLACETRI
GGERAFTER_
LOGON
af
terl
ogonondat
abase
BEGI
N
i
nser
tint
ouser
_logsv
alues(
user
,cur
rent
_ti
mest
amp)
;
ENDAFTER_
LOGON;

Out
put
:

SQL>sel
ect*f
rom user
_logs;

nor
owssel
ect
ed

SQL>connsaket
h/saket
h
SQL>sel
ect*f
rom user
_logs;

U_
NAME LOG_
TIME
-
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
SAKETH 22-
JUL-
0712.
07.
13.
140000AM

SQL>connsy
stem/
oracl
e
SQL>sel
ect*f
rom user
_logs;

U_
NAME LOG_
TIME
-
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
SAKETH 22-
JUL-
0712.
07.
13.
140000AM
SYSTEM 22-
JUL-
0712.
07.
34.
218000AM

SQL>connscot
t/t
iger
SQL>sel
ect*f
rom user
_logs;

U_
NAME LOG_
TIME
-
--
--
--
--
---
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
-
SAKETH 22-
JUL-
0712.
07.
13.
140000AM
SYSTEM 22-
JUL-
0712.
07.
34.
218000AM

.
296

SCOTT 22-
JUL-
0712.
08.
43.
093000AM

SERVERERROR

TheSERVERERRORev
entcanbeusedt
otr
acker
ror
sthatoccuri
nthedat
abase.Theer
rorcodei
sav
ail
abl
einsi
de
t
het
ri
ggert
hrought
heSERVER_ERRORat
tri
but
efunct
ion.
Ex:

SQL>cr
eat
etabl
emy
_er
ror
s(er
ror
_msgv
archar
(200)
);

CREATEORREPLACETRI
GGERSERVER_
ERROR_
TRI
GGER
af
terser
ver
err
orondat
abase
BEGI
N
i
nser
tint
omy
_er
ror
sval
ues(
dbms_
uti
li
ty.
for
mat
_er
ror
_st
ack)
;
ENDSERVER_
ERROR_
TRI
GGER;

Out
put
:

SQL>cr
eat
etabl
ess(
no)
);
cr
eat
etabl
ess(
no)
)
*
ERRORatl
ine1:
ORA-
00922:mi
ssi
ngori
nval
idopt
ion

SQL>sel
ect*f
rom my
_er
ror
s;
ERROR_
MSG
-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
ORA-
00922:mi
ssi
ngori
nval
idopt
ion

SQL>i
nser
tint
ost
udentv
alues(
1,2,
3);
i
nser
tint
ost
udentv
alues(
1,2,
3)
*
ERRORatl
ine1:
ORA-
00942:t
abl
eorv
iewdoesnotexi
st

SQL>sel
ect*f
rom my
_er
ror
s;

ERROR_
MSG

.
297

-
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
--
ORA-
00922:mi
ssi
ngori
nval
idopt
ion
ORA-
00942:t
abl
eorv
iewdoesnotexi
st

SERVER_
ERRORATTRI
BUTEFUNCTI
ON

I
ttakesasi
ngl
enumbert
ypeofar
gumentandr
etur
nst
heer
roratt
heposi
ti
onont
heer
rorst
acki
ndi
cat
edbyt
he
ar
gument
.Theposi
ti
on1i
sthet
opoft
hest
ack.

Ex:

CREATEORREPLACETRI
GGERSERVER_
ERROR_
TRI
GGER
af
terser
ver
err
orondat
abase
BEGI
N
i
nser
tint
omy
_er
ror
sval
ues(
ser
ver
_er
ror
(1)
);
ENDSERVER_
ERROR_
TRI
GGER;

SUSPENDTRI
GGERS

Thi
swi
l
lfi
rewhenev
erast
atementi
ssuspended.Thi
smi
ghtoccurast
her
esul
tofaspacei
ssuesuchas
exceedi
nganal
locat
edt
abl
epacequot
a.Thi
sfunct
ional
it
ycanbeusedt
oaddr
esst
hepr
obl
em andal
low t
he
oper
ati
ntocont
inue.

Sy
ntax:

Cr
eat
eorr
epl
acet
rgger<t
i ri
gger
_name>
af
tersuspendon{
dat
abase|schema}
[
When(
…)]
[
Decl
are]
-
-decl
arat
ionsect
ion
Begi
n
-
-tr
iggerbody
[
Except
ion]
-
-except
ionsect
ion
End<t
ri
gger
_name>;

Ex:

.
298

SQL>cr
eat
etabl
espacemy
_spacedat
afi
le'
f:
\my
_fi
le.
dbf
'si
ze2m;
SQL>cr
eat
etabl
est
udent
(snonumber
(2)
,snamev
archar
(10)
)tabl
espacemy
_space;

CREATEORREPLACETRI
GGERSUSPEND_
TRI
GGER
af
tersuspendondat
abase
BEGI
N
dbms_
out
put
.put
_li
ne(
‘Nor
oom t
oinser
tiny
ourt
abl
espace'
);
ENDSUSPEND_
TRI
GGER;

Out
put
:

I
nser
tmor
erowsi
nst
udentt
abl
ethen,
youwi
llget

Nor
oom t
oinser
tiny
ourt
abl
espace

AUTONOMOUSTRANSACTI
ON

Pr
iort
oOr
acl
e8i
,ther
ewasnowayi
nwhi
chsomeSQLoper
ati
onswi
thi
nat
ransact
ioncoul
dbecommi
tt
ed
i
ndependentoft
her
estoft
heoper
ati
ons.Or
acl
eal
lowst
his,howev
er,t
hroughaut
onomoust
ransact
ions.An
aut
onomoust
ransact
ioni
sat
ransact
iont
hati
sst
art
edwi
thi
nthecont
extofanot
hert
ransact
ion,knownas
par
entt
ransact
ion,buti
sindependentofi
t.Theaut
onomoust
ransact
ioncanbecommi
tt
edorr
oll
edback
r
egar
dlessott
hest
ateoft
hepar
entt
ransact
ion.

Ex:

CREATEORREPLACETRI
GGERAUTONOMOUS_
TRANSACTI
ON_
TRI
GGER
af
teri
nser
tonst
udent
DECLARE
pr
agmaaut
onomous_
transact
ion;
BEGI
N
updat
est
udentsetmar
ks=555;
commi
t;
ENDAUTONOMOUS_
TRANSACTI
ON_
TRI
GGER;

Out
put
:

SQL>sel
ect*f
rom st
udent
;

.
299

NO NA MARKS
-
--
---
--
---
---
--
--
--
--
1 a 111
2 b 222
3 c 300

SQL> i
nser
tint
ost
udentv
alues(
4,'
d'
,444)
;

SQL>sel
ect*f
rom st
udent
;

NO NA MARKS
-
--
---
--
---
---
--
--
--
--
1 a 555
2 b 555
3 c 555
4 d 444

RESTRI
CTI
ONSONAUTONOMOUSTRANSACTI
ON

 I
fanaut
onomoust
ransact
ionat
tempt
stoaccessar
esour
cehel
dbyt
hemai
ntr
ansact
ion,adeadl
ock
canoccuri
nyoupr
ogr
am.
 Youcannotmar
kal
lpr
ogr
amsi
napackageasaut
onomouswi
thasi
ngl
ePRAGMAdecl
arat
ion.Youmust
i
ndi
cat
eaut
onomoust
ransact
ionsexpl
ici
tyi
neachpr
ogr
am.
 Toexi
twi
thouter
ror
sfr
om anaut
onomoust
ransact
ionpr
ogr
am t
hathasexecut
edatl
eastoneI
NSERTor
UPDATEorDELETE,
youmustper
for
m anexpl
ici
tcommi
torr
oll
back.
 TheCOMMI
TandROLLBACK st
atement
sendt
heact
iveaut
onomoust
ransact
ion,butt
heydonotf
orcet
he
t
ermi
nat
ionoft
heaut
onomousr
out
ine.Youcanhav
emul
ti
ple COMMI
Tand/
orROLLBACK st
atement
s
i
nsi
dey
ouraut
onomousbl
ock.
 Youcannotr
oll
backt
oasav
epoi
ntseti
nthemai
ntr
ansact
ion.
 The TRANSACTI
ONS par
amet
eri
nthe or
acl
eini
ti
ali
zat
ion f
il
e speci
fi
es t
he maxi
mum numberof
t
ransact
ionsal
lowedconcur
rent
lyi
nasessi
on.Thedef
aul
tval
uei
s75f
ort
his,buty
oucani
ncr
easet
he
l
imi
t.

MUTATI
NGTABLES

.
300

Ther
ear
erest
ri
cti
onsont
het
abl
esandcol
umnst
hatat
ri
ggerbodymayaccess.I
nor
dert
odef
inet
hese
r
est
ri
cti
ons,
iti
snecessar
ytounder
standmut
ati
ngandconst
rai
ningt
abl
es.

Amut
ati
ngt
abl
eist
abl
ethati
scur
rent
lt
ybei
ngmodi
fi
edbyaDMLst
atementandt
het
ri
ggerev
ental
soDML
st
atement
.Amut
ati
ngt
abl
eer
roroccur
swhenar
ow-
lev
elt
ri
ggert
ri
est
oexami
neorchangeat
abl
ethati
s
al
readyunder
goi
ngchange.

Aconst
rai
ningt
abl
eisat
abl
ethatmi
ghtneedt
ober
eadf
rom f
orar
efer
ent
iali
ntegr
it
yconst
rai
nt.

Ex:

CREATEORREPLACETRI
GGERMUTATI
NG_
TRI
GGER
bef
oredel
eteonst
udent
f
oreachr
ow

DECLARE
ctnumber
;
BEGI
N
sel
ectcount
(*)i
ntoctf
rom st
udentwher
eno=:
old.
no;
ENDMUTATI
NG_
TRI
GGER;

Out
put
:

SQL>del
etest
udentwher
eno=1;
del
etest
udentwher
eno=1
*
ERRORatl
ine1:
ORA-
04091:t
abl
eSCOTT.
STUDENTi
smut
ati
ng,
tri
gger
/funct
ionmaynotseei
t
ORA-
06512:at"
SCOTT.
T",
li
ne4
ORA-
04088:er
rordur
ingexecut
ionoft
ri
gger'
SCOTT.
T'

HOW TOAVOI
DMUTATI
NGTABLEERROR?

 Byusi
ngaut
onomoust
ransact
ion
 Byusi
ngst
atementl
evelt
ri
gger

.
301

You might also like