Sei sulla pagina 1di 213

1

INTRODUCTION
SQL is divided into the following Data Definition Language (DDL) Data Manipulation Language (DML) Data Ret ieval Language (DRL) T ansa!tion Cont ol Language (TCL) Data Cont ol Language (DCL)

DDL "" ! eate# alte # d op# t un!ate# ena$e DML "" inse t# update# delete DRL "" sele!t TCL "" !o$$it# oll%a!&# savepoint DCL "" g ant# evo&e CR'(T' T()L' S*NT(+ C eate ta%le ,table_name- (col1 datatype1, col2 datatype2 coln datatypen). '/0
SQL-

! eate ta%le student (no nu$%e (1)# na$e va !ha (23)# $a &s nu$%e (4)).

Ta%le ! eated5 INS'RT This will %e used to inse t the e!o ds into ta%le5 6e have two $ethods to inse t5 a) )7 value $ethod )7 add ess $ethod

USIN8 9(LU' M'T:OD

s7nta/0 inse t into ,table_name) values (value1, value2, value3 . Valuen). '/0

2
SQL-

inse t into student values (2# ;sudha;# 233).

2 ow ! eated5 inse t into student values (1# ;sað# 133).

SQL-

2 ow ! eated5 To inse t a new e!o d again 7ou have to t7pe enti e inse t !o$$and# if the e a e lot of e!o ds this will %e diffi!ult5 This will %e avoided %7 using add ess $ethod5 %)

USIN8 (DDR'SS M'T:OD

s7nta/0 inse t into ,table_name) values (&col1, &col2, &col3 . &coln). This will p o$pt 7ou fo the values %ut fo eve 7 inse t 7ou have to use fo wa d slash5 '/0
SQL-

inse t into student values (<no# =<na$e=# <$a &s).

'nte value fo no0 2 'nte value fo na$e0 >agan 'nte value fo $a &s0 433 old new 20 inse t into student values(<no# =<na$e=# <$a &s) 20 inse t into student values(2# =>agan=# 433)

2 ow ! eated5 ?

SQL-

'nte value fo no0 1 'nte value fo na$e0 Na en 'nte value fo $a &s0 @33 old new 20 inse t into student values(<no# =<na$e=# <$a &s) 20 inse t into student values(1# =Na en=# @33)

2 ow ! eated5

3
!)
INS'RTIN8 D(T( INTO SA'CIBI'D COLUMNS USIN8 9(LU' M'T:OD

s7nta/0 inse t into ,table_name)(col1, col2, col3 Coln) values (value1, value2, value3 . Valuen). '/0
SQL-

inse t into student (no# na$e) values (4# ;Ra$esh;).

2 ow ! eated5 inse t into student (no# na$e) values (@# ;Madhu;).

SQL-

2 ow ! eated5 d)

INS'RTIN8 D(T( INTO SA'CIBI'D COLUMNS USIN8 (DDR'SS M'T:OD

s7nta/0 inse t into ,table_name)(col1, col2, col3 coln) values (&col1, &col2, &col3 . &coln). This will p o$pt 7ou fo the values %ut fo eve 7 inse t 7ou have to use fo wa d slash5 '/0
SQL-

inse t into student (no# na$e) values (<no# =<na$e=).

'nte value fo no0 C 'nte value fo na$e0 9isu old new 20 inse t into student (no# na$e) values(<no# =<na$e=) 20 inse t into student (no# na$e) values(C# =9isu=)

2 ow ! eated5 ?

SQL-

'nte value fo no0 D 'nte value fo na$e0 Rattu old new 20 inse t into student (no# na$e) values(<no# =<na$e=) 20 inse t into student (no# na$e) values(D# =Rattu=)

2 ow ! eated5

4
S'L'CTIN8 D(T( S7nta/0 Sele!t E f o$ ,table_name-. o Sele!t col1, col2, coln f o$ ,table_name-. '/0
SQL-

"" he e E indi!ates all !olu$ns

sele!t E f o$ student. NO N(M' """ """""" 2 1 2 1 4 @ C D Sudha Sa&eth >agan Na en Ra$esh Madhu 9isu Rattu M(RFS """""""" 233 133 433 @33

G ows sele!ted5 sele!t no# na$e# $a &s f o$ student. NO N(M' """ """""" 2 1 2 1 4 @ C D Sudha Sa&eth >agan Na en Ra$esh Madhu 9isu Rattu M(RFS """""""" 233 133 433 @33

SQL-

G ows sele!ted5

5
SQL-

sele!t no# na$e f o$ student. NO N(M' """ """"""" 2 1 2 1 4 @ C D Sudha Sa&eth >agan Na en Ra$esh Madhu 9isu Rattu

G ows sele!ted5

CONDITION(L S'L'CTIONS (ND OA'R(TORS


6e have two !lauses used in this 6he e O de %7

USIN8 6:'R' s7nta/0 sele!t E f o$ ,table_name- whe e ,condition-. the following a e the diffe ent t7pes of ope ato s used in whe e !lause5 ( ith$eti! ope ato s Co$pa ison ope ato s Logi!al ope ato s ( ith$eti! ope ato s H# "# E# ? Co$pa ison ope ato s a)
USIN8

"" highest p e!eden!e

I# JI# -# ,# -I# ,I# ,%etween# not %etween in# not in null# not null li&e Logi!al ope ato s (nd O not "" lowest p e!eden!e

I# -# ,# -I# ,I# JI# ,sele!t E f o$ student whe e no I 1. NO N(M' """ """"""" 1 Sa&eth M(RFS """"""""" 133

'/0
SQL-

7
1 Na en @33

1 ows sele!ted5 sele!t E f o$ student whe e no , 1. NO N(M' """ """"""" 2 2 Sudha >agan M(RFS """""""""" 233 433

SQL-

1 ows sele!ted5
SQL-

sele!t E f o$ student whe e no - 1. NO N(M' """ """"""" 4 @ C D Ra$esh Madhu 9isu Rattu M(RFS """"""""""

@ ows sele!ted5 sele!t E f o$ student whe e no ,I 1. NO N(M' """ """"""" 2 1 2 1 Sudha Sa&eth >agan Na en M(RFS """""""""" 233 133 433 @33

SQL-

@ ows sele!ted5 sele!t E f o$ student whe e no -I 1.

SQL-

8
NO N(M' """ """"""" 1 1 4 @ C D Sa&eth Na en Ra$esh Madhu 9isu Rattu M(RFS """"""""" 133 @33

D ows sele!ted5 sele!t E f o$ student whe e no JI 1. NO N(M' """ """"""" 2 2 4 @ C D Sudha >agan Ra$esh Madhu 9isu Rattu M(RFS """""""""" 233 433

SQL-

D ows sele!ted5 sele!t E f o$ student whe e no ,- 1. NO N(M' """ """"""" 2 2 4 @ C D Sudha >agan Ra$esh Madhu 9isu Rattu M(RFS """""""""" 233 433

SQL-

D ows sele!ted5

9
%)
USIN8 (ND

This will gives the output when all the !onditions %e!o$e t ue5 s7nta/0 sele!t E f o$ ,table_name- whe e ,condition1- and ,condition2- and 55 ,conditionn-. '/0 sele!t E f o$ student whe e no I 1 and $a &s -I 133.

SQL-

NO N(M' """ """"""" 1 1 Sa&eth Na en

M(RFS """""""" 133 @33

1 ows sele!ted5 !)

USIN8 OR

This will gives the output when eithe of the !onditions %e!o$e t ue5 s7nta/0 sele!t E f o$ ,table_name- whe e ,condition1- and ,condition2- o 55 ,conditionn-. '/0
SQL-

sele!t E f o$ student whe e no I 1 o $a &s -I 133. NO N(M' """ """"""" 1 2 1 Sa&eth >agan Na en M(RFS """"""""" 133 433 @33

4 ows sele!ted5 d)

USIN8 )'T6''N

This will gives the output %ased on the !olu$n and its lowe %ound# uppe %ound5 s7nta/0 sele!t E f o$ ,table_name- whe e ,col- %etween ,lower bound- and ,upper bound-.

10
'/0
SQL-

sele!t E f o$ student whe e $a &s %etween 133 and @33. NO N(M' """ """"""" 1 2 1 Sa&eth >agan Na en M(RFS """"""""" 133 433 @33

4 ows sele!ted5 e)
USIN8 NOT )'T6''N

This will gives the output %ased on the !olu$n whi!h values a e not in its lowe %ound# uppe %ound5 s7nta/0 sele!t E f o$ ,table_name- whe e ,col- not %etween ,lower bound- and ,upper bound-. '/0
SQL-

sele!t E f o$ student whe e $a &s not %etween 133 and @33. NO N(M' """ """"""" 2 Sudha M(RFS """"""""" 233

2 ow sele!ted5 f)

USIN8 IN

This will gives the output %ased on the !olu$n and its list of values spe!ified5 s7nta/0 sele!t E f o$ ,table_name- whe e ,col- in ( value1, value2, value3 valuen). '/0
SQL-

sele!t E f o$ student whe e no in (2# 1# 4). NO N(M' """ """"""" 2 1 2 Sudha Sa&eth >agan M(RFS """"""""" 233 133 433

11
1 4 Na en Ra$esh @33

C ows sele!ted5 g)

USIN8 NOT IN

This will gives the output %ased on the !olu$n whi!h values a e not in the list of values spe!ified5 s7nta/0 sele!t E f o$ ,table_name- whe e ,col- not in ( value1, value2, value3 valuen). '/0
SQL-

sele!t E f o$ student whe e no not in (2# 1# 4). NO N(M' """ """"""" @ C D Madhu 9isu Rattu M(RFS """""""""

4 ows sele!ted5 h)

USIN8 NULL

This will gives the output %ased on the null values in the spe!ified !olu$n5 s7nta/0 sele!t E f o$ ,table_name- whe e ,col- is null. '/0
SQL-

sele!t E f o$ student whe e $a &s is null. NO N(M' """ """"""" 4 @ C D Ra$esh Madhu 9isu Rattu M(RFS """""""""

@ ows sele!ted5

12
i)
USIN8 NOT NULL

This will gives the output %ased on the not null values in the spe!ified !olu$n5 s7nta/0 sele!t E f o$ ,table_name- whe e ,col- is not null. '/0
SQL-

sele!t E f o$ student whe e $a &s is not null. NO N(M' """ """"""" 2 1 2 1 Sudha Sa&eth >agan Na en M(RFS """"""""" 233 133 433 @33

@ ows sele!ted5 K)

USIN8 LIF'

This will %e used to sea !h th ough the ows of data%ase !olu$n %ased on the patte n 7ou spe!if75 s7nta/0 sele!t E f o$ ,table_name- whe e ,col- li&e ,pattern-. '/0 i) This will give the ows whose $a &s a e 2335 sele!t E f o$ student whe e $a &s li&e 233. NO N(M' """ """"""" 2 Sudha M(RFS """"""""" 233

SQL-

2 ow sele!ted5 ii) This will give the ows whose na$e sta t with LS;5 sele!t E f o$ student whe e na$e li&e =SM=.

SQL-

13

NO N(M' """ """"""" 2 1 Sudha Sa&eth

M(RFS """"""""" 233 133

1 ows sele!ted5 iii) This will give the ows whose na$e ends with Lh;5 sele!t E f o$ student whe e na$e li&e =Mh=. NO N(M' """ """"""" 1 4 Sa&eth Ra$esh M(RFS """"""""" 133

SQL-

1 ows sele!ted5 i9) This will give the ows whose na$e;s se!ond lette sta t with La;5 sele!t E f o$ student whe e na$e li&e =NaM=. NO N(M' """ """"""" 1 2 1 4 @ D Sa&eth >agan Na en Ra$esh Madhu Rattu M(RFS """""""" 133 433 @33

SQL-

D ows sele!ted5 9) This will give the ows whose na$e;s thi d lette sta t with Ld;5 sele!t E f o$ student whe e na$e li&e =NNdM=.

SQL-

14
NO N(M' """ """"""" 2 @ Sudha Madhu M(RFS """"""""" 233

1 ows sele!ted5 9i) This will give the ows whose na$e;s se!ond lette sta t with Lt; f o$ ending5 sele!t E f o$ student whe e na$e li&e =MNtM=. NO N(M' """ """"""" 1 D Sa&eth Rattu M(RFS """"""""" 133

SQL-

1 ows sele!ted5 9ii) This will give the ows whose na$e;s thi d lette sta t with Le; f o$ ending5 sele!t E f o$ student whe e na$e li&e =MeNNM=. NO N(M' """ """"""" 1 4 Sa&eth Ra$esh M(RFS """"""""" 133

SQL-

1 ows sele!ted5 9iii) This will give the ows whose na$e !otains 1 a;s5 sele!t E f o$ student whe e na$e li&e =MaM a M=. NO N(M' """ """"""" 2 >agan 2 ow sele!ted5 E *ou have to spe!if7 the patte ns in like using unde s!o e ( N )5 M(RFS """""""""" 433

SQL-

15
USIN8 ORD'R )* This will %e used to o de ing the !olu$ns data (as!ending o des!ending)5 S7nta/0 Sele!t E f o$ ,table_name- o de %7 ,col- des!. )7 default o a!le will use as!ending o de 5 If 7ou want output in des!ending o de 7ou have to use desc &e7wo d afte the !olu$n5 '/0
SQL-

sele!t E f o$ student o de %7 no. NO N(M' """ """"""" 2 2 1 1 4 @ C D Sudha >agan Sa&eth Na en Ra$esh Madhu 9isu Rattu M(RFS """"""""" 233 433 133 @33

G ows sele!ted5 sele!t E f o$ student o de %7 no des!. NO N(M' """ """"""" D Rattu C 9isu @ Madhu 4 Ra$esh 1 Sa&eth 1 Na en 2 Sudha 2 >agan G ows sele!ted5 133 @33 233 433 M(RFS """""""""

SQL-

16

USIN8 DML
USIN8 UAD(T' This !an %e used to $odif7 the ta%le data5 S7nta/0 Update ,table_name- set ,col1- I value2# ,col2- I value1 whe e ,condition-. '/0
SQL-

update student set $a &s I C33.

If 7ou a e not spe!if7ing an7 !ondition this will update enti e ta%le5 update student set $a &s I C33 whe e no I 1. update student set $a &s I C33# na$e I =9enu= whe e no I 2.

SQLSQL-

USIN8 D'L'T' This !an %e used to delete the ta%le data te$po a il75 S7nta/0 Delete ,table_name- whe e ,condition-. '/0
SQL-

delete student.

If 7ou a e not spe!if7ing an7 !ondition this will delete enti e ta%le5 delete student whe e no I 1.

SQL-

17

USIN8 DDL
USIN8 (LT'R This !an %e used to add o a) e$ove !olu$ns and to $odif7 the p e!ision of the datat7pe5

(DDIN8 COLUMN

s7nta/0 alte ta%le ,table_name- add ,col datatype-. '/0


SQL-

alte ta%le student add sdo% date.

%)

R'MO9IN8 COLUMN

s7nta/0 alte ta%le ,table_name- d op ,col datatype-. '/0


SQL-

alte ta%le student d op !olu$n sdo%.

!)

INCR'(SIN8 OR D'CR'(SIN8 AR'CISION OB ( COLUMN

s7nta/0 alte ta%le ,table_name- $odif7 ,col datatype-. '/0


SQL-

alte ta%le student $odif7 $a &s nu$%e (C).

E To de! ease p e!ision the !olu$n should %e e$pt75 d)

M(FIN8 COLUMN UNUS'D

s7nta/0 alte ta%le ,table_name- set unused !olu$n ,col-. '/0


SQL-

alte ta%le student set unused !olu$n $a &s.

'ven though the !olu$n is unused still it will o!!up7 $e$o 75 d)

DROAAIN8 UNUS'D COLUMNS

s7nta/0 alte ta%le ,table_name- d op unused !olu$ns.

18
'/0
SQL-

alte ta%le student d op unused !olu$ns.

E *ou !an not d op individual unused !olu$ns of a ta%le5 e)

R'N(MIN8 COLUMN

s7nta/0 alte ta%le ,table_name- ena$e !olu$n ,old_col_name- to ,new_col_name-. '/0


SQL-

alte ta%le student ena$e !olu$n $a &s to s$a &s.

USIN8 TRUNC(T' This !an %e used to delete the enti e ta%le data pe $anentl75 S7nta/0 t un!ate ta%le ,table_name-. '/0
SQL-

t un!ate ta%le student.

USIN8 DROA This will %e used to d op the data%ase o%Ke!t. S7nta/0 D op ta%le ,table_name-. '/0
SQL-

d op ta%le student.

USIN8 R'N(M' This will %e used to ena$e the data%ase o%Ke!t. S7nta/0 ena$e ,oldNtable_name- to ,new_table_name-. '/0
SQL-

ena$e student to stud.

19

USIN8 TCL
USIN8 COMMIT This will %e used to save the wo &5 Co$$it is of two t7pes5 a) I$pli!it '/pli!it

IMALICIT

This will %e issued %7 o a!le inte nall7 in two situations5 %) 6hen an7 DDL ope ation is pe fo $ed5 6hen 7ou a e e/iting f o$ SQL E ALUS5

'+ALICIT

This will %e issued %7 the use 5 S7nta/0 Co$$it o !o$$it wo &. E 6hen eve 7ou !o$$itted then the t ansa!tion was !o$pleted5 USIN8 ROLL)(CF This will undo the ope ation5 This will %e applied in two $ethods5 S7nta/0 Roll o O Roll%a!& o oll%a!& wo &. E 6hile p o!ess is going on# if suddenl7 powe goes then o a!le will oll%a!& the t ansa!tion5 USIN8 S(9'AOINT *ou !an use savepoints to oll%a!& po tions of 7ou !u ent set of t ansa!tions5 oll wo &. Upto p evious !o$$it Upto p evious oll%a!&

20
S7nta/0 Savepoint ,savepoint_name-. '/0
SQLSQLSQLSQLSQLSQLSQLSQL-

savepoint s2. inse t into student values(2# La;# 233). savepoint s1. inse t into student values(1# L%;# 133). savepoint s4. inse t into student values(4# L!;# 433). savepoint s@. inse t into student values(@# Ld;# @33).

)efo e oll%a!&
SQL-

sele!t E f o$ student. NO N(M' """ """"""" 2 1 4 @ a % ! d M(RFS """""""""" 233 133 433 @33

@ ows sele!ted5 oll%a!& to savepoint s4. O


SQL-

SQL-

oll%a!& to s4.

This will oll%a!& last two e!o ds5


SQL-

sele!t E f o$ student. NO N(M' """ """"""" 2 1 a % M(RFS """""""""" 233 133

1 ows sele!ted5

21

USIN8 DCL
DCL !o$$ands a e used to g anting and evo&ing the pe $issions5 USIN8 8R(NT This is used to g ant the p ivileges to othe use s5 S7nta/0 8 ant ,privile es- on ,ob!ect_name- to ,user_name- Owith g ant optionP. '/0
SQLSQLSQL-

g ant sele!t on student to sudha. g ant all on student to sudha.

"" 7ou !an give individual p ivilege "" 7ou !an give set of p ivileges "" 7ou !an give all p ivileges

g ant sele!t# inse t on student to sudha.

The sudha use has to use dot $ethod to a!!ess the o%Ke!t5
SQL-

sele!t E f o$ sa&eth5student.

The sudha use !an not g ant pe $ission on student ta%le to othe use s5 To get this t7pe of option use the following5
SQL-

g ant all on student to sudha with g ant option.

Now sudha use also g ant pe $issions on student ta%le5 USIN8 R'9OF' This is used to evo&e the p ivileges f o$ the use s to whi!h 7ou g anted the p ivileges5 S7nta/0 Revo&e ,privile es- on ,ob!ect_name- f o$ ,user_name-. '/0
SQLSQLSQL-

evo&e sele!t on student fo $ sudha. evo&e all on student f o$ sudha.

"" 7ou !an evo&e individual p ivilege "" 7ou !an evo&e set of p ivileges

evo&e sele!t# inse t on student f o$ sudha.

"" 7ou !an evo&e all p ivileges

22

USIN8 (LI(S'S
CR'(T' 6IT: S'L'CT 6e !an ! eate a ta%le using e/isting ta%le Oalong with dataP5 S7nta/0 C eate ta%le ,new_table_name- Ocol1, col2, col3 ... colnP as sele!t E f o$ ,old_table_name-. '/0
SQL-

! eate ta%le student2 as sele!t E f o$ student.

C eating ta%le with 7ou own !olu$n na$es5


SQL-

! eate ta%le student1(sno# sna$e# s$a &s) as sele!t E f o$ student.

C eating ta%le with spe!ified !olu$ns5


SQL-

! eate ta%le student4 as sele!t no#na$e f o$ student.

C eating ta%le with out ta%le data5


SQL-

! eate ta%le student1(sno# sna$e# s$a &s) as sele!t E f o$ student whe e 2 I 1.

In the a%ove whe e !lause give an7 !ondition whi!h does not satisf75 INS'RT 6IT: S'L'CT Using this we !an inse t e/isting ta%le data to a anothe ta%le in a single t ip5 )ut the ta%le st u!tu e should %e sa$e5 S7nta/0 Inse t into ,table1- sele!t E f o$ ,table2-. '/0
SQL-

inse t into student2 sele!t E f o$ student.

Inse ting data into spe!ified !olu$ns


SQL-

inse t into student2(no# na$e) sele!t no# na$e f o$ student.

COLUMN (LI(S'S S7nta/0 "elect #or inal_col$ #alias_name$ %rom #tableNna$e-.

23
'/0
SQL-

sele!t no sno f o$ student. o sele!t no QsnoR f o$ student.

SQL-

T()L' (LI(S'S If 7ou a e using ta%le aliases 7ou !an use dot $ethod to the !olu$ns5 S7nta/0 Sele!t ,alias_name-5,col1-# ,alias_name-5,col2- S ,alias_name-5,coln- f o$ ,table_name- ,alias_name-. '/0
SQL-

sele!t s5no# s5na$e f o$ student s.

24

USIN8 M'R8'
M'R8' *ou !an use $e ge !o$$and to pe fo $ inse t and update in a single !o$$and5 '/0
SQL-

Me ge into student2 s2 Using (sele!t EB o$ student1) s1 On(s25noIs15no) 6hen $at!hed then Update set $a &s I s15$a &s 6hen not $at!hed then Inse t (s25no#s25na$e#s25$a &s) 9alues(s15no#s15na$e#s15$a &s).

In the a%ove the two ta%les a e with the sa$e st u!tu e %ut we !an $e ge diffe ent st u!tu ed ta%les also %ut the datat7pe of the !olu$ns should $at!h5 (ssu$e that student2 has !olu$ns li&e no#na$e#$a &s and student1 has !olu$ns li&e no# na$e# hno# !it75 Me ge into student2 s2 Using (sele!t EB o$ student1) s1 On(s25noIs15no) 6hen $at!hed then Update set $a &s I s15hno 6hen not $at!hed then Inse t (s25no#s25na$e#s25$a &s) 9alues(s15no#s15na$e#s15hno).

SQL-

25

MULTI)L' INS'RTS
6e have ta%le !alled D'AT with the following !olu$ns and data D'ATNO """""""" 23 13 43 @3 a) DN(M' """""""" a!!ounting esea !h sales ope ations LOC """" new 7o & dallas Chi!ago %oston

CR'(T' STUD'NT T()L' SQL-

C eate ta%le student(no nu$%e (1)#na$e va !ha (1)#$a &s nu$%e (4)).

%)

MULTI INS'RT 6IT: (LL BI'LDS SQL-

Inse t all Into student values(2#;a;#233) Into student values(1#;%;#133) Into student values(4#;!;#433) Sele!t Ef o$ dept whe e deptnoI23.

"" This inse ts 4 ows !)

MULTI INS'RT 6IT: SA'CIBI'D BI'LDS SQL-

inse t all Into student (no#na$e) values(@#;d;) Into student(na$e#$a &s) values(;e;#@33) Into student values(4#;!;#433) Sele!t Ef o$ dept whe e deptnoI23.

"" This inse ts 4 ows d)

MULTI INS'RT 6IT: DUALIC(T' RO6S SQL-

inse t all Into student values(2#;a;#233) Into student values(1#;%;#133)

26
Into student values(4#;!;#433) Sele!t Ef o$ dept whe e deptno - 23. "" This inse ts T ows %e!ause in the sele!t state$ent et ieves 4 e!o ds (4 inse ts fo ea!h ow et ieved) e)

MULTI INS'RT 6IT: CONDITIONS )(S'D SQL-

Inse t all 6hen deptno - 23 then Into student2 values(2#;a;#233) 6hen dna$e I LS(L'S; then Into student1 values(1#;%;#133) 6hen lo! I LN'6 *ORF; then Into student4 values(4#;!;#433) Sele!t Ef o$ dept whe e deptno-23.

"" This inse ts @ ows %e!ause the fi st !ondition satisfied 4 ti$es# se!ond !ondition satisfied on!e and the last none5 f)

MULTI INS'RT 6IT: CONDITIONS )(S'D (ND 'LS' SQL-

Inse t all 6hen deptno - 233 then Into student2 values(2#;a;#233) 6hen dna$e I LS; then Into student1 values(1#;%;#133) 6hen lo! I LN'6 *ORF; then Into student4 values(4#;!;#433) 'lse Into student values(@#;d;#@33) Sele!t Ef o$ dept whe e deptno-23.

"" This inse ts 4 e!o ds %e!ause the else satisfied 4 ti$es g)

MULTI INS'RT 6IT: CONDITIONS )(S'D (ND BIRST SQL-

Inse t fi st

27
6hen deptno I 13 then Into student2 values(2#;a;#233) 6hen dna$e I LR'S'(RC:; then Into student1 values(1#;%;#133) 6hen lo! I LN'6 *ORF; then Into student4 values(4#;!;#433) Sele!t Ef o$ dept whe e deptnoI13. "" This inse ts 2 e!o d %e!ause the fi st !lause avoid to !he!& the e$aining !onditions on!e the !ondition is satisfied5 h)

MULTI INS'RT 6IT: CONDITIONS )(S'D# BIRST (ND 'LS' SQL-

Inse t fi st 6hen deptno I 43 then Into student2 values(2#;a;#233) 6hen dna$e I LR; then Into student1 values(1#;%;#133) 6hen lo! I LN'6 *ORF; then Into student4 values(4#;!;#433) 'lse Into student values(@#;d;#@33) Sele!t Ef o$ dept whe e deptnoI13.

"" This inse ts 2 e!o d %e!ause the else !lause satisfied on!e i)

MULTI INS'RT 6IT: MULTI)L' T()L'S SQL-

Inse t all Into student2 values(2#;a;#233) Into student1 values(1#;%;#133) Into student4 values(4#;!;#433) Sele!t Ef o$ dept whe e deptnoI23.

"" This inse ts 4 ows EE *ou !an use $ulti ta%les with spe!ified fields# with dupli!ate ows# with !onditions# with fi st and else !lauses5

28

BUNCTIONS
Bun!tions !an %e !atego iUed as follows5 Single ow fun!tions 8 oup fun!tions

SIN8L' RO6 BUNCTIONS Single ow fun!tions !an %e !atego iUed into five5 These will %e applied fo ea!h ow and p odu!es individual output fo ea!h ow5 Nu$e i! fun!tions St ing fun!tions Date fun!tions Mis!ellaneous fun!tions Conve sion fun!tions

NUM'RIC BUNCTIONS

(%s Sign SV t Mod Nvl Aowe '/p Ln Log Ceil Bloo Round T un& )itand 8 eatest Least

29
a)
()S

Coales!e

(%solute value is the $easu e of the $agnitude of value5 (%solute value is alwa7s a positive nu$%e 5 S7nta/0 a%s (value) '/0
SQL-

sele!t a%s(C)# a%s("C)# a%s(3)# a%s(null) f o$ dual. ()S(C) C ()S("C) "C ()S(3) ()S(NULL) """""""""" """"""""""""" 3

"""""""""" """"""""""

%)

SI8N

Sign gives the sign of a value5 S7nta/0 sign (value) '/0


SQL-

sele!t sign(C)# sign("C)# sign(3)# sign(null) f o$ dual. SI8N(C) """""""""" 2 SI8N("C) """""""""" "2 SI8N(3) SI8N(NULL) """""""""" """""""""""""" 3

!)

SQRT

This will give the sVua e oot of the given value5 S7nta/0 sV t (value) '/0
SQL-

"" he e value $ust %e positive5

sele!t sV t(@)# sV t(3)# sV t(null)# sV t(2) f o$ dual. SQRT(@) """""""""" 1 SQRT(3) SQRT(NULL) """""""""" """"""""""""""" 3 SQRT(2) """""""""" 2

d)

MOD

This will give the e$ainde 5 S7nta/0 $od (value, divisor)

30
'/0
SQL-

sele!t $od(W#@)# $od(2#C)# $od(null#null)# $od(3#3)# $od("W#@) f o$ dual. MOD(W#@) """""""""""" 4 MOD(2#C) MOD(NULL#NULL) """""""""" """"""""""""""""""""" 2 MOD(3#3) MOD("W#@) """"""""""" """"""""""""" 3 "4

e)

N9L

This will su%stitutes the spe!ified value in the pla!e of null values5 S7nta/0 nvl (null_col, replacement_value) '/0
SQL-

sele!t E f o$ student. NO N(M' """ """"""" 2 1 4 a % ! M(RFS """"""""" 233 133

"" he e fo 4 d ow $a &s value is null

SQL-

sele!t no# na$e# nvl($a &s#433) f o$ student. NO N(M' N9L(M(RFS#433) """ """"""" """"""""""""""""""""" 2 1 4 a % ! 233 133 433

SQL-

sele!t nvl(2#1)# nvl(1#4)# nvl(@#4)# nvl(C#@) f o$ dual.

N9L(2#1) """""""""" 2

N9L(1#4) """""""""" 1

N9L(@#4) """""""""" @

N9L(C#@) """""""""" C

SQL-

sele!t nvl(3#3)# nvl(2#2)# nvl(null#null)# nvl(@#@) f o$ dual.

31

N9L(3#3) """""""""" 3 f)

N9L(2#2) N9L(null#null) N9L(@#@) """""""""" """"""""""""""""" 2 """""""""" @

AO6'R

Aowe is the a%ilit7 to aise a value to a given e/ponent5 S7nta/0 powe (value, e&ponent) '/0
SQL-

sele!t powe (1#C)# powe (3#3)# powe (2#2)# powe (null#null)# powe (1#"C) f o$ dual. AO6'R(1#C) AO6'R(3#3) AO6'R(2#2) AO6'R(NULL#NULL) AO6'R(1#"C) """""""""""""" """""""""""""" """"" """"""""" """"""""""""""""""""""" """"""""""""""" 41 2 2 53421C

g)

'+A

This will aise e value to the give powe 5 S7nta/0 e/p (value) '/0
SQL-

sele!t e/p(2)# e/p(1)# e/p(3)# e/p(null)# e/p("1) f o$ dual. '+A(2) """""""" '+A(1) """"""""" '+A(3) '+A(NULL) """""""" """"""""""""" 2 '+A("1) """""""""" 524C44C1G4

15W2G1G2G4 W54GT3CD2 h)

LN

This is %ased on natu al o %ase e loga ith$5 S7nta/0 ln (value) '/0


SQL-

"" he e value $ust %e g eate than Ue o whi!h is positive onl75

sele!t ln(2)# ln(1)# ln(null) f o$ dual. LN(2) """"""" 3 LN(1) """"""" LN(NULL) """"""""""""

5DT42@W2G2

32

Ln and '/p a e e!ip o!al to ea!h othe 5


'+A LN

(4) I 1353GCC4DT

(1353GCC4DT) I 4

i)

LO8

This is %ased on 23 %ased loga ith$5 S7nta/0 log (23# value) "" he e value $ust %e g eate than Ue o whi!h is positive onl75 '/0
SQL-

sele!t log(23#233)# log(23#1)# log(23#2)# log(23#null) f o$ dual. LO8(23#233) LO8(23#1) LO8(23#2) LO8(23#NULL) """"""""""""""" 1 """"""""""" 543231TTTD ('+A(2)# value) """""""""""" """"""""""""""""" 3

LN

(value) I

LO8

SQL-

sele!t ln(4)# log(e/p(2)#4) f o$ dual. LN(4) """"""" LO8('+A(2)#4) """"""""""""""""" 253TGD211T

253TGD211T K)

C'IL

This will p odu!e a whole nu$%e that is g eate than o eVual to the spe!ified value5 S7nta/0 !eil (value) '/0
SQL-

sele!t !eil(C)# !eil(C52)# !eil("C)# !eil( "C52)# !eil(3)# !eil(null) f o$ dual. C'IL(C) C'IL(C52) """"""""" """"""""""" C D C'IL("C) C'IL("C52) """""""""" """""""""""" "C "C C'IL(3) C'IL(NULL) """""""" """""""""""""" 3

&)

BLOOR

This will p odu!e a whole nu$%e that is less than o eVual to the spe!ified value5

33
S7nta/0 floo (value) '/0
SQL-

sele!t floo (C)# floo (C52)# floo ("C)# floo ( "C52)# floo (3)# floo (null) f o$ dual. BLOOR(C) BLOOR(C52) BLOOR("C) BLOOR("C52) """"""""""" """"""""""""" C C """""""""""" """""""""""""" "C "D BLOOR(3) BLOOR(NULL) """"""""""" """""""""""""""" 3

l)

ROUND

This will ounds nu$%e s to a given nu$%e of digits of p e!ision5 S7nta/0 ound (value, precision) '/0
SQL-

sele!t ound(214514@C)# ound(214514@C#1)# ound(214514C@#1) f o$ dual. ROUND(214514@C) ROUND(214514@C#3) ROUND(214514@C#1) ROUND(214514C@#1) """"""""""""""""""""" 214 """""""""""""""""""""""" """"""""""""""""""""""" """"""""""""""""""""""" 214 214514 21451@

SQL-

sele!t ound(214514@C#"2)# ound(214514@C#"1)# ound(214514@C#"4)# ound(214514@C#"@) f o$ dual.

ROUND(214514@C#"2) ROUND(214514@C#"1) ROUND(214514@C#"4) ROUND(214514@C#"@) """""""""""""""""""""""" """"""""""""""""""""""""" """""""""""""""""""""""" 213 233 3 """""""""""""""""""""""" 3

SQL-

sele!t ound(214#3)# ound(214#2)# ound(214#1) f o$ dual. ROUND(214#3) ROUND(214#2) ROUND(214#1) """"""""""""""""" """"""""""""""""" """""""""""""""" 214 214 214

SQL-

sele!t ound("214#3)# ound("214#2)# ound("214#1) f o$ dual. ROUND("214#3) ROUND("214#2) ROUND("214#1) """""""""""""""""" """"""""""""""""" "214 "214 """"""""""""""""""" "214

34
SQL-

sele!t ound(214#"2)# ound(214#"1)# ound(214#"4)# ound("214#"2)# ound("214#" 1)# ound("214#"4) f o$ dual.

ROUND(214#"2) ROUND(214#"1) ROUND(214#"4) ROUND("214#"2) ROUND("214#"1) ROUND("214#"4) """"""""""""" """"""""""""" """"""""""""" """""""""""""" """""""""""""" """""""""""""" 213 233 3 "213 "233 3

SQL- sele!t ound(null#null)# ound(3#3)# ound(2#2)# ound("2#"2)# ound("1#"1) f o$ dual. ROUND(NULL#NULL) ROUND(3#3) ROUND(2#2) ROUND("2#"2) ROUND("1#"1) """"""""""""""""""""""" """""""""""""" """""""""""""" """""""""""""""" """""""""""""""" 3 $) 2 3 3

TRUNC

This will t un!ates o !hops off digits of p e!ision f o$ a nu$%e 5 S7nta/0 t un! (value, precision) '/0
SQL-

sele!t t un!(214514@C)# t un!(214514@C#1)# t un!(214514C@#1) f o$ dual. TRUNC(214514@C) TRUNC(214514@C#1) TRUNC(214514C@#1) """"""""""""""""""""" """"""""""""""""""""""" """"""""""""""""""""""" 214 214514 214514

SQL-

sele!t t un!(214514@C#"2)# t un!(214514@C#"1)# t un!(214514@C#"4)# t un!(214514@C#"@) f o$ dual.

TRUNC(214514@C#"2) TRUNC(214514@C#"1) TRUNC(214514@C#"4) TRUNC(214514@C#"@) """""""""""""""""""""""" """""""""""""""""""""""" 213 233 """"""""""""""""""""""" """""""""""""""""""""""" 3 3

SQL-

sele!t t un!(214#3)# t un!(214#2)# t un!(214#1) f o$ dual.

35

TRUNC(214#3) TRUNC(214#2) TRUNC(214#1) """""""""""""""" 214 """""""""""""""" """"""""""""""""" 214 214

SQL-

sele!t t un!("214#3)# t un!("214#2)# t un!("214#1) f o$ dual. TRUNC("214#3) TRUNC("214#2) TRUNC("214#1) """"""""""""""""" "214 """"""""""""""""" """"""""""""""""" "214 "214

SQL-

sele!t t un!(214#"2)# t un!(214#"1)# t un!(214#"4)# t un!("214#"2)# t un!("214#1)# t un!("214#"4) f o$ dual.

TRUNC(214#"2) TRUNC(214#"1) TRUNC(214#"4) TRUNC("214#"2) TRUNC("214#1) TRUNC(" 214#"4) """"""""""""" """"""""""""" """"""""""""" """""""""""""" """"""""""""" """""""""""""" 213 233 3 "213 "214 3

SQL-

sele!t t un!(null#null)# t un!(3#3)# t un!(2#2)# t un!("2#"2)# t un!("1#"1) f o$ dual. TRUNC(NULL#NULL) TRUNC(3#3) TRUNC(2#2) TRUNC("2#"2) TRUNC("1#"1) """"""""""""""""""""""" """"""""""""" """"""""""""" """"""""""""""" """""""""""""""" 3 2 3 3

n)

)IT(ND

This will pe fo $ %itwise and ope ation5 S7nta/0 %itand (value1, value2) '/0
SQL-

sele!t %itand(1#4)# %itand(3#3)# %itand(2#2)# %itand(null#null)# %itand("1#"4) f o$ dual. )IT(ND(1#4) )IT(ND(3#3) )IT(ND(2#2) )IT(ND(NULL#NULL) )IT(ND("1#"4) """""""""""""" """"""""""""""" """""""""""""" 1 3 2 """""""""""""""""""""""" """"""""""""""""" "@

36

o)

8R'(T'ST

This will give the g eatest nu$%e 5 S7nta/0 g eatest (value1, value2, value3 valuen) '/0
SQL-

sele!t g eatest(2# 1# 4)# g eatest("2# "1# "4) f o$ dual. 8R'(T'ST(2#1#4) 8R'(T'ST("2#"1#"4) """""""""""""""""""" """"""""""""""""""""""" 4 "2

p)
L'(ST

If all the values a e Ue os then it will displa7 Ue o5 If all the pa a$ete s a e nulls then it will displa7 nothing5 If an7 of the pa a$ete s is null it will displa7 nothing5

This will give the least nu$%e 5 S7nta/0 least (value1, value2, value3 valuen) '/0
SQL-

sele!t least(2# 1# 4)# least("2# "1# "4) f o$ dual. L'(ST(2#1#4) 2 L'(ST("2#"1#"4) "4

"""""""""""""""""""" """"""""""""""""""""""" If all the values a e Ue os then it will displa7 Ue o5 If all the pa a$ete s a e nulls then it will displa7 nothing5 If an7 of the pa a$ete s is null it will displa7 nothing5

V)

CO(L'SC'

This will etu n fi st non"null value5 S7nta/0 !oales!e (value1, value2, value3 valuen) '/0
SQL-

sele!t !oales!e(2#1#4)# !oales!e(null#1#null#C) f o$ dual. CO(L'SC'(2#1#4) CO(L'SC'(NULL#1#NULL#C) """"""""""""""""""" """""""""""""""""""""""""""""""

37
2
STRIN8 BUNCTIONS

a)
INITC(A

Init!ap Uppe Lowe Length Rpad Lpad Lt i$ Rt i$ T i$ T anslate Repla!e Sounde/ Con!at ( L XX L Con!atenation ope ato ) (s!ii Ch Su%st Inst De!ode 8 eatest Least Coales!e

This will !apitaliUe the initial lette of the st ing5 S7nta/0 init!ap (strin ) '/0
SQL-

sele!t init!ap(=!o$pute =) f o$ dual. INITC(A """"""""""" Co$pute

%)

UAA'R

38
This will !onve t the st ing into uppe !ase5 S7nta/0 uppe (strin ) '/0
SQL-

sele!t uppe (=!o$pute =) f o$ dual. UAA'R """""""""""


COMAUT'R

!)

LO6'R

This will !onve t the st ing into lowe !ase5 S7nta/0 lowe (strin ) '/0
SQL-

sele!t lowe (=COMAUT'R=) f o$ dual. LO6'R """"""""""" !o$pute

d)

L'N8T:

This will give length of the st ing5 S7nta/0 length (strin ) '/0
SQL-

sele!t length(=!o$pute =) f o$ dual. L'N8T: """"""""""" G

e)

RA(D

This will allows 7ou to pad the ight side of a !olu$n with an7 set of !ha a!te s5 S7nta/0 pad (strin , len t' (, paddin _c'ar)) '/0
SQL-

sele!t pad(=!o$pute =#2C#=E=)# pad(=!o$pute =#2C#=EY=) f o$ dual. RA(D(=COMAUT'R= RA(D(=COMAUT'R= """""""""""""""""""""" """""""""""""""""""""" !o$pute EEEEEEE !o$pute EYEYEYE

39

"" Default padding !ha a!te was %lan& spa!e5 f)


LA(D

This will allows 7ou to pad the left side of a !olu$n with an7 set of !ha a!te s5 S7nta/0 lpad (strin , len t' (, paddin _c'ar)) '/0
SQL-

sele!t lpad(=!o$pute =#2C#=E=)# lpad(=!o$pute =#2C#=EY=) f o$ dual. LA(D(=COMAUT'R= LA(D(=COMAUT'R= """"""""""""""""""""" EEEEEEE!o$pute """"""""""""""""""""" EYEYEYE!o$pute

"" Default padding !ha a!te was %lan& spa!e5 g)

LTRIM

This will t i$ off unwanted !ha a!te s f o$ the left end of st ing5 S7nta/0 lt i$ (strin '/0
SQL-

(,unwanted_c'ars))

sele!t lt i$(=!o$pute =#=!o=)# lt i$(=!o$pute =#=!o$=) f o$ dual. LTRIM( LTRIM """""""" """"""""" $pute pute

SQL-

sele!t lt i$(=!o$pute =#=pute =)# lt i$(=!o$pute =#=o$pute =) f o$ dual. LTRIM(=C LTRIM(=C """""""""" !o$pute """""""""" !o$pute

"" If 7ou haven;t spe!if7 an7 unwanted !ha a!te s it will displa7 enti e st ing5 h)

RTRIM

This will t i$ off unwanted !ha a!te s f o$ the ight end of st ing5 S7nta/0 t i$ (strin (, unwanted_c'ars))

40
'/0
SQL-

sele!t t i$(=!o$pute =#=e =)# t i$(=!o$pute =#=te =) f o$ dual. RTRIM( RTRIM """""""" """"""""" !o$put !o$pu

SQL-

sele!t t i$(=!o$pute =#=!o$put;)# t i$(=!o$pute =#=!o$pute=) f o$ dual. RTRIM(=C RTRIM(=C """""""""" !o$pute """""""""" !o$pute

"" If 7ou haven;t spe!if7 an7 unwanted !ha a!te s it will displa7 enti e st ing5 i)

TRIM

This will t i$ off unwanted !ha a!te s f o$ the %oth sides of st ing5 S7nta/0 t i$ (unwanted_c'ars f o$ strin ) '/0
SQL-

sele!t t i$( =i= f o$ =indiani=) f o$ dual. TRIM( """"" ndian sele!t t i$( leading=i= f o$ =indiani=) f o$ dual. TRIM(L """""" ndiani sele!t t i$( t ailing=i= f o$ =indiani=) f o$ dual. TRIM(T """""" indian "" this will wo & as "" this will wo & as

SQL-

LTRIM

SQL-

RTRIM

K)

TR(NSL(T'

41
This will epla!e the set of !ha a!te s# !ha a!te %7 !ha a!te 5 S7nta/0 t anslate (strin , old_c'ars, new_c'ars) '/0
SQL-

sele!t t anslate(=india=#=in=#=/7=) f o$ dual. TR(NS """""""" /7d/a

&)

R'AL(C'

This will epla!e the set of !ha a!te s# st ing %7 st ing5 S7nta/0 epla!e (strin , old_c'ars (, new_c'ars)) '/0
SQL-

sele!t epla!e(=india=#=in=#=/7=)# epla!e(Lindia;#;in;) f o$ dual. R'AL(C' +7dia R'AL(C' dia

""""""""""" """""""""""

l)

SOUND'+

This will %e used to find wo ds that sound li&e othe wo ds# e/!lusivel7 used in whe e !lause5 S7nta/0 sounde/ (strin ) '/0
SQL-

sele!t E f o$ e$p whe e sounde/(ena$e) I sounde/(=SMIT=). >O) """"" CL'RF M8R :IR'D(T' """"" """""""""""" WT31 2W"D'C"G3 S(L C33 D'ATNO 13

'MANO 'N(M' """""""" """""""" W4DT $) SMIT:

""""""""" """"""""""

CONC(T

This will %e used to !o$%ine two st ings onl75 S7nta/0 !on!at (strin 1, strin 2) '/0
SQL-

sele!t !on!at(=!o$pute =#= ope ato =) f o$ dual. CONC(T(=COMAUT'R=

42
""""""""""""""""""""""""" !o$pute ope ato If 7ou want to !o$%ine $o e than two st ings 7ou have to use !on!atenation ope ato (XX)5
SQL-

sele!t =how= XX = a e= XX = 7ou= f o$ dual. =:O6=XX=(R' """"""""""""""" how a e 7ou

n)

(SCII

This will etu n the de!i$al ep esentation in the data%ase !ha a!te set of the fi st !ha a!te of the st ing5 S7nta/0 as!ii (strin ) '/0
SQL-

sele!t as!ii(=a=)# as!ii(=apple=) f o$ dual. (SCII(=(=) (SCII(=(AAL'=) """""""""""" """""""""""""""""" TW TW

o)

C:R

This will etu n the !ha a!te having the %ina 7 eVuivalent to the st ing in eithe the data%ase !ha a!te set o the national !ha a!te set5 S7nta/0 !h (number) '/0
SQL-

sele!t !h (TW) f o$ dual. C:R """"" a

p)

SU)STR

This will %e used to e/t a!t su%st ings5 S7nta/0 su%st (strin , start_c'r_count (, no_o%_c'ars)) '/0

43
SQL-

sele!t su%st (=!o$pute =#1)# su%st (=!o$pute =#1#C)# su%st (=!o$pute =#4#W) f o$ dual. SU)STR( SU)ST SU)STR """""""""" """"""" """""""" o$pute o$put $pute If no_o%_c'ars pa a$ete is negative then it will displa7 nothing5 If %oth pa a$ete s e/!ept strin a e null o Ue os then it will displa7 nothing5

If no_o%_c'ars pa a$ete is g eate than the length of the st ing then it igno es and !al!ulates %ased on the o ginal st ing length5 If start_c'r_count is negative then it will e/t a!t the su%st ing f o$ ight end5 2 C "G 1 O "W 4 M "D @ A "C C U "@ D T "4 W ' "1 G R "2

V)

INSTR

This will allows 7ou fo sea !hing th ough a st ing fo set of !ha a!te s5 S7nta/0 inst (strin , searc'_str (, start_c'r_count (, occurrence) )) '/0
SQL-

sele!t inst (=info $ation=#=o=#@#2)# inst (=info $ation=#=o=#@#1) f o$ dual. INSTR(=INBORM(TION=#=O=#@#2) INSTR(=INBORM(TION=#=O=#@#1) """""""""""""""""""""""""""""""""""" """"""""""""""""""""""""""""""""""""" @ 23

If 7ou a e not spe!if7ing start_c'r_count and occurrence then it will sta t sea !h f o$ the %eginning and finds fi st o!!u en!e onl75 If %oth pa a$ete s start_c'r_count and occurrence a e null# it will displa7 nothing5

D'COD'

De!ode will a!t as value %7 value su%stitution5 Bo eve 7 value of field# it will !he!&s fo a $at!h in a se ies of if?then tests5 S7nta/0 de!ode (value, i%1, t'en1, i%2, t'en2, . else). '/0
SQL-

sele!t sal# de!ode(sal#C33#=Low=#C333#=:igh=#=Mediu$=) f o$ e$p.

44

S(L """"" C33 1C33 1333 4C33 4333 C333 @333 C333 2G33 2133 1333 1W33 1133 4133

D'COD' """"""""" Low Mediu$ Mediu$ Mediu$ Mediu$ :igh Mediu$ :igh Mediu$ Mediu$ Mediu$ Mediu$ Mediu$ Mediu$

2@ ows sele!ted5 sele!t de!ode(2#2#4)# de!ode(2#1#4#@#@#D) f o$ dual. D'COD'(2#2#4) D'COD'(2#1#4#@#@#D) """"""""""""""""" """""""""""""""""""""""" 4 D If the nu$%e of pa a$ete s a e odd and diffe ent then de!ode will displa7 nothing5 If the nu$%e of pa a$ete s a e even and diffe ent then de!ode will displa7 last value5 s) If all the pa a$ete s a e null then de!ode will displa7 nothing5 If all the pa a$ete s a e Ue os then de!ode will displa7 Ue o5

SQL-

8R'(T'ST

This will give the g eatest st ing5 S7nta/0 g eatest (strn 1, strin 2, strin 3 strin n) '/0
SQL-

sele!t g eatest(=a=# =%=# =!=)# g eatest(=satish=#=s inu=#=sa&eth=) f o$ dual.

45

8R'(T 8R'(T """"""" """"""" ! t) s inu

If all the pa a$ete s a e nulls then it will displa7 nothing5 If an7 of the pa a$ete s is null it will displa7 nothing5

L'(ST

This will give the least st ing5 S7nta/0 g eatest (strn 1, strin 2, strin 3 strin n) '/0
SQL-

sele!t least(=a=# =%=# =!=)# least(=satish=#=s inu=#=sa&eth=) f o$ dual.

L'(ST L'(ST """"""" """"""" a u) sa&eth

If all the pa a$ete s a e nulls then it will displa7 nothing5 If an7 of the pa a$ete s is null it will displa7 nothing5

CO(L'SC'

This will gives the fi st non"null st ing5 S7nta/0 !oales!e (strn 1, strin 2, strin 3 strin n) '/0
SQL-

sele!t !oales!e(=a=#=%=#=!=)# !oales!e(null#=a=#null#=%=) f o$ dual. CO(L'SC' CO(L'SC' """"""""""" a """"""""""" a

46

D(T' BUNCTIONS

S7sdate Cu entNdate Cu entNti$esta$p S7sti$esta$p Lo!alti$esta$p D%ti$eUone Sessionti$eUone ToN!ha ToNdate (ddN$onths MonthsN%etween Ne/tNda7 LastNda7 '/t a!t 8 eatest Least Round T un! NewNti$e Coales!e
DD"MON"**5

O a!le default date fo $at is

6e !an !hange the default fo $at to ou desi ed fo $at %7 using the following !o$$and5
SQL-

alte session set nlsNdateNfo $at I LDD"MONT:"****;.

)ut this will e/pi e on!e the session was !losed5 a)

S*SD(T'

This will give the !u ent date and ti$e5 '/0


SQL-

sele!t s7sdate f o$ dual. S*SD(T'

47
""""""""""" 1@"D'C"3D %)
CURR'NTND(T'

This will etu ns the !u ent date in the session;s ti$eUone5 '/0
SQL-

sele!t !u entNdate f o$ dual. CURR'NTND(T' """""""""""""""""" 1@"D'C"3D

!)

CURR'NTNTIM'ST(MA

This will etu ns the !u ent ti$esta$p with the a!tive ti$e Uone info $ation5 '/0
SQL-

sele!t !u entNti$esta$p f o$ dual. CURR'NTNTIM'ST(MA """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 1@"D'C"3D 345@15@254G44DT (M H3C043

d)

S*STIM'ST(MA

This will etu ns the s7ste$ date# in!luding f a!tional se!onds and ti$e Uone of the data%ase5 '/0
SQL-

sele!t s7sti$esta$p f o$ dual. S*STIM'ST(MA """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 1@"D'C"3D 345@T5425G433TT (M H3C043

e)

LOC(LTIM'ST(MA

This will etu ns lo!al ti$esta$p in the a!tive ti$e Uone info $ation# with no ti$e Uone info $ation shown5 '/0
SQL-

sele!t lo!alti$esta$p f o$ dual.

48
LOC(LTIM'ST(MA """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 1@"D'C"3D 345@@52G5C31GW@ (M
D)TIM'ZON'

f)

This will etu ns the !u ent data%ase ti$e Uone in UTC fo $at5 (Coo dinated Unive sal Ti$e) '/0
SQL-

sele!t d%ti$eUone f o$ dual. D)TIM'ZON' """"""""""""""" "3W033

g)

S'SSIONTIM'ZON'

This will etu ns the value of the !u ent session;s ti$e Uone5 '/0
SQL-

sele!t sessionti$eUone f o$ dual. S'SSIONTIM'ZON' """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" H3C043

h)

TONC:(R

This will %e used to e/t a!t va ious data fo $ats5 The availa%le date fo $ats as follows5 S7nta/0 toN!ha (date# %ormat)
D(T' BORM(TS

D DD DDD MM MON MONT: RM D* D(* * **

"" "" "" "" "" "" "" "" "" "" ""

No of da7s in wee& No of da7s in $onth No of da7s in 7ea No of $onth Th ee lette a%% eviation of $onth Bull7 spelled out $onth Ro$an nu$e al $onth Th ee lette a%% eviated da7 Bull7 spelled out da7 Last one digit of the 7ea Last two digits of the 7ea

49
*** **** S**** I I* I** I*** *# *** *'(R CC Q 6 66 I6 :: MI SS BB (M (5M (D (5D BM T: SA SAT: T:SA

"" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" "" ""

Last th ee digits of the 7ea Bull fou digit 7ea One digit 7ea f o$ ISO standa d Two digit 7ea f o$ ISO standa d Th ee digit 7ea f o$ ISO standa d Bou digit 7ea f o$ ISO standa d *ea with !o$$a Bull7 spelled out 7ea Centu 7 No of Vua te s No of wee&s in $onth No of wee&s in 7ea No of wee&s in 7ea f o$ ISO standa d :ou s Minutes Se!onds B a!tional se!onds Displa7s (M o AM depending upon ti$e of da7 Displa7s (5M o A5M depending upon ti$e of da7 Displa7s (D o )C depending upon the date Displa7s (D o )C depending upon the date A efi/ to $onth o da7# supp esses padding of $onth o da7 Suffi/ to a nu$%e suffi/ to a nu$%e to %e spelled out Suffi/ !o$%ination of T: and SA to %e %oth spelled out sa$e as SAT:

Signed 7ea

o o o o

AM A5M )C )5C

"" "" "" "" "" "" "" "" ""

'/0
SQL-

sele!t toN!ha (s7sdate#=dd $onth 7777 hh0$i0ss a$ d7=) f o$ dual. TONC:(R(S*SD(T'#=DD MONT: ****::0MI """""""""""""""""""""""""""""""""""""""""""""""""""" 1@ de!e$%e 133D 31034014 p$ sun

SQL-

sele!t toN!ha (s7sdate#=dd $onth 7ea =) f o$ dual.

50

TONC:(R(S*SD(T'#=DDMONT:*'(R=) """"""""""""""""""""""""""""""""""""""""""""""""""""""" 1@ de!e$%e


SQL-

two thousand si/

sele!t toN!ha (s7sdate#=dd f$$onth 7ea =) f o$ dual. TONC:(R(S*SD(T'#=DD BMMONT: *'(R=) """"""""""""""""""""""""""""""""""""""""""""""""""""""" 1@ de!e$%e two thousand si/

SQL-

sele!t toN!ha (s7sdate#=ddth DDT:=) f o$ dual. TONC:(R(S """""""""""" 1@th 1@T: sele!t toN!ha (s7sdate#=ddspth DDSAT:=) f o$ dual. TONC:(R(S*SD(T'#=DDSAT:DDSAT: """""""""""""""""""""""""""""""""""""""""" twent7"fou th T6'NT*"BOURT:

SQL-

SQL-

sele!t toN!ha (s7sdate#=ddsp Ddsp DDSA =) f o$ dual. TONC:(R(S*SD(T'#=DDSADDSADDSA=) """""""""""""""""""""""""""""""""""""""""""""""" twent7"fou Twent7"Bou T6'NT*"BOUR

i)

TOND(T'

This will %e used to !onve t the st ing into data fo $at5 S7nta/0 toNdate (date) '/0
SQL-

sele!t toN!ha (toNdate(=1@?de!?133D=#=dd?$on?7777=)# =dd E $onth E da7=) f o$ dual.

51

TONC:(R(TOND(T'(=1@?D'C?13 """""""""""""""""""""""""" 1@ E de!e$%e E Sunda7

"" If 7ou a e not using toN!ha o a!le will displa7 output in default date fo $at5 K)
(DDNMONT:S

This will add the spe!ified $onths to the given date5 S7nta/0 addN$onths (date, no_o%_mont's) '/0
SQL-

sele!t addN$onths(toNdate(=22"Kan"2TT3=#=dd"$on"7777=)# C) f o$ dual. (DDNMONT:S """""""""""""""" 22">UN"T3 sele!t addN$onths(toNdate(=22"Kan"2TT3=#=dd"$on"7777=)# "C) f o$ dual. (DDNMONT: """"""""""""""" 22"(U8"GT

SQL-

&)

If no_o%_mont's is Ue o then it will displa7 the sa$e date5 If no_o%_mont's is null then it will displa7 nothing5

MONT:SN)'T6''N

This will give diffe en!e of $onths %etween two dates5 S7nta/0 $onthsN%etween (date1, date2) '/0
SQL-

sele!t $onthsN%etween(toNdate(=22"aug"2TT3=#=dd"$on"7777=)# toNdate(=22"Kan" 2TT3=#=dd"$on"7777=)) f o$ dual.

MONT:SN)'T6''N(TOND(T'(=22"(U8"2TT3=#=DD"MON"****=)#TOND(T'(=22">(N" 2TT3=#=DD"MON"****=))

52
""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" W
SQL-

sele!t $onthsN%etween(toNdate(=22"Kan"2TT3=#=dd"$on"7777=)# toNdate(=22"aug" 2TT3=#=dd"$on"7777=)) f o$ dual.

MONT:SN)'T6''N(TOND(T'(=22">(N"2TT3=#=DD"MON"****=)#TOND(T'(=22"(U8" 2TT3=#=DD"MON"****=)) """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" "W l)

N'+TND(*

This will p odu!e ne/t da7 of the given da7 f o$ the spe!ified date5 S7nta/0 ne/tNda7 (date, day) '/0
SQL-

sele!t ne/tNda7(toNdate(=1@"de!"133D=#=dd"$on"7777=)#=sun=) f o$ dual. N'+TND(*( """"""""""""" 42"D'C"3D

"" If the da7 pa a$ete is null then it will displa7 nothing5 $)

L(STND(*

This will p odu!e last da7 of the given date5 S7nta/0 lastNda7 (date) '/0
SQL-

sele!t lastNda7(toNdate(=1@"de!"133D=#=dd"$on"7777=)#=sun=) f o$ dual. L(STND(*( """"""""""""" 42"D'C"3D

n)

'+TR(CT

This is used to e/t a!t a po tion of the date value5 S7nta/0 e/t a!t ((7ea X $onth X da7 X hou X $inute X se!ond)# date)

53
'/0
SQL-

sele!t e/t a!t(7ea f o$ s7sdate) f o$ dual. '+TR(CT(*'(RBROMS*SD(T') """""""""""""""""""""""""""""""""""" 133D

"" *ou !an e/t a!t onl7 one value at a ti$e5 o)

8R'(T'ST

This will give the g eatest date5 S7nta/0 g eatest (date1, date2, date3 daten) '/0
SQL-

sele!t g eatest(toNdate(=22"Kan"T3=#=dd"$on"77=)#toNdate(=22"$a "T3=#=dd"$on" 77=)#toNdate(=22"ap "T3=#=dd"$on"77=)) f o$ dual. 8R'(T'ST( """"""""""""" 22"(AR"T3

p)

L'(ST

This will give the least date5 S7nta/0 least (date1, date2, date3 daten) '/0
SQL-

sele!t least(toNdate(=22"Kan"T3=#=dd"$on"77=)#toNdate(=22"$a "T3=#=dd"$on" 77=)#toNdate(=22"ap "T3=#=dd"$on"77=)) f o$ dual. L'(ST( """"""""""""" 22">(N"T3

V)

ROUND

Round will ounds the date to whi!h it was eVual to o g eate than the given date5 S7nta/0 ound (date, (da7 X $onth X 7ea *) If the se!ond pa a$ete was year then ound will !he!&s the $onth of the given date in the

54
following anges5

>(N >UL

"" ""

>UN D'C

If the $onth falls %etween >(N and >UN then it etu ns the fi st da7 of the !u ent 7ea 5 If the $onth falls %etween >UL and D'C then it etu ns the fi st da7 of the ne/t 7ea 5 If the se!ond pa a$ete was mont' then ound will !he!&s the da7 of the given date in the following anges5 2 2D "" "" 2C 42

If the da7 falls %etween 2 and 2C then it etu ns the fi st da7 of the !u ent $onth5 If the da7 falls %etween 2D and 42 then it etu ns the fi st da7 of the ne/t $onth5 If the se!ond pa a$ete was day then ound will !he!&s the wee& da7 of the given date in the following anges5
SUN T:U "" "" 6'D SUN

If the wee& da7 falls %etween SUN and 6'D then it etu ns the p evious sunda75 If the wee&da7 falls %etween T:U and SUN then it etu ns the ne/t sunda75 If the se!ond pa a$ete was null then it etu ns nothing5 If the 7ou a e not spe!if7ing the se!ond pa a$ete then ound will esets the ti$e to the %egining of the !u ent da7 in !ase of use spe!ified date5 If the 7ou a e not spe!if7ing the se!ond pa a$ete then ound will esets the ti$e to the %egining of the ne/t da7 in !ase of s7sdate5 '/0
SQL-

sele!t ound(toNdate(=1@"de!"3@=#=dd"$on"77=)#=7ea =)# ound(toNdate(=22"$a " 3D=#=dd"$on"77=)#=7ea =) f o$ dual.

55
ROUND(TON ROUND(TON """""""""""" 32">(N"3C
SQL-

""""""""""""""" 32">(N"3D

sele!t ound(toNdate(=22"Kan"3@=#=dd"$on"77=)#=$onth=)# ound(toNdate(=2G"Kan" 3@=#=dd"$on"77=)#=$onth=) f o$ dual. ROUND(TON ROUND(TON """"""""""""" """"""""""""""" 32">(N"3@ 32"B')"3@

SQL-

sele!t ound(toNdate(=1D"de!"3D=#=dd"$on"77=)#=da7=)# ound(toNdate(=1T"de!" 3D=#=dd"$on"77=)#=da7=) f o$ dual. ROUND(TON ROUND(TON """""""""""""" """""""""""""" 1@"D'C"3D 42"D'C"3D

SQL-

sele!t toN!ha ( ound(toNdate(=1@"de!"3D=#=dd"$on"77=))# =dd $on 7777 hh0$i0ss a$=) f o$ dual. TONC:(R(ROUND(TOND(T'(= """"""""""""""""""""""""""""""""" 1@ de! 133D 21033033 a$

TRUNC

T un! will !hops off the date to whi!h it was eVual to o less than the given date5 S7nta/0 t un! (date, (da7 X $onth X 7ea *) If the se!ond pa a$ete was year then it alwa7s etu ns the fi st da7 of the !u ent 7ea 5 If the se!ond pa a$ete was mont' then it alwa7s etu ns the fi st da7 of the !u ent $onth5 If the se!ond pa a$ete was day then it alwa7s etu ns the p evious sunda75 If the se!ond pa a$ete was null then it etu ns nothing5 If the 7ou a e not spe!if7ing the se!ond pa a$ete then t un& will esets the ti$e to the %egining of the !u ent da75

56
'/0
SQL-

sele!t t un!(toNdate(=1@"de!"3@=#=dd"$on"77=)#=7ea =)# t un!(toNdate(=22"$a " 3D=#=dd"$on"77=)#=7ea =) f o$ dual. TRUNC(TON TRUNC(TON """"""""""""" """""""""""""" 32">(N"3@ 32">(N"3D

SQL-

sele!t t un!(toNdate(=22"Kan"3@=#=dd"$on"77=)#=$onth=)# t un!(toNdate(=2G"Kan" 3@=#=dd"$on"77=)#=$onth=) f o$ dual. TRUNC(TON TRUNC(TON """"""""""""" """"""""""""" 32">(N"3@ 32">(N"3@

SQL-

sele!t t un!(toNdate(=1D"de!"3D=#=dd"$on"77=)#=da7=)# t un!(toNdate(=1T"de!"3D=#=dd" $on"77=)#=da7=) f o$ dual. TRUNC(TON TRUNC(TON """"""""""""" """""""""""""" 1@"D'C"3D 1@"D'C"3D

SQL-

sele!t toN!ha (t un!(toNdate(=1@"de!"3D=#=dd"$on"77=))# =dd $on 7777 hh0$i0ss a$=) f o$ dual. TONC:(R(TRUNC(TOND(T'(= """"""""""""""""""""""""""""""""" 1@ de! 133D 21033033 a$

s)

N'6NTIM'

This will give the desi ed ti$eUone;s date and ti$e5 S7nta/0 newNti$e (date, current_time+one, desired_time+one) (vaila%le ti$eUones a e as follows5
TIM'ZON'S

(ST?(DT

""

(tlanti! standa d?da7 light ti$e

57
)ST?)DT CST?CDT 'ST?'DT 8MT :ST?:DT MST?MDT NST AST?ADT *ST?*DT

"" "" "" "" "" "" "" "" ""

)e ing standa d?da7 light ti$e Cent al standa d?da7 light ti$e 'aste n standa d?da7 light ti$e 8 eenwi!h $ean ti$e (las&a":awaii standa d?da7 light ti$e Mountain standa d?da7 light ti$e Newfoundland standa d ti$e Aa!ifi! standa d?da7 light ti$e *u&on standa d?da7 light ti$e

'/0
SQL-

sele!t toN!ha (newNti$e(s7sdate#=g$t=#=7st=)#=dd $on 7777 hh0$i0ss a$=) f o$ dual. TONC:(R(N'6NTIM'(S*SD(T """"""""""""""""""""""""""""""""""" 1@ de! 133D 310C2013 p$ sele!t toN!ha (newNti$e(s7sdate#=g$t=#=est=)#=dd $on 7777 hh0$i0ss a$=) f o$ dual. TONC:(R(N'6NTIM'(S*SD(T """"""""""""""""""""""" 1@ de! 133D 3D0C201D p$

SQL-

t)

CO(L'SC'

This will give the fi st non"null date5 S7nta/0 !oales!e (date1, date2, date3 daten) '/0
SQL-

sele!t !oales!e(=21"Kan"T3=#=24"Kan"TT=)# !oales!e(null#=21"Kan"T3=#=14"$a "TG=#null) f o$ dual. CO(L'SC'( CO(L'SC'( """"""""""""" """""""""""" 21"Kan"T3 21"Kan"T3

58

MISC'LL(N'OUS BUNCTIONS

a)
UID

Uid Use 9siUe

This will etu ns the intege value !o esponding to the use !u entl7 logged in5 '/0
SQL-

sele!t uid f o$ dual. UID """""""""" 42T

%)

US'R

This will etu ns the login;s use na$e5 '/0


SQL-

sele!t use f o$ dual. US'R """""""""""""""" S(F'T:

!)

9SIZ'

This will etu ns the nu$%e of %7tes in the e/p ession5 '/0
SQL-

sele!t vsiUe(214)# vsiUe(=!o$pute =)# vsiUe(=21"Kan"T3=) f o$ dual. 9SIZ'(214) 9SIZ'(=COMAUT'R=) 9SIZ'(=21">(N"T3=) """"""""""""" """"""""""""""""""""""" """""""""""""""""""""" 4 G T

59

CON9'RSION BUNCTIONS

a)
)INNTONNUM

)inNtoNnu$ Cha to owid Rowidto!ha ToNnu$%e ToN!ha ToNdate

This will !onve t the %ina 7 value to its nu$e i!al eVuivalent5 S7nta/0 %inNtoNnu$( binary_bits) '/0
SQL-

sele!t %inNtoNnu$(2#2#3) f o$ dual. )INNTONNUM(2#2#3) """""""""""""""""""""""" D

%)

If all the %its a e Ue o then it p odu!es Ue o5 If all the %its a e null then it p odu!es an e o 5

C:(RTORO6ID

This will !onve t a !ha a!te st ing to a!t li&e an inte nal o a!le ow identifie o !)

owid5

RO6IDTOC:(R

This will !onve t an inte nal o a!le ow identifie o d)

owid to !ha a!te st ing5

TONNUM)'R

This will !onve t a !ha o va !ha to nu$%e 5 e)

TONC:(R

This will !onve t a nu$%e o date to !ha a!te st ing5

60
f)
TOND(T'

This will !onve t a nu$%e # !ha o va !ha to a date5

8ROUA BUNCTIONS Su$ (vg Ma/ Min Count

8 oup fun!tions will %e applied on all the ows %ut p odu!es single output5 a)

SUM

This will give the su$ of the values of the spe!ified !olu$n5 S7nta/0 su$ (column) '/0
SQL-

sele!t su$(sal) f o$ e$p. SUM(S(L) """""""""" 4GD33

%)

(98

This will give the ave age of the values of the spe!ified !olu$n5 S7nta/0 avg (column) '/0
SQL-

sele!t avg(sal) f o$ e$p. (98(S(L) """"""""""""""" 1WCW52@1GD

!)

M(+

This will give the $a/i$u$ of the values of the spe!ified !olu$n5 S7nta/0 $a/ (column)

61
'/0
SQL-

sele!t $a/(sal) f o$ e$p. M(+(S(L) """""""""" C333

d)

MIN

This will give the $ini$u$ of the values of the spe!ified !olu$n5 S7nta/0 $in (column) '/0
SQL-

sele!t $in(sal) f o$ e$p. MIN(S(L) """""""""" C33

e)

COUNT

This will give the !ount of the values of the spe!ified !olu$n5 S7nta/0 !ount (column) '/0
SQL-

sele!t !ount(sal)#!ount(E) f o$ e$p. COUNT(S(L) """""""""""""" 2@ COUNT(E) """""""""""" 2@

62

CONSTR(INTS
Const aints a e !atego iUed as follows5 Do$ain integ it7 !onst aints Not null Che!& 'ntit7 integ it7 !onst aints UniVue A i$a 7 &e7 Refe ential integ it7 !onst aints Bo eign &e7 Const aints a e alwa7s atta!hed to a !olu$n not a ta%le5 6e !an add !onst aints in th ee wa7s5 Colu$n level Ta%le level (lte level "" along with the !olu$n definition "" afte the ta%le definition "" using alte !o$$and

6hile adding !onst aints 7ou need not spe!if7 the na$e %ut the t7pe onl7# o a!le will inte nall7 na$e the !onst aint5 If 7ou want to give a na$e to the !onst aint# 7ou have to use the !onst aint !lause5 NOT NULL This is used to avoid null values5 6e !an add this !onst aint in !olu$n level onl75 '/0

63
SQLSQL-

! eate ta%le student(no nu$%e (1) not null# na$e va !ha (23)# $a &s nu$%e (4)). ! eate ta%le student(no nu$%e (1) !onst aint nn not null# na$e va !ha (23)# $a &s nu$%e (4)).

C:'CF This is used to inse t the values %ased on spe!ified !ondition5 6e !an add this !onst aint in all th ee levels5 '/0
COLUMN L'9'L

SQL-

! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4) !he!& ($a &s - 433)). ! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4) !onst aint !h !he!&($a &s - 433)).

SQL-

T()L' L'9'L

SQL-

! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# !he!& ($a &s - 433)). ! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# !onst aint !h !he!&($a &s - 433)).

SQL-

(LT'R L'9'L

SQLSQL-

alte ta%le student add !he!&($a &s-433). alte ta%le student add !onst aint !h !he!&($a &s-433).

UNIQU' This is used to avoid dupli!ates %ut it allow nulls5 6e !an add this !onst aint in all th ee levels5 '/0
COLUMN L'9'L

SQLSQL-

! eate ta%le student(no nu$%e (1) uniVue# na$e va !ha (23)# $a &s nu$%e (4)). ! eate ta%le student(no nu$%e (1) !onst aint un uniVue# na$e va !ha (23)# $a &s

64
nu$%e (4)).

T()L' L'9'L

SQL-

! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# uniVue(no)). ! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# !onst aint un uniVue(no)).

SQL-

(LT'R L'9'L

SQLSQL-

alte ta%le student add uniVue(no). alte ta%le student add !onst aint un uniVue(no).

ARIM(R* F'* This is used to avoid dupli!ates and nulls5 This will wo & as !o$%ination of uniVue and not null5 A i$a 7 &e7 alwa7s atta!hed to the pa ent ta%le5 6e !an add this !onst aint in all th ee levels5 '/0
COLUMN L'9'L

SQLSQL-

! eate ta%le student(no nu$%e (1) p i$a 7 &e7# na$e va !ha (23)# $a &s nu$%e (4)). ! eate ta%le student(no nu$%e (1) !onst aint p& p i$a 7 &e7# na$e va !ha (23)# $a &s nu$%e (4)).

T()L' L'9'L

SQL-

! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# p i$a 7 &e7(no)). ! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# !onst aint p& p i$a 7 &e7(no)).

SQL-

(LT'R L'9'L

65
SQLSQL-

alte ta%le student add p i$a 7 &e7(no). alte ta%le student add !onst aint p& p i$a 7 &e7(no).

BOR'I8N F'* This is used to efe en!e the pa ent ta%le p i$a 7 &e7 !olu$n whi!h allows dupli!ates5 Bo eign &e7 alwa7s atta!hed to the !hild ta%le5 6e !an add this !onst aint in ta%le and alte levels onl75 '/0
T()L' L'9'L

SQL-

! eate ta%le e$p(e$pno nu$%e (1)# ena$e va !ha (23)# deptno nu$%e (1)# p i$a 7 &e7(e$pno)# fo eign &e7(deptno) efe en!es dept(deptno)). ! eate ta%le e$p(e$pno nu$%e (1)# ena$e va !ha (23)# deptno nu$%e (1)# !onst aint p& p i$a 7 &e7(e$pno)# !onst aint f& fo eign &e7(deptno) efe en!es dept(deptno)).

SQL-

(LT'R L'9'L

SQLSQL-

alte ta%le e$p add fo eign &e7(deptno) efe en!es dept(deptno). alte ta%le e$p add !onst aint f& fo eign &e7(deptno) efe en!es dept(deptno).

On!e the p i$a 7 &e7 and fo eign &e7 elationship has %een ! eated then 7ou !an not e$ove an7 pa ent e!o d if the dependent !hilds e/ists5 USIN8 ON D'LT' C(SC(D' )7 using this !lause 7ou !an e$ove the pa ent e!o d even it !hilds e/ists5 )e!ause when eve 7ou e$ove pa ent e!o d o a!le auto$ati!all7 e$oves all its dependent e!o ds f o$ !hild ta%le# if this !lause is p esent while ! eating fo eign &e7 !onst aint5 '/0
T()L' L'9'L

SQL-

! eate ta%le e$p(e$pno nu$%e (1)# ena$e va !ha (23)# deptno nu$%e (1)# p i$a 7 &e7(e$pno)# fo eign &e7(deptno) efe en!es dept(deptno) on delete !as!ade).

66
SQL-

! eate ta%le e$p(e$pno nu$%e (1)# ena$e va !ha (23)# deptno nu$%e (1)# !onst aint p& p i$a 7 &e7(e$pno)# !onst aint f& fo eign &e7(deptno) efe en!es dept(deptno) on delete !as!ade).

(LT'R L'9'L

SQLSQL-

alte ta%le e$p add fo eign &e7(deptno) efe en!es dept(deptno) on delete !as!ade. alte ta%le e$p add !onst aint f& fo eign &e7(deptno) efe en!es dept(deptno) on delete !as!ade.

COMAOSIT' F'*S ( !o$posite &e7 !an %e defined on a !o$%ination of !olu$ns5 6e !an define !o$posite &e7s on entit7 integ it7 and efe ential integ it7 !onst aints5 Co$posite &e7 !an %e defined in ta%le and alte levels onl75 '/0
UNIQU' (T()L' L'9'L)

SQL-

! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# uniVue(no#na$e)). ! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# !onst aint un uniVue(no#na$e)).

SQL-

UNIQU' ((LT'R L'9'L)

SQLSQL-

alte ta%le student add uniVue(no#na$e). alte ta%le student add !onst aint un uniVue(no#na$e).

ARIM(R* F'* (T()L' L'9'L)

SQL-

! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# p i$a 7 &e7(no#na$e)). ! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# !onst aint p& p i$a 7 &e7(no#na$e)).

SQL-

67
ARIM(R* F'* ((LT'R L'9'L)

SQLSQL-

alte ta%le student add p i$a 7 &e7(no#an$e). alte ta%le student add !onst aint p& p i$a 7 &e7(no#na$e).

BOR'I8N F'* (T()L' L'9'L)

SQL-

! eate ta%le e$p(e$pno nu$%e (1)# ena$e va !ha (23)# deptno nu$%e (1)# dna$e va !ha (23)# p i$a 7 &e7(e$pno)# fo eign &e7(deptno#dna$e) efe en!es dept(deptno#dna$e)). ! eate ta%le e$p(e$pno nu$%e (1)# ena$e va !ha (23)# deptno nu$%e (1)# dna$e va !ha (23)# !onst aint p& p i$a 7 &e7(e$pno)# !onst aint f& fo eign &e7(deptno#dna$e) efe en!es dept(deptno#dna$e)).

SQL-

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

SQLSQL-

alte ta%le e$p add fo eign &e7(deptno#dna$e) efe en!es dept(deptno#dna$e). alte ta%le e$p add !onst aint f& fo eign &e7(deptno#dna$e) efe en!es dept(deptno#dna$e).

D'B'RR()L' CONSTR(INTS 'a!h !onst aint has two additional att i%utes to suppo t defe ed !he!&ing of !onst aints5 Defe ed initiall7 i$$ediate Defe ed initiall7 defe ed

Defe ed initiall7 i$$ediate !he!&s fo !onst aint violation at the ti$e of inse t5 Defe ed initiall7 defe ed !he!&s fo !onst aint violation at the ti$e of !o$$it5 '/0
SQL-

! eate ta%le student(no nu$%e (1)# na$e va !ha (23)# $a &s nu$%e (4)# !onst aint un uniVue(no) defe ed initiall7 i$$ediate). ! eate ta%le student(no nu$%e (1)# na$e va !ha (23)# $a &s nu$%e (4)# !onst aint un uniVue(no) defe ed initiall7 defe ed).

SQL-

68

SQL-

alte ta%le student add !onst aint un uniVue(no) defe a%le initiall7 defe ed. set !onst aints all i$$ediate. set !onst aints all defe ed.

SQL-

This will ena%le all the !onst aints violations at the ti$e of inse ting5
SQL-

This will ena%le all the !onst aints violations at the ti$e of !o$$it5 OA'R(TIONS 6IT: CONSTR(INTS Aossi%le ope ations with !onst aints as follows5 'na%le Disa%le 'nfo !e D op


'N()L'

This will ena%le the !onst aint5 )efo e ena%le# the !onst aint will !he!& the e/isting data5 '/0
SQL-

alte ta%le student ena%le !onst aint un.

DIS()L'

This will disa%le the !onst aint5 '/0


SQL-

alte ta%le student ena%le !onst aint un.

'NBORC'

This will enfo !e the !onst aint athe than ena%le fo futu e inse ts o updates5 This will not !he!& fo e/isting data while enfo !ing data5 '/0
SQL-

alte ta%le student enfo !e !onst aint un.

DROA

69

This will e$ove the !onst aint5 '/0


SQL-

alte ta%le student d op !onst aint un.

On!e the ta%le is d opped# !onst aints auto$ati!all7 will d op5

C(S' (ND D'B(ULT

C(S' Case is si$ila to de!ode %ut easie to unde stand while going th ough !oding '/0 SQL- Sele!t sal# Case sal 6hen C33 then Llow; 6hen C333 then Lhigh; 'lse L$ediu$; 'nd !ase B o$ e$p. S(L """"" C33 1C33 1333 4C33 4333 C333 @333 C333 2G33 2133 1333 1W33 1133 C(S' """""""" low $ediu$ $ediu$ $ediu$ $ediu$ high $ediu$ high $ediu$ $ediu$ $ediu$ $ediu$ $ediu$

70
4133 $ediu$

2@ ows sele!ted5

D'B(ULT ,e%ault !an %e !onside ed as a su%stitute %ehavio of not null !onst aint when applied to new ows %eing ente ed into the ta%le5 6hen 7ou define a !olu$n with the de%ault &e7wo d followed %7 a value# 7ou a e a!tuall7 telling the data%ase that# on inse t if a ow was not assigned a value fo this !olu$n# use the default value that 7ou have spe!ified5 Default is applied onl7 du ing inse tion of new ows5 '/0
SQLSQLSQL-

! eate ta%le student(no nu$%e (1) default 22#na$e va !ha (1)). inse t into student values(2#=a=). inse t into student(na$e) values(=%=). sele!t E f o$ student. NO 2 22 N(M' a %

SQL-

"""""" """""""""

SQLSQL-

inse t into student values(null# L!;). sele!t E f o$ student. NO 2 22 N(M' a % C

"""""" """""""""

"" Default !an not ove ide nulls5

71

()STR(CT D(T( T*A'S


So$e ti$es 7ou $a7 want t7pe whi!h holds all t7pes of data in!luding nu$%e s# !ha s and spe!ial !ha a!te s so$ething li&e this5 *ou !an not a!hieve this using p e"defined t7pes5 *ou !an define !usto$ t7pes whi!h holds 7ou desi ed data5 '/0 Suppose in a ta%le we have add ess !olu$n whi!h holds hno and !it7 info $ation5 6e will define a !usto$ t7pe whi!h holds %oth nu$e i! as well as !ha data5
CR'(TIN8 (DT

SQL-

! eate t7pe add as o%Ke!t(hno nu$%e (4)#!it7 va !ha (23)). ?

CR'(TIN8 T()L' )(S'D ON (DT

SQL-

! eate ta%le student(no nu$%e (1)#na$e va !ha (1)#add ess add ).

INS'RTIN8 D(T( INTO (DT T()L'S

SQLSQLSQL-

inse t into student values(2#=a=#add (222#=h7d=)). inse t into student values(1#=%=#add (111#=%ang=)). inse t into student values(4#=!=#add (444#=delhi=)).

S'L'CTIN8 D(T( BROM (DT T()L'S

SQL-

sele!t E f o$ student.

NO N(M' (DDR'SS(:NO# CIT*) """ """"""" """"""""""""""""""""""""" 2 1 a % (DDR(222# =h7d=) (DDR(111# =%ang=)

72
4 ! (DDR(444# =delhi=)

SQL-

sele!t no#na$e#s5add ess5hno#s5add ess5!it7 f o$ student s.

NO N(M' (DDR'SS5:NO (DDR'SS5CIT* """" """"""" """"""""""""""""" """""""""""""""" 2 1 4 a % ! 222 111 444 h7d %ang delhi

UAD(T' 6IT: (DT T()L'S

SQLSQL-

update student s set s5add ess5!it7 I =%o$%a7= whe e s5add ess5hno I 444. sele!t no#na$e#s5add ess5hno#s5add ess5!it7 f o$ student s. NO N(M' (DDR'SS5:NO (DDR'SS5CIT* """" """"""" """"""""""""""""" """""""""""""""" 2 1 4 a % ! 222 111 444 h7d %ang %o$%a7

D'L'T' 6IT: (DT T()L'S

SQLSQL-

delete student s whe e s5add ess5hno I 222. sele!t no#na$e#s5add ess5hno#s5add ess5!it7 f o$ student s. NO N(M' (DDR'SS5:NO (DDR'SS5CIT* """" """"""" """"""""""""""""" """""""""""""""" 1 4 % ! 111 444 %ang %o$%a7

DROAAIN8 (DT

SQL-

d op t7pe add .

73

O)>'CT 9I'6S (ND M'T:ODS


O)>'CT 9I'6S If 7ou want to i$ple$ent o%Ke!ts with the e/isting ta%le# o%Ke!t views !o$e into pi!tu e5 *ou define the o%Ke!t and ! eate a view whi!h elates this o%Ke!t to the e/isting ta%le nothing %ut ob!ect view5 O%Ke!t views a e used to elate the use defined o%Ke!ts to the e/isting ta%le5 '/0 2) (ssu$e that the ta%le student has al ead7 %een ! eated with the following !olu$ns
SQL-

! eate ta%le student(no nu$%e (1)#na$e va !ha (23)#hno nu$%e (4)#!it7 va !ha (23)).

1) C eate the following t7pes


SQLSQL-

! eate t7pe add as o%Ke!t(hno nu$%e (1)#!it7 va !ha (23)).? ! eate t7pe stud as o%Ke!t(na$e va !ha (23)#add ess add ).? ! eate view studentNov(no#studNinfo) as sele!t no#stud(na$e#add (hno#!it7)) f o$ student.

4) Relate the o%Ke!ts to the student ta%le %7 ! eating the o%Ke!t view
SQL-

@) Now 7ou !an inse t data into student ta%le in two wa7s a) )7 egula inse t
SQL-

Inse t into student values(2#;sudha;#222#;h7d;). Inse t into studentNov values(2#stud(Lsudha;#add (222#;h7d;))).

%) )7 using o%Ke!t view


SQL-

M'T:ODS *ou !an define $ethods whi!h a e nothing %ut fun!tions in t7pes and appl7 in the ta%les whi!h holds the t7pes.

74
'/0 2) Defining $ethods in t7pes
SQL-

C eate t7pe stud as o%Ke!t(na$e va !ha (23)#$a &s nu$%e (4)# Me$%e fun!tion $a& sNf($a &s in nu$%e ) etu n nu$%e # A ag$a est i!tN efe en!es($a &sNf#wnds# nds#wnps#fnps)).?

1) Defining t7pe %od7


SQL-

C eate t7pe %od7 stud as Me$%e fun!tion $a &sNf($a &s in nu$%e ) etu n nu$%e is )egin Retu n ($a &sH233). 'nd $a &sNf. 'nd.?

4) C eate a ta%le using stud t7pe


SQL-

C eate ta%le student(no nu$%e (1)#info stud). Inse t into student values(2#stud(Lsudha;#233)). Sele!t s5info5$a &sNf(s5info5$a &s) f o$ student s.

@) Inse t so$e data into student ta%le


SQL-

C) Using $ethod in sele!t


SQL-

"" :e e we a e using the p ag$a est i!tN efe en!es to avoid the w ites to the data%ase5

75

9(RR(*S (ND N'ST'D T()L'S


9(RR(*S ( va 7ing a a7 allows 7ou to sto e epeating att i%utes of a e!o d in a single ow %ut with li$it5 '/0 2) 6e !an ! eate va a7s using o a!le t7pes as well as use defined t7pes5 a) 9a a7 using p e"defined t7pes
SQL-

C eate t7pe va as va a7(C) of va !ha (23).? C eate t7pe add as o%Ke!t(hno nu$%e (4)#!it7 va !ha (23)).? C eate t7pe va as va a7(C) of add .?

%) 9a a7s using use defined t7pes


SQLSQL-

1) Using va a7 in ta%le
SQL-

C eate ta%le student(no nu$%e (1)#na$e va !ha (23)#add ess va). a7 ta%le Inse t into student values(2#;sudha;#va(add (222#;h7d;))). Inse t into student values(1#;Kagan;#va(add (222#;h7d;)#add (111#;%ang;))). Sele!t E f o$ student. Sele!t no#na$e# s5E f o$ student s2# ta%le(s25add ess) s.

4) Inse ting values into va


SQLSQL-

@) Sele!ting data f o$ va a7 ta%le


SQL-

"" This will displa7 va a7 !olu$n data along with va a7 and adt.
SQL-

"" This will displa7 in gene al fo $at C) Instead of s5E 7ou !an spe!if7 the !olu$ns in va a7
SQL-

Sele!t no#na$e# s5hno#s5!it7 f o$ student s2#ta%le(s25add ess) s. a7s5

"" Update and delete not possi%le in va output e/!luding va a7 and t7pes5

"" :e e we used ta%le fun!tion whi!h will ta&e the va a7 !olu$n as input fo p odu!ing

76

N'ST'D T()L'S ( nested ta%le is# as its na$e i$plies# a ta%le within a ta%le5 In this !ase it is a ta%le that is ep esented as a !olu$n within anothe ta%le5 Nested ta%le has the sa$e effe!t of va a7s %ut has no li$it5 '/0 2) 6e !an ! eate nested ta%les using o a!le t7pes and use defined t7pes whi!h has no li$it a) Nested ta%les using p e"defined t7pes
SQL-

C eate t7pe nt as ta%le of va !ha (23).? C eate t7pe add as o%Ke!t(hno nu$%e (4)#!it7 va !ha (23)).? C eate t7pe nt as ta%le of add .?

%) Nested ta%les using use defined t7pes


SQLSQL-

1) Using nested ta%le in ta%le


SQL-

C eate ta%le student(no nu$%e (1)#na$e va !ha (23)#add ess nt) nested ta%le add ess sto e as studentNte$p.

4) Inse ting values into ta%le whi!h has nested ta%le


SQLSQL-

Inse t into student values (2#;sudha;#nt(add (222#;h7d;))). Inse t into student values (1#;Kagan;#nt(add (222#;h7d;)#add (111#;%ang;))). Sele!t E f o$ student. Sele!t no#na$e# s5E f o$ student s2# ta%le(s25add ess) s.

@) Sele!ting data f o$ ta%le whi!h has nested ta%le


SQL-

"" This will displa7 nested ta%le !olu$n data along with nested ta%le and adt.
SQL-

"" This will displa7 in gene al fo $at C) Instead of s5E 7ou !an spe!if7 the !olu$ns in nested ta%le
SQL-

Sele!t no#na$e# s5hno#s5!it7 f o$ student s2#ta%le(s25add ess) s. Inse t into ta%le(sele!t add ess f o$ student whe e noI2) values(add (CCC#;!hennai;)).

D) Inse ting nested ta%le data to the e/isting ow


SQL-

W) Update in nested ta%les


SQL-

Update ta%le(sele!t add ess f o$ student whe e noI1) s set s5!it7I;%o$%a7; whe e s5hno I 111.

77
G) Delete in nested ta%le
SQL-

Delete ta%le(sele!t add ess f o$ student whe e noI4) s whe e s5hnoI444.

BL(S:)(CF QU'R*
Used to et ieve the data whi!h has %een al ead7 !o$$itted with out going fo Blash%a!&s a e of two t7pes '/0 2) Using ti$e %ased flash%a!& a) %) !) d)
SQL-

e!ove 75

Ti$e %ase flash%a!& SCN %ased flash%a!& (SCN stands fo S7ste$ Change Nu$%e )

Sele!t Ef o$ student. Delete student. Co$$it. "" this will !o$$it the wo &5 Sele!t Ef o$ student.

"" This will displa7 all the ows


SQLSQLSQL-

"" :e e it will displa7 nothing e) Then e/e!ute the following p o!edu es


SQL-

'/e! d%$sNflash%a!&5ena%leNatNti$e(s7sdate"1?2@@3) Sele!t Ef o$ student.

f)

SQL-

"" :e e it will displa7 the lost data "" The lost data will !o$e %ut the !u ent s7ste$ ti$e was used g)
SQL-

'/e! d%$sNflash%a!&5disa%le

"" :e e we have to disa%le the flash%a!& to ena%le it again 1) Using SCN %ased flash%a!& a) De!la e a va ia%le to sto e SCN
SQL-

9a ia%le s nu$%e '/e! 0s 0I e/e! d%$sNflash%a!&5getNs7ste$N!hangeNnu$%e

%) 8et the SCN


SQL-

!) To see the SCN


SQL-

A int s

78
d) Then e/e!ute the following p o!edu es
SQLSQL-

'/e! d%$sNflash%a!&5ena%leNatNs7ste$N!hangeNnu$%e (0s) '/e! d%$sNflash%a!&5disa%le

'+T'RN(L T()L'S
*ou !an use data%ase5 6hen 7ou ! eate an e/te nal ta%le# 7ou define its st u!tu e and lo!ation with in o a!le5 6hen 7ou Vue 7 the ta%le# o a!le eads the e/te nal ta%le and etu ns the esults Kust as if the data had %een sto ed with in the data%ase5 (CC'SSIN8 '+T'RN(L T()L' D(T( To a!!ess e/te nal files f o$ within o a!le# 7ou $ust fi st use the ! eate di e!to 7 !o$$and to define a di e!to 7 o%Ke!t pointing to the e/te nal file lo!ation Use s who will a!!ess the e/te nal files $ust have the di e!to 75 '/0
CR'(TIN8 DIR'CTOR* (ND OS L'9'L BIL'

e/te nal ta%le featu e to a!!ess e/te nal files as if the7 a e ta%les inside the

ead and w ite p ivilege on the

SQLSQLSQLSQLSQLSQLSQL-

SVlplus s7ste$?$anage C eate di e!to 7 sa&ethNdi as L?9isd%?visd%?T5153?e/te nal;. 8 ant all on di e!to 7 sa&ethNdi to sa&eth. Conn sa&eth?sa&eth Spool dept5lst Sele!t deptno XX L#; XX dna$e XX L#; XX lo! f o$ dept. Spool off

CR'(TIN8 '+T'RN(L T()L'

SQL-

C eate ta%le deptNe/t (deptno nu$%e (1)# Dna$e va !ha (2@)#

79
Lo! va !ha (24)) O ganiUation e/te nal ( t7pe o a!leNloade Default di e!to 7 sa&ethNdi (!!ess pa a$ete s ( e!o ds deli$ited %7 newline Bields te $inated %7 Q#R ( deptno nu$%e (1)# Dna$e va !ha (2@)# Lo! va !ha (24))) Lo!ation (L?9isd%?visd%?T5153?dept5lst;)).
S'L'CTIN8 D(T( BROM '+T'RN(L T()L'

SQL-

sele!t E f o$ deptNe/t.

This will ead f o$ dept5lst whi!h is a ope ating s7ste$ level file5
LIMIT(TIONS ON '+T'RN(L T()L'S

a) *ou !an not pe fo $ inse t# update# and delete ope ations a) Inde/ing not possi%le %) Const aints not possi%le
)'N'BITS OB '+T'RN(L T()L'S

a) Que ies of e/te nal ta%les !o$plete ve 7 Vui!&l7 even though a full ta%le s!an id eVui ed with ea!h a!!ess %) *ou !an Koin e/te nal ta%les to ea!h othe o to standa d ta%les

80

R'B D'R'B 9(LU'


R'B The ef fun!tion allows efe en!ing of e/isting ow o%Ke!ts5 'a!h of the ow o%Ke!ts has an o%Ke!t id value assigned to it5 The o%Ke!t id assigned !an %e seen %7 using ef fun!tion5

D'R'B The de ef fun!tion pe fo $s opposite a!tion5 It ta&es a efe en!e value of o%Ke!t id and etu ns the value of the ow o%Ke!ts5

9(LU' 'ven though the p i$a 7 ta%le is o%Ke!t ta%le# still it displa7s the ows in gene al fo $at5 To displa7 the enti e st u!tu e of the o%Ke!t# this will %e used5

'/0

2) ! eate vendotNadt t7pe


SQL-

C eate t7pe vendo Nadt as o%Ke!t (vendo N!ode nu$%e (1)# vendo Nna$e va !ha (1)# vendo Nadd ess va !ha (23)).?

1) ! eate o%Ke!t ta%les vendo s and vendo s2


SQLSQL-

C eate ta%le vendo s of vendo Nadt. C eate ta%le vendo s2 of vendo Nadt. inse t into vendo s values(2# La;# Lh7d;). inse t into vendo s values(1# L%;# L%ang;). inse t into vendo s2 values(4# L!;# Ldelhi;). inse t into vendo s2 values(@# Ld;# L!hennai;).

4) inse t the data into o%Ke!t ta%les


SQLSQLSQLSQL-

@) ! eate anothe ta%le o de s whi!h holds the vendo Nadt t7pe also5

81
SQL-

C eate ta%le o de s (o de Nno nu$%e (1)# vendo Ninfo ef vendo Nadt). O C eate ta%le o de s (o de Nno nu$%e (1)# vendo Ninfo ef vendo Nadt with owid).

SQL-

C) inse t the data into o de s ta%le The vendo Ninfo !olu$n in the following s7nta/es will sto e o%Ke!t id of an7 ta%le whi!h is efe en!ed %7 vendo Nadt o%Ke!t ( %oth vendo s and vendo s2)5 inse t into o de s values(22#(sele!t ef(v) f o$ vendo s v whe e vendo N!ode I 2)). inse t into o de s values(21#(sele!t ef(v) f o$ vendo s v whe e vendo N!ode I 1)). inse t into o de s values(24#(sele!t ef(v2) f o$ vendo s2 v2 whe e vendo N!ode I 2)).
SQL-

SQLSQLSQL-

inse t into o de s values(2@#(sele!t ef(v2) f o$ vendo s2 v2 whe e vendo N!ode I 2)).

D) To see the o%Ke!t ids of vendo ta%le


SQL-

Sele!t ef(9) f o$ vendo s v.

W) If 7ou see the vendo Ninfo of o de s it will show onl7 the o%Ke!t ids not the values# to see the values
SQL-

Sele!t de ef(o5vendo Ninfo) f o$ o de s o.

G) 'ven though the vendo s ta%le is o%Ke!t ta%le it will not show the adt along with data# to see the data along with the adt
SQL-Sele!t

E f o$ vendo s. value(v) f o$ vendo s v.

This will give the data without adt5


SQL-Sele!t

This will give the !olu$ns data along wih the t7pe5

R'B CONSTR(INTS Ref !an also a!ts as !onst aint5 'ven though vendo s2 also holding vendo Nadt# the o de s ta%le will sto e the o%Ke!t ids of vendo s onl7 %e!ause it is !onst ained to that ta%le onl75 The vendo Ninfo !olu$n in the following s7nta/es will sto e o%Ke!t ids of vendo s onl75
SQL-

C eate ta%le o de s (o de Nno nu$%e (1)# vendo Ninfo ef vendo Nadt s!ope is vendo s).

82
O
SQL-

C eate ta%le o de s (o de Nno nu$%e (1)# vendo Ninfo ef vendo Nadt !onst aint f& efe en!es vendo s).

O)>'CT 9I'6S 6IT: R'B'R'NC'S


To i$ple$ent the o%Ke!ts and the ef !onst aints to the e/isting ta%les# what we !an do[

Si$pl7 d op the %oth ta%les and e! eate with o%Ke!ts and ef !onst ains5 )ut 7ou !an a!hieve this with out d opping the ta%les and without losing the data %7 ! eating o%Ke!t views with efe en!es5 '/0 a) C eate the following ta%les
SQL-

C eate ta%le student2(no nu$%e (1) p i$a 7 &e7#na$e va !ha (1)#$a &s nu$%e (4)). C eate ta%le student1(no nu$%e (1) p i$a 7 &e7#hno nu$%e (4)#!it7 va !ha (23)#id nu$%e (1)#fo eign Fe7(id) efe en!es student2(no)).

SQL-

%) Inse t the e!o ds into %oth ta%les


SQLSQLSQLSQLSQL-

inse t into student2(2#;a;#233). inse t into student2(1#;%;#133). inse t into student1(22#222#;h7d;#2). inse t into student1(21#111#;%ang;#1). inse t into student1(24#444#;%o$%a7;#2). epla!e t7pe stud as o%Ke!t(no nu$%e (1)#na$e va !ha (1)#$a &s

!) C eate the t7pe


SQL-

! eate o

nu$%e (4)).? d) 8ene ating OIDs


SQL-

C eate o

epla!e view student2Nov of stud with o%Ke!t identifie (o id) (no) as

Sele!t E f o$ Student2. e) 8ene ating efe en!es


SQL-

C eate o

epla!e view student1Nov as sele!t no#hno#!it7#$a&eN ef(student2Nov#id)

id f o$ Student1. d) Que 7 the following


SQL-

sele!t Ef o$ student2Nov.

83
SQLSQLSQ-

sele!t ef(s) f o$ student2Nov s. sele!t values(s) f o$ student2Nov. sele!t de ef(s5id) f o$ student1Nov s.

sele!t Ef o$ student1Nov.

SQL-

A(RTITIONS
( single logi!al ta%le !an %e split into a nu$%e of ph7si!all7 sepa ate pie!es %ased on anges of &e7 values5 'a!h of the pa ts of the ta%le is !alled a pa tition5 ( non"pa titioned ta%le !an not %e pa titioned late 5 T*A'S Range pa titions List pa titions :ash pa titions Su% pa titions

(D9(NT(8'S Redu!ing downti$e fo s!heduled $aintenan!e# whi!h allows $aintenan!e ope ations to %e !a ied out on sele!ted pa titions while othe pa titions a e availa%le to use s5 Redu!ing downti$e due to data failu e# failu e of a pa ti!ula pa tition will no wa7 affe!t othe pa titions5 Aa tition independen!e allows fo pu poses5 (D9(NT(8'S OB A(RTITIONS )* STORIN8 T:'M IN DIBB'R'NT T()L'SA(C'S Redu!es the possi%ilit7 of data !o uption in $ultiple pa titions5 )a!& up and e!ove 7 of ea!h pa tition !an %e done independentl75 !on!u ent use of the va ious pa titions fo va ious

DIS(D9(NT(8'S Aa titioned ta%les !annot !ontain an7 !olu$ns with long o t7pes o o%Ke!t t7pes5 long aw datat7pes# LO)

84

R(N8' A(RTITIONS a) C eating ange pa titioned ta%le


SQL-

C eate ta%le student(no nu$%e (1)#na$e va !ha (1)) pa tition %7 ange(no) (pa tition p2 values less than(23)# pa tition p1 values less than(13)# pa tition p4 values less than(43)#pa tition p@ values less than($a/value)).

EE if 7ou a e using $a/value fo the last pa tition# 7ou !an not add a pa tition5 %) Inse ting e!o ds into ange pa titioned ta%le
SQLSQLSQLSQL-

Inse t into student values(2#;a;). Inse t into student values(22#;%;). Inse t into student values(12#;!;). Inse t into student values(42#;d;). Sele!t Ef o$ student. Sele!t Ef o$ student pa tition(p2). (dd D op T un!ate Rena$e Split Move '/!hange

"" this will go to p2 "" this will go to p1 "" this will go to p4 "" this will go to p@

!) Ret ieving e!o ds f o$ ange pa titioned ta%le


SQLSQL-

d) Aossi%le ope ations with ange pa titions

e) (dding a pa tition
SQL-

(lte ta%le student add pa tition pC values less than(@3).

f) D opping a pa tition
SQL-

(lte ta%le student d op pa tition p@. (lte ta%le student ena$e pa tition p4 to pD.

g) Rena$ing a pa tition
SQL-

h) T un!ate a pa tition

85
SQL-

(lte ta%le student t un!ate pa tition pD.

i) Splitting a pa tition
SQL-

(lte ta%le student split pa tition p1 at(2C) into (pa tition p12#pa tition p11). (lte ta%le student e/!hange pa tition p2 with ta%le student1. (lte ta%le student $ove pa tition p12 ta%lespa!e sa&ethNts.

K) '/!hanging a pa tition
SQL-

&) Moving a pa tition


SQL-

LIST A(RTITIONS a) C eating list pa titioned ta%le


SQL-

C eate ta%le student(no nu$%e (1)#na$e va !ha (1)) pa tition %7 list(no) (pa tition p2

values(2#1#4#@#C)# pa tition p1 values(D#W#G#T#23)#pa tition p4 values(22#21#24#2@#2C)# pa tition p@ values(2D#2W#2G#2T#13)). %) Inse ting e!o ds into list pa titioned ta%le
SQLSQLSQLSQL-

Inse t into student values(2#;a;). Inse t into student values(D#;%;). Inse t into student values(22#;!;). Inse t into student values(2D#;d;).

"" this will go to p2 "" this will go to p1 "" this will go to p4 "" this will go to p@

!) Ret ieving e!o ds f o$ list pa titioned ta%le


SQLSQL-

Sele!t Ef o$ student. Sele!t Ef o$ student pa tition(p2). (dd D op T un!ate Rena$e Move '/!hange

d) Aossi%le ope ations with list pa titions

e) (dding a pa tition
SQL-

(lte ta%le student add pa tition pC values(12#11#14#1@#1C). (lte ta%le student d op pa tition p@.

f) D opping a pa tition
SQL-

g) Rena$ing a pa tition

86
SQL-

(lte ta%le student ena$e pa tition p4 to pD. (lte ta%le student t un!ate pa tition pD.

h) T un!ate a pa tition
SQL-

i) '/!hanging a pa tition
SQL-

(lte ta%le student e/!hange pa tition p2 with ta%le student1. (lte ta%le student $ove pa tition p1 ta%lespa!e sa&ethNts.

K) Moving a pa tition
SQL-

:(S: A(RTITIONS a) C eating hash pa titioned ta%le


SQL-

C eate ta%le student(no nu$%e (1)#na$e va !ha (1)) pa tition %7 hash(no) pa titions C.

:e e o a!le auto$ati!all7 gives pa tition na$es li&e S*SNA2 S*SNA1 S*SNA4 S*SNA@ S*SNAC %) Inse ting e!o ds into hash pa titioned ta%le it will inse t the e!o ds %ased on hash fun!tion !al!ulated %7 ta&ing the pa tition &e7
SQLSQLSQLSQL-

Inse t into student values(2#;a;). Inse t into student values(D#;%;). Inse t into student values(22#;!;). Inse t into student values(2D#;d;). Sele!t Ef o$ student. Sele!t Ef o$ student pa tition(s7sNp2). (dd T un!ate Rena$e Move '/!hange

!) Ret ieving e!o ds f o$ hash pa titioned ta%le


SQLSQL-

d) Aossi%le ope ations with hash pa titions

87
e) (dding a pa tition
SQL-

(lte ta%le student add pa tition pD .

f) Rena$ing a pa tition
SQL-

(lte ta%le student ena$e pa tition pD to pW. (lte ta%le student t un!ate pa tition pW. (lte ta%le student e/!hange pa tition s7sNp2 with ta%le student1.

g) T un!ate a pa tition
SQL-

h) '/!hanging a pa tition
SQL-

i) Moving a pa tition
SQL-

(lte ta%le student $ove pa tition s7sNp1 ta%lespa!e sa&ethNts.

SU)"A(RTITIONS 6IT: R(N8' (ND :(S: Su%pa titions !lause is used %7 hash onl75 6e !an not ! eate su%pa titions with list and hash pa titions5 a) C eating su%pa titioned ta%le
SQL-

C eate ta%le student(no nu$%e (1)#na$e va !ha (1)#$a &s nu$%e (4)) Aa tition %7 ange(no) su%pa tition %7 hash(na$e) su%pa titions 4 (Aa tition p2 values less than(23)#pa tition p1 values less than(13)).

This will ! eate two pa titions p2 and p1 with th ee su%pa titions fo ea!h pa tition A2 \ S*SNSU)A2 S*SNSU)A1 S*SNSU)A4 A1 \ S*SNSU)A@ S*SNSU)AC S*SNSU)AD EE if 7ou a e using $a/value fo the last pa tition# 7ou !an not add a pa tition5 %) Inse ting e!o ds into su%pa titioned ta%le
SQLSQL-

Inse t into student values(2#;a;). Inse t into student values(22#;%;). Sele!t Ef o$ student. Sele!t Ef o$ student pa tition(p2).

"" this will go to p2 "" this will go to p1

!) Ret ieving e!o ds f o$ su%pa titioned ta%le


SQLSQLSQL-

Sele!t Ef o$ student su%pa tition(s7sNsu%p2).

88
d) Aossi%le ope ations with su%pa titions (dd D op T un!ate Rena$e Split e) (dding a pa tition
SQL-

(lte ta%le student add pa tition p4 values less than(43). (lte ta%le student d op pa tition p4. (lte ta%le student ena$e pa tition p1 to p4. (lte ta%le student t un!ate pa tition p2. (lte ta%le student split pa tition p4 at(2C) into (pa tition p42#pa tition p41).

f) D opping a pa tition
SQL-

g) Rena$ing a pa tition
SQL-

h) T un!ate a pa tition
SQL-

i) Splitting a pa tition
SQL-

89

8ROUA )* (ND :(9IN8


8ROUA )* Using g oup %7# we !an ! eate g oups of elated info $ation5 Colu$ns used in sele!t $ust %e used with g oup %7# othe wise it was not a g oup %7 e/p ession5 '/0
SQL-

sele!t deptno# su$(sal) f o$ e$p g oup %7 deptno. D'ATNO 23 13 43 SUM(S(L) GWC3 23GWC T@33

"""""""""" """"""""""

SQL-

sele!t deptno#Ko%#su$(sal) f o$ e$p g oup %7 deptno#Ko%. D'ATNO >O) """""""""" """"""""" 23 23 23 13 13 13 43 43 43 CL'RF M(N(8'R AR'SID'NT (N(L*ST CL'RF M(N(8'R CL'RF M(N(8'R S(L'SM(N SUM(S(L) """""""""" 2433 1@C3 C333 D333 2T33 1TWC TC3 1GC3 CD33

T ows sele!ted5

90

:(9IN8 This will wo & as whe e !lause whi!h !an %e used onl7 with g oup %7 %e!ause of a%sen!e of whe e !lause in g oup %75 '/0
SQL-

sele!t deptno#Ko%#su$(sal) tsal f o$ e$p g oup %7 deptno#Ko% having su$(sal) - 4333. D'ATNO 23 13 43 >O) TS(L """""""""" C333 D333 CD33

"""""""""" """"""""" AR'SID'NT (N(L*ST S(L'SM(N

SQL-

sele!t deptno#Ko%#su$(sal) tsal f o$ e$p g oup %7 deptno#Ko% having su$(sal) - 4333 o de %7 Ko%. D'ATNO 13 23 43 >O) (N(L*ST AR'SID'NT S(L'SM(N TS(L """""""""" D333 C333 CD33

"""""""""" """""""""

ORD'R OB '+'CUTION 8 oup the ows togethe %ased on g oup %7 !lause5 Cal!ulate the g oup fun!tions fo ea!h g oup5 Choose and eli$inate the g oups %ased on the having !lause5 O de the g oups %ased on the spe!ified !olu$n5

91

ROLLUA 8ROUAIN8 CU)'

These a e the enhan!e$ents to the g oup %7 featu e5 USIN8 ROLLUA This will give the sala ies in ea!h depa t$ent in ea!h Ko% !atego 7 along wih the total sala 7 fot individual depa t$ents and the total sala 7 of all the depa t$ents5
SQL-

Sele!t deptno#Ko%#su$(sal) f o$ e$p g oup %7 ollup(deptno#Ko%). D'ATNO 23 23 23 23 13 13 13 13 43 43 43 43 CL'RF M(N(8'R S(L'SM(N (N(L*ST CL'RF M(N(8'R >O) CL'RF M(N(8'R AR'SID'NT SUM(S(L) """""""""" 2433 1@C3 C333 GWC3 D333 2T33 1TWC 23GWC TC3 1GC3 CD33 T@33 1T31C

"""""""""" """""""""

24 ows sele!ted5 USIN8 8ROUAIN8

92
In the a%ove Vue 7 it will give the total sala 7 of the individual depa t$ents %ut with a %lan& in the Ko% !olu$n and gives the total sala 7 of all the depa t$ents with %lan&s in deptno and Ko% !olu$ns5 To epla!e these %lan&s with 7ou desi ed st ing g ouping will %e used
SQL-

sele!t de!ode(g ouping(deptno)#2#=(ll Depts=#deptno)#de!ode(g ouping(Ko%)#2#=(ll Ko%s=#Ko%)#su$(sal) f o$ e$p g oup %7 ollup(deptno#Ko%). D'COD'(8ROUAIN8(D'ATNO)#2#=(LLD'ATS=#D'A D'COD'(8R """"""""""""""""""""""""""""""""""" """""""""""""""""""""""""""""""""" 23 23 23 23 13 13 13 13 43 43 43 43 (ll Depts CL'RF M(N(8'R AR'SID'NT (ll Ko%s (N(L*ST CL'RF M(N(8'R (ll Ko%s CL'RF M(N(8'R S(L'SM(N (ll Ko%s (ll Ko%s SUM(S(L) """""""""""""" 2433 1@C3 C333 GWC3 D333 2T33 1TWC 23GWC TC3 1GC3 CD33 T@33 1T31C

24 ows sele!ted5 8 ouping will etu n 2 if the !olu$n whi!h is spe!ified in the g ouping fun!tion has %een used in ollup5 8 ouping will %e used in asso!iation with de!ode5 USIN8 CU)' This will give the sala ies in ea!h depa t$ent in ea!h Ko% !atego 7# the total sala 7 fo individual depa t$ents# the total sala 7 of all the depa t$ents and the sala ies in ea!h Ko% !atego 75

93

SQL-

sele!t de!ode(g ouping(deptno)#2#;(ll Depts;#deptno)#de!ode(g ouping(Ko%)#2#;(ll >o%s;#Ko%)#su$(sal) f o$ e$p g oup %7 !u%e(deptno#Ko%). D'COD'(8ROUAIN8(D'ATNO)#2#=(LLD'ATS=#D'A D'COD'(8R 23 23 23 23 13 13 13 13 43 43 43 43 (ll Depts (ll Depts (ll Depts (ll Depts (ll Depts (ll Depts CL'RF M(N(8'R AR'SID'NT (ll >o%s (N(L*ST CL'RF M(N(8'R (ll >o%s CL'RF M(N(8'R S(L'SM(N (ll >o%s (N(L*ST CL'RF M(N(8'R AR'SID'NT S(L'SM(N (ll >o%s SUM(S(L) 2433 1@C3 C333 GWC3 D333 2T33 1TWC 23GWC TC3 1GC3 CD33 T@33 D333 @2C3 G1WC C333 CD33 1T31C

""""""""""""""""""""""""""""""""""" """""""""""""""""""""""""""""""""""" """"""""""""

2G ows sele!ted5

94

S'T OA'R(TORS
T*A'S Union Union all Inte se!t Minus

UNION This will !o$%ine the e!o ds of $ultiple ta%les having the sa$e st u!tu e5 '/0
SQL-

sele!t E f o$ student2 union sele!t E f o$ student1.

UNION (LL This will !o$%ine the e!o ds of $ultiple ta%les having the sa$e st u!tu e %ut in!luding dupli!ates5 '/0
SQL-

sele!t E f o$ student2 union all sele!t E f o$ student1.

INT'RS'CT This will give the !o$$on e!o ds of $ultiple ta%les having the sa$e st u!tu e5 '/0
SQL-

sele!t E f o$ student2 inte se!t sele!t E f o$ student1.

MINUS This will give the e!o ds of a ta%le whose e!o ds a e not in othe ta%les having the sa$e st u!tu e5

95
'/0
SQL-

sele!t E f o$ student2 $inus sele!t E f o$ student1.

9I'6S
( view is a data%ase o%Ke!t that is a logi!al ep esentation of a ta%le5 It is delive ed f o$ a ta%le %ut has no sto age of its own and often $a7 %e used in the sa$e $anne as a ta%le5 ( view ta&es the output of the Vue 7 and t eats it as a ta%le# the efo e a view !an %e thought of as a sto ed Vue 7 o a vi tual ta%le5 T*A'S Si$ple view Co$ple/ view

Si$ple view !an %e ! eated f o$ one ta%le whe e as !o$ple/ view !an %e ! eated f o$ $ultiple ta%les5 6:* 9I'6S[ A ovides additional level of se!u it7 %7 est i!ting a!!ess to a p edete $ined set of ows and?o !olu$ns of a ta%le5 :ide the data !o$ple/it75 Si$plif7 !o$$ands fo the use 5

9I'6S 6IT:OUT DML Read onl7 view 9iew with g oup %7 9iew with agg egate fun!tions 9iew with ownu$ Aa tition view 9iew with distin!t

'/0
SQLSQL-

C eate view deptNv as sele!t Ef o$ dept with ead onl7. C eate view deptNv as sele!t deptno# su$(sal) tNsal f o$ e$p g oup %7 deptno.

96
SQLSQLSQL-

C eate view stud as sele!t ownu$ no# na$e# $a &s f o$ student. C eate view student as sele!t Ef o$ student2 union sele!t Ef o$ student1. C eate view stud as sele!t distin!t no#na$e f o$ student.

9I'6S 6IT: DML 9iew with not null !olu$n "" inse t with out not null !olu$n not possi%le "" update not null !olu$n to null is not possi%le "" delete possi%le 9iew with out not null !olu$n whi!h was in %ase ta%le "" inse t not possi%le "" update# delete possi%le 9iew with e/p ession "" inse t # update not possi%le "" delete possi%le 9iew with fun!tions (e/!ept agg egate) "" inse t# update not possi%le "" delete possi%le 9iew was ! eated %ut the unde l7ing ta%le was d opped then we will get the $essage li&e Q view has e o s R5 9iew was ! eated %ut the %ase ta%le has %een alte ed %ut still the view was with the initial definition# we have to epla!e the view to affe!t the !hanges5 Co$ple/ view (view with $o e than one ta%le) "" inse t not possi%le "" update# delete possi%le (not alwa7s) CR'(TIN8 9I'6 6IT:OUT :(9IN8 T:' )(S' T()L' C eate fo !e view stud as sele!t EB o$ student.

SQL-

"" On!e the %ase ta%le was ! eated then the view is validated5 9I'6 6IT: C:'CF OATION CONSTR(INT C eate view stud as sele!t Ef o$ student whe e $a &s I C33 with !he!& option !onst aint C&. " Inse t possi%le with $a &s value as C33 " Update possi%le e/!luding $a &s !olu$n " Delete possi%le DROAAIN8 9I'6S

SQL-

97

SQL-

d op view deptNv.

S*NON*M (ND S'QU'NC'


S*NON*M ( s7non7$ is a data%ase o%Ke!t# whi!h is used as an alias fo a ta%le# view o seVuen!e5
T*A'S

A ivate Au%li!

A ivate s7non7$ is availa%le to the pa ti!ula use who ! eates5 Au%li! s7non7$ is ! eated %7 D)( whi!h is availa%le to all the use s5
(D9(NT(8'S

:ide the na$e and owne of the o%Ke!t5 A ovides lo!ation t anspa en!7 fo e$ote o%Ke!ts of a dist i%uted data%ase5

CR'(T' (ND DROA

SQLSQLSQL-

! eate s7non7$ s2 fo e$p. ! eate pu%li! s7non7$ s1 fo e$p. d op s7non7$ s2.

S'QU'NC' ( seVuen!e is a data%ase o%Ke!t# whi!h !an gene ate uniVue# seVuential intege values5 It !an %e used to auto$ati!all7 gene ate p i$a 7 &e7 o uniVue &e7 values5 ( seVuen!e !an %e eithe in an as!ending o des!ending o de 5 S7nta/0 C eate seVuen!e ,se-_name- Oin! e$ent %t7 nP Osta t with nP O$a/value nP O$invalue nP O!7!le?no!7!leP O!a!he?no!a!heP.

98
)7 defalult the seVuen!e sta ts with 2# in! e$ents %7 2 with $invalue of 2 and with no!7!le# no!a!he5 Ca!he option p e"alloo!ates a set of seVuen!e nu$%e s and etains the$ in $e$o 7 fo faste a!!ess5 '/0
SQLSQL-

! eate seVuen!e s. ! eate seVuen!e s in! e$ent %7 23 sta t with 233 $invalue C $a/value 133 !7!le !a!he 13.

USIN8 S'QU'NC'

SQLSQL-

! eate ta%le student(no nu$%e (1)#na$e va !ha (23)). inse t into student values(s5ne/tval# Lsa&eth;). Initiall7 !u val is not defined and ne/tval is sta ting value5 (fte that ne/tval and !u val a e alwa7s eVual5

CR'(TIN8 (LA:("NUM'RIC S'QU'NC'

SQLSQL-

! eate seVuen!e s sta t with 22214@. Inse t into student values (s5ne/tval XX t anslate (s5ne/tval#;214@CDWGT3;#;a%!defghiK;)).

(LT'RIN8 S'QU'NC'

'/0

6e !an alte the seVuen!e to pe fo $ the following5 Set o eli$inate $inalue of $a/value5 Change the in! e$ent value5 Change the nu$%e of !a!hed seVuen!e nu$%e s5

SQLSQLSQL-

alte seVuen!e s $invalue C. alte seVuen!e s in! e$ent %7 1. alte seVuen!e s !a!he 23.

DROAAIN8 S'QU'NC'

99
SQL-

d op seVuen!e s.

>OINS
The pu pose of a Koin is to !o$%ine the data a! oss ta%les5 ( Koin is a!tuall7 pe fo $ed %7 the whe e !lause whi!h !o$%ines the spe!ified ows of ta%les5 If a Koin involves in $o e than two ta%les then o a!le Koins fi st two ta%les %ased on the Koins !ondition and then !o$pa es the esult with the ne/t ta%le and so on5 T*A'S 'Vui Koin Non"eVui Koin Self Koin Natu al Koin C oss Koin Oute Koin Left oute Right oute Bull oute

Inne Koin Using !lause On !lause

(ssu$e that we have the following ta%les5


SQL-

sele!t E f o$ dept. LOC h7d %ang %o$%a7

D'ATNO DN(M' 23 13 43 $&t fin h

"""""" """""""""" """"""""""

SQL-

sele!t E f o$ e$p.

100
'MANO 222 111 444 @@@ 'QUI >OIN ( Koin whi!h !ontains an LI; ope ato in the Koins !ondition5 '/0
SQL-

'N(M' sa&eth sudha Kagan $adhu

>O) anal7st !le & $anage enginee

M8R

D'ATNO @@@ 444 222 111 23 13 23 @3

"""""""""" """""""""" """""""""" """""""""" """"""""""

sele!t e$pno#ena$e#Ko%#dna$e#lo! f o$ e$p e#dept d whe e e5deptnoId5deptno. 'MANO 222 444 111 'N(M' sa&eth Kagan sudha >O) anal7st $anage !le & DN(M' $&t $&t fin LOC h7d h7d %ang

"""""""""" """""""""" """""""""" """""""""" """"""""""

USIN8 CL(US' sele!t e$pno#ena$e#Ko% #dna$e#lo! f o$ e$p e Koin dept d using(deptno). 'MANO 222 444 111 ON CL(US' sele!t e$pno#ena$e#Ko%#dna$e#lo! f o$ e$p e Koin dept d on(e5deptnoId5deptno). 'MANO 'N(M' >O) DN(M' LOC 'N(M' sa&eth Kagan sudha >O) anal7st $anage !le & DN(M' $&t $&t fin LOC h7d h7d %ang

SQL-

"""""""""" """""""""" """""""""" """""""""" """"""""""

SQL-

"""""""""" """""""""" """""""""" """""""""" """"""""""

101
222 444 111 NON"'QUI >OIN ( Koin whi!h !ontains an ope ato othe than LI; in the Koins !ondition5 '/0
SQL-

sa&eth Kagan sudha

anal7st $anage !le &

$&t $&t fin

h7d h7d %ang

sele!t e$pno#ena$e#Ko%#dna$e#lo! f o$ e$p e#dept d whe e e5deptno - d5deptno. 'MANO 111 @@@ @@@ @@@ 'N(M' sudha $adhu $adhu $adhu >O) !le & enginee enginee enginee DN(M' $&t $&t fin h LOC h7d h7d %ang %o$%a7

"""""""""" """""""""" """""""""" """""""""" """"""""""

S'LB >OIN >oining the ta%le itself is !alled self Koin5 '/0
SQL-

sele!t e25e$pno#e15ena$e#e25Ko%#e15deptno f o$ e$p e2#e$p e1 whe e e25e$pnoIe15$g . 'MANO 222 111 444 @@@ 'N(M' Kagan $adhu sudha sa&eth >O) anal7st !le & $anage enginee D'ATNO 23 @3 13 23

"""""""""" """""""""" """""""""" """"""""""

N(TUR(L >OIN Natu al Koin !o$pa es all the !o$$on !olu$ns5 '/0
SQL-

sele!t e$pno#ena$e#Ko%#dna$e#lo! f o$ e$p natu al Koin dept.

102
'MANO 222 444 111 CROSS >OIN This will gives the ! oss p odu!t5 '/0
SQL-

'N(M' sa&eth Kagan sudha

>O) anal7st $anage !le &

DN(M' $&t $&t fin

LOC h7d h7d %ang

"""""""""" """""""""" """""""""" """""""""" """"""""""

sele!t e$pno#ena$e#Ko%#dna$e#lo! f o$ e$p ! oss Koin dept. 'MANO 'N(M' 222 111 444 @@@ 222 111 444 @@@ 222 111 444 @@@ sa&eth sudha Kagan $adhu sa&eth sudha Kagan $adhu sa&eth sudha Kagan $adhu >O) anal7st !le & $anage enginee anal7st !le & $anage enginee anal7st !le & $anage enginee DN(M' $&t $&t $&t $&t fin fin fin fin h h h h LOC h7d h7d h7d h7d %ang %ang %ang %ang %o$%a7 %o$%a7 %o$%a7 %o$%a7

"""""""""" """""""""" """""""""" """""""""" """"""""""

21 ows sele!ted5 OUT'R >OIN Oute Koin gives the non"$at!hing e!o ds along with $at!hing e!o ds5
L'BT OUT'R >OIN

103
This will displa7 the all $at!hing e!o ds and the e!o ds whi!h a e in left hand side ta%le

those that a e not in ight hand side ta%le5

'/0
SQL-

sele!t e$pno#ena$e#Ko%#dna$e#lo! f o$ e$p e left oute Koin dept d on(e5deptnoId5deptno). O sele!t e$pno#ena$e#Ko%#dna$e#lo! f o$ e$p e#dept d whe e e5deptnoId5deptno(H).

SQL-

'MANO 222 444 111 @@@


RI8:T OUT'R >OIN

'N(M' sa&eth Kagan sudha $adhu

>O) anal7st $anage !le & enginee

DN(M' $&t $&t fin

LOC h7d h7d %ang

"""""""""" """""""""" """""""""" """""""""" """"""""""

This will displa7 the all $at!hing

e!o ds and the e!o ds whi!h a e in ight hand side ta%le

those that a e not in left hand side ta%le5 '/0


SQL-

sele!t e$pno#ena$e#Ko%#dna$e#lo! f o$ e$p e ight oute Koin dept d on(e5deptnoId5deptno). O sele!t e$pno#ena$e#Ko%#dna$e#lo! f o$ e$p e#dept d whe e e5deptno(H) I d5deptno. 'MANO 222 444 111 'N(M' sa&eth Kagan sudha >O) anal7st $anage !le & DN(M' $&t $&t fin h LOC h7d h7d %ang %o$%a7

SQL-

"""""""""" """""""""" """""""""" """""""""" """"""""""

BULL OUT'R >OIN

104

This will displa7 the all $at!hing e!o ds and the non"$at!hing e!o ds f o$ %oth ta%les5 '/0
SQL-

sele!t e$pno#ena$e#Ko%#dna$e#lo! f o$ e$p e full oute Koin dept d on(e5deptnoId5deptno). 'MANO 444 222 111 @@@ 'N(M' Kagan sa&eth sudha $adhu >O) $anage anal7st !le & enginee h %o$%a7 DN(M' $&t $&t fin LOC h7d h7d %ang

"""""""""" """""""""" """""""""" """""""""" """"""""""

INN'R >OIN This will displa7 all the e!o ds that have $at!hed5 '/0
SQL-

sele!t e$pno#ena$e#Ko%#dna$e#lo! f o$ e$p inne Koin dept using(deptno). 'MANO 222 444 111 'N(M' sa&eth Kagan sudha >O) anal7st $anage !le & DN(M' $&t $&t fin LOC h7d h7d %ang

"""""""""" """""""""" """""""""" """""""""" """"""""""

105

SU)QU'RI'S (ND '+ISTS


SU)QU'RI'S Nesting of Vue ies# one within the othe is te $ed as a su%Vue 75 ( state$ent !ontaining a su%Vue 7 is !alled a pa ent Vue 75 Su%Vue ies a e used to et ieve data f o$ ta%les that depend on the values in the ta%le itself5
T*A'S

Single ow su%Vue ies Multi ow su%Vue ies Multiple su%Vue ies Co elated su%Vue ies

SIN8L' RO6 SU)QU'RI'S

In single ow su%Vue 7# it will etu n one value5 '/0


SQL-

sele!t E f o$ e$p whe e sal - (sele!t sal f o$ e$p whe e e$pno I WCDD). 'MANO WWGG WG4T WT31 'N(M' SCOTT FIN8 BORD >O) (N(L*ST AR'SID'NT (N(L*ST WCDD M8R WCDD :IR'D(T' 2T"(AR"GW 34"D'C"G2 S(L COMM D'ATNO 13 23 13

"""""""""" """""""""" """"""""" """""""""" """""""""""" """"""" """""""""" """""""""" 4333 4333 2W"NO9"G2 C333

MULTI RO6 SU)QU'RI'S

106
In $ulti '/0
SQL-

ow su%Vue 7# it will

etu n $o e than one value5 In su!h !ases we should in!lude

ope ato s li&e an7# all# in o not in %etween the !o$pa ision ope ato and the su%Vue 75 sele!t E f o$ e$p whe e sal - an7 (sele!t sal f o$ e$p whe e sal %etween 1C33 and @333). 'MANO WCDD WWGG WG4T WT31 'N(M' >ON'S SCOTT FIN8 BORD >O) M8R :IR'D(T' S(L COMM D'ATNO 13 13 23 13 """""""""" """""""""" """"""""" """""""""" """"""""""" M(N(8'R (N(L*ST AR'SID'NT (N(L*ST WCDD 2T"(AR"GW 2W"NO9"G2 WCDD 34"D'C"G2 """""""" """""""""" """""""""" 1TWC 4333 C333 4333

WG4T 31"(AR"G2

SQL-

sele!t E f o$ e$p whe e sal - all (sele!t sal f o$ e$p whe e sal %etween 1C33 and @333). 'MANO WG4T 'N(M' FIN8 >O) M8R :IR'D(T' S(L COMM D'ATNO 23

"""""""""" """""""""" """"""""" """""""""" """"""""""""" """""" """""""""" """""""""" AR'SID'NT 2W"NO9"G2 C333

MULTIAL' SU)QU'RI'S

The e is no li$it on the nu$%e of su%Vue ies in!luded in a whe e !lause5 It allows nesting of a Vue 7 within a su%Vue 75 '/0
SQL-

sele!t E f o$ e$p whe e sal I (sele!t $a/(sal) f o$ e$p whe e sal , (sele!t $a/(sal) f o$ e$p)). 'MANO WWGG WT31 'N(M' SCOTT BORD >O) M8R :IR'D(T' 2T"(AR"GW 34"D'C"G2 S(L 4333 4333 COMM D'ATNO 13 13

"""""""""" """""""""" """"""""" """""""""" """""""""""" """"""" """""""""" """""""""" (N(L*ST WCDD (N(L*ST WCDD

CORR'L(T'D SU)QU'RI'S

107
( su%Vue 7 is evaluated on!e fo the enti e pa ent state$ent whe e as a !o elated su%Vue 7 is evaluated on!e fo eve 7 ow p o!essed %7 the pa ent state$ent5 '/0
SQL-

sele!t distin!t deptno f o$ e$p e whe e C ,I (sele!t !ount(ena$e) f o$ e$p whe e e5deptno I deptno). D'ATNO """""""""" 13 43

'+ISTS '/ists fun!tion is a test fo e/isten!e5 This is a logi!al test fo the etu n of ows f o$ a Vue 75 '/0 Suppose we want to displa7 the depa t$ent nu$%e s whi!h has $o e than @ e$plo7ees5
SQL-

sele!t deptno#!ount(E) f o$ e$p g oup %7 deptno having !ount(E) - @. D'ATNO """"""""" 13 43 COUNT(E) """""""""" C D

B o$ the a%ove Vue 7 !an 7ou want to displa7 the na$es of e$plo7ees[
SQL-

sele!t deptno#ena$e# !ount(E) f o$ e$p g oup %7 deptno#ena$e having !ount(E) - @.

no ows sele!ted The a%ove Vue 7 etu ns nothing %e!ause !o$%ination of deptno and ena$e neve $o e than one !ount5 The solution is to use e/ists whi!h follows5
SQL-

etu n

sele!t deptno#ena$e f o$ e$p e2 whe e e/ists (sele!t E f o$ e$p e1 whe e e25deptnoIe15deptno g oup %7 e15deptno having !ount(e15ena$e) - @) o de %7 deptno#ena$e. D'ATNO 'N(M'

108
"""""""""" """""""""" 13 13 13 13 13 43 43 43 43 43 43 22 ows sele!ted5 (D(MS BORD >ON'S SCOTT SMIT: (LL'N )L(F' >(M'S M(RTIN TURN'R 6(RD

NOT '+ISTS sele!t deptno#ena$e f o$ e$p e2 whe e not e/ists (sele!t E f o$ e$p e1 whe e e25deptnoIe15deptno g oup %7 e15deptno having !ount(e15ena$e) - @) o de %7 deptno#ena$e. D'ATNO 'N(M' """"""""" """""""""" 23 23 23 CL(RF FIN8 MILL'R

SQL-

109

6(LFUA TR''S (ND INLIN' 9I'6


6(LFUA TR''S Using hie a !hi!al Vue ies# 7ou !an et ieve data %ased on a natu al hie a !hi!al elationship %etween ows in a ta%le5 :oweve # whe e a hie a !hi!al elationship e/ists %etween the ows of a ta%le# a p o!ess !alled t ee wal&ing ena%les the hie a !h7 to %e !onst u!ted5 '/0
SQL-

sele!t ena$e XX =II-= XX p io ena$e# level f o$ e$p sta t with ena$e I =FIN8= !onne!t %7 p io e$pnoI$g . 'N(M'XX=II-=XXARIOR'N(M """""""""""""""""""""""""""""""""""" FIN8II>ON'SII-FIN8 SCOTTII->ON'S (D(MSII-SCOTT BORDII->ON'S SMIT:II-BORD )L(F'II-FIN8 (LL'NII-)L(F' 6(RDII-)L(F' M(RTINII-)L(F' TURN'RII-)L(F' >(M'SII-)L(F' CL(RFII-FIN8 MILL'RII-CL(RF L'9'L """""""" 2 1 4 @ 4 @ 1 4 4 4 4 4 1 4

110
2@ ows sele!ted5 In the a%ove Sta t with !lause spe!ifies the oot ow of the ta%le5 Level pseudo !olu$n gives the 2 fo INLIN' 9I'6 OR TOA"N (N(L*SIS In the sele!t state$ent instead of ta%le na$e# epla!ing the sele!t state$ent is &nown as inline view5 '/0
SQL-

oot # 1 fo !hild and so on5

Conne!t %7 p io !lause spe!ifies the !olu$ns whi!h has pa ent"!hild elationship5

Sele!t ena$e# sal# ownu$ an& f o$ (sele!t Ef o$ e$p o de %7 sal). 'N(M' SMIT: >(M'S (D(MS 6(RD M(RTIN MILL'R TURN'R (LL'N CL(RF )L(F' >ON'S SCOTT BORD FIN8 S(L G33 TC3 2233 21C3 21C3 2433 2C33 2D33 1@C3 1GC3 1TWC 4333 4333 C333 R(NF 2 1 4 @ C D W G T 23 22 21 24 2@

"""""""""" """""""""" """"""""""

2@ ows sele!ted5

111

LOCFS
Lo!&s a e the $e!hanis$s used to p event dest u!tive inte a!tion %etween use s a!!essing sa$e esou !e si$ultaneousl75 Lo!&s p ovides high deg ee of data !on!u en!75 T*A'S Row level lo!&s Ta%le level lo!&s

RO6 L'9'L LOCFS In the ow level lo!& a ow is lo!&ed e/!lusivel7 so that othe !annot $odif7 the ow until the t ansa!tion holding the lo!& is !o$$itted o update !lause5 '/0
SQL-

olled %a!&5 This !an %e done %7 using sele!t55fo

sele!t E f o$ e$p whe e sal - 4333 fo update of !o$$5.

T()L' L'9'L LOCFS ( ta%le level lo!& will p ote!t ta%le data the e%7 gua anteeing data integ it7 when data is %eing a!!essed !on!u entl7 %7 $ultiple use s5 ( ta%le lo!& !an %e held in seve al $odes5 Sha e lo!& Sha e update lo!& '/!lusive lo!&


S:(R' LOCF

112

( sha e lo!& lo!&s the ta%le allowing othe use s to onl7 Vue 7 %ut not inse t# update o delete ows in a ta%le5 Multiple use s !an pla!e sha e lo!&s on the sa$e esou !e at the sa$e ti$e5 '/0
SQL-

lo!& ta%le e$p in sha e $ode.

S:(R' UAD(T' LOCF

It lo!&s ows that a e to %e updated in a ta%le5 It pe $its othe use s to !on!u entl7 Vue 7# inse t # update o '/0
SQL-

even lo!& othe

ows in the sa$e ta%le5 It p events the othe

use s f o$

updating the ow that has %een lo!&ed5 lo!& ta%le e$p in sha e update $ode.

'+CLUSI9' LOCF

'/!lusive lo!& is the $ost on a ta%le at a ti$e5 '/0


SQL-

est i!tive of ta%les lo!&s5 6hen issued %7 an7 use # it allows the

othe use to onl7 Vue 75 It is si$ila to sha e lo!& %ut onl7 one use !an pla!e e/!lusive lo!&

lo!& ta%le e$p in sha e e/!lusive $ode.

NO6(IT If one use lo!&ed the ta%le without nowait then anothe use t 7ing to lo!& the sa$e ta%le oll%a!&

then he has to wait until the use who has initiall7 lo!&ed the ta%le issues a !o$$it o !o$$and5 '/0
SQL-

state$ent5 This dela7 !ould %e avoided %7 appending a nowait !lause in the lo!& ta%le

lo!& ta%le e$p in e/!lusive $ode nowait5

D'(DLOCF ( deadlo!& o!!u s when tow use s have a lo!& ea!h on sepa ate o%Ke!t# and the7 want to a!Vui e a lo!& on the ea!h othe ;s o%Ke!t5 6hen this happens# the fi st use has to wait fo the

113
se!ond use to elease the lo!&# %ut the se!ond use will not elease it until the lo!& on the fi st use ;s o%Ke!t is f eed5 In su!h a !ase# o a!le dete!ts the deadlo!& auto$ati!all7 and solves the p o%le$ %7 a%o ting one of the two t ansa!tions5

IND'+'S

Inde/ is t7pi!all7 a listing of &e7wo ds a!!o$panied %7 the lo!ation of info $ation on a su%Ke!t5 6e !an ! eate inde/es e/pli!itl7 to speed up SQL state$ent e/e!ution on a ta%le5 The inde/ points di e!tl7 to the lo!ation of the ows !ontaining the value5 6:* IND'+'S[ Inde/es a e $ost useful on la ge ta%les# on !olu$ns that a e li&el7 to appea in whe e !lauses as si$ple eVualit75 T*A'S UniVue inde/ Non"uniVue inde/ )t ee inde/ )it$ap inde/ Co$posite inde/ Reve se &e7 inde/ Bun!tion"%ased inde/ Do$ain inde/ O%Ke!t inde/ Cluste inde/ Te/t inde/ Inde/ o ganiUed ta%le Aa tition inde/ Lo!al inde/

114
Lo!al p efi/ed Lo!al non"p efi/ed 8lo%al inde/ 8lo%al p efi/ed 8lo%al non"p efi/ed

UNIQU' IND'+ UniVue inde/es gua antee that no two !onst aint is ! eated5 '/0
SQL-

ows of a ta%le have dupli!ate values in the !olu$ns uniVue

that define the inde/5 UniVue inde/ is auto$ati!all7 ! eated when p i$a 7 &e7 o

! eate uniVue inde/ studNind on student(sno).

NON"UNIQU' IND'+ Non"UniVue inde/es do not i$pose the a%ove est i!tion on the !olu$n values5 '/0
SQL-

! eate inde/ studNind on student(sno).

)TR'' IND'+ The default t7pe of inde/ used in an o a!le data%ase is the %t ee inde/5 ( %t ee inde/ is designed to p ovide %oth apid a!!ess to individual ows and Vui!& a!!ess to g oups of ows within a '/0
SQL-

ange5 The %t ee inde/ does this %7 pe fo $ing a su!!ession of value !o$pa isons5

'a!h !o$pa ison eli$inates $an7 of the ows5 ! eate inde/ studNind on student(sno).

)ITM(A IND'+ This !an %e used fo low !a dinalit7 !olu$ns0 that is !olu$ns in whi!h the nu$%e of distin!t values is snall when !o$pa ed to the nu$%e of the ows in the ta%le5 '/0

115
SQL-

! eate %it$ap inde/ studNind on student(se/).

COMAOSIT' IND'+ ( !o$posite inde/ also !alled a !on!atenated inde/ is an inde/ ! eated on $ultiple !olu$ns of a ta%le5 Colu$ns in a !o$posite inde/ !an appea !olu$ns of the ta%le5 '/0
SQL-

in an7 o de

and need not %e adKa!ent

! eate %it$ap inde/ studNind on student(sno# sna$e).

R'9'RS' F'* IND'+ ( eve se &e7 inde/ when !o$pa ed to standa d inde/# eve ses ea!h %7te of the !olu$n %eing inde/ed while &eeping the !olu$n o de 5 Su!h an a ange$ent !an help avoid pe fo $an!e deg adations in inde/es5 '/0
SQL-

! eate inde/ studNind on student(sno# eve se).

6e !an e%uild a eve se &e7 inde/ into no $al inde/ using the no eve se &e7wo d5 '/0
SQL-

alte inde/ studNind e%uild no eve se.

BUNCTION )(S'D IND'+ This will use esult of the fun!tion as &e7 instead of using !olu$n as the value fo the &e75 '/0
SQL-

! eate inde/ studNind on student(uppe (sna$e)).

IND'+"OR8(NIZ'D T()L' (n inde/"o ganiUed ta%le &eeps its data so ted a!!o ding to the p i$a 7 &e7 !olu$n values fo the ta%le5 Inde/"o ganiUed ta%les sto e thei data as if the enti e ta%le was sto ed in an inde/5 (n inde/"o ganiUed ta%le allows 7ou to sto e the enti e ta%le;s data in an inde/5 '/0
SQL-

! eate ta%le student (sno nu$%e (1)#sna$e va !ha (23)#s$a &s nu$%e (4) !onst aint

116
p& p i$a 7 &e7(sno) o ganiUation inde/.

117

IMA QU'RI'S
2) To find the nth ow of a ta%le
SQL-

Sele!t Ef o$ e$p whe e owid I (sele!t $a/( owid) f o$ e$p whe e ownu$ ,I @). O Sele!t Ef o$ e$p whe e ownu$ ,I @ $inus sele!t Ef o$ e$p whe e ownu$ ,I 4.

SQL-

1) To find dupli!ate ows


SQL-

Sele!t Ef o$ e$p whe e owid in (sele!t $a/( owid) f o$ e$p g oup %7 e$pno# ena$e# $g # Ko%# hi edate# !o$$# deptno# sal). O Sele!t e$pno#ena$e#sal#Ko%#hi edate#!o$$ # !ount(E) f o$ e$p g oup %7 e$pno#ena$e#sal#Ko%#hi edate#!o$$ having !ount(E) -I2.

SQL-

4) To delete dupli!ate ows


SQL-

Delete e$p whe e owid in (sele!t $a/( owid) f o$ e$p g oup %7 e$pno#ena$e#$g #Ko%#hi edate#sal#!o$$#deptno).

@) To find the !ount of dupli!ate ows


SQL-

Sele!t ena$e# !ount(E) f o$ e$p g oup %7 ena$e having !ount(E) -I 2.

C) :ow to displa7 alte native ows in a ta%le[


SQL-

sele!t Ef o$ e$p whe e ( owid#3) in (sele!t owid#$od( ownu$#1) f o$ e$p).

D) 8etting e$plo7ee details of ea!h depa t$ent who is d awing $a/i$u$ sal[
SQL-

sele!t Ef o$ e$p whe e (deptno#sal) in ( sele!t deptno#$a/(sal) f o$ e$p g oup %7 deptno).

118
W) :ow to get nu$%e of e$plo7ees in ea!h depa t$ent # in whi!h depa t$ent is having $o e than 1C33 e$plo7ees[
SQL-

Sele!t deptno#!ount(E) f o$ e$p g oup %7 deptno having !ount(E) -1C33.

T) To eset the ti$e to the %eginning of the da7


SQL-

Sele!t toN!ha (t un!(s7sdate)#;dd"$on"7777 hh0$i0ss a$;) f o$ dual.

23) To find nth $a/i$u$ sal


SQL-

Sele!t Ef o$ e$p whe e sal in (sele!t $a/(sal) f o$ (sele!t Ef o$ e$p o de %7 sal) whe e owu$ ,I C).

119

INTRODUCTION
The %asi! unit in an7 AL?SQL p og a$ is %lo!&5 (ll AL?SQL p og a$s a e !o$posed of %lo!&s whi!h !an o!!u seVuentiall7 o nested5 )LOCF STRUCTUR' De!la e "" de!la ative se!tion )egin "" e/e!uta%le se!tion '/!eption "" e/!eption se!tion 'nd. In the a%ove de!la ative and e/!eptiona se!tions a e optional5 )LOCF T*A'S (non7$ous %lo!&s Na$ed %lo!&s La%eled %lo!&s Su%p og a$s T igge s
(NON*MOUS )LOCFS

(non7$ous %lo!&s i$plies %asi! %lo!& st u!tu e5

120
'/0
)'8IN

D%$sNoutput5putNline(LM7 fi st p og a$;)0
'ND. L()'L'D )LOCFS

La%eled %lo!&s a e anon7$ous %lo!&s with a la%el whi!h gives a na$e to the %lo!&5 '/0 ,,$7N%loo!&-)'8IN

D%$sNoutput5putNline(LM7 fi st p og a$;)0
'ND.

SU)ARO8R(MS

Su%p og a$s a e p o!edu es and fun!tions5 The7 !an %e sto ed in the data%ase as stand"alone o%Ke!ts# as pa t of pa!&age o as $ethods of an o%Ke!t t7pe5
TRI88'RS

T igge s !onsists of a AL?SQL %lo!& that is asso!iated with an event that o!!u in the data%ase5 N'ST'D )LOCFS ( %lo!& !an %e nested within the e/e!uta%le o e/!eption se!tion of an oute %lo!&5 ID'NTIBI'RS Identifie s a e used to na$e AL?SQL o%Ke!ts# su!h as va ia%les# !u so s# t7pes and su%p og a$s5 Identifie s !onsists of a lette # optionall7 followed %7 an7 seVuen!e of !ha a!te s# in!luding lette s# nu$%e s# dolla signs# unde s!o es# and pound signs onl75 The $a/i$u$ length fo an identifie is 43 !ha a!te s5 QUOT'D ID'NTIBI'RS

121
If 7ou want to $a&e an identifie '/0
D'CL(R'

!ase sensitive# in!lude !ha a!te s su!h as spa!es o use a

ese ved wo d# 7ou !an en!lose the identifie in dou%le Vuotation $a &s5

]a] nu$%e 0I C. ](] nu$%e 0I D.


)'8IN

d%$sNoutput5putNline(=a I = XX a). d%$sNoutput5putNline(=( I = XX ().


'ND.

Output0 aID (ID COMM'NTS Co$$ents i$p ove eada%ilit7 and $a&e 7ou p og a$ $o e unde standa%le5 The7 a e igno ed %7 the AL?SQL !o$pile 5 The e a e two t7pes of !o$$ents availa%le5 Single line !o$$ents Multiline !o$$ents

SIN8L' LIN' COMM'NTS

( single"line !o$$ent !an sta t an7 point on a line with two dashes and !ontinues until the end of the line5 '/0
)'8IN

D%$sNoutput5putNline(Lhello;).
'ND.

"" sa$ple p og a$

MULTILIN' COMM'NTS

Multiline !o$$ents sta t with the ?E deli$ite and ends with E? deli$ite 5 '/0

122
)'8IN

D%$sNoutput5putNline(Lhello;).
'ND.

?E sa$ple p og a$ E?

9(RI()L' D'CL'R(TIONS 9a ia%les !an %e de!la ed in de!la ative se!tion of the %lo!&.

'/0
D'CL(R'

a nu$%e . % nu$%e 0I C. ! nu$%e default D. d nu$%e !onstant 0I W. (NC:OR'D D'CL'R(TIONS AL?SQL offe s two &inds of a!ho ing5 S!ala an!ho ing Re!o d an!ho ing

SC(L(R (NC:ORIN8

Use the MT*A' att i%ute to define 7ou va ia%le %ased on ta%le;s !olu$n of so$e othe AL?SQL s!ala va ia%le5 '/0
D'CL(R'

dno dept5deptnoMt7pe. Su%t7pe tNnu$%e is nu$%e . a tNnu$%e . Su%t7pe tNsno is student5snoMt7pe. 9Nsno tNsno.
SC(L(R (NC:ORIN8

Use the MRO6T*A' att i%ute to define 7ou

e!o d st u!tu e %ased on a ta%le5

123
'/0
D'CL(R'

9Ndept deptM owt7pe. D(T(T*A' CON9'RSIONS AL?SQL !an handle !onve sions %etween diffe ent fa$ilies a$ong the datat7pes5 Conve sion !an %e done in two wa7s5 '/pli!it !onve sion I$pli!it !onve sion

'+ALICIT CON9'RSION

This !an %e done using the %uilt"in fun!tions availa%le5


IMALICIT CON9'RSION

AL?SQL will auto$ati!all7 !onve t %etween datat7pe fa$ilies when possi%le5 '/0
D'CL(R'

a va !ha (23).
)'8IN

sele!t deptno into a f o$ dept whe e dna$eI=(CCOUNTIN8=.


'ND.

In the a%ove va ia%le a is !ha

t7pe and deptno is nu$%e

t7pe even though# o a!le will

auto$ati!all7 !onve ts the nu$e i! data into !ha t7pe assigns to the va ia%le5 AL?SQL !an auto$ati!all7 !onve t %etween Cha a!te s and nu$%e s Cha a!te s and dates

9(RI()L' SCOA' (ND 9ISI)ILIT*

124
The s!ope of a va ia%le is the po tion of the p og a$ in whi!h the va ia%le !an %e a!!essed5 Bo AL?SQL va ia%les# this is f o$ the va ia%le de!la ation until the end of the %lo!&5 6hen a va ia%le goes out of s!ope# the AL?SQL engine will f ee the $e$o 7 used to sto e the va ia%le5 The visi%ilit7 of a va ia%le is the po tion of the p og a$ whe e the va ia%le !an %e a!!essed without having to Vualif7 the efe en!e5 The visi%ilit7 is alwa7s within the s!ope5 If it is out of s!ope# it is not visi%le5 '/20
D'CL(R'

a nu$%e .
)'8IN """""""" D'CL(R'

"" s!ope of a

% nu$%e .
)'8IN

"" s!ope of %

"""""
'ND.

""""""
'ND.

'/10
D'CL(R'

a nu$%e . % nu$%e .
)'8IN

"" a # % availa%le he e
D'CL(R'

% !ha (23).
)'8IN

"" a and !ha t7pe % is availa%le he e


'ND.

"""""
'ND.

'/40 ,,$7N%lo!&-D'CL(R'

125
a nu$%e . % nu$%e .
)'8IN

"" a # % availa%le he e
D'CL(R'

% !ha (23).
)'8IN

"" a and !ha t7pe % is availa%le he e "" nu$%e t7pe % is availa%le using ,,$7N%lo!&--5%
'ND.

""""""
'ND.

AL?SQL CONTROL STRUCTUR'S AL?SQL has a va iet7 of !ont ol st u!tu es that allow 7ou to !ont ol the %ehaviou of the %lo!& as it uns5 These st u!tu es in!lude !onditional state$ents and loops5 If"then"else Case Case with no else La%eled !ase Sea !hed !ase Si$ple loop 6hile loop Bo loop 8oto and La%els

IB"T:'N"'LS' S7nta/0 If ,condition1- then "e-uence o% statements. 'lsif ,condition1- then "e-uence o% statements. SS

126
'lse "e-uence o% statements. 'nd if. '/0
D'CL(R'

dno nu$%e (1).


)'8IN

sele!t deptno into dno f o$ dept whe e dna$e I =(CCOUNTIN8=. if dno I 23 then d%$sNoutput5putNline(=Lo!ation is elsif dno I 13 then d%$sNoutput5putNline(=Lo!ation is elsif dno I 43 then d%$sNoutput5putNline(=Lo!ation is else d%$sNoutput5putNline(=Lo!ation is end if.
'ND. )OSTON=). C:IC(8O=). D(LL(S=). N'6 *ORF=).

Output0 Lo!ation is C(S' S7nta/0 Case test/variable 6hen value1 then se-uence o% statements. 6hen value2 then se-uence o% statements. SS 6hen valuen then se-uence o% statements. 'lse se-uence o% statements. 'nd !ase. '/0
D'CL(R' N'6 *ORF

dno nu$%e (1).

127
)'8IN

sele!t deptno into dno f o$ dept whe e dna$e I =(CCOUNTIN8=. !ase dno when 23 then d%$sNoutput5putNline(=Lo!ation is when 13 then d%$sNoutput5putNline(=Lo!ation is when 43 then d%$sNoutput5putNline(=Lo!ation is else d%$sNoutput5putNline(=Lo!ation is end !ase.
'ND. )OSTON=). C:IC(8O=). D(LL(S=). N'6 *ORF=).

Output0 Lo!ation is C(S' 6IT:OUT 'LS' S7nta/0 Case test/variable 6hen value1 then se-uence o% statements. 6hen value2 then se-uence o% statements. SS 6hen valuen then se-uence o% statements. 'nd !ase. '/0
D'CL(R' N'6 *ORF

dno nu$%e (1).


)'8IN

sele!t deptno into dno f o$ dept whe e dna$e I =(CCOUNTIN8=. !ase dno when 23 then d%$sNoutput5putNline(=Lo!ation is when 13 then d%$sNoutput5putNline(=Lo!ation is when 43 then
D(LL(S=). N'6 *ORF=).

128
d%$sNoutput5putNline(=Lo!ation is when @3 then d%$sNoutput5putNline(=Lo!ation is end !ase.
'ND. )OSTON=). C:IC(8O=).

Output0 Lo!ation is L()'L'D C(S' S7nta/0 ,,label-Case test/variable 6hen value1 then se-uence o% statements. 6hen value2 then se-uence o% statements. SS 6hen valuen then se-uence o% statements. 'nd !ase. '/0
D'CL(R' N'6 *ORF

dno nu$%e (1).


)'8IN

sele!t deptno into dno f o$ dept whe e dna$e I =(CCOUNTIN8=. ,,$7N!ase-!ase dno when 23 then d%$sNoutput5putNline(=Lo!ation is when 13 then d%$sNoutput5putNline(=Lo!ation is when 43 then d%$sNoutput5putNline(=Lo!ation is when @3 then d%$sNoutput5putNline(=Lo!ation is end !ase $7N!ase.
'ND. )OSTON=). C:IC(8O=). D(LL(S=). N'6 *ORF=).

129

Output0 Lo!ation is S'(RC:'D C(S' S7nta/0 Case 6hen ,condition1- then se-uence o% statements. 6hen ,condition2- then se-uence o% statements. SS 6hen ,conditionn- then se-uence o% statements. 'nd !ase. '/0
D'CL(R' N'6 *ORF

dno nu$%e (1).


)'8IN

sele!t deptno into dno f o$ dept whe e dna$e I =(CCOUNTIN8=. !ase dno when dno I 23 then d%$sNoutput5putNline(=Lo!ation is when dno I 13 then d%$sNoutput5putNline(=Lo!ation is when dno I 43 then d%$sNoutput5putNline(=Lo!ation is when dno I @3 then d%$sNoutput5putNline(=Lo!ation is end !ase.
'ND. )OSTON=). C:IC(8O=). D(LL(S=). N'6 *ORF=).

Output0 Lo!ation is SIMAL' LOOA S7nta/0


N'6 *ORF

130
Loop "e-uence o% statements. '/it when ,condition-. 'nd loop. In the s7nta/ e/it when ,condition- is eVuivalent to If ,condition- then '/it. 'nd if. '/0
D'CL(R'

i nu$%e 0I 2.
)'8IN

loop d%$sNoutput5putNline(=i I = XX i). i 0I i H 2. e/it when i - C. end loop.


'ND.

Output0 iI2 iI1 iI4 iI@ iIC 6:IL' LOOA S7nta/0 6hile ,!ondition- loop "e-uence o% statements. 'nd loop. '/0
D'CL(R'

i nu$%e 0I 2.

131
)'8IN

6hile i ,I C loop d%$sNoutput5putNline(=i I = XX i). i 0I i H 2. end loop.


'ND.

Output0 iI2 iI1 iI4 iI@ iIC BOR LOOA S7nta/0 Bo ,loop_counter_variable- in low_bound55'i '_bound loop "e-uence o% statements. 'nd loop. '/20
)'8IN

Bo i in 255C loop d%$sNoutput5putNline(=i I = XX i). end loop.


'ND.

Output0 iI2 iI1 iI4 iI@ iIC '/10


)'8IN

Bo i in eve se 255C loop

132
d%$sNoutput5putNline(=i I = XX i). end loop.
'ND.

Output0 iIC iI@ iI4 iI1 iI2

8OTO (ND L()'LS S7nta/0 8oto label. 6he e label is a la%el defined in the AL?SQL %lo!&5 La%els a e en!losed in dou%le angle % a!&ets5 6hen a goto state$ent is evaluated# !ont ol i$$ediatel7 passes to the state$ent identified %7 the la%el5 '/0
)'8IN

Bo i in 255C loop d%$sNoutput5putNline(=i I = XX i). if i I @ then goto e/itNloop. end if. end loop. ,,e/itNloop-Null.
'ND.

Output0 iI2 iI1

133
iI4 iI@
R'STRICTIONS ON 8OTO

It is illegal to % an!h into an inne %lo!&# loop5 It is illegal to % an!h into an if state$ent5 It is illegal to % an!h f o$ one if state$ent to anothe if state$ent5 It is illegal to % an!h f o$ e/!eption %lo!& to the !u ent %lo!&5

AR(8M(S A ag$as a e !o$pile di e!tives5 The7 se ve as inst u!tions to the AL?SQL !o$pile 5 The

!o$pile will a!t on the p ag$a du ing the !o$pilation of the %lo!&5

134

SU)ARO8R(MS
In pa a$ete will a!t as pl?sVl !onstant5 Out pa a$ete will a!t as unintialiUed va ia%le5 In out pa a$ete will a!t as initialiUed va ia%le5 In pa a$ete s %7 default !all %7 efe en!e whe e as out and in out !all %7 value5 6e !an !o$%ine positional and na$e notation %ut positional notation !an not %e followed %7 the na$e notation5 6e !an not use L!all; without pa a$ete s5 6e !an not use !all with out and in out pa a$ete s5 Out and In Out pa a$ete s !an not have default values5 6e !an not use Le/e!; with out o in out pa a$ete s5 Default Aa a$ete s will not allow in the %eginning and $iddle5 '/0 p o!edu e p(a in nu$%e default C# % in nu$%e default D# ! in nu$%e default W) \ valild p o!edu e p(a in nu$%e # % in nu$%e default D# ! in nu$%e default W) \ valild p o!edu e p(a in nu$%e # % in nu$%e # ! in nu$%e default W) \ valild p o!edu e p(a in nu$%e # % in nu$%e default D# ! in nu$%e ) \ invalild p o!edu e p(a in nu$%e default C# % in nu$%e default D# ! in nu$%e ) \ invalild p o!edu e p(a in nu$%e default C# % in nu$%e # ! in nu$%e ) \ invalild

135
If the su%p og a$ !ontains a va ia%le with the sa$e na$e as the !olu$n na$e of the ta%le then use the dot $ethod to diffe entiate (p5sal)5 If an7 su%p og a$ was !alled# on!e the !all was !o$pleted then the values of fo $al pa a$ete s a e !opied to the a!tual pa a$ete s5 The !op7ing f o$ fo $al to a!tual !an %e est i!ted %7 issuing no!op7 Vualifie 5 Su%p og a$s !an %e lo!al5 6hen a sto ed su%p og a$ is ! eated# it is sto ed in the data di!tiona 75 The su%p og a$ is sto ed in !o$pile fo $ whi!h is &nown as p"!ode in addition to the sou !e te/t5 The p"!ode has all of the efe en!es in the su%p og a$ evaluated# and the sou !e !ode is t anslated into a fo $ that is easil7 eada%le %7 pl?sVl engine5 6hen the su%p og a$ is !alled# the p"!ode is e/e!uted5 On!e it eads f o$ the dis&# the p"!ode is sto ed in the sha ed pool po tion of the s7ste$ glo%al a ea (S8()# whe e it !an %e a!!essed %7 $ultiple use s as needed5 Li&e all of the !ontents of the sha ed pool# p"!ode is aged out of the sha ed pool a!!o ding to a least e!entl7 used (LRU) algo ith$5 Lo!al su%p og a$s $ust %e de!la ed in the de!la ative se!tion of pl?sVl %lo!& and !alled f o$ the e/e!uta%le se!tion5 '/0
CR'(T' OR R'AL(C' AROC'DUR' S(MAL'(a )'8IN

ead f o$ the dis&# if ne!essa 7# and

Su%p og a$s !an not have the de!la ative se!tion sepa atel75 Sto ed su%p og a$s !an have lo!al su%p og a$s. (n7 lo!al su%p og a$ $ust %e de!la ed at the end of the de!la ative se!tion5

in nu$%e #% out nu$%e #! in out nu$%e ) is

d%$sNoutput5putNline(=(fte !all=). d%$sNoutput5putNline(=a I = XX a XX= % I = XX % XX = ! I = XX !). % 0I 23. ! 0I 13. d%$sNoutput5putNline(=(fte assign$ent=). d%$sNoutput5putNline(=a I = XX a XX= % I = XX % XX = ! I = XX !).
'ND S(MAL'. D'CL(R'

v2 nu$%e 0I @.

136
v1 nu$%e 0I C. v4 nu$%e 0I D.
)'8IN

d%$sNoutput5putNline(=)efo e !all=). d%$sNoutput5putNline(=v2 I = XX v2 XX = v1 I = XX v1 XX = v4 I = XX v4). sa$ple(v2#v1#v4). d%$sNoutput5putNline(=(fte !o$pletion of !all=). d%$sNoutput5putNline(=v2 I = XX v2 XX = v1 I = XX v1 XX = v4 I = XX v4).
'ND.

Output0 )efo e !all v2 I @ v1 I C v4 I D (fte !all aI@%I !ID (fte assign$ent a I @ % I 23 ! I 13 (fte !o$pletion of !all v2 I @ v1 I 23 v4 I 13 C(LL )* R'B'R'NC' (ND C(LL )* 9(LU' 6hen pa a$ete passed %7 efe en!e# a pointe to the a!tual pa a$ete is passed to the !o esponding fo $al pa a$ete 5 6hen pa a$ete fo $al pa a$ete 5 Call %7 efe en!e is faste than the !all %7 value %e!ause it avoids the !op7ing5 passed %7 value it !opies the value of the a!tual pa a$ete to the

R'STRICTIONS ON BORM(L A(R(M'T'RS )7 de!la ing with spe!ified siUe in a!tual pa a$ete s5 )7 de!la ing fo $al pa a$ete s with Mt7pe spe!ifie 5

SU)ARO8R(MS O9'RLO(DIN8 Aossi%le with diffe ent nu$%e of pa a$ete s5

137
'/0
SQLSQL-

Aossi%le with diffe ent t7pes of data5 Aossi%le with sa$e t7pe with o%Ke!ts5 Can not %e possi%le with diffe ent t7pes of $odes5 6e !an ove load lo!al su%p og a$s also5 ! eate o ! eate o epla!e t7pe t2 as o%Ke!t(a nu$%e ).? epla!e t7pe t2 as o%Ke!t(a nu$%e ).?

D'CL(R'

i t2 0I t2(C). K t1 0I t1(C).
AROC'DUR' A($ t2) IS )'8IN

d%$sNoutput5putNline(=a I = XX $5a).
'ND A. AROC'DUR' A(n t1) IS )'8IN

d%$sNoutput5putNline(=% I = XX n5%).
'ND A. AROC'DUR' ARODUCT(a nu$%e #% nu$%e ) IS )'8IN

d%$sNoutput5putNline(=A odu!t of a#% I = XX a E %).


'ND ARODUCT. AROC'DUR' ARODUCT(a nu$%e #% nu$%e #! nu$%e ) IS )'8IN

d%$sNoutput5putNline(=A odu!t of a#% I = XX a E % E !).


'ND ARODUCT. )'8IN

p(i). p(K). p odu!t(@#C). p odu!t(@#C#D).


'ND.

Output0 aIC %IC

138
A odu!t of a#% I 13 A odu!t of a#% I 213 AROC'DUR'S 9'RS'S BUNCTIONS A o!edu es $a7 etu n5 A o!edu es !an not have etu n !lause whe e as fun!tions $ust5 6e !an use !all state$ent di e!tl7 fo e/e!uting p o!edu e whe e as we need to de!la e a va ia%le in !ase of fun!tions5 Bun!tions !an use in sele!t state$ents whe e as p o!edu es !an not5 Bun!tions !an !all f o$ epo ts envi on$ent whe e as p o!edu es !an not5 6e !an use e/e! fo e/e!uting p o!edu es whe e as fun!tions !an not5 Bun!tion !an %e used in d%$sNoutput whe e as p o!edu e !an not5 etu n th ough out and in out pa a$ete s whe e as fun!tion $ust

STOR' 9'RS'S LOC(L SU)ARO8R(MS The sto ed su%p og a$ is sto ed in !o$piled p"!ode in the data%ase# when the p o!edu e is !alled it does not have to %e !o$piled5 The lo!al su%p og a$ is !o$piled as pa t of its !ontaining %lo!&5 If the !ontaining %lo!& is anon7$ous and is un $ultiple ti$es# the su%p og a$ has to %e !o$piled ea!h ti$e5 Sto ed su%p og a$s !an %e !alled f o$ an7 %lo!& su%$itted %7 a use who has e/e!ute p ivileges on the su%p og a$5 Lo!al su%p og a$s !an %e !alled onl7 f o$ the %lo!& !ontaining the su%p og a$5 )7 &eeping the sto ed su%p og a$ !ode sepa ate f o$ the !alling %lo!&# the !alling %lo!& is sho te and easie to unde stand5 The lo!al su%p og a$ and the !alling %lo!& a e one and the sa$e# whi!h !an lead to pa t !onfusion5 If a !hange to the !alling %lo!& is $ade# the su%p og a$ will %e e!o$piled as of the e!o$pilation of the !ontaining %lo!&5 The !o$piled p"!ode !an %e pinned in the sha ed pool using the D)MSNS:(R'DNAOOL Aa!&age5 This !an i$p ove pe fo $an!e5 Lo!al su%p og a$s !annot %e pinned in the sha ed pool %7 the$selves5 Stand alone sto ed su%p og a$s !an not %e ove loaded# %ut pa!&aged su%p og a$s !an %e ove loaded within the sa$e pa!&age5 Lo!al su%p og a$s !an %e ove loaded within the sa$e %lo!&5

139

'/20
CR'(T' OR R'AL(C' AROC'DUR' A IS )'8IN

d%$sNoutput5putNline(=Sto ed su%p og a$=).


'ND.

Output0
SQL-

e/e! p

Sto ed su%p og a$ '/10


D'CL(R' AROC'DUR' A IS )'8IN

d%$sNoutput5putNline(=Sto ed su%p og a$=).


'ND. )'8IN

p.
'ND.

Output0 Sto ed su%p og a$ COMAILIN8 SU)ARO8R(MS

SQLSQL-

(lte p o!edu e A2 !o$pile. (lte fun!tion B2 !o$pile.

SU)ARO8R(MS D'A'ND'CI'S ( sto ed su%p og a$ is $a &ed as invalid in the data di!tiona 7 if it has !o$pile e o s5 ( sto ed su%p og a$ !an also %e!o$e invalid if a DDL ope ation is pe fo $ed on one of its dependent o%Ke!ts5 If a su%p og a$ is invalidated# the AL?SQL engine will auto$ati!all7 atte$pt to e!o$pile in the ne/t ti$e it is !alled5 If we have two p o!edu es li&e A2 and A1 in whi!h A2 depends on A15 If we !o$pile A1 then A2 is invalidated5

140
SU)ARO8R(MS D'A'ND'NCI'S IN R'MOT' D(T()(S'S

6e will !all e$ote su%p og a$ using !onne!t st ing li&e A2^9IS. If we have two p o!edu es li&e A2 and A1 in whi!h A2 depends on A1 %ut A1 was in e$ote data%ase5 If we !o$pile A1 it will not invalidate A2 i$$ediatel7 %e!ause the data di!tiona 7 does not t a!& e$ote dependen!ies5 Instead the validit7 of e$ote o%Ke!ts is !he!&ed at unti$e5 6hen A2 is !alled# the

e$ote data di!tiona 7 is Vue ied to dete $ine the status of A15 A2 and A1 a e !o$pa ed to see it A2 needs to %e $ethods of !o$pa ision Ti$esta$p Model Signatu e Model TIM'ST(MA MOD'L This is the default $odel used %7 o a!le5 6ith this $odel# the ti$esta$ps of the last $odifi!ations of the two o%Ke!ts a e !o$pa ed5 The last_ddl_time field of user_ob!ects !ontains the ti$esta$p5 If the %ase o%Ke!t has a newe ti$esta$p than the dependent o%Ke!t# the dependent o%Ke!t will %e e!o$piled5
ISSU'S 6IT: T:IS MOD'L

e!o$piled# the e a e two diffe ent

If the o%Ke!ts a e in diffe ent ti$e Uones# the !o$pa ison is invalid5 6hen A2 is in a !lient side AL?SQL engine su!h as o a!le fo $s# in this !ase it $a7 not possi%le to e!o$pile A2# %e!ause the sou !e fo it $a7 not %e in!luded with the fo $s5

SI8N(TUR' MOD'L 6hen a p o!edu e is ! eated# a signatu e is sto ed in the data di!tiona 7 in addition to the p"!ode5 The signatu e en!odes the t7pes and o de of the pa a$etes5 6hen A2 is !o$piled the fi st ti$e# the signatu e of A1 is in!luded5 Thus# A2 onl7 needs to e!o$piled when the signatu e of A1 !hanges5

141 In o de to use the signatu e $odel# the pa a$ete


set to
SI8N(TUR'5 R'MOT'ND'A'ND'NCI'SNMOD'

$ust %e

This is a pa a$ete in the data%ase initialiUation file5

T:R'' 6(*S OB S'TTIN8 T:IS MOD'

(dd the line

R'MOT'ND'A'ND'NCI'SNMOD'ISI8N(TUR'

to the data%ase initialiUation file5 The


SI8N(TUR'

ne/t ti$e the data%ase is sta ted# the $ode will %e set to

fo all sessions5

(lte s7ste$ set e$oteNdependen!iesN$ode I signatu e. This will affe!t the enti e data%ase (all sessions) f o$ the ti$e the state$ent is issued5 *ou $ust have the
(LT'R S*ST'M

p ivilege to issue this !o$$and5

(lte session set e$oteNdependen!iesN$ode I signatu e. This will onl7 affe!t 7ou session

ISSU'S 6IT: T:IS MOD'L

Signatu es don;t get $odified if the default values of fo $al pa a$ete s a e !hanged5 Suppose A1 has a default value fo one of its pa a$ete s# and A2 is using this default value5 If the default in the spe!ifi!ation fo A1 is !hanged# A2 will not %e e!o$piled %7 default5 The old value fo the default pa a$ete will still %e used until A2 is $anuall7 e!o$piled5 If A2 is !alling a pa!&aged p o!edu e A1# and a new ove loaded ve sion of A1 is added to the e$ote pa!&age# the signatu e is not !hanged5 A2 will still use the old ve sion (not the new ove loaded one) until A2 is e!o$piled $anuall75

BOR6(RD D'CL'R(TION )efo e going to use the p o!edu e in an7 othe su%p og a$ o othe %lo!& # 7ou $ust de!la e the p otot7pe of the p o!edu e in de!la ative se!tion5 '/20
D'CL(R' AROC'DUR' A2 IS )'8IN

d%$sNoutput5putNline(=B o$ p o!edu e p2=). p1.

142
'ND A2. AROC'DUR' A1 IS )'8IN

d%$sNoutput5putNline(=B o$ p o!edu e p1=). p4.


'ND A1. AROC'DUR' A4 IS )'8IN

d%$sNoutput5putNline(=B o$ p o!edu e p4=).


'ND A4. )'8IN

p2.
'ND.

Output0 p1. E 'RROR at line C0 OR("3DCC30 line C# !olu$n 20 ALS"334240 =A1= not de!la ed in this s!ope OR("3DCC30 line C# !olu$n 20 AL?SQL0 State$ent igno ed OR("3DCC30 line 23# !olu$n 20 ALS"334240 =A4= not de!la ed in this s!ope OR("3DCC30 line 23# !olu$n 20 AL?SQL0 State$ent igno ed '/10
D'CL(R' AROC'DUR' A1. AROC'DUR' A4. AROC'DUR' A2 IS )'8IN

"" fo wa d de!la ation

d%$sNoutput5putNline(=B o$ p o!edu e p2=). p1.


'ND A2. AROC'DUR' A1 IS )'8IN

d%$sNoutput5putNline(=B o$ p o!edu e p1=).

143
p4.
'ND A1. AROC'DUR' A4 IS )'8IN

d%$sNoutput5putNline(=B o$ p o!edu e p4=).


'ND A4. )'8IN

p2.
'ND.

Output0 B o$ p o!edu e p2 B o$ p o!edu e p1 B o$ p o!edu e p4 ARI9IL'8'S (ND STOR'D SU)ARO8R(MS


'+'CUT' AR'9IL'8'

Bo sto ed su%p og a$s and pa!&ages the elevant p ivilege is


p o!edu e to use ) with the following !o$$and5 8 ant e/e!ute on e$pNp o! to use )5 Then use ) !an un the p o!edu e %7 issuing '/e! use (5e$pNp o! use ( ! eated the following p o!edu e
CR'(T' OR R'AL(C' AROC'DUR' A IS

'+'CUT'5

If use ( had the p o!edu e !alled e$pNp o! then use ( g ants e/e!ute p ivilege on

!u so is sele!t Ef o$ student2.
)'8IN

fo v in ! loop inse t into student1 values(v5no#v5na$e#v5$a &s). end loop.


'ND A.

use ( g anted e/e!ute p ivilege to use ) using


SQL-

g ant e/e!ute on p to use )

144

Then use ) e/e!uted the p o!edu e


SQL-

'/e! use (5p

If suppose use ) also having student1 ta%le then whi!h ta%le will populate whethe use (;s o use );s5 The answe is use (;s student1 ta%le onl7 %e!ause %7 default the p o!edu e will e/e!ute unde the p ivlige set of its owne 5 The a%ove p o!edu e is &nown as define ;s p o!edu e5
:O6 TO AOAUL(T' US'R );s T()L'

O a!le int odu!es Invo&e ;s and Define ;s ights5 )7 default it will use the define ;s ights5 (n invo&e ;s use );s ta%le5 It is valid fo ights outine !an %e ! eated %7 using (UT:ID !lause to populate the

stand"alone su%p og a$s# pa!&age spe!ifi!ations# and o%Ke!t t7pe

spe!ifi!ations onl75 use ( ! eated the following p o!edu e


CR'(T' OR R'AL(C' AROC'DUR' A (UT:ID CURR'NTNUS'R IS

!u so is sele!t Ef o$ student2.
)'8IN

fo v in ! loop inse t into student1 values(v5no#v5na$e#v5$a &s). end loop.


'ND A.

Then g ant e/e!ute p ivilege on p to use )5 '/e!uting the p o!edu e %7 use )# whi!h populates use );s ta%le5 The a%ove p o!edu e is !alled invo&e ;s p o!edu e5 Instead of !u entNuse p o!edu e5
STOR'D SU)ARO8R(MS (ND ROL'S

of authid !lause# if 7ou use define

then it will %e !alled define ;

we have two use s sa&eth and sudha in whi!h sa&eth has student ta%le and sudha does not5

145
Sudha is going to ! eate a p o!edu e %ased on student ta%le owned %7 sa&eth5 )efo e doing this sa&eth $ust g ant the pe $issions on this ta%le to sudha5 !onn sa&eth?sa&eth g ant all on student to sudha. !onn sudha?sudha

SQLSQL-

then sudha !an ! eate p o!edu e


SQL-

CR'(T' OR R'AL(C' AROC'DUR' A IS

!u so ! is sele!t Ef o$ sa&eth5student.
)'8IN

fo v in ! loop d%$sNoutput5putNline(LNo I L XX v5no). end loop.


'ND A.

he e p o!edu e will %e ! eated5 If the sa$e p ivilege was g anted th ough a ole it wont ! eate the p o!edu e5 '/a$ine the following !ode !onn sa&eth?sa&eth ! eate ole sa&ethN ole. g ant all on student to sa&ethN ole. g ant sa&ethN ole to sudha.

SQLSQLSQLSQL-

then !onn sudha?sudha


CR'(T' OR R'AL(C' AROC'DUR' A IS

!u so ! is sele!t Ef o$ sa&eth5student.
)'8IN

fo v in ! loop d%$sNoutput5putNline(LNo I L XX v5no). end loop.


'ND A.

The a%ove !ode will aise e o instead of ! eating p o!edu e 5

146
This is %e!ause of ea l7 %inding whi!h AL?SQL uses %7 default in whi!h efe en!es a e

evaluated in !o$pile ti$e %ut when 7ou a e using a ole this will affe!t i$$ediatel75

ISSU'S 6IT: IN9OF'R;S RI8:TS

In an invo&e ;s

ights

outine# e/te nal

efe en!es in SQL state$ents will %e

esolved

using the !alle ;s p ivilege set5 )ut efe en!es in AL?SQL state$ents a e still esolved unde the owne ;s p ivilege set5

TRI88'RS# 9I'6S (ND IN9OF'R;S RI8:TS

( data%ase t igge will alwa7s %e e/e!uted with define ;s ights and will e/e!ute unde the p ivilege set of the s!he$a that owns the t igge ing ta%le5 This is also t ue fo AL?SQL fun!tion that is !alled f o$ a view5 In this !ase# the fun!tion will e/e!ute unde the p ivilege set of the view;s owne 5

147

A(CF(8'S
The fi st ti$e a pa!&aged su%p og a$ is !alled o an7 efe en!e to a pa!&aged va ia%le o t7pe is $ade# the pa!&age is instantiated5 'a!h session will have its own !op7 of pa!&aged va ia%les# ensu ing that two sessions e/e!uting su%p og a$s in the sa$e pa!&age use diffe ent $e$o 7 lo!ations5 In $an7 !ases initialiUation needs to %e afte all the o%Ke!ts5 Aa!&ages a e sto ed in the data di!tiona 7 and !an not %e lo!al5 Aa!&aged su%p og a$s has an advantage ove stand alone su%p og a$5 6hen eve an7 efe en!e to pa!&age# the whole pa!&age p"!ode was sto ed in sha ed pool of S8(5 Aa!&age $a7 have lo!al su%p og a$s5 un the fi st ti$e the pa!&age is instantiated within a session5 This !an %e done %7 adding initialiUation se!tion to the pa!&age %od7

COMAILIN8 A(CF(8'S (lte pa!&age AF8 !o$pile. (lte pa!&age AF8 !o$pile spe!ifi!ation. (lte pa!&age AF8 !o$pile %od7.

A(CF(8' D'A'ND'NCI'S The pa!&age %od7 depends on the so$e o%Ke!ts and the pa!&age heade 5 The pa!&age heade pa!&ages5 6e !an !hange the pa!&age %od7 with out !hanging the heade 5 does not depend on the pa!&age %od7# whi!h is an advantage of

A(CF(8' RUNTIM' ST(T'

148

Aa!&age unti$e state is diffe fo the following pa!&ages5 Se iall7 eusa%le pa!&ages Non se iall7 eusa%le pa!&ages

S'RI(LL* R'US()L' A(CF(8'S

To fo !e the o a!le to use se iall7 eusa%le ve sion then in!lude AR(8M( S'RI(LL*NR'US()L' in %oth pa!&age spe! and %od75 '/a$ine the following pa!&age ! eate o epla!e pa!&age p&g is

p ag$a se iall7N eusa%le. p o!edu e e$pNp o!. end p&g. ! eate o epla!e pa!&age %od7 p&g is

p ag$a se iall7N eusa%le. !u so ! is sele!t ena$e f o$ e$p. p o!edu e e$pNp o! is vNena$e e$p5ena$eMt7pe. vNflag %oolean 0I t ue. vNnu$ ows nu$%e 0I 3. %egin if not !Misopen then open !. end if. while vNflag loop fet!h ! into vNena$e. vNnu$ ows 0I vNnu$ ows H 2. if vNnu$ ows I C then vNflag 0I false. end if. d%$sNoutput5putNline(='na$e I = XX vNena$e). end loop.

149
end e$pNp o!. end p&g. e/e! p&g5e$pNp o!
SMIT: (LL'N 6(RD

SQL-

'na$e I 'na$e I 'na$e I 'na$e I

'na$e I >ON'S
M(RTIN

SQL-

e/e! p&g5e$pNp o!
SMIT: (LL'N 6(RD >ON'S M(RTIN

'na$e I 'na$e I 'na$e I 'na$e I 'na$e I

The a%ove pa!&age displa7s the sa$e output fo ea!h e/e!ution even though the !u so is not !losed5 )e!ause the se iall7 !alled5 eusa%le ve sion esets the state of the !u so ea!h ti$e it was

NON S'RI(LL * R'US()L' A(CF(8'S

This is the default ve sion used %7 the o a!le5 '/a$ine the following pa!&age ! eate o end p&g. ! eate o epla!e pa!&age %od7 p&g is epla!e pa!&age p&g is

p o!edu e e$pNp o!.

!u so ! is sele!t ena$e f o$ e$p. p o!edu e e$pNp o! is vNena$e e$p5ena$eMt7pe.

150
vNflag %oolean 0I t ue. vNnu$ ows nu$%e 0I 3. %egin if not !Misopen then open !. end if. while vNflag loop fet!h ! into vNena$e. vNnu$ ows 0I vNnu$ ows H 2. if vNnu$ ows I C then vNflag 0I false. end if. d%$sNoutput5putNline(='na$e I = XX vNena$e). end loop. end e$pNp o!. end p&g. e/e! p&g5e$pNp o!
SMIT: (LL'N 6(RD >ON'S M(RTIN

SQL-

'na$e I 'na$e I 'na$e I 'na$e I 'na$e I

SQL-

e/e! p&g5e$pNp o!
)L(F' CL(RF SCOTT FIN8 TURN'R

'na$e I 'na$e I 'na$e I 'na$e I 'na$e I

The a%ove pa!&age displa7s the diffe ent output fo !u so is not !losed5

ea!h e/e!ution even though the

)e!ause the non"se iall7 eusa%le ve sion e$ains the state of the !u so ove data%ase !alls5

151

D'A'ND'NCI'S OB A(CF(8' RUNTIM' ST(T'

Dependen!ies !an e/ists %etween pa!&age state and anon7$ous %lo!&s5 '/a$ine the following p og a$ C eate this pa!&age in fi st session ! eate o epla!e pa!&age p&g is v nu$%e 0I C. p o!edu e p. end p&g.

! eate o %egin

epla!e pa!&age %od7 p&g is

p o!edu e p is d%$sNoutput5putNline(=v I = XX v). v 0I 23. d%$sNoutput5putNline(=v I = XX v). end p. end p&g. Conne!t to se!ond session Run the following !ode %egin p&g5p. end. The a%ove !ode wil wo &5 8o %a!& to fi st session and e! eate the pa!&age using ! eate5 Then !onne!t to se!ond session and un the following !ode again5 %egin p&g5p. end. This a%ove !ode will not wo & %e!ause of the following5

152

The anon7$ous %lo!& depends on p&g5 This is !o$pile ti$e dependen!75 The e is also a unti$e dependen!7 on the pa!&aged va ia%les# sin!e ea!h session has its own !op7 of pa!&aged va ia%les5 Thus when p&g is e!o$piled the unti$e dependen!7 is followed# whi!h invalidates the %lo!& and aises the o a!le e o 5 Runti$e dependen!ies e/ist onl7 on pa!&age state5 This in!ludes va ia%les and !u so s de!la ed in a pa!&age5 If the pa!&age had no glo%al va ia%les# the se!ond e/e!ution of the anon7$ous %lo!& would have su!!eeded5

D(T( MOD'L BOR SU)ARO8R(MS (ND A(CF(8'S Use No%Ke!ts Use Nsou !e Use Ne os

153

CURSORS
Cursor is a pointe to $e$o 7 lo!ation whi!h is !alled as conte&t area whi!h !ontains the p o!essing# in!luding the nu$%e of ows p o!essed %7 the

info $ation ne!essa 7 fo

state$ent# a pointe to the pa sed ep esentation of the state$ent# and the active set whi!h is the set of ows etu ned %7 the Vue 75 Cu so !ontains two pa ts :eade )od7 :eade in!ludes !u so na$e# an7 pa a$ete s and the t7pe of data %eing loaded5 )od7 in!ludes the sele!t state$ent5 '/0 Cu so !(dno in nu$%e ) etu n deptM owt7pe is sele!t Ef o$ dept. In the a%ove :eade \ !u so !(dno in nu$%e ) etu n deptM owt7pe )od7 \ sele!t Ef o$ dept CURSOR T*A'S I$pli!it (SQL) '/pli!it Aa a$ete iUed !u so s R'B !u so s CURSOR ST(8'S Open Bet!h Close

CURSOR (TTRI)UT'S

154
Mfound Mnotfound M ow!ount Misopen M%ul&N ow!ount M%ul&Ne/!eptions

CURSOR D'CL'R(TION S7nta/0 Cu so ,cursor_name- is select statement. '/0 Cu so ! is sele!t Ef o$ dept. CURSOR LOOAS Si$ple loop 6hile loop Bo loop


SIMAL' LOOA

S7nta/0 Loop Bet!h ,cursor_name- into ,record_variable-. '/it when ,cursor_name- M notfound. ,statements-. 'nd loop. '/0
D'CL(R'

!u so ! is sele!t E f o$ student. vNstud studentM owt7pe.


)'8IN

open !. loop fet!h ! into vNstud. e/it when !Mnotfound.

155
d%$sNoutput5putNline(=Na$e I = XX vNstud5na$e). end loop. !lose !.
'ND.

Output0 Na$e I sa&eth Na$e I s inu Na$e I satish Na$e I sudha


6:IL' LOOA

S7nta/0 6hile ,cursor_name- M found loop Bet!h ,cursor_name- into ,record_variable-. ,statements-. 'nd loop. '/0
D'CL(R'

!u so ! is sele!t E f o$ student. vNstud studentM owt7pe.


)'8IN

open !. fet!h ! into vNstud. while !Mfound loop fet!h ! into vNstud. d%$sNoutput5putNline(=Na$e I = XX vNstud5na$e). end loop. !lose !.
'ND.

Output0 Na$e I sa&eth Na$e I s inu Na$e I satish

156
Na$e I sudha
BOR LOOA

S7nta/0 fo ,record_variable- in ,cursor_name- loop ,statements-. 'nd loop. '/0


D'CL(R'

!u so ! is sele!t E f o$ student.
)'8IN

fo vNstud in ! loop d%$sNoutput5putNline(=Na$e I = XX vNstud5na$e). end loop. 'ND. Output0 Na$e I sa&eth Na$e I s inu Na$e I satish Na$e I sudha A(R(M'T(RIZ'D CURSORS This was used when 7ou a e going to use the !u so diffe ent values fo the sa$e whe e !lause5 '/0
D'CL(R'

in $o e than one pla!e with

Cu so pa a$ete s $ust %e in $ode5 Cu so pa a$ete s $a7 have default values5 The s!ope of !u so pa a$ete is within the sele!t state$ent5

!u so !(dno in nu$%e ) is sele!t E f o$ dept whe e deptno I dno. vNdept deptM owt7pe.
)'8IN

157
open !(13). loop fet!h ! into vNdept. e/it when !Mnotfound. d%$sNoutput5putNline(=Dna$e I = XX vNdept5dna$e XX = Lo! I = XX vNdept5lo!). end loop. !lose !.
'ND.

Output0 Dna$e I
R'S'(RC:

Lo! I

D(LL(S

A(CF(8'D CURSORS 6IT: :'(D'R IN SA'C (ND )OD* IN A(CF(8' )OD* !u so s de!la ed in pa!&ages will not !lose auto$ati!all75 In pa!&aged !u so s 7ou !an $odif7 the sele!t state$ent without $a&ing an7 !hanges to the !u so heade in the pa!&age spe!ifi!ation5 *ou !an use dot $ethod fo pa!&aged !u so s li&e p&g5! Aa!&aged !u so s with $ust %e defined in the pa!&age %od7 itself# and then use it as glo%al fo the pa!&age5 *ou !an not define the pa!&aged !u so in an7 su%p og a$s5 Cu so de!la ation in pa!&age with out %od7 needs the etu n !lause5

R'B CURSORS (ND CURSOR 9(RI()L'S This is un!onst ained !u so input5 Ref !u so s !an not %e !losed i$pli!itl75 Ref !u so with etu n t7pe is !alled stron cursor5 whi!h will etu n diffe ent t7pes depends upon the use

Ref !u so with out etu n t7pe is !alled weak cursor5 Cu so va ia%les !an %e assigned f o$ one to anothe 5 *ou !an de!la e a !u so va ia%le in one s!ope and assign anothe !u so va ia%le with diffe ent s!ope# then 7ou !an use the !u so va ia%le even though the assigned !u so va ia%le goes out of s!ope5 Cu so va ia%les !an %e passed as a pa a$ete s to the su%p og a$s5 Cu so va ia%les $odes a e in o out o in out5

158
Cu so va ia%les !an not %e de!la ed in pa!&age spe! and pa!&age %od7 (e/!luding

su%p og a$s)5 *ou !an de!la e ef !u so t7pe in pa!&age spe! as well as %od75 *ou !an de!la e ef !u so t7pes in lo!al su%p og a$s o anon7$ous %lo!&s5 *ou !an not use anothe 5 '/0
CR'(T' OR R'AL(C' AROC'DUR' R'BNCURSOR(T()L'NN(M' IN 9(RC:(R) IS

e$ote p o!edu e !alls to pass !u so

va ia%les f o$ one se ve

to

Cu so va ia%les !an not use fo update !lause5 *ou !an not assign nulls to !u so va ia%les5 *ou !an not !o$pa e !u so va ia%les fo eVualit7# ineVualit7 and nullit75

t7pe t is ef !u so . ! t. vNdept deptM owt7pe. t7pe is e!o d(ena$e e$p5ena$eMt7pe#Ko% e$p5Ko%Mt7pe#sal e$p5salMt7pe). vNe$p . vNstud student5na$eMt7pe.
)'8IN

if ta%leNna$e I

=D'AT=

then then then

open ! fo sele!t E f o$ dept. elsif ta%leNna$e I elsif ta%leNna$e I end if. loop if ta%leNna$e I
=D'AT= ='MA=

open ! fo sele!t ena$e#Ko%#sal f o$ e$p.


=STUD'NT=

open ! fo sele!t na$e f o$ student.

then

fet!h ! into vNdept. e/it when !Mnotfound. d%$sNoutput5putNline(=Deptno I = XX vNdept5deptno XX = Dna$e I = XX vNdept5dna$e XX = Lo! I = XX vNdept5lo!).
='MA=

elsif ta%leNna$e I

then

fet!h ! into vNe$p. e/it when !Mnotfound. d%$sNoutput5putNline(='na$e I = XX vNe$p5ena$e XX = >o% I = XX vNe$p5Ko% XX = Sal

159
I = XX vNe$p5sal). elsif ta%leNna$e I
=STUD'NT=

then

fet!h ! into vNstud. e/it when !Mnotfound. d%$sNoutput5putNline(=Na$e I = XX vNstud). end if. end loop. !lose !.
'ND.

Output0
SQL-

e/e! efN!u so (=D'AT=) Lo! I

Deptno I 23 Dna$e I Deptno I 13 Dna$e I Deptno I 43 Dna$e I Deptno I @3 Dna$e I

(CCOUNTIN8 R'S'(RC: S(L'S

N'6 *ORF

Lo! I

D(LL(S

Lo! I

C:IC(8O

OA'R(TIONS

Lo! I

)OSTON

SQL-

e/e! efN!u so (='MA=) >o% I >o% I >o% I >o% I >o% I >o% I >o% I Sal I G33 Sal I 2D33 Sal I 21C3 Sal I 1TWC Sal I 21C3 Sal I 1GC3 Sal I 1@C3 Sal I C333 Sal I 2C33

'na$e I 'na$e I 'na$e I 'na$e I 'na$e I 'na$e I 'na$e I 'na$e I 'na$e I 'na$e I 'na$e I 'na$e I 'na$e I 'na$e I

SMIT: (LL'N 6(RD >ON'S

CL'RF

S(L'SM(N S(L'SM(N M(N(8'R

M(RTIN )L(F' CL(RF SCOTT FIN8

>o% I

S(L'SM(N

M(N(8'R M(N(8'R (N(L*ST

Sal I 4333

>o% I

AR'SID'NT

TURN'R (D(MS >(M'S BORD

>o% I

S(L'SM(N

>o% I >o% I

CL'RF CL'RF

Sal I 2233 Sal I TC3 Sal I 4333 Sal I 2433

>o% I

(N(L*ST CL'RF

MILL'R

>o% I

SQL-

e/e! efN!u so (=STUD'NT=)

160

Na$e I sa&eth Na$e I s inu Na$e I satish Na$e I sudha CURSOR '+AR'SSIONS *ou !an use !u so e/p essions in e/pli!it !u so s5 *ou !an use !u so e/p essions in d7na$i! SQL5 *ou !an use !u so e/p essions in R'B !u so de!la ations and va ia%les5 *ou !an not use !u so e/p essions in i$pli!it !u so s5 O a!le opens the nested !u so defined %7 a !u so e/p ession i$pli!itl7 as soon as it fet!hes the data !ontaining the !u so e/p ession f o$ the pa ent o oute !u so 5 Nested !u so !loses if 7ou !lose e/pli!itl75 Nested !u so !loses wheneve the oute o pa ent !u so is e/e!uted again o !losed o !an!eled5 Nested !u so !loses wheneve an e/!eption is aised while fet!hing data f o$ a pa ent !u so 5 Cu so e/p essions !an not %e used when de!la ing a view5 Cu so e/p essions !an %e used as an a gu$ent to ta%le fun!tion5 *ou !an not pe fo $ %ind and e/e!ute ope ations on !u so e/p essions when using the !u so e/p essions in d7na$i! SQL5
USIN8 N'ST'D CURSORS OR CURSOR '+AR'SSIONS

'/0
D'CL(R'

!u so ! is sele!t ena$e#!u so (sele!t dna$e f o$ dept d whe e e5e$pno I d5deptno) f o$ e$p e. t7pe t is ef !u so . !2 t. !1 t. v2 e$p5ena$eMt7pe. v1 dept5dna$eMt7pe.
)'8IN

161
open !. loop fet!h !2 into v2. e/it when !2Mnotfound. fet!h !1 into v1. e/it when !1Mnotfound. d%$sNoutput5putNline(='na$e I = XX v2 XX = Dna$e I = XX v1). end loop. end loop. !lose !.
'ND.

CURSOR CL(US'S Retu n Bo update 6he e !u ent of )ul& !olle!t

R'TURN

Cu so ! etu n deptM owt7pe is sele!t Ef o$ dept. O Cu so !2 is sele!t Ef o$ dept. Cu so ! etu n !2M owt7pe is sele!t Ef o$ dept. O T7pe t is e!o d(deptno dept5deptnoMt7pe# dna$e dept5dna$eMt7pe). Cu so ! etu n t is sele!t deptno# dna$e f o$ dept. BOR UAD(T' (ND 6:'R' CURR'NT OB Cu so ! is sele!t Ef o$ dept fo update. O Cu so ! is sele!t Ef o$ dept fo update of dna$e. Update dept set dna$e I Lfin; whe e deptno I 13 whe e !u ent of !. Co$$it.

162

)ULF COLL'CT This is used fo a a7 fet!hes 6ith this 7ou !an et ieve $ultiple ows of data with a single oundt ip5 This edu!es the nu$%e of !onte/t swit!hes %etween the pl?sVl and sVl engines5 Redu!es the ove head of et ieving data5 *ou !an use %ul& !olle!t in %oth d7na$i! and stati! sVl5 *ou !an use %ul& !olle!t in sele!t# fet!h into and etu ning into !lauses5 SQL engine auto$ati!all7 initialiUes and e/tends the !olle!tions 7ou efe en!e in the %ul& !olle!t !lause5 )ul& !olle!t ope ation e$pties the !olle!tion e/e!uting the Vue 75 *ou !an use the li$it !lause of %ul& !olle!t to est i!t the no of ows et ieved5 *ou !an fet!h into $ulti%le !olle!tions with one !olu$n ea!h5 Using the etu ning !lause we !an etu n data to the anothe !olle!tion5 efe en!ed in the into !lause %efo e

)ULF COLL'CT IN B'TC: de!la e T7pe t is ta%le of deptM owt7pe. nt t. Cu so ! is sele!t Ef o$ dept. %egin Open !. Bet!h ! %ul& !olle!t into nt. Close !. 'nd. )ULF COLL'CT IN S'L'CT de!la e T7pe t is ta%le of deptM owt7pe. Nt t. %egin

163
Sele!t E into nt f o$ dept. 'nd. LIMIT IN )ULF COLL'CT de!la e T7pe t is ta%le of e$pM owt7pe. nt t. Cu so ! is sele!t Ef o$ e$p. %egin Open !. Bet!h ! %ul& !olle!t into nt li$it C. Close !. 'nd. MULTIAL' B'TC:'S IN INTO CL(US' de!la e t7pe t is ta%le of dept5deptnoMt7pe. nt t. t7pe t2 is ta%le of dept5dna$eMt7pe. nt2 t2. !u so ! is sele!t deptno#dna$e f o$ dept. %egin open !. fet!h ! %ul& !olle!t into nt#nt2. !lose !. end. R'TURNIN8 CL(US' IN )ULF COLL'CT de!la e t7pe t is ta%le of nu$%e (1). nt t 0I t(2#1#4#@). t7pe t2 is ta%le of va !ha (1).

164
nt2 t2. t7pe t1 is ta%le of studentM owt7pe. nt1 t1. %egin sele!t na$e %ul& !olle!t into nt2 f o$ student. fo all v in nt25fi st55nt25last update student set no I nt(v) whe e na$e I nt2(v) etu ning no#na$e#$a &s %ul& !olle!t into nt1. fo v in nt15fi st55nt15last loop d%$sNoutput5putNline(=Ma &s I = XX nt1(v)). end loop. end. AOINTS TO R'M'M)'R Cu so na$e !an %e up to 43 !ha a!te s in length5 Cu so s de!la ed in anon7$ous %lo!&s o %lo!& te $inates e/e!ution5 M%ul&N ow!ount and M%ul&Ne/!eptions !an %e used onl7 with fo all !onst u!t5 Cu so de!la ations $a7 have e/p essions with !olu$n aliases5 These e/p essions a e !alled vi tual !olu$ns o !al!ulated !olu$ns5 su%p og a$s !loses auto$ati!all7 when that

165

SQL IN AL?SQL
The onl7 state$ents allowed di e!tl7 in pl?sVl a e DML and TCL5 )INDIN8 )inding a va ia%le is the p o!ess of identif7ing the sto age lo!ation asso!iated with an identifie in the p og a$5 T7pes of %inding 'a l7 %inding Late %inding

)inding du ing the !o$piled phase is ea l7 %inding5 )inding du ing the unti$e phase is late %inding5 In ea l7 %inding !o$pile phase will ta&e longe %e!ause of %inding wo & %ut the e/e!ution is faste 5 In late %inding it will sho ten the !o$pile phase %ut lengthens the e/e!ution ti$e5 Al?sVl %7 default uses ea l7 %inding5 )inding also involves !he!&ing the data%ase fo pe $issions to a!!ess the o%Ke!t Refe en!ed5

D*N(MIC SQL If 7ou use DDL in pl?sVl it validates the pe $issions and e/isten!e if eVui es du ing !o$pile ti$e whi!h $a&es invalid5 6e !an avoid this %7 using D7na$i! SQL5 D7na$i! SQL allows 7ou to ! eate a SQL state$ent d7na$i!all7 at unti$e5 Two te!hniVues a e availa%le fo D7na$i! SQL5 Native D7na$i! SQL D)MSNSQL pa!&age

USIN8 N(TI9' D*N(MIC SQL

166
Using e/e!ute i$$ediate )egin '/e!ute i$$ediate L! eate ta%le student(no nu$%e (1)#na$e va !ha (23));. o '/e!ute i$$ediate (L! eate ta%le student(no nu$%e (1)#na$e va !ha (23));). 'nd. Using e/e!ute i$$ediate with pl?sVl va ia%les de!la e v va !ha (233). %egin v 0I =! eate ta%le student(no nu$%e (1)#na$e va !ha (23))=. e/e!ute i$$ediate v. end. Using e/e!ute i$$ediate with %ind va ia%les and using !lause de!la e v va !ha (233). %egin v 0I =inse t into student values(0v2#0v1#0v4)=. e/e!ute i$$ediate v using D#=f=#D33. end. '/e!uting Vue ies with open fo and using !lause ! eate o epla!e p o!edu e p(s$a &s in nu$%e ) is s va !ha (233) 0I =sele!t Ef o$ student whe e $a &s - 0$=. t7pe t is ef !u so . ! t. v studentM owt7pe. %egin open ! fo s using s$a &s. loop fet!h ! into v. e/it when !Mnotfound. d%$sNoutput5putNline(=Student Ma &s I = XX v5$a &s). end loop.

167
!lose !. end. Que ies with e/e!ute i$$ediate de!la e dNna$e dept5dna$eMt7pe. l! dept5lo!Mt7pe. v va !ha (233). %egin v 0I =sele!t dna$e f o$ dept whe e deptno I 23=. e/e!ute i$$ediate v into dNna$e. d%$sNoutput5putNline(=Dna$e I =XX dNna$e). v 0I =sele!t lo! f o$ dept whe e dna$e I 0dn=. e/e!ute i$$ediate v into l! using dNna$e. d%$sNoutput5putNline(=Lo! I = XX l!). end. )ind va ia%les De!la e 9 nu$%e 0I C33. )egin Update student set $a &s I v whe e. 'nd. 9a ia%le Na$es De!la e Ma &s nu$%e (4) 0I 233. )egin Delete student whe e $a &s I $a &s. 'nd. This !an %e avoided %7 using the la%eled %lo!&s5 ,,$7N%lo!&-De!la e Ma &s nu$%e (4) 0I 233. "" this will delete all the ows in the student ta%le "" he e v is %ind va ia%le

168
)egin Delete student whe e $a &s I $7N%lo!&5$a &s. 'nd. 8etting data into pl?sVl va ia%les De!la e 92 nu$%e . 91 va !ha (1). )egin Sele!t no#na$e into v2#v1 f o$ student whe e $a &s I 233. 'nd. DML and Re!o ds ! eate o %egin inse t into student values s ow. end p. de!la e s studentM owt7pe. %egin s5no 0I 22. s5na$e 0I =aa=. s5$a &s 0I 233. p(s). end. Re!o d %ased inse ts de!la e s ow studentM owt7pe. %egin s ow5no 0I W. s ow5na$e 0I =!!=. s ow5$a &s 0I C33. inse t into student values s ow. epla!e p o!edu e p(s ow in studentM owt7pe) is "" delete ows whi!h has a $a &s of 233

169
end. Re!o d %ased updates de!la e s ow studentM owt7pe. %egin s ow5no 0I D. s ow5na$e 0I =!!=. s ow5$a &s 0I C33. update student set owIs ow whe e no I s ow5no. end. Using e!o ds with etu ning !lause de!la e s ow studentM owt7pe. s etu n studentM owt7pe. %egin s ow5no 0I G. s ow5na$e 0I =dd=. s ow5$a &s 0I C33. inse t into student values s ow etu ning no#na$e#$a &s into s etu n. d%$sNoutput5putNline(=No I = XX s etu n5no). d%$sNoutput5putNline(=No I = XX s etu n5na$e). d%$sNoutput5putNline(=No I = XX s etu n5$a &s). end. Bo all with non"seVuential a a7s de!la e t7pe t is ta%le of student5noMt7pe inde/ %7 %ina 7Nintege . i%t t. %egin i%t(2) 0I 2. i%t(23) 0I 1. fo all i in i%t5fi st55i%t5last update student set $a &s I T33 whe e no I i%t(i).

170
end. The a%ove p og a$ will give e o li&e Lele$ent at inde/ O1P does not e/ists5 Usage of indi!es of to avoid the a%ove e o de!la e t7pe t is ta%le of student5noMt7pe inde/ %7 %ina 7Nintege . i%t t. t7pe t2 is ta%le of %oolean inde/ %7 %ina 7Nintege . i%t2 t2. %egin i%t(2) 0I 2. i%t(23) 0I 1. i%t(233) 0I 4. i%t2(2) 0I t ue. i%t(23) 0I t ue. i%t2(233) 0I t ue. fo all i in indi!es of i%t2 update student set $a &s I T33 whe e no I i%t(i). end. )ul& )inds Aassing the enti e pl?sVl ta%le to the SQL engine in one step is &nown as %ul& %ind5 )ul& %inds a e done using the fo all state$ent5 If the e is an e o p o!essing one of the ows in %ul& DML ope ation# onl7 that ow is olled %a!&5 Retu ning !lause This will %e used onl7 with DML state$ents to etu n data into pl?sVl va ia%les5 This will %e useful in situations li&e # when pe fo $ing inse t o update o delete if 7ou want to &now the data of the ta%le whi!h has %een effe!ted %7 the DML5 6ith out going fo anothe S'L'CT using R'TURNIN8 !lause we will get the data whi!h will avoid a !all to RD)MS &e nel5

171

COLL'CTIONS
Colle!tions a e also !o$posite t7pes# in that the7 allow 7ou to t eat seve al va ia%les as a unit5 ( !olle!tion !o$%ines va ia%les of the sa$e t7pe5 T*A'S 9(RR(*S ( va a7 is datat7pe ve 7 si$ila to an a a75 ( va a7 has a fi/ed li$it on its siUe# spe!ified as pa t of the de!la ation5 'le$ents a e inse ted into va a7 sta ting at inde/ 2# up to $a/i$u$ lenth de!la ed in the va a7 t7pe5 The $a/i$u$ siUe of the va a7 is 1 giga %7tes5 S7nta/0 T7pe ,type_name$ is va a7 X va 7ing a a7 (,limit$) of ,element_type$. '/20
D'CL(R'

9a a7s Nested ta%les Inde/ " %7 ta%les

t7pe t is va a7(23) of va !ha (1). va t 0I t(=a=#=%=#=!=#=d=). flag %oolean.


)'8IN

d%$sNoutput5putNline(=Li$it I = XX va5li$it). d%$sNoutput5putNline(=Count I = XX va5!ount). d%$sNoutput5putNline(=Bi st Inde/ I = XX va5fi st). d%$sNoutput5putNline(=Last Inde/ I = XX va5last). d%$sNoutput5putNline(=Ne/t Inde/ I = XX va5ne/t(1)).

172
d%$sNoutput5putNline(=A evious Inde/ I = XX va5p io (4)). d%$sNoutput5putNline(=9(RR(* fo i in va5fi st55va5last loop d%$sNoutput5putNline(=vaO= XX i XX =P I = XX va(i)). end loop. flag 0I va5e/ists(4). if flag I t ue then d%$sNoutput5putNline(=Inde/ 4 e/ists with an ele$ent = XX va(4)). else d%$sNoutput5putNline(=Inde/ 4 does not e/ists=). end if. va5e/tend. d%$sNoutput5putNline(=(fte e/tend of one inde/# Count I = XX va5!ount). flag 0I va5e/ists(C). if flag I t ue then d%$sNoutput5putNline(=Inde/ C e/ists with an ele$ent = XX va(C)). else d%$sNoutput5putNline(=Inde/ C does not e/ists=). end if. flag 0I va5e/ists(D). if flag I t ue then d%$sNoutput5putNline(=Inde/ D e/ists with an ele$ent = XX va(D)). else d%$sNoutput5putNline(=Inde/ D does not e/ists=). end if. va5e/tend(1). d%$sNoutput5putNline(=(fte e/tend of two inde/es# Count I = XX va5!ount). d%$sNoutput5putNline(=9(RR(* fo i in va5fi st55va5last loop d%$sNoutput5putNline(=vaO= XX i XX =P I = XX va(i)). end loop. va(C) 0I =e=. va(D) 0I =f=. va(W) 0I =g=. d%$sNoutput5putNline(=(BT'R (SSIN8NIN8 9(LU'S TO '+T'ND'D 'L'M'NTS#
'L'M'NTS=). 'L'M'NTS=).

173
9(RR(* 'L'M'NTS=).

fo i in va5fi st55va5last loop d%$sNoutput5putNline(=vaO= XX i XX =P I = XX va(i)). end loop. va5e/tend(4#1). d%$sNoutput5putNline(=(fte e/tend of th ee inde/es# Count I = XX va5!ount). d%$sNoutput5putNline(=9(RR(* 'L'M'NTS=). fo i in va5fi st55va5last loop d%$sNoutput5putNline(=vaO= XX i XX =P I = XX va(i)). end loop. va5t i$. d%$sNoutput5putNline(=(fte t i$ of one inde/# Count I = XX va5!ount). va5t i$(4). d%$sNoutput5putNline(=(fte t i$ of th ee inde/s# Count I = XX va5!ount). d%$sNoutput5putNline(=(BT'R TRIM# 9(RR(* 'L'M'NTS=). fo i in va5fi st55va5last loop d%$sNoutput5putNline(=vaO= XX i XX =P I = XX va(i)). end loop. va5delete. d%$sNoutput5putNline(=(fte delete of enti e va a7# Count I = XX va5!ount).
'ND.

Output0 Li$it I 23 Count I @ Bi st Inde/ I 2 Last Inde/ I @ Ne/t Inde/ I 4 A evious Inde/ I 1 9(RR(* 'L'M'NTS vaO2P I a vaO1P I % vaO4P I ! vaO@P I d Inde/ 4 e/ists with an ele$ent ! (fte e/tend of one inde/# Count I C

174
Inde/ C e/ists with an ele$ent Inde/ D does not e/ists (fte e/tend of two inde/es# Count I W 9(RR(* 'L'M'NTS vaO2P I a vaO1P I % vaO4P I ! vaO@P I d vaOCP I vaODP I vaOWP I (BT'R (SSIN8NIN8 9(LU'S TO '+T'ND'D 'L'M'NTS# 9(RR(* 'L'M'NTS vaO2P I a vaO1P I % vaO4P I ! vaO@P I d vaOCP I e vaODP I f vaOWP I g (fte e/tend of th ee inde/es# Count I 23 9(RR(* 'L'M'NTS vaO2P I a vaO1P I % vaO4P I ! vaO@P I d vaOCP I e vaODP I f vaOWP I g vaOGP I % vaOTP I % vaO23P I % (fte t i$ of one inde/# Count I T (fte t i$ of th ee inde/s# Count I D (BT'R TRIM# 9(RR(* 'L'M'NTS vaO2P I a

175
vaO1P I % vaO4P I ! vaO@P I d vaOCP I e vaODP I f (fte delete of enti e va a7# Count I 3 '/10
D'CL(R'

t7pe t is va a7(@) of studentM owt7pe. va t 0I t(null#null#null#null).


)'8IN

fo i in 255va5!ount loop sele!t E into va(i) f o$ student whe e sno I i. d%$sNoutput5putNline(=Sno I = XX va(i)5sno XX = Sna$e I = XX va(i)5sna$e). end loop.
'ND.

Output0 Sno I 2 Sna$e I sa&eth Sno I 1 Sna$e I s inu Sno I 4 Sna$e I div7a Sno I @ Sna$e I $anogni '/40
D'CL(R'

t7pe t is va a7(@) of student5s$a &sMt7pe. va t 0I t(null#null#null#null).


)'8IN

fo i in 255va5!ount loop sele!t s$a &s into va(i) f o$ student whe e sno I i. d%$sNoutput5putNline(=S$a &s I = XX va(i)). end loop.
'ND.

Output0 S$a &s I 233 S$a &s I 133

176
S$a &s I 433 S$a &s I @33 '/@0
D'CL(R'

t7pe

is e!o d(!2 student5sna$eMt7pe#!1 student5s$a &sMt7pe).

t7pe t is va a7(@) of . va t 0I t(null#null#null#null).


)'8IN

fo i in 255va5!ount loop sele!t sna$e#s$a &s into va(i) f o$ student whe e sno I i. d%$sNoutput5putNline(=Sna$e I = XX va(i)5!2 XX = S$a &s I = XX va(i)5!1). end loop.
'ND.

Output0 Sna$e I sa&eth S$a &s I 233 Sna$e I s inu S$a &s I 133 Sna$e I div7a S$a &s I 433 Sna$e I $anogni S$a &s I @33 '/C0
D'CL(R'

t7pe t is va a7(2) of add . va t 0I t(null). !u so ! is sele!t E f o$ e$plo7. i nu$%e 0I 2.


)'8IN

fo v in ! loop sele!t add ess into va(i) f o$ e$plo7 whe e ena$e I v5ena$e. d%$sNoutput5putNline(=:no I = XX va(i)5hno XX = Cit7 I = XX va(i)5!it7). end loop.
'ND.

Output0 :no I 22 Cit7 I h7d :no I 11 Cit7 I %ang

177
:no I 44 Cit7 I &o!hi

'/D0
D'CL(R'

t7pe t is va a7(C) of va !ha (1). va2 t. va1 t 0I t().


)'8IN

if va2 is null then d%$sNoutput5putNline(=va2 is null=). else d%$sNoutput5putNline(=va2 is not null=). end if. if va1 is null then d%$sNoutput5putNline(=va1 is null=). else d%$sNoutput5putNline(=va1 is not null=). end if.
'ND.

Output0 va2 is null va1 is not null

N'ST'D T()L'S ( nested ta%le is thought of a data%ase ta%le whi!h has no li$it on its siUe5 'le$ents a e inse ted into nested ta%le sta ting at inde/ 25 The $a/i$u$ siUe of the va S7nta/0 T7pe ,type_name$ is ta%le of ,table_type$. '/20 a7 is 1 giga %7tes5

178
D'CL(R'

t7pe t is ta%le of va !ha (1). nt t 0I t(=a=#=%=#=!=#=d=). flag %oolean.


)'8IN

if nt5li$it is null then d%$sNoutput5putNline(=No li$it to Nested Ta%les=). else d%$sNoutput5putNline(=Li$it I = XX nt5li$it). end if. d%$sNoutput5putNline(=Count I = XX nt5!ount). d%$sNoutput5putNline(=Bi st Inde/ I = XX nt5fi st). d%$sNoutput5putNline(=Last Inde/ I = XX nt5last). d%$sNoutput5putNline(=Ne/t Inde/ I = XX nt5ne/t(1)). d%$sNoutput5putNline(=A evious Inde/ I = XX nt5p io (4)). d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=). fo i in 255nt5!ount loop d%$sNoutput5putNline(=ntO= XX i XX =P I = XX nt(i)). end loop. flag 0I nt5e/ists(4). if flag I t ue then d%$sNoutput5putNline(=Inde/ 4 e/ists with an ele$ent = XX nt(4)). else d%$sNoutput5putNline(=Inde/ 4 does not e/ists=). end if. nt5e/tend. d%$sNoutput5putNline(=(fte e/tend of one inde/# Count I = XX nt5!ount). flag 0I nt5e/ists(C). if flag I t ue then d%$sNoutput5putNline(=Inde/ C e/ists with an ele$ent = XX nt(C)). else d%$sNoutput5putNline(=Inde/ C does not e/ists=). end if. flag 0I nt5e/ists(D). if flag I t ue then d%$sNoutput5putNline(=Inde/ D e/ists with an ele$ent = XX nt(D)).

179
else d%$sNoutput5putNline(=Inde/ D does not e/ists=). end if. nt5e/tend(1). d%$sNoutput5putNline(=(fte e/tend of two inde/es# Count I = XX nt5!ount). d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=). fo i in 255nt5!ount loop d%$sNoutput5putNline(=ntO= XX i XX =P I = XX nt(i)). end loop. nt(C) 0I =e=. nt(D) 0I =f=. nt(W) 0I =g=. d%$sNoutput5putNline(=(BT'R (SSIN8NIN8 9(LU'S TO '+T'ND'D 'L'M'NTS# N'ST'D T()L'
'L'M'NTS=).

fo i in 255nt5!ount loop d%$sNoutput5putNline(=ntO= XX i XX =P I = XX nt(i)). end loop. nt5e/tend(C#1). d%$sNoutput5putNline(=(fte e/tend of five inde/es# Count I = XX nt5!ount). d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=). fo i in 255nt5!ount loop d%$sNoutput5putNline(=ntO= XX i XX =P I = XX nt(i)). end loop. nt5t i$. d%$sNoutput5putNline(=(fte t i$ of one inde/# Count I = XX nt5!ount). nt5t i$(4). d%$sNoutput5putNline(=(fte t i$ of th ee inde/s# Count I = XX nt5!ount). d%$sNoutput5putNline(=(BT'R TRIM# N'ST'D T()L' 'L'M'NTS=). fo i in 255nt5!ount loop d%$sNoutput5putNline(=ntO= XX i XX =P I = XX nt(i)). end loop. nt5delete(2). d%$sNoutput5putNline(=(fte delete of fi st inde/# Count I = XX nt5!ount). d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=). fo i in 155nt5!ountH2 loop d%$sNoutput5putNline(=ntO= XX i XX =P I = XX nt(i)).

180
end loop. nt5delete(@). d%$sNoutput5putNline(=(fte delete of fou th inde/# Count I = XX nt5!ount). d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=). fo i in 1554 loop d%$sNoutput5putNline(=ntO= XX i XX =P I = XX nt(i)). end loop. fo i in C55nt5!ountH1 loop d%$sNoutput5putNline(=ntO= XX i XX =P I = XX nt(i)). end loop. nt5delete. d%$sNoutput5putNline(=(fte delete of enti e nested ta%le# Count I = XX nt5!ount).
'ND.

Output0 No li$it to Nested Ta%les Count I @ Bi st Inde/ I 2 Last Inde/ I @ Ne/t Inde/ I 4 A evious Inde/ I 1 N'ST'D T()L' 'L'M'NTS ntO2P I a ntO1P I % ntO4P I ! ntO@P I d Inde/ 4 e/ists with an ele$ent ! (fte e/tend of one inde/# Count I C Inde/ C e/ists with an ele$ent Inde/ D does not e/ists (fte e/tend of two inde/es# Count I W N'ST'D T()L' 'L'M'NTS ntO2P I a ntO1P I % ntO4P I ! ntO@P I d

181
ntOCP I ntODP I ntOWP I (BT'R (SSIN8NIN8 9(LU'S TO '+T'ND'D 'L'M'NTS# N'ST'D T()L' 'L'M'NTS ntO2P I a ntO1P I % ntO4P I ! ntO@P I d ntOCP I e ntODP I f ntOWP I g (fte e/tend of five inde/es# Count I 21 N'ST'D T()L' 'L'M'NTS ntO2P I a ntO1P I % ntO4P I ! ntO@P I d ntOCP I e ntODP I f ntOWP I g ntOGP I % ntOTP I % ntO23P I % ntO22P I % ntO21P I % (fte t i$ of one inde/# Count I 22 (fte t i$ of th ee inde/s# Count I G (BT'R TRIM# N'ST'D T()L' 'L'M'NTS ntO2P I a ntO1P I % ntO4P I ! ntO@P I d ntOCP I e ntODP I f ntOWP I g

182
ntOGP I % (fte delete of fi st inde/# Count I W N'ST'D T()L' 'L'M'NTS ntO1P I % ntO4P I ! ntO@P I d ntOCP I e ntODP I f ntOWP I g ntOGP I % (fte delete of fou th inde/# Count I D N'ST'D T()L' 'L'M'NTS ntO1P I % ntO4P I ! ntOCP I e ntODP I f ntOWP I g ntOGP I % (fte delete of enti e nested ta%le# Count I 3 '/10
D'CL(R'

t7pe t is ta%le of studentM owt7pe. nt t 0I t(null#null#null#null).


)'8IN

fo i in 255nt5!ount loop sele!t E into nt(i) f o$ student whe e sno I i. d%$sNoutput5putNline(=Sno I = XX nt(i)5sno XX = Sna$e I = XX nt(i)5sna$e). end loop.
'ND.

Output0 Sno I 2 Sna$e I sa&eth Sno I 1 Sna$e I s inu Sno I 4 Sna$e I div7a Sno I @ Sna$e I $anogni

183

'/40
D'CL(R'

t7pe t is ta%le of student5s$a &sMt7pe. nt t 0I t(null#null#null#null).


)'8IN

fo i in 255nt5!ount loop sele!t s$a &s into nt(i) f o$ student whe e sno I i. d%$sNoutput5putNline(=S$a &s I = XX nt(i)). end loop.
'ND.

Output0 S$a &s I 233 S$a &s I 133 S$a &s I 433 S$a &s I @33 '/@0
D'CL(R'

t7pe

is e!o d(!2 student5sna$eMt7pe#!1 student5s$a &sMt7pe).

t7pe t is ta%le of . nt t 0I t(null#null#null#null).


)'8IN

fo i in 255nt5!ount loop sele!t sna$e#s$a &s into nt(i) f o$ student whe e sno I i. d%$sNoutput5putNline(=Sna$e I = XX nt(i)5!2 XX = S$a &s I = XX nt(i)5!1). end loop.
'ND.

Output0 Sna$e I sa&eth S$a &s I 233 Sna$e I s inu S$a &s I 133 Sna$e I div7a S$a &s I 433 Sna$e I $anogni S$a &s I @33 '/C0
D'CL(R'

184
t7pe t is ta%le of add . nt t 0I t(null). !u so ! is sele!t E f o$ e$plo7. i nu$%e 0I 2.
)'8IN

fo v in ! loop sele!t add ess into nt(i) f o$ e$plo7 whe e ena$e I v5ena$e. d%$sNoutput5putNline(=:no I = XX nt(i)5hno XX = Cit7 I = XX nt(i)5!it7). end loop.
'ND.

Output0 :no I 22 Cit7 I h7d :no I 11 Cit7 I %ang :no I 44 Cit7 I &o!hi '/D0
D'CL(R'

t7pe t is va a7(C) of va !ha (1). nt2 t. nt1 t 0I t().


)'8IN

if nt2 is null then d%$sNoutput5putNline(=nt2 is null=). else d%$sNoutput5putNline(=nt2 is not null=). end if. if nt1 is null then d%$sNoutput5putNline(=nt1 is null=). else d%$sNoutput5putNline(=nt1 is not null=). end if.
'ND.

Output0 nt2 is null

185
nt1 is not null

IND'+")* T()L'S (n inde/"%7 ta%le has no li$it on its siUe5 'le$ents a e inse ted into inde/"%7 ta%le whose inde/ $a7 sta t non"seVuentiall7 in!luding negative intege s5 S7nta/0 T7pe ,type_name$ is ta%le of ,table_type$ inde/ %7 %ina 7Nintege . '/0
D'CL(R'

t7pe t is ta%le of va !ha (1) inde/ %7 %ina 7Nintege . i%t t. flag %oolean.
)'8IN

i%t(2) 0I =a=. i%t("13) 0I =%=. i%t(43) 0I =!=. i%t(233) 0I =d=. if i%t5li$it is null then d%$sNoutput5putNline(=No li$it to Inde/ %7 Ta%les=). else d%$sNoutput5putNline(=Li$it I = XX i%t5li$it). end if. d%$sNoutput5putNline(=Count I = XX i%t5!ount). d%$sNoutput5putNline(=Bi st Inde/ I = XX i%t5fi st). d%$sNoutput5putNline(=Last Inde/ I = XX i%t5last). d%$sNoutput5putNline(=Ne/t Inde/ I = XX i%t5ne/t(1)). d%$sNoutput5putNline(=A evious Inde/ I = XX i%t5p io (4)). d%$sNoutput5putNline(=IND'+ )* T()L' 'L'M'NTS=). d%$sNoutput5putNline(=i%tO"13P I = XX i%t("13)). d%$sNoutput5putNline(=i%tO2P I = XX i%t(2)). d%$sNoutput5putNline(=i%tO43P I = XX i%t(43)).

186
d%$sNoutput5putNline(=i%tO233P I = XX i%t(233)). flag 0I i%t5e/ists(43). if flag I t ue then d%$sNoutput5putNline(=Inde/ 43 e/ists with an ele$ent = XX i%t(43)). else d%$sNoutput5putNline(=Inde/ 43 does not e/ists=). end if. flag 0I i%t5e/ists(C3). if flag I t ue then d%$sNoutput5putNline(=Inde/ C3 e/ists with an ele$ent = XX i%t(43)). else d%$sNoutput5putNline(=Inde/ C3 does not e/ists=). end if. i%t5delete(2). d%$sNoutput5putNline(=(fte delete of fi st inde/# Count I = XX i%t5!ount). i%t5delete(43). d%$sNoutput5putNline(=(fte delete of inde/ thi t7# Count I = XX i%t5!ount). d%$sNoutput5putNline(=IND'+ )* T()L' 'L'M'NTS=). d%$sNoutput5putNline(=i%tO"13P I = XX i%t("13)). d%$sNoutput5putNline(=i%tO233P I = XX i%t(233)). i%t5delete. d%$sNoutput5putNline(=(fte delete of enti e inde/"%7 ta%le# Count I = XX i%t5!ount).
'ND.

Output0 No li$it to Inde/ %7 Ta%les Count I @ Bi st Inde/ I "13 Last Inde/ I 233 Ne/t Inde/ I 43 A evious Inde/ I 2 IND'+ )* T()L' 'L'M'NTS i%tO"13P I % i%tO2P I a i%tO43P I ! i%tO233P I d

187
Inde/ 43 e/ists with an ele$ent ! Inde/ C3 does not e/ists (fte delete of fi st inde/# Count I 4 (fte delete of inde/ thi t7# Count I 1 IND'+ )* T()L' 'L'M'NTS i%tO"13P I % i%tO233P I d (fte delete of enti e inde/"%7 ta%le# Count I 3

DIBB'R'NC'S (MON8 COLL'CTIONS 9a a7s has li$it# nested ta%les and inde/"%7 ta%les has no li$it5 9a a7s and nested ta%les $ust %e initialiUed %efo e assign$ent of ele$ents# in inde/"%7 ta%les we !an di e!tl7 assign ele$ents5 9a a7s and nested ta%les sto ed in data%ase# %ut inde/"%7 ta%les !an not5
AL?SQL

Nested ta%les and inde/"%7 ta%les a e


&e7s !an %e positive o negative5

ta%les# %ut va a7s !an not5

Fe7s $ust %e positive in !ase of nested ta%les and va a7s# in !ase of inde/"%7 ta%les

Refe en!ing none/istent ele$ents aises


and va a7s# %ut in !ase of inde/"%7 ta%les

SU)SCRIATN)'*ONDNCOUNT NOND(T(NBOUND

in %oth nested ta%les

aises5

Fe7s a e seVuential in %oth nested ta%les and va a7s# non"seVuential in inde/"%7 ta%les5 Individual inde/es !an %e deleted in %oth nested ta%les and inde/"%7 ta%les# %ut in va a7s !an not5 Individual inde/es !an %e t i$$ed in %oth nested ta%les and va a7s# %ut in inde/"%7 ta%les !an not5 Individual inde/es !an %e e/tended in %oth nested ta%les and va a7s# %ut in inde/"%7 ta%les !an not5

MULTIL'9'L COLL'CTIONS Colle!tions of $o e than one di$ension whi!h is a !olle!tion of !olle!tions# &nown as $ultilevel !olle!tions5

188
S7nta/0 T7pe ,type_name1$ is ta%le of ,table_type$ inde/ %7 %ina 7Nintege . T7pe ,type_name2$ is va a7(,limit-) X ta%le X of ,type_name1$ 0 inde/ %7 %ina 7Nintege . '/20
D'CL(R'

t7pe t2 is ta%le of va !ha (1) inde/ %7 %ina 7Nintege . t7pe t1 is va a7(C) of t2. va t1 0I t1(). ! nu$%e 0I TW. flag %oolean.
)'8IN

va5e/tend(@). d%$sNoutput5putNline(=Count I = XX va5!ount). d%$sNoutput5putNline(=Li$it I = XX va5li$it). fo i in 255va5!ount loop fo K in 255va5!ount loop va(i)(K) 0I !h (!). ! 0I ! H 2. end loop. end loop. d%$sNoutput5putNline(=9(RR(* 'L'M'NTS=). fo i in 255va5!ount loop fo K in 255va5!ount loop d%$sNoutput5putNline(=vaO= XX i XX =PO= XX K XX =P I = XX va(i)(K)). end loop. end loop. d%$sNoutput5putNline(=Bi st inde/ I = XX va5fi st). d%$sNoutput5putNline(=Last inde/ I = XX va5last). d%$sNoutput5putNline(=Ne/t inde/ I = XX va5ne/t(1)). d%$sNoutput5putNline(=A evious inde/ I = XX va5p io (4)). flag 0I va5e/ists(1). if flag I t ue then d%$sNoutput5putNline(=Inde/ 1 e/ists=). else d%$sNoutput5putNline(=Inde/ 1 e/ists=).

189
end if. va5e/tend. va(2)(C) 0I =V=. va(1)(C) 0I = =. va(4)(C) 0I =s=. va(@)(C) 0I =t=. va(C)(2) 0I =u=. va(C)(1) 0I =v=. va(C)(4) 0I =w=. va(C)(@) 0I =/=. va(C)(C) 0I =7=. d%$sNoutput5putNline(=(fte e/tend of one inde/# Count I = XX va5!ount). d%$sNoutput5putNline(=9(RR(* 'L'M'NTS=). fo i in 255va5!ount loop fo K in 255va5!ount loop d%$sNoutput5putNline(=vaO= XX i XX =PO= XX K XX =P I = XX va(i)(K)). end loop. end loop. va5t i$. d%$sNoutput5putNline(=(fte t i$ of one inde/# Count I = XX va5!ount). va5t i$(1). d%$sNoutput5putNline(=(fte t i$ of two inde/es# Count I = XX va5!ount). d%$sNoutput5putNline(=9(RR(* 'L'M'NTS=). fo i in 255va5!ount loop fo K in 255va5!ount loop d%$sNoutput5putNline(=vaO= XX i XX =PO= XX K XX =P I = XX va(i)(K)). end loop. end loop. va5delete. d%$sNoutput5putNline(=(fte delete of enti e va a7# Count I = XX va5!ount).
'ND.

Output0 Count I @ Li$it I C 9(RR(* 'L'M'NTS

190
vaO2PO2P I a vaO2PO1P I % vaO2PO4P I ! vaO2PO@P I d vaO1PO2P I e vaO1PO1P I f vaO1PO4P I g vaO1PO@P I h vaO4PO2P I i vaO4PO1P I K vaO4PO4P I & vaO4PO@P I l vaO@PO2P I $ vaO@PO1P I n vaO@PO4P I o vaO@PO@P I p Bi st inde/ I 2 Last inde/ I @ Ne/t inde/ I 4 A evious inde/ I 1 Inde/ 1 e/ists (fte e/tend of one inde/# Count I C 9(RR(* 'L'M'NTS vaO2PO2P I a vaO2PO1P I % vaO2PO4P I ! vaO2PO@P I d vaO2POCP I V vaO1PO2P I e vaO1PO1P I f vaO1PO4P I g vaO1PO@P I h vaO1POCP I vaO4PO2P I i vaO4PO1P I K

191
vaO4PO4P I & vaO4PO@P I l vaO4POCP I s vaO@PO2P I $ vaO@PO1P I n vaO@PO4P I o vaO@PO@P I p vaO@POCP I t vaOCPO2P I u vaOCPO1P I v vaOCPO4P I w vaOCPO@P I / vaOCPOCP I 7 (fte t i$ of one inde/# Count I @ (fte t i$ of two inde/es# Count I 1 9(RR(* 'L'M'NTS vaO2PO2P I a vaO2PO1P I % vaO1PO2P I e vaO1PO1P I f (fte delete of enti e va a7# Count I 3 '/10
D'CL(R'

t7pe t2 is ta%le of va !ha (1) inde/ %7 %ina 7Nintege . t7pe t1 is ta%le of t2. nt t1 0I t1(). ! nu$%e 0I DC. v nu$%e 0I 2. flag %oolean.
)'8IN

nt5e/tend(@). d%$sNoutput5putNline(=Count I = XX nt5!ount). if nt5li$it is null then d%$sNoutput5putNline(=No li$it to Nested Ta%les=). else

192
d%$sNoutput5putNline(=Li$it I = XX nt5li$it). end if. fo i in 255nt5!ount loop fo K in 255nt5!ount loop nt(i)(K) 0I !h (!). ! 0I ! H 2. if ! I T2 then ! 0I TW. end if. end loop. end loop. d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=). fo i in 255nt5!ount loop fo K in 255nt5!ount loop d%$sNoutput5putNline(=ntO= XX i XX =PO= XX K XX =P I = XX nt(i)(K)). end loop. end loop. d%$sNoutput5putNline(=Bi st inde/ I = XX nt5fi st). d%$sNoutput5putNline(=Last inde/ I = XX nt5last). d%$sNoutput5putNline(=Ne/t inde/ I = XX nt5ne/t(1)). d%$sNoutput5putNline(=A evious inde/ I = XX nt5p io (4)). flag 0I nt5e/ists(1). if flag I t ue then d%$sNoutput5putNline(=Inde/ 1 e/ists=). else d%$sNoutput5putNline(=Inde/ 1 e/ists=). end if. nt5e/tend(1). nt(2)(C) 0I =Q=. nt(2)(D) 0I =R=. nt(1)(C) 0I =S=. nt(1)(D) 0I =T=. nt(4)(C) 0I =U=. nt(4)(D) 0I =9=. nt(@)(C) 0I =6=.

193
nt(@)(D) 0I =+=. nt(C)(2) 0I =*=. nt(C)(1) 0I =Z=. nt(C)(4) 0I =a=. nt(C)(@) 0I =%=. nt(C)(C) 0I =!=. nt(C)(D) 0I =d=. nt(D)(2) 0I =e=. nt(D)(1) 0I =f=. nt(D)(4) 0I =g=. nt(D)(@) 0I =h=. nt(D)(C) 0I =i=. nt(D)(D) 0I =K=. d%$sNoutput5putNline(=(fte e/tend of one inde/# Count I = XX nt5!ount). d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=). fo i in 255nt5!ount loop fo K in 255nt5!ount loop d%$sNoutput5putNline(=ntO= XX i XX =PO= XX K XX =P I = XX nt(i)(K)). end loop. end loop. nt5t i$. d%$sNoutput5putNline(=(fte t i$ of one inde/e# Count I = XX nt5!ount). nt5t i$(1). d%$sNoutput5putNline(=(fte t i$ of two inde/es# Count I = XX nt5!ount). d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=). fo i in 255nt5!ount loop fo K in 255nt5!ount loop d%$sNoutput5putNline(=ntO= XX i XX =PO= XX K XX =P I = XX nt(i)(K)). end loop. end loop. nt5delete(1). d%$sNoutput5putNline(=(fte delete of se!ond inde/# Count I = XX nt5!ount). d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=). loop e/it when v I @.

194
fo K in 255nt5!ountH2 loop d%$sNoutput5putNline(=ntO= XX v XX =PO= XX K XX =P I = XX nt(v)(K)). end loop. v 0I v H 2. if vI 1 then v 0I 4. end if. end loop. nt5delete. d%$sNoutput5putNline(=(fte delete of enti e nested ta%le# Count I = XX nt5!ount).
'ND.

Output0 Count I @ No li$it to Nested Ta%les N'ST'D T()L' 'L'M'NTS ntO2PO2P I ( ntO2PO1P I ) ntO2PO4P I C ntO2PO@P I D ntO1PO2P I ' ntO1PO1P I B ntO1PO4P I 8 ntO1PO@P I : ntO4PO2P I I ntO4PO1P I > ntO4PO4P I F ntO4PO@P I L ntO@PO2P I M ntO@PO1P I N ntO@PO4P I O ntO@PO@P I A Bi st inde/ I 2 Last inde/ I @ Ne/t inde/ I 4 A evious inde/ I 1

195
Inde/ 1 e/ists (fte e/tend of one inde/# Count I D N'ST'D T()L' 'L'M'NTS ntO2PO2P I ( ntO2PO1P I ) ntO2PO4P I C ntO2PO@P I D ntO2POCP I Q ntO2PODP I R ntO1PO2P I ' ntO1PO1P I B ntO1PO4P I 8 ntO1PO@P I : ntO1POCP I S ntO1PODP I T ntO4PO2P I I ntO4PO1P I > ntO4PO4P I F ntO4PO@P I L ntO4POCP I U ntO4PODP I 9 ntO@PO2P I M ntO@PO1P I N ntO@PO4P I O ntO@PO@P I A ntO@POCP I 6 ntO@PODP I + ntOCPO2P I * ntOCPO1P I Z ntOCPO4P I a ntOCPO@P I % ntOCPOCP I ! ntOCPODP I d ntODPO2P I e ntODPO1P I f

196
ntODPO4P I g ntODPO@P I h ntODPOCP I i ntODPODP I K (fte t i$ of one inde/e# Count I C (fte t i$ of two inde/es# Count I 4 N'ST'D T()L' 'L'M'NTS ntO2PO2P I ( ntO2PO1P I ) ntO2PO4P I C ntO1PO2P I ' ntO1PO1P I B ntO1PO4P I 8 ntO4PO2P I I ntO4PO1P I > ntO4PO4P I F (fte delete of se!ond inde/# Count I 1 N'ST'D T()L' 'L'M'NTS ntO2PO2P I ( ntO2PO1P I ) ntO2PO4P I C ntO4PO2P I I ntO4PO1P I > ntO4PO4P I F (fte delete of enti e nested ta%le# Count I 3 '/40
D'CL(R'

t7pe t2 is ta%le of va !ha (1) inde/ %7 %ina 7Nintege . t7pe t1 is ta%le of t2 inde/ %7 %ina 7Nintege . i%t t1. flag %oolean.
)'8IN

d%$sNoutput5putNline(=Count I = XX i%t5!ount). if i%t5li$it is null then d%$sNoutput5putNline(=No li$it to Inde/"%7 Ta%les=).

197
else d%$sNoutput5putNline(=Li$it I = XX i%t5li$it). end if. i%t(2)(2) 0I =a=. i%t(@)(C) 0I =%=. i%t(C)(2) 0I =!=. i%t(D)(1) 0I =d=. i%t(G)(4) 0I =e=. i%t(4)(@) 0I =f=. d%$sNoutput5putNline(=IND'+")* T()L' 'L'M'NTS=). d%$sNoutput5putNline(=i%t(O2PO2P I = XX i%t(2)(2)). d%$sNoutput5putNline(=i%t(O@POCP I = XX i%t(@)(C)). d%$sNoutput5putNline(=i%t(OCPO2P I = XX i%t(C)(2)). d%$sNoutput5putNline(=i%t(ODPO1P I = XX i%t(D)(1)). d%$sNoutput5putNline(=i%t(OGPO4P I = XX i%t(G)(4)). d%$sNoutput5putNline(=i%t(O4PO@P I = XX i%t(4)(@)). d%$sNoutput5putNline(=Bi st Inde/ I = XX i%t5fi st). d%$sNoutput5putNline(=Last Inde/ I = XX i%t5last). d%$sNoutput5putNline(=Ne/t Inde/ I = XX i%t5ne/t(4)). d%$sNoutput5putNline(=A io Inde/ I = XX i%t5p io (G)). i%t(2)(1) 0I =g=. i%t(2)(4) 0I =h=. i%t(2)(@) 0I =i=. i%t(2)(C) 0I =&=. i%t(2)(D) 0I =l=. i%t(2)(W) 0I =$=. i%t(2)(G) 0I =n=. d%$sNoutput5putNline(=Count I = XX i%t5!ount). d%$sNoutput5putNline(=IND'+")* T()L' 'L'M'NTS=). fo i in 255G loop d%$sNoutput5putNline(=i%tO2PO= XX i XX =P I = XX i%t(2)(i)). end loop. d%$sNoutput5putNline(=i%t(O@POCP I = XX i%t(@)(C)). d%$sNoutput5putNline(=i%t(OCPO2P I = XX i%t(C)(2)). d%$sNoutput5putNline(=i%t(ODPO1P I = XX i%t(D)(1)).

198
d%$sNoutput5putNline(=i%t(OGPO4P I = XX i%t(G)(4)). d%$sNoutput5putNline(=i%t(O4PO@P I = XX i%t(4)(@)). flag 0I i%t5e/ists(4). if flag I t ue then d%$sNoutput5putNline(=Inde/ 4 e/ists=). else d%$sNoutput5putNline(=Inde/ 4 e/ists=). end if. i%t5delete(2). d%$sNoutput5putNline(=(fte delete of fi st inde/# Count I = XX i%t5!ount). i%t5delete(@). d%$sNoutput5putNline(=(fte delete of fou th inde/# Count I = XX i%t5!ount). d%$sNoutput5putNline(=IND'+")* T()L' 'L'M'NTS=). d%$sNoutput5putNline(=i%t(OCPO2P I = XX i%t(C)(2)). d%$sNoutput5putNline(=i%t(ODPO1P I = XX i%t(D)(1)). d%$sNoutput5putNline(=i%t(OGPO4P I = XX i%t(G)(4)). d%$sNoutput5putNline(=i%t(O4PO@P I = XX i%t(4)(@)). i%t5delete. d%$sNoutput5putNline(=(fte delete of enti e inde/"%7 ta%le# Count I = XX i%t5!ount).
'ND.

Output0 Count I 3 No li$it to Inde/"%7 Ta%les IND'+")* T()L' 'L'M'NTS i%t(O2PO2P I a i%t(O@POCP I % i%t(OCPO2P I ! i%t(ODPO1P I d i%t(OGPO4P I e i%t(O4PO@P I f Bi st Inde/ I 2 Last Inde/ I G Ne/t Inde/ I @ A io Inde/ I D Count I D

199
IND'+")* T()L' 'L'M'NTS i%tO2PO2P I a i%tO2PO1P I g i%tO2PO4P I h i%tO2PO@P I i i%tO2POCP I & i%tO2PODP I l i%tO2POWP I $ i%tO2POGP I n i%t(O@POCP I % i%t(OCPO2P I ! i%t(ODPO1P I d i%t(OGPO4P I e i%t(O4PO@P I f Inde/ 4 e/ists (fte delete of fi st inde/# Count I C (fte delete of fou th inde/# Count I @ IND'+")* T()L' 'L'M'NTS i%t(OCPO2P I ! i%t(ODPO1P I d i%t(OGPO4P I e i%t(O4PO@P I f (fte delete of enti e inde/"%7 ta%le# Count I 3 '/40
D'CL(R'

t7pe t2 is ta%le of va !ha (1) inde/ %7 %ina 7Nintege . t7pe t1 is ta%le of t2 inde/ %7 %ina 7Nintege . t7pe t4 is ta%le of t1. nt t4 0I t4(). ! nu$%e 0I DC.
)'8IN

nt5e/tend(1). d%$sNoutput5putNline(=Count I = XX nt5!ount). fo i in 255nt5!ount loop fo K in 255nt5!ount loop

200
fo & in 255nt5!ount loop nt(i)(K)(&) 0I !h (!). ! 0I ! H 2. end loop. end loop. end loop. d%$sNoutput5putNline(=N'ST'D T()L' 'L'M'NTS=). fo i in 255nt5!ount loop fo K in 255nt5!ount loop fo & in 255nt5!ount loop d%$sNoutput5putNline(=ntO= XX i XX =PO= XX K XX =PO= XX & XX =P I = XX nt(i)(K)(&)). end loop. end loop. end loop.
'ND.

Output0 Count I 1 N'ST'D T()L' 'L'M'NTS ntO2PO2PO2P I ( ntO2PO2PO1P I ) ntO2PO1PO2P I C ntO2PO1PO1P I D ntO1PO2PO2P I ' ntO1PO2PO1P I B ntO1PO1PO2P I 8 ntO1PO1PO1P I : O)>'CTS US'D IN T:' '+(MAL'S

SQL-

sele!t E f o$ student. SN(M' SM(RFS

SNO

"""""""""" """""""""""""" """"""""""

201
2 1 4 @ sa&eth s inu div7a $anogni 233 133 433 @33

SQL-

! eate o

epla!e t7pe add as o%Ke!t(hno nu$%e (1)#!it7 va !ha (23)).?

SQL-

sele!t E f o$ e$plo7. >O) !le & $anage enginee (DDR'SS(:NO# CIT*) (DDR(22# =h7d=) (DDR(11# =%ang=) (DDR(44# =&o!hi=)

'N(M' RanKit Satish S inu

"""""""""" """""""""" """""""""""""""""""""""""""""

202

'RROR :(NDLIN8
AL?SQL i$ple$ents e o handling with e/!eptions and e/!eption handle s5 '/!eptions !an %e asso!iated with o a!le e o s o with 7ou own use "defined e o s5 )7 using e/!eptions and e/!eption handle s# 7ou !an $a&e 7ou AL?SQL p og a$s o%ust and a%le to deal with %oth une/pe!ted and e/pe!ted e o s du ing e/e!ution5 'RROR T*A'S Co$pile"ti$e e o s Runti$e e o s

' o s that o!!u du ing the !o$pilation phase a e dete!ted %7 the AL?SQL engine and epo ted %a!& to the use # we have to !o e!t the$5 Runti$e e o s a e dete!ted %7 the AL?SQL unti$e engine whi!h !an p og a$$ati!all7 aise and !aught %7 e/!eption handle s5 '/!eptions a e designed fo :(NDLIN8 '+C'ATIONS 6hen e/!eption is aised# !ont ol passes to the e/!eption se!tion of the %lo!&5 The e/!eption se!tion !onsists of handle s fo so$e o all of the e/!eptions5 (n e/!eption handle !ontains the !ode that is e/e!uted when the e o e/!eption is aised5 S7nta/0
'+C'ATION

un"ti$e e o handling# athe than !o$pile"ti$e e o handling5

asso!iated with the e/!eption o!!u s# and the

6hen e/!eptionNna$e then SeVuen!eNofNstate$ents. 6hen e/!eptionNna$e then SeVuen!eNofNstate$ents.

203
6hen othe s then SeVuen!eNofNstate$ents.
'ND.

'+C'ATION T*A'S A edefined e/!eptions Use "defined e/!eptions

AR'D'BIN'D '+C'ATIONS O a!le has p edefined seve al e/!eptions that !o esponds to the $ost !o$$on o a!le e o s5 Li&e the p edefined t7pes# the identifie s of these e/!eptions a e defined in the de!la e the$ in the de!la ative se!ion5 '/20
D'CL(R' ST(ND(RD

pa!&age5 )e!ause of this# the7 a e al ead7 availa%le to the p og a$# it is not ne!essa 7 to

a nu$%e . % va !ha (1). vN$a &s nu$%e . !u so ! is sele!t E f o$ student. t7pe t is va a7(4) of va !ha (1). va t 0I t(=a=#=%=). va2 t.
)'8IN "" NOND(T(NBOUND )'8IN

sele!t s$a &s into vN$a &s f o$ student whe e sno I C3.
'+C'ATION

when noNdataNfound then d%$sNoutput5putNline(=Invalid student nu$%e =).


'ND. "" CURSORN(LR'(D*NOA'N )'8IN

open !. open !.

204
'+C'ATION

when !u so Nal ead7Nopen then d%$sNoutput5putNline(=Cu so is al ead7 opened=).


'ND. "" IN9(LIDNCURSOR )'8IN

!lose !. open !. !lose !. !lose !.


'+C'ATION

when invalidN!u so then d%$sNoutput5putNline(=Cu so is al ead7 !losed=).


'ND. "" TOONM(N*NRO6S )'8IN

sele!t s$a &s into vN$a &s f o$ student whe e sno - 2.


'+C'ATION

when tooN$an7N ows then d%$sNoutput5putNline(=Too $an7 values a e !o$ing to $a &s va ia%le=).
'ND. "" Z'RONDI9ID' )'8IN

a 0I C?3.
'+C'ATION

when Ue oNdivide then d%$sNoutput5putNline(=Divided %7 Ue o " invalid ope ation=).


'ND. "" 9(LU'N'RROR )'8IN

% 0I =sa&eth=.
'+C'ATION

when valueNe o then d%$sNoutput5putNline(=Invalid st ing length=).


'ND. "" IN9(LIDNNUM)'R )'8IN

inse t into student values(=a=#=s inu=#233).


'+C'ATION

205
when invalidNnu$%e then d%$sNoutput5putNline(=Invalid nu$%e =).
'ND. "" SU)SCRIATNOUTSID'NLIMIT )'8IN

va(@) 0I =!=.
'+C'ATION

when su%s! iptNoutsideNli$it then d%$sNoutput5putNline(=Inde/ is g eate than the li$it=).


'ND. "" SU)SCRIATN)'*ONDNCOUNT )'8IN

va(4) 0I =!=.
'+C'ATION

when su%s! iptN%e7ondN!ount then d%$sNoutput5putNline(=Inde/ is g eate than the !ount=).


'ND. "" COLL'CTIONNISNNULL )'8IN

va2(2) 0I =a=.
'+C'ATION

when !olle!tionNisNnull then d%$sNoutput5putNline(=Colle!tion is e$pt7=).


'ND.

""
'ND.

Output0 Invalid student nu$%e Cu so is al ead7 opened Cu so is al ead7 !losed Too $an7 values a e !o$ing to $a &s va ia%le Divided %7 Ue o " invalid ope ation Invalid st ing length Invalid nu$%e Inde/ is g eate than the li$it Inde/ is g eate than the !ount Colle!tion is e$pt7

206

'/10
D'CL(R'

! nu$%e .
)'8IN

! 0I C?3.
'+C'ATION

when Ue oNdivide then d%$sNoutput5putNline(=Invalid Ope ation=). when othe s then d%$sNoutput5putNline(=B o$ OT:'RS handle 0 Invalid Ope ation=).
'ND.

Output0 Invalid Ope ation US'R"D'BIN'D '+C'ATIONS ( use "defined e/!eption is an e o e/eptions have a t7pe
R(ISIN8 '+C'ATIONS

that is defined %7 the p og a$$e 5 Use "defined

e/!eptions a e de!la ed in the de!la ative se!ion of a AL?SQL %lo!&5 >ust li&e va ia%les#
'+C'ATION

and s!ope5

Use "defined e/!eptions a e aised e/pli!itl7 via the R(IS' state$ent5 '/0
D'CL(R'

e e/!eption.
)'8IN

aise e.
'+C'ATION

when e then d%$sNoutput5putNline(=e is aised=).


'ND.

Output0 e is aised

207
SQLCOD' (ND SQL'RRM
SQLCOD'

etu ns the !u ent e o !ode# and


SQLCOD'

SQL'RRM

etu ns the !u ent e o $essage te/t. etu ns Quse "deifned e/!eptionR5

Bo use "defined e/!eption non"o a!le e/!eption5 '/20


D'CL(R'

etu ns 2 and

SQL'RRM

SQL'RRM wiil ta&e onl7 negative value e/!ept 2335 If an7 positive value othe than 233 etu ns

e e/!eption. vNdna$e va !ha (23).


)'8IN "" US'R"D'BIN'D '+C'ATION )'8IN

aise e.
'+C'ATION

when e then d%$sNoutput5putNline(SQLCOD' XX = = XX


'ND. "" AR'D'BIN'D '+C'ATION )'8IN SQL'RRM).

sele!t dna$e into vNdna$e f o$ dept whe e deptno I C3.


'+C'ATION

when noNdataNfound then d%$sNoutput5putNline(SQLCOD' XX = = XX


'ND. 'ND. SQL'RRM).

Output0 2 Use "Defined '/!eption 233 OR("32@340 no data found '/10


)'8IN

d%$sNoutput5putNline(SQL'RRM(233)). d%$sNoutput5putNline(SQL'RRM(3)). d%$sNoutput5putNline(SQL'RRM(2)). d%$sNoutput5putNline(SQL'RRM("233)). d%$sNoutput5putNline(SQL'RRM("C33)).

208
d%$sNoutput5putNline(SQL'RRM(133)). d%$sNoutput5putNline(SQL'RRM("T33)).
'ND.

Output0 OR("32@340 no data found OR("33330 no $al# su!!essful !o$pletion Use "Defined '/!eption OR("332330 no data found OR("33C330 Message C33 not found. p odu!tIRD)MS. fa!ilit7IOR( "1330 non"OR(CL' e/!eption OR("33T330 invalid SQL state$ent '+C'ATIONNINIT AR(8M( Using this 7ou !an asso!iate a na$ed e/!eption with a pa ti!ula o a!le e o 5 This gives 7ou the a%ilit7 to t ap this e o spe!ifi!all7# athe than via an S7nta/0
AR(8M( '+C'ATIONNINIT(e&ception_name# OT:'RS

handle 5

oracle_error_number).

'/0
D'CL(R'

e e/!eption. p ag$a e/!eptionNinit(e#"2@WD). ! nu$%e .


)'8IN

! 0I C?3.
'+C'ATION

when e then d%$sNoutput5putNline(=Invalid Ope ation=).


'ND.

Output0 Invalid Ope ation R(IS'N(AALIC(TIONN'RROR

209
*ou !an use this %uilt"in fun!tion to ! eate 7ou des! iptive than na$ed e/!eptions5 S7nta/0
R(IS'N(AALIC(TIONN'RROR(error_number#

own e o

$essages# whi!h !an %e $o e

error_messa e## Okeep_errors_%la P).


TRU'#

The )oolean pa a$ete keep_errors_%la list of e o s al ead7 aised5 If it is list of e o s5 '/0


D'CL(R' B(LS'#

is optional5 If it is

the new e o is added to the

whi!h is default# the new e o will epla!e the !u ent

! nu$%e .
)'8IN

! 0I C?3.
'+C'ATION

when Ue oNdivide then aiseNappli!ationNe o ("13111#=Invalid Ope ation=).


'ND.

Output0
D'CL(R'

E
'RROR

at line 20

OR("131110 Invalid Ope ation OR("3DC210 at line W '+C'ATION AROA(8(TION '/!eptions !an o!!u in the de!la ative# the e/e!uta%le# o the e/!eption se!tion of a AL?SQL %lo!&5
'+C'ATION R(IS'D IN T:' '+'CU(T()L' S'CTION

'/!eptions aised in e/e!uata%le se!tion !an %e handled in !u ent %lo!& o oute %lo!&5 '/20
D'CL(R'

e e/!eption.
)'8IN )'8IN

210
aise e.
'ND. '+C'ATION

when e then d%$sNoutput5putNline(=e is aised=).


'ND.

Output0 e is aised '/10


D'CL(R'

e e/!eption.
)'8IN )'8IN

aise e.
'ND. 'ND.

Output0
'RROR

at line 20

OR("3DC230 AL?SQL0 unhandled use "defined e/!eption OR("3DC210 at line C


'+C'ATION R(IS'D IN T:' D'CL(R(TI9' S'CTION

'/!eptions aised in the de!la ative se!ion $ust %e handled in the oute %lo!&5 '/20
D'CL(R'

! nu$%e (4) 0I =a%!d=.


)'8IN

d%$sNoutput5putNline(=:ello=).
'+C'ATION

when othe s then d%$sNoutput5putNline(=Invalid st ing length=).


'ND.

Output0
'RROR

at line 20

OR("3DC310 AL?SQL0 nu$e i! o value e o 0 !ha a!te to nu$%e !onve sion e o

211
OR("3DC210 at line 1 '/10
)'8IN D'CL(R'

! nu$%e (4) 0I =a%!d=.


)'8IN

d%$sNoutput5putNline(=:ello=).
'+C'ATION

when othe s then d%$sNoutput5putNline(=Invalid st ing length=).


'ND. '+C'ATION

when othe s then d%$sNoutput5putNline(=B o$ oute %lo!&0 Invalid st ing length=).


'ND.

Output0 B o$ oute %lo!&0 Invalid st ing length


'+C'ATION R(IS'D IN T:' '+C'ATION S'CTION

'/!eptions aised in the de!la ative se!ion $ust %e handled in the oute %lo!&5 '/20
D'CL(R'

e2 e/!eption. e1 e/!eption.
)'8IN

aise e2.
'+C'ATION

when e2 then d%$sNoutput5putNline(=e2 is aised=). aise e1. when e1 then d%$sNoutput5putNline(=e1 is aised=).
'ND.

Output0

212
e2 is aised D'CL(R' E 'RROR at line 20 OR("3DC230 AL?SQL0 unhandled use "defined e/!eption OR("3DC210 at line T OR("3DC230 AL?SQL0 unhandled use "defined e/!eption '/10
D'CL(R'

e2 e/!eption. e1 e/!eption.
)'8IN )'8IN

aise e2.
'+C'ATION

when e2 then d%$sNoutput5putNline(=e2 is aised=). aise e1. when e1 then d%$sNoutput5putNline(=e1 is aised=).
'ND. '+C'ATION

when e1 then d%$sNoutput5putNline(=B o$ oute %lo!&0 e1 is aised=).


'ND.

Output0 e2 is aised B o$ oute %lo!&0 e1 is aised '/40


D'CL(R'

e e/!eption.
)'8IN

aise e.
'+C'ATION

213
when e then d%$sNoutput5putNline(=e is aised=). aise e.
'ND.

Output0 e is aised D'CL(R' E 'RROR at line 20 OR("3DC230 AL?SQL0 unhandled use "defined e/!eption OR("3DC210 at line G OR("3DC230 AL?SQL0 unhandled use "defined e/!eption

Potrebbero piacerti anche