You are on page 1of 318

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)).

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).

Copy rights are reserved.

2
'/0
SQLSQL-

inse t into student values (2# ;sudha;# 233). inse t into student values (1# ;sað# 133).

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) ?

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)

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 .

Copy rights are reserved.

3
Valuen). '/0
SQLSQL-

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

d)

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

S7nta/0 inse t into ,table_name)(col1, col2, col3 coln) values (&col1, &col2 .&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=) ?

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=)

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' M(RFS

Copy rights are reserved.

4
""" """""" 2 1 2 1 4 @ C D Sudha Sa&eth >agan Na en Ra$esh Madhu 9isu Rattu """""""" 233 133 433 @33

SQL-

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-

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

Copy rights are reserved.

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 G# "# E# ? Co$pa ison ope ato s a) H# IH# -# ,# -H# ,H# ,%etween# not %etween in# not in null# not null li&e Logi!al ope ato s (nd O not "" lowest p e!eden!e "" highest p e!eden!e

USIN8

H# -# ,# -H# ,H# IH# ,-

'/0
SQL-

sele!t E f o$ student whe e no H 1.

Copy rights are reserved.

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

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

SQL-

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

SQL-

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

SQL-

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

SQL-

sele!t E f o$ student whe e no -H 1. NO N(M' """ """"""" M(RFS """""""""

Copy rights are reserved.

7
1 1 4 @ C D Sa&eth Na en Ra$esh Madhu 9isu Rattu 133 @33

SQL-

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

SQL-

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

%)

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

Copy rights are reserved.

8
,conditionn-. '/0
SQL-

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

!)

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 H 1 o $a &s -H 133. NO N(M' """ """"""" 1 2 1 Sa&eth >agan Na en M(RFS """"""""" 133 433 @33

d)

USIN8 )'T6''N

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

sele!t E f o$ student whe e $a &s %etween 133 and @33.

Copy rights are reserved.

NO N(M' """ """"""" 1 2 1 e) Sa&eth >agan Na en

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

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

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 Sudha M(RFS """"""""" 233

Copy rights are reserved.

10
1 2 1 4 g) Sa&eth >agan Na en Ra$esh 133 433 @33

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 """""""""

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 Ra$esh M(RFS """""""""

Copy rights are reserved.

11
@ C D i) Madhu 9isu Rattu

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

J)

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' """ """"""" M(RFS """""""""

SQL-

Copy rights are reserved.

12
2 Sudha 233

ii) This will give the ows whose na$e sta t with KS;5 sele!t E f o$ student whe e na$e li&e =SL=. NO N(M' """ """"""" 2 1 Sudha Sa&eth M(RFS """"""""" 233 133

SQL-

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

SQL-

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

SQL-

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

SQL-

Copy rights are reserved.

13

NO N(M' """ """"""" 2 @ Sudha Madhu

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

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

SQL-

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

SQL-

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 =LaL a L=. NO N(M' """ """"""" 2 >agan M(RFS """""""""" 433

SQL-

E *ou have to spe!if7 the patte ns in li e using unde s!o e ( M )5

Copy rights are reserved.

14

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 de!c &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

SQL-

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 133 @33 233 M(RFS """""""""

Copy rights are reserved.

15
2 >agan 433

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

update student set $a &s H C33.

If 7ou a e not spe!if7ing an7 !ondition this will update enti e ta%le5 update student set $a &s H C33 whe e no H 1. update student set $a &s H C33# na$e H =9enu= whe e no H 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 H 1.

SQL-

Copy rights are reserved.

16

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-.

Copy rights are reserved.

17
'/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. '/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%Je!t.

Copy rights are reserved.

18
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%Je!t. S7nta/0 ena$e ,oldMtable_name- to ,new_table_name-. '/0
SQL-

ena$e student to stud.

Copy rights are reserved.

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 &. oll wo &. Upto p evious !o$$it Upto p evious oll%a!&

Copy rights are reserved.

20
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 S7nta/0 Savepoint ,!avepoint_name-. '/0
SQLSQLSQLSQLSQLSQLSQLSQL-

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

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

SQL-

SQL-

oll%a!& to savepoint s4. O oll%a!& to s4.

SQL-

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

Copy rights are reserved.

21
SQL-

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

Copy rights are reserved.

22

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"e!- on ,ob#ect_name- to ,u!er_name- Nwith g ant optionO. '/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%Je!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"e!- on ,ob#ect_name- f o$ ,u!er_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

Copy rights are reserved.

23

USIN8 (LI(S'S
CR'(T' 6IT: S'L'CT 6e !an ! eate a ta%le using e/isting ta%le Nalong with dataO5 S7nta/0 C eate ta%le ,new_table_name- Ncol1, col2, col3 ... colnO 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 H 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.

Copy rights are reserved.

24
COLUMN (LI(S'S S7nta/0 $elect %or"inal_col& %alia!_name& 'rom %tableMna$e-. '/0
SQL-

sele!t no sno f o$ student. o sele!t no PsnoQ 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 ,alia!_name-5,col1-# ,alia!_name-5,col2- R ,alia!_name-5,coln- f o$ ,table_name- ,alia!_name-. '/0
SQL-

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

Copy rights are reserved.

25

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(s25noHs15no) 6hen $at!hed then Update set $a &s H 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(s25noHs15no) 6hen $at!hed then Update set $a &s H s15hno 6hen not $at!hed then Inse t (s25no#s25na$e#s25$a &s) 9alues(s15no#s15na$e#s15hno).

SQL-

Copy rights are reserved.

26

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 deptnoH23.

"" 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 deptnoH23.

"" This inse ts 4 ows

Copy rights are reserved.

27
d)
MULTI INS'RT 6IT: DUALIC(T' RO6S

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 deptno - 23.

"" This inse ts S 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 H KS(L'S; then Into student1 values(1#;%;#133) 6hen lo! H KN'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 H KS; then Into student1 values(1#;%;#133) 6hen lo! H KN'6 *ORF; then Into student4 values(4#;!;#433) 'lse Into student values(@#;d;#@33)

Copy rights are reserved.

28
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 6hen deptno H 13 then Into student2 values(2#;a;#233) 6hen dna$e H KR'S'(RC:; then Into student1 values(1#;%;#133) 6hen lo! H KN'6 *ORF; then Into student4 values(4#;!;#433) Sele!t Ef o$ dept whe e deptnoH13.

"" 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 H 43 then Into student2 values(2#;a;#233) 6hen dna$e H KR; then Into student1 values(1#;%;#133) 6hen lo! H KN'6 *ORF; then Into student4 values(4#;!;#433) 'lse Into student values(@#;d;#@33) Sele!t Ef o$ dept whe e deptnoH13.

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

Copy rights are reserved.

29
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 deptnoH23.

"" 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

Copy rights are reserved.

30

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

SIN8L' RO6 BUNCTIONS Single ow fun!tions !an %e !atego iTed 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 SU t Mod Nvl Aowe '/p Ln Log Ceil Bloo Round T un& )itand 8 eatest Least

Copy rights are reserved.

31
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 sUua e oot of the given value5 S7nta/0 sU t (value) '/0 "" he e value $ust %e positive5

Copy rights are reserved.

32
SQL-

sele!t sU t(@)# sU t(3)# sU t(null)# sU 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, divi!or) '/0
SQL-

sele!t $od(V#@)# $od(2#C)# $od(null#null)# $od(3#3)# $od("V#@) f o$ dual. MOD(V#@) """""""""""" 4 MOD(2#C) MOD(NULL#NULL) """""""""" """"""""""""""""""""" 2 MOD(3#3) MOD("V#@) """"""""""" """"""""""""" 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)

Copy rights are reserved.

33
""" """"""" """"""""""""""""""""" 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. N9L(3#3) """""""""" 3 N9L(2#2) N9L(null#null) N9L(@#@) """""""""" """"""""""""""""" 2 """""""""" @

f)

AO6'R

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

sele!t powe (1#C)# powe (3#3)# powe (2#2)# powe (null#null)# powe (1#"C) 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

Copy rights are reserved.

34
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) """""""""" 524C44C1W4

15V2W1W2W4 V54WS3CD2 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 Te o whi!h is positive onl75

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

5DS42@V2W2

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


'+A LN

(4) H 1353WCC4DS

(1353WCC4DS) H 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 Te 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)

Copy rights are reserved.

35
""""""""""""""" 1 (value) H """"""""""" 543231SSSD ('+A(2)# value) """""""""""" """"""""""""""""" 3

LN

LO8

SQL-

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

253SWD211S J)

C'IL

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

sele!t !eil(C)# !eil(C52)# !eil("C)# !eil( "C52)# !eil(3)# !eil(null) 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 eUual to the spe!ified value5 S7nta/0 floo (value) '/0
SQL-

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

Copy rights are reserved.

36
C l)
ROUND

"C

"D

This will ounds nu$%e s to a given nu$%e of digits of p e!ision5 S7nta/0 ound (value, preci!ion) '/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


SQL-

"""""""""""""""""""""""" """"""""""""""""""""""" """"""""""""""""""""""" 214 214514 21451@

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

SQL-

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

Copy rights are reserved.

37

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, preci!ion) '/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.

Copy rights are reserved.

38

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)

Copy rights are reserved.

39
f o$ dual. )IT(ND(1#4) )IT(ND(3#3) )IT(ND(2#2) )IT(ND(NULL#NULL) )IT(ND("1#"4) """""""""""""" """"""""""""""" """""""""""""" 1 o) 3 2 """""""""""""""""""""""" """"""""""""""""" "@

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 Te os then it will displa7 Te 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 Te os then it will displa7 Te o5 If all the pa a$ete s a e nulls then it will displa7 nothing5

Copy rights are reserved.

40
U) If an7 of the pa a$ete s is null it will displa7 nothing5

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) """"""""""""""""""" 2 """"""""""""""""""""""""""""""" 1

STRIN8 BUNCTIONS

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

Copy rights are reserved.

41
a)
INITC(A

Coales!e

This will !apitaliTe the initial lette of the st ing5 S7nta/0 init!ap (!trin") '/0
SQL-

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

%)

UAA'R

This will !onve t the st ing into uppe !ase5 S7nta/0 uppe (!trin") '/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 (!trin") '/0
SQL-

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

Copy rights are reserved.

42

LO6'R """"""""""" !o$pute d)

L'N8T:

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

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

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 (!trin", 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

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

LA(D

Copy rights are reserved.

43
This will allows 7ou to pad the left side of a !olu$n with an7 set of !ha a!te s5 S7nta/0 lpad (!trin", 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$ (!trin" *,unwanted_c)ar!+) '/0
SQL-

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

Copy rights are reserved.

44
This will t i$ off unwanted !ha a!te s f o$ the ight end of st ing5 S7nta/0 t i$ (!trin" *, unwanted_c)ar!+) '/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)ar! f o$ !trin") '/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 "" this will wo & as

SQL-

LTRIM

Copy rights are reserved.

45

SQL-

sele!t t i$( t ailing=i= f o$ =indiani=) f o$ dual. TRIM(T """""" Indian

"" this will wo & as

RTRIM

J)

TR(NSL(T'

This will epla!e the set of !ha a!te s# !ha a!te %7 !ha a!te 5 S7nta/0 t anslate (!trin", old_c)ar!, new_c)ar!) '/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 (!trin", old_c)ar! *, new_c)ar!+) '/0
SQL-

sele!t epla!e(=india=#=in=#=/7=)# epla!e(Kindia;#;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

Copy rights are reserved.

46
!lause5 S7nta/0 sounde/ (!trin") '/0
SQL-

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

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

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

CONC(T

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

sele!t !on!at(=!o$pute =#= ope ato =) f o$ dual. CONC(T(=COMAUT'R= """"""""""""""""""""""""" !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 sele!t =how= XX = a e= XX = 7ou= f o$ dual. =:O6=XX=(R' """"""""""""""" how a e 7ou n)

SQL-

(SCII

This will etu n the de!i$al ep esentation in the data%ase !ha a!te set of the fi st

Copy rights are reserved.

47
!ha a!te of the st ing5 S7nta/0 as!ii (!trin") '/0
SQL-

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

o)

C:R

This will etu n the !ha a!te having the %ina 7 eUuivalent 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 (SV) 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 (!trin", !tart_c)r_count *, no_o'_c)ar!+) '/0
SQL-

sele!t su%st (=!o$pute =#1)# su%st (=!o$pute =#1#C)# su%st (=!o$pute =#4#V) f o$ dual.

Copy rights are reserved.

48

SU)STR( SU)ST SU)STR """""""""" """"""" """""""" o$pute o$put $pute If no_o'_c)ar! pa a$ete is negative then it will displa7 nothing5 If %oth pa a$ete s e/!ept !trin" a e null o Te os then it will displa7 nothing5 If no_o'_c)ar! 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 !tart_c)r_count is negative then it will e/t a!t the su%st ing f o$ ight end5 2 C "W U)
INSTR

1 O "V

4 M "D

@ A "C

C U "@

D T "4

V ' "1

W R "2

This will allows 7ou fo sea !hing th ough a st ing fo set of !ha a!te s5 S7nta/0 inst (!trin", !earc)_!tr *, !tart_c)r_count *, occurrence+ +) '/0
SQL-

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

If 7ou a e not spe!if7ing !tart_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 !tart_c)r_count and occurrence a e null# it will displa7 nothing5

D'COD'

Copy rights are reserved.

49
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, . el!e). '/0
SQL-

sele!t sal# de!ode(sal#C33#=Low=#C333#=:igh=#=Mediu$=) f o$ e$p. S(L """"" C33 1C33 1333 4C33 4333 C333 @333 C333 2W33 2133 1333 1V33 1133 4133 D'COD' """"""""" Low Mediu$ Mediu$ Mediu$ Mediu$ :igh Mediu$ :igh Mediu$ Mediu$ Mediu$ Mediu$ Mediu$ Mediu$

SQL-

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 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 Te os then de!ode will displa7 Te o5

Copy rights are reserved.

50

s)

8R'(T'ST

This will give the g eatest st ing5 S7nta/0 g eatest (!trn"1, !trin"2, !trin"3 !trin"n) '/0
SQL-

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

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 (!trn"1, !trin"2, !trin"3 !trin"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'

Copy rights are reserved.

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

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

D(T' BUNCTIONS

S7sdate Cu entMdate Cu entMti$esta$p S7sti$esta$p Lo!alti$esta$p D%ti$eTone Sessionti$eTone ToM!ha ToMdate (ddM$onths MonthsM%etween Ne/tMda7 LastMda7 '/t a!t 8 eatest Least Round T un! NewMti$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

Copy rights are reserved.

52

SQL-

alte session set nlsMdateMfo $at H KDD"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' """"""""""" 1@"D'C"3D

%)

CURR'NTMD(T'

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

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

!)

CURR'NTMTIM'ST(MA

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

sele!t !u entMti$esta$p f o$ dual. CURR'NTMTIM'ST(MA """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 1@"D'C"3D 345@15@254W44DS (M G3C043

Copy rights are reserved.

53

d)

S*STIM'ST(MA

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

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

e)

LOC(LTIM'ST(MA

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

sele!t lo!alti$esta$p f o$ dual. LOC(LTIM'ST(MA """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 1@"D'C"3D 345@@52W5C31WV@ (M

f)

D)TIM'ZON'

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

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

g)

S'SSIONTIM'ZON'

This will etu ns the value of the !u ent session;s ti$e Tone5

Copy rights are reserved.

54

'/0
SQL-

sele!t sessionti$eTone f o$ dual. S'SSIONTIM'ZON' """""""""""""""""""""""""""""""""""" G3C043

h)

TOMC:(R

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

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

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

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 Last th ee digits of the 7ea Bull fou digit 7ea Signed 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

Copy rights are reserved.

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

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

*ea with !o$$a Bull7 spelled out 7ea Centu 7 No of Uua 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:

o o o o

AM A5M )C )5C

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

'/0
SQL-

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

SQL-

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

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


SQL-

two thousand si/

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

Copy rights are reserved.

56

TOMC:(R(S*SD(T'#=DD BMMONT: *'(R=) """"""""""""""""""""""""""""""""""""""""""""""""""""""" 1@ de!e$%e two thousand si/


SQL-

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

SQL-

SQL-

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

i)

TOMD(T'

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

sele!t toM!ha (toMdate(=1@?de!?133D=#=dd?$on?7777=)# =dd E $onth E da7=) f o$ dual. TOMC:(R(TOMD(T'(=1@?D'C?13 """""""""""""""""""""""""" 1@ E de!e$%e E Sunda7

Copy rights are reserved.

57

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

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

sele!t addM$onths(toMdate(=22"Jan"2SS3=#=dd"$on"7777=)# C) f o$ dual. (DDMMONT:S """""""""""""""" 22">UN"S3 sele!t addM$onths(toMdate(=22"Jan"2SS3=#=dd"$on"7777=)# "C) f o$ dual. (DDMMONT: """"""""""""""" 22"(U8"WS

SQL-

&)

If no_o'_mont)! is Te o then it will displa7 the sa$e date5 If no_o'_mont)! is null then it will displa7 nothing5

MONT:SM)'T6''N

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

sele!t $onthsM%etween(toMdate(=22"aug"2SS3=#=dd"$on"7777=)# toMdate(=22" Jan"2SS3=#=dd"$on"7777=)) f o$ dual.

Copy rights are reserved.

58

MONT:SM)'T6''N(TOMD(T'(=22"(U8"2SS3=#=DD"MON"****=)#TOMD(T'(=22">(N" 2SS3=#=DD"MON"****=)) """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" V


SQL-

sele!t $onthsM%etween(toMdate(=22"Jan"2SS3=#=dd"$on"7777=)# toMdate(=22" aug"2SS3=#=dd"$on"7777=)) f o$ dual.

MONT:SM)'T6''N(TOMD(T'(=22">(N"2SS3=#=DD"MON"****=)#TOMD(T'(=22"(U8" 2SS3=#=DD"MON"****=)) """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" "V l)

N'+TMD(*

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

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

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

L(STMD(*

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

Copy rights are reserved.

59
SQL-

sele!t lastMda7(toMdate(=1@"de!"133D=#=dd"$on"7777=)#=sun=) f o$ dual. L(STMD(*( """"""""""""" 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) '/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(toMdate(=22"Jan"S3=#=dd"$on"77=)#toMdate(=22"$a "S3=#=dd" $on"77=)#toMdate(=22"ap "S3=#=dd"$on"77=)) f o$ dual. 8R'(T'ST( """"""""""""" 22"(AR"S3

p)

L'(ST

Copy rights are reserved.

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

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

U)

ROUND

Round will ounds the date to whi!h it was eUual 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 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

Copy rights are reserved.

61

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(toMdate(=1@"de!"3@=#=dd"$on"77=)#=7ea =)# ound(toMdate(=22"$a " 3D=#=dd"$on"77=)#=7ea =) f o$ dual. ROUND(TOM ROUND(TOM """""""""""" 32">(N"3C """"""""""""""" 32">(N"3D

SQL-

sele!t ound(toMdate(=22"Jan"3@=#=dd"$on"77=)#=$onth=)# ound(toMdate(=2W" Jan"3@=#=dd"$on"77=)#=$onth=) f o$ dual. ROUND(TOM ROUND(TOM """"""""""""" """"""""""""""" 32">(N"3@ 32"B')"3@

SQL-

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

SQL-

sele!t toM!ha ( ound(toMdate(=1@"de!"3D=#=dd"$on"77=))# =dd $on 7777

Copy rights are reserved.

62
hh0$i0ss a$=) f o$ dual. TOMC:(R(ROUND(TOMD(T'(= """"""""""""""""""""""""""""""""" 1@ de! 133D 21033033 a$ )
TRUNC

T un! will !hops off the date to whi!h it was eUual 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 '/0
SQL-

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

SQL-

sele!t t un!(toMdate(=22"Jan"3@=#=dd"$on"77=)#=$onth=)# t un!(toMdate(=2W"Jan" 3@=#=dd"$on"77=)#=$onth=) f o$ dual. TRUNC(TOM TRUNC(TOM """"""""""""" """"""""""""" 32">(N"3@ 32">(N"3@

SQL-

sele!t t un!(toMdate(=1D"de!"3D=#=dd"$on"77=)#=da7=)# t un!(toMdate(=1S"de!"

Copy rights are reserved.

63
3D=#=dd"$on"77=)#=da7=) f o$ dual. TRUNC(TOM TRUNC(TOM """"""""""""" """""""""""""" 1@"D'C"3D 1@"D'C"3D
SQL-

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

s)

N'6MTIM'

This will give the desi ed ti$eTone;s date and ti$e5 S7nta/0 newMti$e (date, current_time,one, de!ired_time,one) (vaila%le ti$eTones a e as follows5
TIM'ZON'S

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

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

(tlanti! standa d?da7 light ti$e )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 toM!ha (newMti$e(s7sdate#=g$t=#=7st=)#=dd $on 7777 hh0$i0ss a$=) f o$ dual.

Copy rights are reserved.

64

TOMC:(R(N'6MTIM'(S*SD(T """"""""""""""""""""""""""""""""""" 1@ de! 133D 310C2013 p$ sele!t toM!ha (newMti$e(s7sdate#=g$t=#=est=)#=dd $on 7777 hh0$i0ss a$=) f o$ dual. TOMC:(R(N'6MTIM'(S*SD(T """"""""""""""""""""""" 1@ de! 133D 3D0C201D p$ t)
CO(L'SC'

SQL-

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

sele!t !oales!e(=21"Jan"S3=#=24"Jan"SS=)# !oales!e(null#=21"Jan"S3=#=14"$a " SW=#null) f o$ dual. CO(L'SC'( CO(L'SC'( """"""""""""" """""""""""" 21"Jan"S3 21"Jan"S3

MISC'LL(N'OUS BUNCTIONS

a)
UID

Uid Use 9siTe Ran& DenseM an&

Copy rights are reserved.

65
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 """""""""" 42S

%)

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 vsiTe(214)# vsiTe(=!o$pute =)# vsiTe(=21"Jan"S3=) f o$ dual. 9SIZ'(214) 9SIZ'(=COMAUT'R=) 9SIZ'(=21">(N"S3=) """"""""""""" """"""""""""""""""""""" """""""""""""""""""""" 4 W S

d)

R(NF

This will give the non"seUuential an&ing5

Copy rights are reserved.

66
'/0
SQL-

sele!t ownu$#sal f o$ (sele!t sal f o$ e$p o de %7 sal des!). RO6NUM 2 1 4 @ C D V W S 23 22 21 24 2@ 2C S(L """""""""" """""""""" C333 4333 4333 1SVC 1WC3 1@C3 2D33 2C33 2433 21C3 21C3 2233 2333 SC3 W33

SQL-

sele!t an&(1SVC) within g oup(o de %7 sal des!) f o$ e$p. R(NF(1SVC)6IT:IN8ROUA(ORD'R)*S(LD'SC) """"""""""""""""""""""""""""""""""""""""""""""""""""""""" @

d)

D'NS'MR(NF

This will give the seUuential an&ing5 '/0


SQL-

sele!t denseM an&(1SVC) within g oup(o de %7 sal des!) f o$ e$p. D'NS'MR(NF(1SVC)6IT:IN8ROUA(ORD'R)*S(LD'SC) """""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""

Copy rights are reserved.

67
4

CON9'RSION BUNCTIONS

a)
)INMTOMNUM

)inMtoMnu$ Cha to owid Rowidto!ha ToMnu$%e ToM!ha ToMdate

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

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

%)

If all the %its a e Te o then it p odu!es Te 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

TOMNUM)'R

Copy rights are reserved.

68

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

TOMC:(R

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

TOMD(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) """""""""" 4WD33

%)

(98

Copy rights are reserved.

69

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) """"""""""""""" 1VCV52@1WD

!)

M(+

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

sele!t $a/(sal) 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) """"""""""

Copy rights are reserved.

70
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@

Copy rights are reserved.

71

CONSTR(INTS
Const aints a e !atego iTed as follows5 Do$ain integ it7 !onst aints Not null Che!& 'ntit7 integ it7 !onst aints UniUue 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

Copy rights are reserved.

72
SQL-

! 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)).

SQL-

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

Copy rights are reserved.

73

'/0
COLUMN L'9'L

SQL-

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

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)# uniUue(no)). ! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# !onst aint un uniUue(no)).

SQL-

(LT'R L'9'L

SQLSQL-

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

ARIM(R* F'* This is used to avoid dupli!ates and nulls5 This will wo & as !o$%ination of uniUue and not null5 '/0
COLUMN L'9'L

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

SQL-

! 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)#

SQL-

Copy rights are reserved.

74
$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

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 '/0
T()L' L'9'L

Bo eign &e7 alwa7s atta!hed to the !hild ta%le5 6e !an add this !onst aint in ta%le and alte levels onl75

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).

Copy rights are reserved.

75
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 dependent !onst aint5 '/0
T()L' L'9'L

7ou

e$ove pa ent

e!o d o a!le auto$ati!all7

e$oves all its

e!o ds f o$ !hild ta%le# if this !lause is p esent while ! eating fo eign &e7

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). ! 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).

SQL-

(LT'R L'9'L

SQL-

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.

SQL-

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)

Copy rights are reserved.

76
SQL-

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

SQL-

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

SQLSQL-

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

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

SQL-

! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# 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-

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

Copy rights are reserved.

77

'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 uniUue(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 uniUue(no) defe ed initiall7 defe ed). alte ta%le student add !onst aint un uniUue(no) defe a%le initiall7 defe ed. set !onst aints all i$$ediate.

SQL-

SQL-

SQL-

This will ena%le all the !onst aints violations at the ti$e of inse ting5 set !onst aints all defe ed.

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.

Copy rights are reserved.

78
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

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

Copy rights are reserved.

79

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 Klow; 6hen C333 then Khigh; 'lse K$ediu$; 'nd !ase B o$ e$p. S(L """"" C33 1C33 1333 4C33 4333 C333 @333 C333 2W33 2133 1333 1V33 1133 C(S' """""""" low $ediu$ $ediu$ $ediu$ $ediu$ high $ediu$ high $ediu$ $ediu$ $ediu$ $ediu$ $ediu$

Copy rights are reserved.

80
4133 $ediu$

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-

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

SQL-

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

SQL-

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

"" Default !an not ove ide nulls5

Copy rights are reserved.

81

()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%Je!t(hno nu$%e (4)#!it7 va !ha (23)). ?

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

SQL-

! eate ta%le student(no nu$%e (1)#na$e va !ha (1)#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=)

Copy rights are reserved.

82
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 H =%o$%a7= whe e s5add ess5hno H 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 H 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 .

Copy rights are reserved.

83

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


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

elates this o%Je!t to the e/isting ta%le

! 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%Je!t(hno nu$%e (1)#!it7 va !ha (23)).? ! eate t7pe stud as o%Je!t(na$e va !ha (23)#add ess add ).? ! eate view studentMov(no#studMinfo) as sele!t no#stud(na$e#add (hno#!it7)) f o$ student.

4) Relate the o%Je!ts to the student ta%le %7 ! eating the o%Je!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 studentMov values(2#stud(Ksudha;#add (222#;h7d;))).

%) )7 using o%Je!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. '/0

Copy rights are reserved.

84
2) Defining $ethods in t7pes
SQL-

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

1) Defining t7pe %od7


SQL-

C eate t7pe %od7 stud as Me$%e fun!tion $a &sMf($a &s in nu$%e ) etu n nu$%e is )egin Retu n ($a &sG233). 'nd $a &sMf. '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(Ksudha;#233)). Sele!t s5info5$a &sMf(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!tM efe en!es to avoid the w ites to the Data%ase5

Copy rights are reserved.

85

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%Je!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#;Jagan;#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

Copy rights are reserved.

86

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$it5 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%Je!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 studentMte$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#;Jagan;#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 noH2) values(add (CCC#;!hennai;)).

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


SQL-

V) Update in nested ta%les


SQL-

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

Copy rights are reserved.

87
W) Delete in nested ta%le
SQL-

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

D(T( MOD'L (LLMCOLLMT*A'S (LLMT*A'S D)(MCOLLMT*A'S D)(MT*A'S US'RMCOLLMT*A'S US'RMT*A'S

Copy rights are reserved.

88

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%$sMflash%a!&5ena%leMatMti$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%$sMflash%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 0H e/e! d%$sMflash%a!&5getMs7ste$M!hangeMnu$%e

%) 8et the SCN


SQL-

!) To see the SCN


SQL-

A int s

Copy rights are reserved.

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

'/e! d%$sMflash%a!&5ena%leMatMs7ste$M!hangeMnu$%e (0s) '/e! d%$sMflash%a!&5disa%le

'+T'RN(L T()L'S
*ou !an use e/te nal ta%le featu e to a!!ess e/te nal files as if the7 a e ta%les inside the 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 Uue 7 the ta%le# o a!le eads the e/te nal ta%le and etu ns the esults Just 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%Je!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'

ead and w ite p ivilege on the

SQLSQLSQLSQLSQLSQLSQL-

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

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

SQL-

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

Copy rights are reserved.

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

SQL-

sele!t E f o$ deptMe/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 Uui!&l7 even though a full ta%le s!an id eUui ed with ea!h a!!ess %) *ou !an Join e/te nal ta%les to ea!h othe o to standa d ta%les

Copy rights are reserved.

91

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


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

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

9(LU' 'ven though the p i$a 7 ta%le is o%Je!t ta%le# still it displa7s the ows in gene al fo $at5 '/0 2) ! eate vendotMadt t7pe
SQL-

To displa7 the enti e st u!tu e of the o%Je!t# this will %e used5

C eate t7pe vendo Madt as o%Je!t (vendo M!ode nu$%e (1)# vendo Mna$e va !ha (1)# vendo Madd ess va !ha (23)).?

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


SQLSQL-

C eate ta%le vendo s of vendo Madt. C eate ta%le vendo s2 of vendo Madt. inse t into vendo s values(2# Ka;# Kh7d;). inse t into vendo s values(1# K%;# K%ang;). inse t into vendo s2 values(4# K!;# Kdelhi;). inse t into vendo s2 values(@# Kd;# K!hennai;).

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


SQLSQLSQLSQL-

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

Copy rights are reserved.

92
SQL-

C eate ta%le o de s (o de Mno nu$%e (1)# vendo Minfo ef vendo Madt). O C eate ta%le o de s (o de Mno nu$%e (1)# vendo Minfo ef vendo Madt with owid).

SQL-

C) inse t the data into o de s ta%le The vendo Minfo !olu$n in the following s7nta/es will sto e o%Je!t id of an7 ta%le whi!h is efe en!ed %7 vendo Madt o%Je!t ( %oth vendo s and vendo s2)5
SQL-

inse t into o de s values(22#(sele!t ef(v) f o$ vendo s v whe e vendo M!ode H 2)). inse t into o de s values(21#(sele!t ef(v) f o$ vendo s v whe e vendo M!ode inse t into o de s values(24#(sele!t ef(v2) f o$ vendo s2 v2 whe e vendo M!ode H 2)). inse t into o de s values(2@#(sele!t ef(v2) f o$ vendo s2 v2 whe e vendo M!ode H 2)).

SQL-

H 1)).
SQL-

SQL-

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


SQL-

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

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

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

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

E 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 Madt# the o de s ta%le will sto e the o%Je!t ids of vendo s onl7 %e!ause it is !onst ained to that ta%le onl75 The vendo Minfo !olu$n in the following s7nta/es will sto e o%Je!t ids of vendo s onl75

Copy rights are reserved.

93

SQL-

C eate ta%le o de s (o de Mno nu$%e (1)# vendo Minfo ef vendo Madt s!ope is vendo s). O C eate ta%le o de s (o de Mno nu$%e (1)# vendo Minfo ef vendo Madt !onst aint f& efe en!es vendo s).

SQL-

Copy rights are reserved.

94

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


To i$ple$ent the o%Je!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%Je!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%Je!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%Je!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 student2Mov of stud with o%Je!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 student1Mov as sele!t no#hno#!it7#

$a&eM ef(student2Mov#id) id f o$ Student1. d) Que 7 the following


SQLSQL-

sele!t Ef o$ student2Mov. sele!t ef(s) f o$ student2Mov s.

Copy rights are reserved.

95
SQLSQ-

sele!t values(s) f o$ student2Mov. sele!t de ef(s5id) f o$ student1Mov s.

sele!t Ef o$ student1Mov.

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 pa titions a e

ope ations to %e !a ied out on sele!ted pa titions while othe 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 va ious pu poses5 !on!u ent use of the va ious pa titions fo

(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

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

Copy rights are reserved.

96

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. (lte ta%le student t un!ate pa tition pD.

g) Rena$ing a pa tition
SQL-

h) T un!ate a pa tition
SQL-

Copy rights are reserved.

97
i) Splitting a pa tition
SQL-

(lte ta%le student split pa tition p1 at(2C) into (pa tition p12#pa tition p11).

J) '/!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 p12 ta%lespa!e sa&ethMts.

&) 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#V#W#S#23)#pa tition p4 values(22#21#24#2@#2C)# pa tition p@ values(2D#2V#2W#2S#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@. (lte ta%le student ena$e pa tition p4 to pD.

f) D opping a pa tition
SQL-

g) Rena$ing a pa tition
SQL-

Copy rights are reserved.

98
h) T un!ate a pa tition
SQL-

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

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&ethMts.

J) 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*SMA2 S*SMA1 S*SMA4 S*SMA@ S*SMAC %) 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(s7sMp2). (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

e) (dding a pa tition

Copy rights are reserved.

99
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 pV. (lte ta%le student t un!ate pa tition pV. (lte ta%le student e/!hange pa tition s7sMp2 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 s7sMp1 ta%lespa!e sa&ethMts.

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*SMSU)A2 S*SMSU)A1 S*SMSU)A4 A1 \ S*SMSU)A@ S*SMSU)AC S*SMSU)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(s7sMsu%p2).

d) Aossi%le ope ations with su%pa titions

Copy rights are reserved.

100
(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-

D(T( MOD'L (LLMINDMA(RTITIONS (LLMINDMSU)A(RTITIONS (LLMT()MA(RTITIONS (LLMT()MSU)A(RTITIONS D)(MINDMA(RTITIONS D)(MINDMSU)A(RTITIONS D)(MT()MA(RTITIONS D)(MT()MSU)A(RTITIONS US'RMINDMA(RTITIONS US'RMINDMSU)A(RTITIONS US'RMT()MA(RTITIONS US'RMT()MSU)A(RTITIONS

Copy rights are reserved.

101

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) WVC3 23WVC S@33

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

SQL-

sele!t deptno#Jo%#su$(sal) f o$ e$p g oup %7 deptno#Jo%. 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 2S33 1SVC SC3 1WC3 CD33

Copy rights are reserved.

102
:(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#Jo%#su$(sal) tsal f o$ e$p g oup %7 deptno#Jo% 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#Jo%#su$(sal) tsal f o$ e$p g oup %7 deptno#Jo% having su$(sal) 4333 o de %7 Jo%. 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

Copy rights are reserved.

103

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 Jo% !atego 7 along wih the total sala 7 fo individual depa t$ents and the total sala 7 of all the depa t$ents5
SQL-

Sele!t deptno#Jo%#su$(sal) f o$ e$p g oup %7 ollup(deptno#Jo%). 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 WVC3 D333 2S33 1SVC 23WVC SC3 1WC3 CD33 S@33 1S31C

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

USIN8 8ROUAIN8 In the a%ove Uue 7 it will give the total sala 7 of the individual depa t$ents %ut with a

Copy rights are reserved.

104
%lan& in the Jo% !olu$n and gives the total sala 7 of all the depa t$ents with %lan&s in deptno and Jo% !olu$ns5 To epla!e these %lan&s with 7ou desi ed st ing g ouping will %e used sele!t de!ode(g ouping(deptno)#2#=(ll Depts=#deptno)#de!ode(g ouping(Jo%)#2#=(ll Jo%s=#Jo%)#su$(sal) f o$ e$p g oup %7 ollup(deptno#Jo%). 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 Jo%s (N(L*ST CL'RF M(N(8'R (ll Jo%s CL'RF M(N(8'R S(L'SM(N (ll Jo%s (ll Jo%s SUM(S(L) """""""""""""" 2433 1@C3 C333 WVC3 D333 2S33 1SVC 23WVC SC3 1WC3 CD33 S@33 1S31C

SQL-

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 Jo% !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 Jo% !atego 75 sele!t de!ode(g ouping(deptno)#2#;(ll Depts;#deptno)#de!ode(g ouping(Jo%)#2#;(ll

SQL-

Copy rights are reserved.

105
>o%s;#Jo%)#su$(sal) f o$ e$p g oup %7 !u%e(deptno#Jo%).

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 WVC3 D333 2S33 1SVC 23WVC SC3 1WC3 CD33 S@33 D333 @2C3 W1VC C333 CD33 1S31C

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

Copy rights are reserved.

106

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.

Copy rights are reserved.

107

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 '/0
SQL-

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

Copy rights are reserved.

108

9I'6S
( view is a data%ase o%Je!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 Uue 7 and t eats it as a ta%le# the efo e a view !an %e thought of as a sto ed Uue 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

Copy rights are reserved.

109
'/0
SQLSQLSQLSQLSQL-

9iew with distin!t

C eate view deptMv as sele!t Ef o$ dept with ead onl7. C eate view deptMv as sele!t deptno# su$(sal) tMsal f o$ e$p g oup %7 deptno. 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 P view has e o s Q5 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 H C33 with !he!& option

SQL-

Copy rights are reserved.

110
!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 d op view deptMv.

SQL-

D(T( MOD'L (LLM9I'6 D)(M9I'6 US'RM9I'6S

Copy rights are reserved.

111

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


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

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%Je!t5 A ovides lo!ation t anspa en!7 fo e$ote o%Je!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' ( seUuen!e is a data%ase o%Je!t# whi!h !an gene ate uniUue# seUuential intege values5 It !an %e used to auto$ati!all7 gene ate p i$a 7 &e7 o uniUue &e7 values5 ( seUuen!e !an %e eithe in an as!ending o des!ending o de 5 S7nta/0 C eate seUuen!e ,!e._name- Nin! e$ent %t7 nO Nsta t with nO N$a/value nO

Copy rights are reserved.

112
N$invalue nO N!7!le?no!7!leO N!a!he?no!a!heO. )7 defalult the seUuen!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 seUuen!e nu$%e s and etains the$ in $e$o 7 fo faste a!!ess5 '/0
SQLSQL-

! eate seUuen!e s. ! eate seUuen!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# Ksa&eth;). Initiall7 !u val is not defined and ne/tval is sta ting value5 (fte that ne/tval and !u val a e alwa7s eUual5

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

SQLSQL-

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

(LT'RIN8 S'QU'NC'

6e !an alte the seUuen!e to pe fo $ the following5 '/0


SQLSQL-

Set o eli$inate $invalue o $a/value5 Change the in! e$ent value5 Change the nu$%e of !a!hed seUuen!e nu$%e s5

alte seUuen!e s $invalue C. alte seUuen!e s in! e$ent %7 1.

Copy rights are reserved.

113
SQL-

alte seUuen!e s !a!he 23.

DROAAIN8 S'QU'NC'

SQL-

d op seUuen!e s.

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

Inne Join Using !lause On !lause

(ssu$e that we have the following ta%les5 sele!t E f o$ dept. LOC h7d %ang

SQL-

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

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

Copy rights are reserved.

114
43 h %o$%a7

SQL-

sele!t E f o$ e$p. 'N(M' sa&eth sudha Jagan $adhu >O) anal7st !le & $anage enginee M8R D'ATNO @@@ 444 222 111 23 13 23 @3

'MANO 222 111 444 @@@ 'QUI >OIN

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

( Join whi!h !ontains an KH; ope ato in the Joins !ondition5 '/0
SQL-

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

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

USIN8 CL(US' sele!t e$pno#ena$e#Jo% #dna$e#lo! f o$ e$p e Join dept d using(deptno). 'MANO 222 444 'N(M' sa&eth Jagan >O) anal7st $anage DN(M' $&t $&t LOC h7d h7d

SQL-

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

Copy rights are reserved.

115
111 ON CL(US' sele!t e$pno#ena$e#Jo%#dna$e#lo! f o$ e$p e Join dept d on(e5deptnoHd5deptno). 'MANO 222 444 111 NON"'QUI >OIN ( Join whi!h !ontains an ope ato othe than KH; in the Joins !ondition5 '/0
SQL-

sudha

!le &

fin

%ang

SQL-

'N(M' sa&eth Jagan sudha

>O) anal7st

DN(M' $&t $&t fin

LOC h7d h7d %ang

"""""""""" """""""""" """""""""" """""""""" """""""""" $anage !le &

sele!t e$pno#ena$e#Jo%#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 Join5 '/0
SQL-

sele!t e25e$pno#e15ena$e#e25Jo%#e15deptno f o$ e$p e2#e$p e1 whe e e25e$pnoHe15$g . 'MANO 'N(M' >O) D'ATNO

Copy rights are reserved.

116
"""""""""" """""""""" """""""""" """""""""" 222 111 444 @@@ N(TUR(L >OIN Natu al Join !o$pa es all the !o$$on !olu$ns5 '/0
SQL-

Jagan $adhu sudha sa&eth

anal7st !le & $anage enginee

23 @3 13 23

sele!t e$pno#ena$e#Jo%#dna$e#lo! f o$ e$p natu al Join dept. 'MANO 222 444 111 'N(M' sa&eth Jagan sudha >O) anal7st $anage !le & DN(M' $&t $&t fin LOC h7d h7d %ang

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

CROSS >OIN This will gives the ! oss p odu!t5 '/0


SQL-

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

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

Copy rights are reserved.

117
222 111 444 @@@ OUT'R >OIN Oute Join gives the non"$at!hing e!o ds along with $at!hing e!o ds5 sa&eth sudha Jagan $adhu anal7st !le & $anage enginee h h h h %o$%a7 %o$%a7 %o$%a7 %o$%a7

L'BT OUT'R >OIN

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#Jo%#dna$e#lo! f o$ e$p e left oute Join dept d on(e5deptnoHd5deptno). O sele!t e$pno#ena$e#Jo%#dna$e#lo! f o$ e$p e#dept d whe e e5deptnoHd5deptno(G). 'MANO 222 444 111 @@@ 'N(M' sa&eth Jagan sudha $adhu >O) anal7st $anage !le & enginee DN(M' $&t $&t fin LOC h7d h7d %ang

SQL-

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

RI8:T OUT'R >OIN

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#Jo%#dna$e#lo! f o$ e$p e ight oute Join dept d on(e5deptnoHd5deptno).

Copy rights are reserved.

118
O
SQL-

sele!t e$pno#ena$e#Jo%#dna$e#lo! f o$ e$p e#dept d whe e e5deptno(G) H d5deptno.

'MANO 222 444 111

'N(M' sa&eth Jagan sudha

>O) anal7st

DN(M' $&t $&t fin h

LOC h7d h7d %ang %o$%a7

"""""""""" """""""""" """""""""" """""""""" """""""""" $anage !le &

BULL OUT'R >OIN

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#Jo%#dna$e#lo! f o$ e$p e full oute Join dept d on(e5deptnoHd5deptno). 'MANO 444 222 111 @@@ 'N(M' Jagan 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#Jo%#dna$e#lo! f o$ e$p inne Join dept using(deptno). 'MANO 'N(M' >O) DN(M' LOC

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

Copy rights are reserved.

119
222 444 111 sa&eth Jagan sudha anal7st $anage !le & $&t $&t fin h7d h7d %ang

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


SU)QU'RI'S Nesting of Uue ies# one within the othe is te $ed as a su%Uue 75 ( state$ent !ontaining a su%Uue 7 is !alled a pa ent Uue 75 Su%Uue 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%Uue ies Multi ow su%Uue ies Multiple su%Uue ies Co elated su%Uue ies

SIN8L' RO6 SU)QU'RI'S

In single ow su%Uue 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 H VCDD). 'MANO VVWW VW4S VS31 'N(M' SCOTT FIN8 BORD >O) (N(L*ST AR'SID'NT (N(L*ST VCDD M8R VCDD :IR'D(T' 2S"(AR"WV 34"D'C"W2 S(L COMM D'ATNO 13 23 13

"""""""""" """""""""" """"""""" """""""""" """""""""""" """"""" """""""""" """""""""" 4333 4333 2V"NO9"W2 C333

MULTI RO6 SU)QU'RI'S

Copy rights are reserved.

120

In $ulti su%Uue 75 '/0


SQL-

ow su%Uue 7# it will

etu n $o e than one value5 In su!h !ases we should not in %etween the !o$pa ision ope ato and the

in!lude ope ato s li&e an7# all# in o

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 VCDD VVWW VW4S VS31 '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 VCDD 2S"(AR"WV

"""""""" """""""""" """""""""" 1SVC 4333 C333 4333

VW4S 31"(AR"W2 2V"NO9"W2 VCDD 34"D'C"W2

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 VW4S 'N(M' FIN8 >O) M8R :IR'D(T' S(L COMM D'ATNO 23

"""""""""" """""""""" """"""""" """""""""" """"""""""""" """""" """""""""" """""""""" AR'SID'NT 2V"NO9"W2 C333

MULTIAL' SU)QU'RI'S

The e is no li$it on the nu$%e

of su%Uue ies in!luded in a whe e !lause5 It allows

nesting of a Uue 7 within a su%Uue 75 '/0


SQL-

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

"""""""""" """""""""" """"""""" """""""""" """""""""""" """"""" """""""""" """""""""" (N(L*ST VCDD (N(L*ST VCDD

Copy rights are reserved.

121

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

( su%Uue 7 is evaluated on!e fo '/0


SQL-

the enti e pa ent state$ent whe e as a !o elated

su%Uue 7 is evaluated on!e fo eve 7 ow p o!essed %7 the pa ent state$ent5 sele!t distin!t deptno f o$ e$p e whe e C ,H (sele!t !ount(ena$e) f o$ e$p whe e e5deptno H 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 Uue 75 '/0 Suppose we want to displa7 the depa t$ent nu$%e s whi!h has $o e than @ e$plo7ees5 sele!t deptno#!ount(E) f o$ e$p g oup %7 deptno having !ount(E) - @. D'ATNO """"""""" 13 43 COUNT(E) """""""""" C D

SQL-

B o$ the a%ove Uue 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

Copy rights are reserved.

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

sele!t deptno#ena$e f o$ e$p e2 whe e e/ists (sele!t E f o$ e$p e1 whe e e25deptnoHe15deptno g oup %7 e15deptno having !ount(e15ena$e) - @) o de %7 deptno#ena$e. D'ATNO 13 13 13 13 13 43 43 43 43 43 43 'N(M' (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 e25deptnoHe15deptno 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-

Copy rights are reserved.

123

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


6(LFUA TR''S Using hie a !hi!al Uue 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 =HH-= XX p io ena$e# level f o$ e$p sta t with ena$e H =FIN8= !onne!t %7 p io e$pnoH$g . 'N(M'XX=HH-=XXARIOR'N(M """""""""""""""""""""""""""""""""""" FIN8HH>ON'SHH-FIN8 SCOTTHH->ON'S (D(MSHH-SCOTT BORDHH->ON'S SMIT:HH-BORD )L(F'HH-FIN8 (LL'NHH-)L(F' 6(RDHH-)L(F' M(RTINHH-)L(F' TURN'RHH-)L(F' >(M'SHH-)L(F' CL(RFHH-FIN8 MILL'RHH-CL(RF L'9'L """""""" 2 1 4 @ 4 @ 1 4 4 4 4 4 1 4

Copy rights are reserved.

124

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 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 INLIN' 9I'6 OR TOA"N (N(L*SIS In the sele!t state$ent instead of ta%le na$e# epla!ing the sele!t state$ent is &nown as inline view5 '/0
SQL-

Sele!t ena$e# sal# ownu$ an& 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 W33 SC3 2233 21C3 21C3 2433 2C33 2D33 1@C3 1WC3 1SVC 4333 4333 C333 R(NF 2 1 4 @ C D V W S 23 22 21 24 2@

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

Copy rights are reserved.

125

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 sele!t55fo update !lause5 '/0
SQL-

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

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

Copy rights are reserved.

126

( sha e lo!& lo!&s the ta%le allowing othe use s to onl7 Uue 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

Uue 7# inse t # update o 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 '/0


SQL-

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

'+CLUSI9' LOCF

'/!lusive lo!& is the $ost est i!tive of ta%les lo!&s5 6hen issued %7 an7 use # it allows the othe use to onl7 Uue 75 It is si$ila to sha e lo!& %ut onl7 one use !an pla!e e/!lusive lo!& on a ta%le at a ti$e5 '/0
SQL-

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

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

ta%le then he has to wait until the use who has initiall7 lo!&ed the ta%le issues a !o$$it oll%a!& state$ent5 This dela7 !ould %e avoided %7 appending a nowait !lause in the lo!& ta%le !o$$and5 '/0
SQL-

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

D'(DLOCF

Copy rights are reserved.

127

( deadlo!& o!!u s when tow use s have a lo!& ea!h on sepa ate o%Je!t# and the7 want to a!Uui e a lo!& on the ea!h othe ;s o%Je!t5 6hen this happens# the fi st use has to wait fo the se!ond use to elease the lo!&# %ut the se!ond use will not elease it until the lo!& on the fi st use ;s o%Je!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%Je!t5 6e !an ! eate inde/es e/pli!itl7 to speed up SQL state$ent e/e!ution on a ta%le5 The inde/ points 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 eUualit75 T*A'S UniUue inde/ Non"uniUue inde/ )t ee inde/ )it$ap inde/ Co$posite inde/ Reve se &e7 inde/ Bun!tion"%ased inde/ Des!ending inde/ Do$ain inde/ O%Je!t inde/ Cluste inde/ Te/t inde/ Inde/ o ganiTed ta%le Aa tition inde/

Copy rights are reserved.

128
Lo!al inde/ Lo!al p efi/ed Lo!al non"p efi/ed UNIQU' IND'+ UniUue inde/es gua antee that no two uniUue !onst aint is ! eated5 '/0
SQL-

8lo%al inde/ 8lo%al p efi/ed 8lo%al non"p efi/ed

ows of a ta%le have dupli!ate values in the

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

! eate uniUue inde/ studMind on student(sno).

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

! eate inde/ studMind on student(sno).

)TR'' IND'+ o (SC'NDIN8 IND'+ The default t7pe of inde/ used in an 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 Uui!& a!!ess to g oups of ows within a 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 '/0
SQL-

! eate inde/ studMind on student(sno).

)ITM(A IND'+

Copy rights are reserved.

129
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
SQL-

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

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

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

R'9'RS' F'* IND'+ ( 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 6hen the !olu$n is inde/ed in eve se $ode then the !olu$n values will %e sto ed in an inde/ in diffe ent %lo!&s as the sta ting value diffe s5 Su!h an a ange$ent !an help avoid pe fo $an!e deg adations in inde/es whe e $odifi!ations to the inde/ a e !on!ent ated on a s$all set of %lo!&s5 '/0
SQL-

! eate inde/ studMind 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/ studMind 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

Copy rights are reserved.

130

'/0
SQL-

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

D'SC'NDIN8 IND'+ The o de used %7 )"t ee inde/es has %een as!ending o de 5 *ou !an !atego iTe data in )" t ee inde/ in des!ending o de as well5 This featu e !an %e useful in appli!ations whe e so ting ope ations a e eUui ed5 '/0
SQL-

! eate inde/ studMind on student(sno des!).

T'+T IND'+ Que 7ing te/t is diffe ent f o$ Uue 7ing data %e!ause wo ds have shades of $eaning# elationships to othe wo ds# and opposites5 *ou $a7 want to sea !h fo wo ds that a e nea ea!h othe # o wo ds that a e elated to the s5 These Uue ies would %e e/t e$el7
SQL

diffi!ult if all 7ou had availa%le was the standa d elational ope ato s5 )7 e/tending te/t5

to

in!lude te/t inde/es# o a!le te/t pe $its 7ou to as& ve 7 !o$ple/ Uuestions a%out the

To use o a!le te/t# 7ou need to ! eate a te(t inde( on the !olu$n in whi!h the te/t is sto ed5 Te/t inde/ is a !olle!tion of ta%les and inde/es that sto e info $ation a%out the te/t sto ed in the !olu$n5
T*A'S

The e a e seve al diffe ent t7pes of inde/es availa%le in o a!le Si5 The fi st# suppo ted in o a!le Wi as well as o a!le Si5 (s of o a!le Si# 7ou !an use the inde/ fo fu the enhan!e 7ou te/t inde/ $anage$ent and Uue 7 !apa%ilities5
CONT'+T

CONT'+T CT+C(T

is

te/t

Copy rights are reserved.

131
CT+C(T CT+RUL'

The

CT+C(T

inde/ t7pe suppo ts the t ansa!tional s7n!h oniTation of data %etween the
CONT'+T

%ase ta%le and its te/t inde/5 6ith

inde/es# 7ou need to $anuall7 tell o a!le to


CT+C(T

update the values in the te/t inde/ afte data !hanges in %ase ta%le5 not gene ate s!o e values du ing the te/t Uue ies5
:O6 TO CR'(T' T'+T IND'+[

inde/ t7pes do

*ou !an ! eate a te/t inde/ via a spe!ial ve sion of the ! eate inde/ !o$$an5 Bo !onte/t inde/# spe!if7 the !t/s7s5!onte/t inde/ t7pe and fo !t/!at inde/# spe!if7 the !t/s7s5!t/!at inde/ t7pe5 '/0 Suppose 7ou have a ta%le !alled )OOFS with the following !olu$ns Title# (utho # Info5 ! eate inde/ %oo&Minde/ on %oo&s(info) inde/t7pe is !t/s7s5!onte/t. ! eate inde/ %oo&Minde/ on %oo&s(info) inde/t7pe is !t/s7s5!t/!at.

SQLSQL-

T'+T QU'RI'S

On!e a te/t inde/ is ! eated on the info !olu$n of in! ease d7na$i!all75
CONT(INS < C(TS'(RC:

)OOFS

ta%le# te/t"sea !hing !apa%ilities

CONT(INS

fun!tion ta&es two pa a$ete s \ the !olu$n na$e and the sea !h st ing5

S7nta/0 Contains(inde(ed_column, !earc)_!tr). If 7ou ! eate a inde/# use the fun!tion in pla!e of
CONT(INS5 C(TS'(RC:

CT+C(T

C(TS'(RC:

ta&es th ee pa a$ete s \ the !olu$n na$e# the sea !h st ing and the inde/ set5 S7nta/0

Copy rights are reserved.

132
Contains(inde(ed_column, !earc)_!tr, inde(_!et).
:O6 ( T'+T Q'UR* 6ORFS[

6hen a fun!tion su!h as

CONT(INS

C(TS'(RC:

is used in Uue 7# the te/t po tion of the of the Uue 7 is p o!essed Just li&e a

Uue 7 is p o!essed %7 o a!le te/t5 The

e$ainde

egula Uue 7 within the data%ase5 The esult of the te/t Uue 7 p o!essing and the egula Uue 7 p o!essing a e $e ged to etu n a single set of e!o ds to the use 5
S'(RC:IN8 BOR (N '+(CT M(TC: OB ( 6ORD

The following Uue ies will sea !h fo a wo d !alled Kp pe t7; whose s!o e is g eate than Te o5 sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7;) - 3. sele!t E f o$ %oo&s whe e !atsea !h(info# Kp ope t7;# null) - 3.

SQLSQL-

Suppose if 7ou want to &now the s!o e of the Kp ope t7; in ea!h %oo&# if s!o e values fo individual sea !hes ange f o$ 3 to 23 fo ea!h o!!u en!e of the st ing within the te/t then use the s!o e fun!tion5 sele!t title# s!o e(23) f o$ %oo&s whe e !ontains(info# Kp ope t7;# 23) - 3.

SQL-

S'(RC:IN8 BOR (N '+(CT M(TC: OB MULTIAL' 6ORDS

The following Uue ies will sea !h fo two wo ds5


SQLSQL-

sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7

(ND

ha vests;) - 3. ha vests;# null) - 3.

sele!t E f o$ %oo&s whe e !atsea !h(info# Kp ope t7

(ND

Instead of using

(ND

7ou !ould hae used an a$pe sand(<)5 )efo e using this $ethod# set

define off so the < !ha a!te will not %e seen as pa t of a va ia%le na$e5 set define off sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7 < ha vests;) - 3. sele!t E f o$ %oo&s whe e !atsea !h(info# Kp ope t7 ha vests;# null) - 3.

SQLSQLSQL-

The following Uue ies will sea !h fo $o e than two wo ds5

Copy rights are reserved.

133

SQLSQL-

sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7

(ND

ha vests

(ND

wo &e s;) - 3.

sele!t E f o$ %oo&s whe e !atsea !h(info# Kp ope t7 ha vests wo &e s;# null) - 3.

The following Uue ies will sea !h fo eithe of the two wo ds5
SQL-

sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7 7ou !an use a ve ti!al line (X)5

OR

ha vests;) - 3.

Instead of

OR

SQLSQL-

sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7

ha vests;) - 3.
X

sele!t E f o$ %oo&s whe e !atsea !h(info# Kp ope t7


(CCUM(a!!u$ulate)

ha vests;# null) - 3.

In the following Uue ies the

ope ato adds togethe the s!o es of the

individual sea !hes and !o$pa es the a!!u$ulated s!o e to the th eshold value5 sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7 ha vests;) - 3. ha vests;# null) - 3.

SQLSQL-

(CCUM

sele!t E f o$ %oo&s whe e !atsea !h(info# Kp ope t7 7ou !an use a !o$$a(#)5

(CCUM

Instead of
SQLSQL-

OR

sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7 # ha vests;) - 3. sele!t E f o$ %oo&s whe e !atsea !h(info# Kp ope t7 # ha vests;# null) - 3. ope ato su%t a!ts the s!o e of the se!ond te $;s

In the following Uue ies the

MINUS

sea !h f o$ the s!o e of the fi st te $;s sea !h5 sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7 ha vests;) - 3.

SQLSQL-

MINUS NOT

sele!t E f o$ %oo&s whe e !atsea !h(info# Kp ope t7 7ou !an use \ and instead of

ha vests;# null) - 3.

Instead of

MINUS

NOT

7ou !an use ]5 ha vests;) - 3.


]

SQLSQL-

sele!t E f o$ %oo&s whe e !ontains(info# Kp ope t7

"

sele!t E f o$ %oo&s whe e !atsea !h(info# Kp ope t7

ha vests;# null) - 3.

S'(RC:IN8 BOR (N '+(CT M(TC: OB ( A:R(S'

Copy rights are reserved.

134

The following Uue ies will sea !h fo the ph ase5 If the sea !h ph ase in!ludes a ese ved wo d within o a!le te/t# the 7ou $ust use !u l7 % a!es (^_) to en!lose te/t5 sele!t E f o$ %oo&s whe e !ontains(info# Kt ansa!tions ^and_ finan!es;) - 3. sele!t E f o$ %oo&s whe e !atsea !h(info# Kt ansa!tions ^and_ finan!es;# null) - 3.

SQLSQL-

*ou !an en!lose the enti e ph ase within !u l7 % a!es# in whi!h !ase an7 ese ved wo ds within the ph ase will %e t eated as pa t of the sea !h ! ite ia5 sele!t E f o$ %oo&s whe e !ontains(info# K^t ansa!tions and finan!es_;) - 3. sele!t E f o$ %oo&s whe e !atsea !h(info# K^t ansa!tions and finan!es_;# null) - 3.

SQLSQL-

S'(RC:IN8 BOR 6ORDS T:(T (R' N'(R '(C: OT:'R

The following Uue ies will sea !h fo the wo ds that a e in %etween the sea !h te $s5 sele!t E f o$ %oo&s whe e !ontains(info# Kwo &e s 7ou !an use .5 ha vests;) - 3.

SQL-

N'(R

Instead of

N'(R

SQL-

sele!t E f o$ %oo&s whe e !ontains(info# Kwo &e s . ha vests;) - 3. inde/ Uue ies# 7ou !an spe!if7 the $a/i$u$ nu$%e of wo ds %etween the

In

CONT'+T

sea !h te $s5
SQL-

sele!t E f o$ %oo&s whe e !ontains(info# KN'(R((wo &e s# ha vests)#23); - 3.

USIN8 6ILDC(RDS DURIN8 S'(RC:'S

*ou !an use wild!a ds to e/pand the list of valid sea !h te $s used du ing 7ou Uue 75 >ust as in egula te/t"st ing wild!a d p o!essing# two wild!a ds a e availa%le5 L M " " pe !ent sign. $ultiple"!ha a!te wild!a d unde s!o e. single"!ha a!te wild!a d

SQL-

sele!t E f o$ %oo&s whe e !ontains(info# Kwo &e L;) - 3.

Copy rights are reserved.

135
SQL-

sele!t E f o$ %oo&s whe e !ontains(info# Kwo &MMM;) - 3.

S'(RC:IN8 BOR 6ORDS T:(T S:(R' T:' S(M' ST'M

Rathe than using wild!a ds# 7ou !an use ste$"e/pansion !apa%ilities to e/pand the list of te/t st ings5 8iven the Kste$; of a wo d# o a!le will e/pand the list of wo ds to sea !h fo to in!lude all wo ds having the sa$e ste$5 Sa$ple e/pansions a e show he e5 Ala7 " pla7s pla7ing pla7ed pla7ful

SQL-

sele!t E f o$ %oo&s whe e !ontains(info# K`$anage;) - 3.

S'(RC:IN8 BOR BUZZ* M(TC:'S

( fuTT7 $at!h e/pands the spe!ified sea !h te $ to in!lude wo ds that a e spelled si$ila l7 %ut that do not ne!essa il7 have the sa$e wo d ste$5 BuTT7 $at!hes a e $ost helpful when the te/t !ontains $isspellings5 The $isspellings !an %e eithe sea !hed te/t o in the sea !h st ing spe!ified %7 the use du ing the Uue 75 The following Uue ies will not wo d Kha dest;5 sele!t E f o$ %oo&s whe e !ontains(info# Kha dest;) - 3. etu n the %oo&s etu n an7thing %e!ause its sea !h does not !ontain the in the

SQL-

It does# howeve # !ontains the wo d Kha vest;5 ( fuTT7 $at!h will wo d used as the sea !h te $5

!ontaining the wo d Kha vest; even though Kha vest; has a diffe ent wo d ste$ thant the

To use a fuTT7 $at!h# p e!ede the sea !h te $ with a Uuestion $a &# with no spa!e %etween the Uuestion $a & and the %eginning of the sea !h te $5 sele!t E f o$ %oo&s whe e !ontains(info# K[ha dest;) - 3.

SQL-

S'(RC:IN8 BOR 6ORDS T:(T SOUND LIF' OT:'R 6ORDS

SOUND'+# e/pands sea !h te $s %ased on how the wo d sounds5 The SOUND'+ e/pansion $ethod uses the sa$e te/t"$at!hing logi! availa%le via the SOUND'+ fun!tion in SQL5

Copy rights are reserved.

136

To use the SOUND'+ option# 7ou $ust p e!ede the sea !h te $ with an e/!la$ation $a &(I)5 sele!t E f o$ %oo&s whe e !ontains(info# KIg ate;) - 3.

SQL-

IND'+ S*NC:RONIZ(TION

6hen using

CONT'+T

inde/es# 7ou need to $anage the te/t inde/ !ontents. the te/t

inde/es a e not updated when the %ase ta%le is updated5 6hen the ta%le was updated# its te/t inde/ is out of s7n! with the %ase ta%le5 To s7n! of the inde/# e/e!ute the p o!edu e of the e/e!
CT+MDDL S*NCMIND'+

pa!&age5

SQL-

CT+MDDL5S*NCMIND'+(K%oo&Minde/;).

IND'+ S'TS

:isto i!all7# p o%le$s with Uue ies of te/t inde/es have o!!u ed when othe ! ite ia a e used alongside te/t sea !hes as pa t of the whe e !lause5 To i$p ove the $i/ed Uue 7 !apa%ilit7# o a!le featu es inde/ sets5 The inde/es within the inde/ set $a7 %e st u!tu ed elational !olu$ns o on te/t !olu$ns5 To ! eate an inde/ set# use the pa!&age to ! eate the inde/ set and add inde/es to

CT+MDDL

it5 6hen 7ou ! eate a te/t inde/# 7ou !an then spe!if7 the inde/ set it %elongs to5 e/e! CT+MDDL5CR'(T'MIND'+MS'T(K%oo&sMinde/Mset;).

SQL-

The add non"te/t inde/es5 e/e!


CT+MDDL5(DDMIND'+(K%oo&sMinde/Mset;#

SQL-

KtitleMinde/;).

Now ! eate a

CT+C(T

te/t inde/5 Spe!if7 !t/s7s5!t/!at as the inde/ t7pe# and list the inde/

set in the pa a$ete s !lause5 ! eate inde/ %oo&Minde/ on %oo&s(info) inde/t7pe is !t/s7s5!t/!at pa a$ete s(Kinde/ set %oo&sMinde/Mset;).

SQL-

Copy rights are reserved.

137

IND'+"OR8(NIZ'D T()L' (n inde/"o ganiTed 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 ganiTed ta%les sto e thei data as if the enti e ta%le was sto ed in an inde/5 (n inde/"o ganiTed 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 p& p i$a 7 &e7(sno) o ganiTation inde/.

A(RTITION IND'+ Si$ila to pa titioning ta%les# o a!le allows 7ou to pa tition inde/es too5 Li&e ta%le

pa titions# inde/ pa titions !ould %e in diffe ent ta%lespa!es5


LOC(L IND'+'S

Lo!al &e7wo d tells o a!le to ! eate a sepa te inde/ fo ea!h pa tition5 In the lo!al p efi/ed inde/ the pa tition &e7 is spe!ified on the left p efi/5 6hen the unde l7ing ta%le is pa titioned %aes on# sa7 two !olu$ns then the inde/ !an %e p efi/ed on the fi st !olu$n spe!ified5 Lo!al p efi/ed inde/es !an %e uniUue o non uniUue5 Lo!al inde/es $a7 %e easie to $anage than glo%al inde/es5

'/0

SQL-

! eate inde/ studMinde/ on student(sno) lo!al.

8LO)(L IND'+'S

( glo%al inde/ $a7 !ontain values f o$ $ultiple pa titions5 (n inde/ is glo%al p efi/ed if it is pa titioned on the left p efi/ of the inde/ !olu$ns5 The glo%al !lause allows 7ou to ! eate a non"pa titioned inde/5 8lo%al inde/es $a7 pe fo $ uniUueness !he!&s faste inde/es5 than lo!al (pa titioned)

*ou !annot ! eate glo%al inde/es fo hash pa titions o su%pa titions5

Copy rights are reserved.

138

'/0
SQL-

! eate inde/ studMinde/ on student(sno) glo%al.

Si$ila to ta%le pa titions# it is possi%le to $ove the$ f o$ one devi!e to anothe 5 )ut unli&e ta%le pa titions# $ove$ent of inde/ pa titions eUui es individual e!onst u!tion of the inde/ o ea!h pa tition (onl7 in the !ase of glo%al inde/)5 '/0
SQL-

alte inde/ studMind e%uild pa tition p1

Inde/ pa titions !annot %e d opped $anuall75 The7 a e d opped i$pli!itl7 when the data the7 pa titioned ta%le5 efe to is d opped f o$ the

MONITORIN8 US' OB IND'+'S On!e 7ou tu ned on the $onito ing the use of inde/es# then we !an !he!& whethe the ta%le is hitting the inde/ o not5 To $onito the use of inde/ use the follwing s7nta/5 S7nta/0 alte inde/ inde(_name $onito ing usage. then !he!& fo the details in 9`O)>'CTMUS(8' view5 If 7ou want to stop $onito ing use the following5 S7nta/0 alte inde/ inde(_name no$onito ing usage. D(T( MOD'L

Copy rights are reserved.

139
(LLMIND'+'S D)(MIND'+'S US'RMIND'+'S (LLMIND"COLUMNS D)("INDMCOLUMNS US'RMINDMCOLUMNS (LLMA(RTMIND'+'S D)(MA(RTMIND'+'S US'RMA(RTMIND'+'S 9`O)>'CTMUS(8'

Copy rights are reserved.

140

SQLEALUS COMMN(NDS
These !o$$ands does not eUui e state$ent te $inato and appli!a%le to the sessions # those will %e auto$ati!all7 !lea ed when session was !losed5 )R'(F This will %e used to % ea&up the data depending on the g ouping5 S7nta/0 ) ea& o % e Non ,!olu$n_name- on epo tO COMAUT' This will %e used to pe fo $ g oup fun!tions on the data5 S7nta/0 Co$pute o !o$p Ng oupMfun!tion of column_name on brea in"_column_name o epo tO TTITL' This will give the top title fo 7ou S7nta/0 Ttitle o ttit Nleft X !ente X ightO title_name s&ip n ot)er_c)aracter! Ttitle o ttit Non o offO )TITL' epo t5 *ou !an on o off the ttitle5

Copy rights are reserved.

141
This will give the %otto$ title fo 7ou S7nta/0 )title o %tit Nleft X !ente X ightO title_name s&ip n ot)er_c)aracter! )title o %tit Non o offO '/0
SQLSQLSQLSQLSQLSQL-

epo t5 *ou !an on o off the %title5

% e on deptno s&ip 2 on epo t !o$p su$ of sal on deptno !o$p su$ of sal on epo t ttitle !ente ='MALO*'' D'T(ILS= s&ip2 !ente =""""""""""""""""= %title !ente =EE T:(NFQ EE= sele!t E f o$ e$p o de %7 deptno.

Output0 'MALO*'' D'T(ILS """"""""""""""""""""""" 'MANO VVW1 VW4S VS4@ 'N(M' CL(RF FIN8 >O) M(N(8'R AR'SID'NT VVW1 M8R VW4S :IR'D(T' 3S">UN"W2 2V"NO9"W2 14">(N"W1 S(L 1@C3 C333 2433 """""""""" WVC3 V4DS VWVD VS31 VVWW VCDD SMIT: BORD CL'RF (N(L*ST VS31 VVWW VCDD VCDD VW4S 2V"D'C"W3 14"M(*"WV 34"D'C"W2 2S"(AR"WV 31"(AR"W2 W33 2233 4333 4333 1SVC """""""""" 23WVC V@SS (LL'N S(L'SM(N VDSW 13"B')"W2 2D33 433 EEEEEEEEEE su$ 43 EEEEEEEEEE su$ 13 COMM D'ATNO 23

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

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

MILL'R CL'RF

(D(MS CL'RF SCOTT (N(L*ST >ON'S M(N(8'R

Copy rights are reserved.

142
VDSW VDC@ VS33 VW@@ VC12 )L(F' >(M'S 6(RD M(N(8'R CL'RF S(L'SM(N VW4S VDSW VDSW VDSW VDSW 32"M(*"W2 1W"S'A"W2 34"D'C"W2 3W"S'A"W2 11"B')"W2 1WC3 21C3 SC3 2C33 21C3 """""""""" S@33 """""""""" su$ EE T:(NFQ EE CL'(R This will !lea the e/isting %uffe s o % ea& o !o$putations o !olu$ns fo $atting5 S7nta/0 Clea o !le %uffe X % e X !o$p X !ol. '/0
SQL-

M(RTIN S(L'SM(N TURN'R S(L'SM(N

2@33 3 C33 EEEEEEEEEE su$

1S31C

!lea %uffe )uffe !lea ed !lea % e ) ea&s !lea ed !lea !o$p Co$putes !lea ed !lea !ol Colu$ns !lea ed

SQL-

SQL-

SQL-

C:(N8' This will %e used to epla!e an7 st ings in S7nta/0 Change o !?old_!trin"?new_!trin" state$ents5

SQL

Copy rights are reserved.

143

If the old_!trin" epeats $an7 ti$es then new_!trin" epla!es the fi st st ing onl75 '/0
SQL-

sele!t E f o$ det. E

sele!t E f o$ det 'RROR at line 20 OR("33S@10 ta%le o view does not e/ist
SQL-

!?det?dept ? LOC

2E sele!t E f o$ dept
SQL-

D'ATNO DN(M' 23 13 43 @3 COLUMN

"""""""""" """""""""""""""" """"""""""" (CCOUNTIN8 N'6 *ORF R'S'(RC: S(L'S OA'R(TIONS (LL(S C:IC(8O )OSTON

This will %e used to in! ease o de! ease the width of the ta%le !olu$ns5 S7nta/0 Colu$n o !ol ,column_name- fo $at ,num_'ormatXte(t_'ormat'/0
SQLSQL-

!ol deptno fo $at SSS !ol dna$e fo $at a23

S(9' This will %e used to save 7ou !u ent SQL state$ent as SQL S! ipt file5

Copy rights are reserved.

144
S7nta/0 Save o sav ,'ile_name-5Ne/tensionO epla!e o ep

If 7ou want to save the filena$e with e/isting filena$e the 7ou have to use epla!e option5 )7 default it will ta&e !.l as the e/tension5 '/0
SQL-

save ss C eated file ss5sUl save ss epla!e 6 ote file ss5sUl

SQL-

'+'CUT' This will %e used to e/e!ute sto ed su%p og a$s o pa!&aged su%p og a$s5 S7nta/0 '/e!ute o e/e! ,!ubpro"ram_name'/0
SQL-

e/e! sa$pleMp o!

SAOOL This will e!o d the data when 7ou spool on# upto when 7ou sa7 spool off5 )7 default it will give l!t as e/tension5 S7nta/0 Spool on X off X out X ,'ile_name-5N'/tensionO '/0
SQLSQL-

spool on sele!t E f o$ dept.

Copy rights are reserved.

145
D'ATNO DN(M' """"""""" """""""""""""" 23 13 43 @3 spool off ed on5lst
SQL-

LOC """""""""" D(LL(S C:IC(8O )OSTON

(CCOUNTIN8 N'6 *ORF R'S'(RC: S(L'S OA'R(TIONS

SQLSQL-

sele!t E f o$ dept. LOC """""""""" D(LL(S C:IC(8O )OSTON

D'ATNO DN(M' """"""""" """""""""""""" 23 13 43 @3


SQL-

(CCOUNTIN8 N'6 *ORF R'S'(RC: S(L'S OA'R(TIONS spool off

LIST This will give the !u ent S7nta/0 List o li N!tart_line_numberO Nend_line_numberO '/0
SQL-

SQL

state$ent5

sele!t

1 E 4 f o$ @ dept.
SQL-

list

2 sele!t 1 E

Copy rights are reserved.

146
4 f o$ @E dept
SQL-

list 2 list 4

2E sele!t
SQL-

4E f o$
SQL-

list 2 4

2 sele!t 1 E 4E f o$ INAUT This will inse t the new line to the !u ent S7nta/0 Input o in ,!trin"'/0
SQLSQL-

SQL

state$ent5

sele!t E list input f o$ dept list

2E sele!t E
SQLSQL-

2 sele!t E 1E f o$ dept (AA'ND This will adds a new st ing to the e/isting st ing in the S7nta/0 (ppend o app ,!trin"'/0 state$ent without an7 spa!e5

SQL

Copy rights are reserved.

147
SQLSQL-

sele!t E list append f o$ dept list

2E sele!t E
SQL-

2E sele!t E f o$ dept
SQL-

2E sele!t E f o$ dept D'L'T' This will delete the !u ent S7nta/0 Delete o del ,!tart_line_number- N,end_line_number-O '/0
SQL-

SQL

state$ent lines5

sele!t

1 E 4 f o$ @ dept C whe e D deptno V -23.


SQL-

list

2 sele!t 1 E 4 f o$ @ dept C whe e D deptno VE -23


SQLSQL-

del 2 list

2 E 1 f o$

Copy rights are reserved.

148
4 dept @ whe e C deptno DE -23
SQLSQL-

del 1 list

2 E 1 dept 4 whe e @ deptno CE -23


SQLSQL-

del 1 @ list

2 E 1E -23
SQLSQL-

del list

2 E 9(RI()L' This will %e used to de!la e a va ia%le5 S7nta/0 9a ia%le o va ,variable_name- ,variable_type'/0
SQLSQL-

va

deptMna$e va !ha (2C)

sele!t dna$e into deptMna$e f o$ dept whe e deptno H 23.

ARINT This will %e used to p int the output of the va ia%les that will %e de!la ed at S7nta/0 level5

SQL

Copy rights are reserved.

149
A int ,variable_name'/0
SQL-

p int deptMna$e D'ATMN(M' """""""""""""" (CCOUNTIN8

ST(RT This will %e used to e/e!ute S7nta/0 sta t ,filena$e_name-5sUl '/0


SQLSQLSQL

s! ipts5

sta t ss5sUl ass5sUl "" this will e/e!ute sUl s! ipt files onl75

:OST This will %e used to inte a!t with the S7nta/0 :ost NoperationO '/0
SQLSQLOS

level f o$

SQL5

host host di

S:O6 Using this# 7ou !an see seve al !o$$ands that use the set !o$$and and status5 S7nta/0

Copy rights are reserved.

150
Show all X ,!et_command'/0
SQL-

show all

appinfo is OBB and set to bSQLEAlusb a a7siTe 2C auto!o$$it OBB autop int OBB auto e!ove 7 OBB autot a!e OBB %lo!&te $inato b5b (he/ 1e) %title OBB and is the fi st few !ha a!te s of the ne/t S'L'CT state$ent !$dsep OBB !olsep b b !o$pati%ilit7 ve sion N(TI9' !on!at b5b (he/ 1e) !op7!o$$it 3 COA*T*A'C:'CF is ON define b<b (he/ 1D) des! i%e D'AT: 2 LIN'NUM OBB IND'NT ON e!ho OBB editfile bafiedt5%ufb e$%edded OBB es!ape OBB B''D)(CF ON fo D o $o e ows flagge OBB flush ON
SQL-

sho ve if7

ve if7 OBB RUN This will uns the !o$$and in the %uffe 5 S7nta/0

Copy rights are reserved.

151
Run X ? '/0
SQLSQL-

un ?

STOR' This will save all the set !o$$and statuses in a file5 S7nta/0 Sto e set ,filena$e-5Ne/tensionO N! eateO X N epla!eO X NappendO '/0
SQL-

sto e set $7Msettings5s!$d sto e set $7Msettings5!$d epla!e sto e set $7Msettings5!$d append

C eated file $7Msettings5s!$d


SQL-

6 ote file $7Msettings5!$d


SQL-

(ppended file to $7Msettings5!$d BOLDM(BT'R This will fold the !olu$ns one afte the othe 5 S7nta/0 Colu$n ,column_name- foldMafte Nno_o'_line!O '/0
SQLSQLSQLSQLSQL-

!ol deptno foldMafte 2 !ol dna$e foldMafte 2 !ol lo! foldMafte 2 set heading off sele!t E f o$ dept. 23 (CCOUNTIN8

Copy rights are reserved.

152
N'6 *ORF 13 R'S'(RC: D(LL(S 43 S(L'S C:IC(8O @3 OA'R(TIONS )OSTON BOLDM)'BOR' This will fold the !olu$ns one %efo e the othe 5 S7nta/0 Colu$n ,column_name- foldM%efo e Nno_o'_line!O D'BIN' This will give the list of all the va ia%les !u entl7 defined5 S7nta/0 Define Nva ia%leMna$eO '/0
SQL-

define H b2D"M(*"3Vb (C:(R) H bSCOTTb (C:(R) H bb (C:(R) H bNotepadb (C:(R) H bO a!le Data%ase 23g 'nte p ise 'dition Release

D'BIN' MD(T' D'BIN' MUS'R D'BIN' MARI9IL'8' D'BIN' M'DITOR

D'BIN' MCONN'CTMID'NTIBI'R H bo a!leb (C:(R)

D'BIN' MSQLALUSMR'L'(S' H b2332333133b (C:(R) D'BIN' MOM9'RSION

Copy rights are reserved.

153
2352535153 \ A odu!tion 6ith the Aa titioning# OL(A and Data Mining optionsb (C:(R) D'BIN' MOMR'L'(S' H b2332333133b (C:(R)

S'T COMM(NDS
These !o$$ands does not eUui e state$ent te $inato and appli!a%le to the sessions # those will %e auto$ati!all7 !lea ed when session was !losed5 LIN'SIZ' This will %e used to set the linesiTe5 Default linesiTe is W35 S7nta/0 Set linesiTe ,value'/0
SQL-

set linesiTe 233

A(8'SIZ' This will %e used to set the pagesiTe5 Default pagesiTe is 2@5 S7nta/0 Set pagesiTe ,value'/0
SQL-

set pagesiTe 43

D'SCRI)' This will %e used to see the o%Je!t;s st u!tu e5 S7nta/0 Des! i%e o des! ,ob#ect_name-

Copy rights are reserved.

154

'/0
SQL-

des! dept Null[ T7pe

Na$e D'ATNO DN(M' LOC A(US'

""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" """"""""""""""""""""" NOT NULL NUM)'R(1) 9(RC:(R1(2@) 9(RC:(R1(24)

6hen the displa7ed data !ontains hund eds o thousands of lines# when 7ou sele!t it then it will auto$ati!all7 s! olls and displa7s the last page data5 To p event this 7ou !an use this pause option5 )7 using this it will displa7 the data !o espoinding to the pagesiTe with a % ea& whi!h will !ontinue %7 hitting the etu n &e75 )7 default this will %e off5 S7nta/0 Set pause on X off '/0
SQL-

set pause on

B''D)(CF This will give the info $ation C ows5 S7nta/0 Set feed%a!& ,value'/0
SQLSQL-

ega ding how$an7

ows 7ou sele!ted the o%Je!t5 )7

default the feed%a!& $essage will %e displa7ed# onl7 when the o%Je!t !ontains $o e than

set feed%a!& @ sele!t E f o$ dept.

Copy rights are reserved.

155

D'ATNO 23 13 43 @3

DN(M' (CCOUNTIN8 R'S'(RC: S(L'S OA'R(TIONS

LOC N'6 *ORF D(LL(S C:IC(8O )OSTON

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

@ ows sele!ted5 :'(DIN8 If 7ou want to displa7 data without headings# then 7ou !an a!hieve with this5 )7 default heading is on5 S7nta/0 Set heading on X off '/0
SQLSQL-

set heading off sele!t E f o$ dept. 23 13 43 @3 (CCOUNTIN8 R'S'(RC: S(L'S OA'R(TIONS N'6 *ORF D(LL(S C:IC(8O )OSTON

S'R9'ROUTAUT This will %e used to displa7 the output of the AL?SQL p og a$s5 )7 default this will %e off5 S7nta/0 Set se ve output on X off '/0

Copy rights are reserved.

156
SQL-

set se ve output on

TIM' This will %e used to displa7 the ti$e5 )7 default this will %e off5 S7nta/0 Set ti$e on X off '/0
SQL-

set ti$e on
SQL-

2S0CD044 TIMIN8

This will give the ti$e ta&en to e/e!ute the !u ent off5 S7nta/0 Set ti$ing on X off '/0
SQLSQL-

SQL

state$ent5 )7 default this will %e

set ti$ing on sele!t E f o$ dept. DN(M' (CCOUNTIN8 R'S'(RC: S(L'S OA'R(TIONS LOC N'6 *ORF D(LL(S C:IC(8O )OSTON

D'ATNO 23 13 43 @3

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

'lapsed0 3303303353D

Copy rights are reserved.

157

SQLAROMAT This will %e used to !hange the S7nta/0 Set sUlp o$pt ,prompt'/0
SQL-

SQL

p o$pt5

set sUlp o$pt =OR(CL'-=

OR(CL'-

SQLC(S' This will %e used to !hange the !ase of the S7nta/0 Set sUl!ase uppe X $i/ed X lowe '/0
SQL-

SQL

state$ents5 )7 default the !ase is $i/ed5

set sUl!ase uppe

SQLT'RMIN(TOR This will %e used to !hange the te $inato of the te $inato is .5 S7nta/0 Set sUlte $inato ,termination_c)aracter'/0
SQLSQL-

SQL

state$ents5 )7 default the

set sUlte $inato 0 sele!t E f o$ dept0

D'BIN'

Copy rights are reserved.

158
)7 default if the < !ha a!te finds then it will t eat as %ind va ia%le and as& fo the input5 Suppose 7ou want to t eat it as a no $al !ha a!te while inse ting data# then 7ou !an p event this %7 using the define option5 )7 default this will %e on

S7nta/0 Set define on X off '/0


SQL-inse

t into dept values(C3#=R<D=#=:*D=).

'nte value fo d0 old new 20 inse t into dept values(C3#=R<D=#=:*D=) 20 INS'RT INTO D'AT 9(LU'S(C3#=R=#=:*D=) set define off t into dept values(C3#=R<D=#=:*D=). "" he e it won;t as& fo value

SQL-

SQL-inse

N'6A(8' This will shows how $an7 %lan& lines will %e left %efo e the epo t5 )7 default it will leave one %lan& line5 S7nta/0 Set newpage ,value'/0
SQL-

set newpage 23 newpage does not p odu!e Te o %lan& lines instead it swit!hes to a

The Te o value fo

spe!ial p ope t7 whi!h p odu!es a top"of"fo $ !ha a!te (he/ 24) Just %efo e the date on ea!h page5 Most $ode n p inte s espond to this %7 $oving i$$ediatel7 to the top of the ne/t page# whe e the p iting of the epo t will %egin5 :'(DS'A

Copy rights are reserved.

159
This allow 7ou to indi!ate whe e 7ou want to % ea& a page title o a !olu$n heading that uns longe than one line5 The default heading sepa ato is ve ti!al %a (X)5 S7nta/0 Set headsep ,!eparation_c)ar'/0
SQL-

sele!t E f o$ dept. DN(M' (CCOUNTIN8 R'S'(RC: S(L'S OA'R(TIONS LOC N'6 *ORF D(LL(S C:IC(8O )OSTON

D'ATNO 23 13 43 @3

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

SQLSQLSQL-

set headsetp I !ol dna$e heading =D'A(RTM'NT I N(M'= ? D'A(RTM'NT

D'ATNO 23

N(M' (CCOUNTIN8

LOC """""""""" N'6 *ORF D(LL(S C:IC(8O )OSTON

"""""""""" """"""""""""""""" 13 R'S'(RC: 43 S(L'S @3 OA'R(TIONS 'C:O 6hen using a %ind va ia%le# the off5 S7nta/0 Set e!ho on X off

SQL

state$ent is $aintained %7 e!ho5 )7 default this is

Copy rights are reserved.

160
9'RIB* 6hen using a %ind va ia%le# the old and new state$ents will %e $aintained %7 ve if75 )7 default this is on5 S7nta/0 Set ve if7 on X off '/0
SQL-

sele!t E f o$ dept whe e deptno H <dno. 20 sele!t E f o$ dept whe e deptno H <dno 20 sele!t E f o$ dept whe e deptno H 23 DN(M' LOC

'nte value fo dno0 23 old new

D'ATNO 23

"""""""""" """""""""""""""" """"""""""" (CCOUNTIN8 N'6 *ORF

SQLSQL-

set ve if7 off sele!t E f o$ dept whe e deptno H <dno.

'nte value fo dno0 13 D'ATNO DN(M' 13 ANO This will give displa7s the page nu$%e s5 )7 default the value would %e Te o5 '/0
SQLSQLSQL-

LOC D(LL(S

"""""""""" """"""""""""" """"""""""" R'S'(RC:

!ol hi edate newMvalue /toda7 nop int fo $at a2 t un! ttitle left /toda7 ight =page= sUl5pno sele!t E f o$ e$p whe e deptno H 23.

Copy rights are reserved.

161
3S">UN"W2 'MANO 'N(M' >O) M(N(8'R AR'SID'NT CL'RF VVW1 M8R VW4S S(L COMM 1@C3 C333 2433 page D'ATNO 23 23 23 2

"""""""""" """""""""" """"""""""""""" """"""""" """"" """""""""" """""""""" VVW1 CL(RF VW4S FIN8 VS4@ MILL'R

In the a%ove nop int tells the


SQL

SQLALUS

not to displa7 this !olu$n when it p ints the esults of efo $atted %7
TOMC:(R

state$ent5 Dates that have %een

get a default width of

a%out 233 !ha a!te s5 )7 !hanging the fo $at to a2 t un!# 7ou $ini$iTe this effe!t5 N'6M9(LU' inse ts !ontents of the !olu$n et ieved %7 the SQL state$ent into a va ia%le !alled /toda75

Copy rights are reserved.

162

SA'CI(L BIL'S
LO8IN5sUl If 7ou would li&e to define 7ou own envi on$ental settings# put all the eUui ed
SQLALUS

SQLALUS

!o$$ands in a file na$ed login5sUl5 This is a spe!ial filena$e that

alwa7s loo&s
SQLALUS# SQLALUS

fo wheneve it sta ts up5 If it finds login5sUl# it e/e!utes an7 !o$$ands in it as if 7ou had ente ed then %7 hand5 *ou !an put an7 !o$$and in login5sUl that 7ou !an use in in!luding
SQLALUS SQL-

!o$$ands and p o$pt5

SQL

state$ents5 (ll ot the$ e/e!uted %efo e

gives 7ou the 8LO8IN5sUl

This is used in the sa$e wa7s as use s of a data%ase5

LO8IN5sUl

%ut to esta%lish default

SQLALUS

settings fo all

Copy rights are reserved.

163

IMAORT(NT QU'RI'S
2) To find the nth ow of a ta%le Sele!t Ef o$ e$p whe e owid H (sele!t $a/( owid) f o$ e$p whe e ownu$ ,H @). O
SQL-

SQL-

Sele!t Ef o$ e$p whe e ownu$ ,H @ $inus sele!t Ef o$ e$p whe e ownu$ ,H 4.

1) To find dupli!ate ows 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 # Jo%# hi edate# !o$$# deptno# sal). O
SQL-

SQL-

Sele!t e$pno#ena$e#sal#Jo%#hi edate#!o$$ # !ount(E) f o$ e$p g oup %7 e$pno#ena$e#sal#Jo%#hi edate#!o$$ having !ount(E) -H2.

4) To delete dupli!ate ows Delete e$p whe e owid in (sele!t $a/( owid) f o$ e$p g oup %7 e$pno#ena$e#$g #Jo%#hi edate#sal#!o$$#deptno). @) To find the !ount of dupli!ate ows Sele!t ena$e# !ount(E) f o$ e$p g oup %7 ena$e having !ount(E) -H 2.

SQL-

SQL-

C) :ow to displa7 alte native ows in a ta%le[ sele!t Ef o$ e$p whe e ( owid#3) in (sele!t owid#$od( ownu$#1) f o$ e$p).

SQL-

Copy rights are reserved.

164

D) 8etting e$plo7ee details of ea!h depa t$ent who is d awing $a/i$u$ sal[ sele!t Ef o$ e$p whe e (deptno#sal) in ( sele!t deptno#$a/(sal) f o$ e$p g oup %7 deptno). V) :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[ Sele!t deptno#!ount(E) f o$ e$p g oup %7 deptno having !ount(E) -1C33.

SQL-

SQL-

W) To eset the ti$e to the %eginning of the da7 Sele!t toM!ha (t un!(s7sdate)#;dd"$on"7777 hh0$i0ss a$;) f o$ dual.

SQL-

S) To find nth $a/i$u$ sal 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 ownu$ ,H C).

SQL-

Copy rights are reserved.

165

INTRODUCTION
C:(R(CT'RSTICS :ighl7 st u!tu ed# eada%le and a!!essi%le language5 Standa d and A ota%le language5 '$%edded language5 I$p oved e/e!ution autho it75

23g B'(TUR'S /ptimi,ed compiler To !hange the opti$iTe pa a$ete 3 2 1 " " " settings fo the enti e data%ase# set the data%ase

ALSQLMOATIMIZ'ML'9'L5

9alid settings a e as follows

No opti$iTation Mode ate opti$iTation (gg essive opti$iTation

These settings a e also $odifia%le fo the !u ent session5


SQL-

alte session set plsUlMopti$TeMlevelH1. etains opti$iTe settings on a $odule"%7"$odule %asis5 6hen 7ou
R'US' S'TTIN8S5

O a!le

e!o$pile a pa ti!ula

$odule with nondefault settings# the settings will sti!&

allowing 7ou to e!o$pile late on using

SQLSQL-

(lte p o!edu e p o! !o$pile plsUlMopti$iTeMlevelH2. (lte p o!edu e p o! !o$pile euse settings.

Compile0time warnin"!.

Copy rights are reserved.

166

Sta ting with o a!le data%ase 23g elease 2 7ou !an ena%le additional !o$pile"ti$e wa nings to help $a&e 7ou p og a$s $o e o%ust5 The !o$pile !an dete!t potential unti$e p o%le$s with 7ou !ode# su!h as identif7ing lines of !ode that will neve %e un5 This p o!ess# also &nown as lint c)ec in"5 To ena%le these wa nings fo the enti e data%ase# set the data%ase pa a$ete
ALSQLM6(RNIN8S5

These settings a e also $odifia%le fo the !u ent session5

SQL-

alte session set plsUlMwa nings H Kena%le0all;.


D)MSM6(RNIN85

The a%ove !an %e a!hieved using the %uilt"in pa!&age Conditional compilation.

Conditional !o$pilation allows the !o$pile to allow to !o$pile sele!ted pa ts of a p og a$ %ased on !onditions 7ou p ovide with the `IB di e!tive5

$upport 'or non0!e.uential collection! in 1/2344.


5mproved datatype !upport. 6ac trace an e(ception to it! line number. 6hen handling an e o # how !an 7ou find the line nu$%e on whi!h the e o was o iginall7 aised[ In ea lie elease# the onl7 wa7 to do this was allow 7ou e/!eption to go unhandled

and then view the full e o t a!e sta!&5 Now 7ou !an !all fun!tion to o%tain that sta!&

D)MSMUTILIT*5BORM(TM'RRORM)(CFTR(C'

and $anipulate it p og a$$ati!all7 within 7ou p og a$5 $et operator! 'or ne!ted table!. $upport 'or re"ular e(pre!!ion!.

Copy rights are reserved.

167
O a!le data%ase 23g suppo ts the use of egula e/p essions inside fou new %uilt"in fun!tions5
R'8'+AMLIF' R'8'+AMINSTR R'8'+AMSU)STR R'8'+AMR'AL(C' AL?SQL

!ode via

7ro"rammer0de'ined .uotin" mec)ani!m. Sta ting with o a!le data%ase 23g $e!hanis$ fo st ing lite als in %oth elease 2# 7ou !an define 7ou
SQL

own Uuoting

and

AL?SQL5

Use the !ha a!te s U;(U followed %7 a single Uuote) to note the p og a$$e " defined delie$ete fo 7ou st ing lite al5 '/0
D'CL(R'

v va !ha (23) 0H =!o$pute =.


)'8IN

d%$sMoutput5putMline(U=Ev H E= XX v). d%$sMoutput5putMline(U=`v H `= XX v).


'ND.

Output0 v H !o$pute v H !o$pute 8any new built0in pac a"e!. D)MSMSC:'DUL'R Rep esents a $aJo update to
D)MSM>O)5 D)MSMSC:'DUL'R

p ovides $u!h i$p oved

fun!tionalit7 fo s!heduling and e/e!uting Jo%s defined via sto ed p o!edu es5 D)MSMCR*ATO Offe s the a%ilit7 to en! 7pt and de! 7pt !o$$on o a!le datat7pe# in!luding
)LO)s# R(6s#

and

CLO)s5

It also p ovides glo%aliTation suppo t fo en! 7pting data a! oss

diffe ent !ha a!te sets5

Copy rights are reserved.

168

D)MSMMONITOR A ovides an
(AI

to !ont ol additional t a!ing and statisti!s gathe ing of sessions5

D)MSM6(RNIN8 A ovides an as e o s5 ST(ND(RD A(CF(8' O a!le has defined in this spe!ial pa!&age5 O a!le defines Uuite a few identifie s in this pa!&age# in!luding %uilt"in e/!eptions# fun!tions and su%t7pes5 *ou !an efe en!e the %uilt"in fo $ %7 p efi/ing it with The %asi! unit in an7 p og a$ is %lo!&5 (ll
ST(ND(RD5 (AI

into the

AL?SQL

!o$pile wa nings $odule# allowing 7ou to ead and

!hange settings that !ont ol whi!h wa nings a e supp essed# displa7ed# o t eated

AL?SQL

AL?SQL

p og a$s a e !o$posed of %lo!&s

whi!h !an o!!u seUuentiall7 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

Copy rights are reserved.

169
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 '/0


)'8IN

D%$sMoutput5putMline(KM7 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 ,,$7M%loo!&-)'8IN

D%$sMoutput5putMline(KM7 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%Je!ts# as pa t of pa!&age o as $ethods of an o%Je!t t7pe5
TRI88'RS

T igge s !onsists of a data%ase5 N'ST'D )LOCFS

AL?SQL

%lo!& that is asso!iated with an event that o!!u

in the

Copy rights are reserved.

170

( %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 o%Je!ts# su!h as va ia%les# !u so s# t7pes and signs# unde s!o es# and pound signs onl75

AL?SQL

su%p og a$s5 Identifie s !onsists of a lette # optionall7 followed %7 an7 seUuen!e of !ha a!te s# in!luding lette s# nu$%e s# dolla The $a/i$u$ length fo an identifie is 43 !ha a!te s5 QUOT'D ID'NTIBI'RS If 7ou want to $a&e an identifie !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 Uuotation $a &s5 '/0
D'CL(R'

bab nu$%e 0H C. b(b nu$%e 0H D.


)'8IN

d%$sMoutput5putMline(=a H = XX a). d%$sMoutput5putMline(=( H = XX ().


'ND.

Output0 aHD (HD COMM'NTS Co$$ents i$p ove igno ed %7 the


AL?SQL

eada%ilit7 and $a&e 7ou

p og a$ $o e unde standa%le5 The7 a e

!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

Copy rights are reserved.

171

( 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%$sMoutput5putMline(Khello;).
'ND. MULTILIN' COMM'NTS

"" sa$ple p og a$

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

D%$sMoutput5putMline(Khello;).
'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 0H C. ! nu$%e default D. CONST(NT D'CL'R(TIONS To de!la e a !onstant# 7ou in!lude the value5 '/0
D'CL(R'

CONST(NT

&e7wo d# and 7ou $ust suppl7 a default

% !onstant nu$%e 0H C. ! !onstant nu$%e default D.

Copy rights are reserved.

172
NOT NULL CL(US' *ou !an also spe!if7 that the va ia%le $ust %e not null5 '/0
D'CL(R'

% !onstant nu$%e not null0H C. ! nu$%e not null default D. (NC:OR'D D'CL'R(TIONS
AL?SQL

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 LT*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 dept5deptnoLt7pe. Su%t7pe tMnu$%e is nu$%e . a tMnu$%e . Su%t7pe tMsno is student5snoLt7pe. 9Msno tMsno.
R'CORD (NC:ORIN8

Use the LRO6T*A' att i%ute to define 7ou '/0 cD'CL(R' 9Mdept deptL owt7pe.
)'N'BITS OB (NC:OR'D D'CL(R(TIONS

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

Copy rights are reserved.

173
S7n!h oniTation with data%ase !olu$ns5 No $aliTation of lo!al va ia%les5

ARO8R(MM'R"D'BIN'D T*A'S 6ith the SU)T*A' state$ent# allows 7ou to define 7ou own su%t7pes o aliases of

AL?SQL

p edefined datat7pes# so$eti$es efe ed to as a%st a!t datat7pes5 The e a e two &inds of su%t7pes5 Const ained Un!onst ained

CONSTR(IN'D SU)T*A'

( su%t7pe that est i!ts o !onst ains the values no $all7 allowd %7 the datat7pe itself5 '/0 Su%t7pe positive is %ina 7Mintege ange 25512@V@W4D@V.

In the a%ove de!la ation a va ia%le that is de!la ed as positive !an sto e onl7 ingege g eate than Te o even though %ina 7Mintege UNCONSTR(IN'D SU)T*A' ( su%t7pe that does not est i!t the values of the o iginal datat7pe in va ia%les de!la ed with the su%t7pe5 '/0 Su%t7pe float is nu$%e . D(T(T*A' CON9'RSIONS !an handle !onve sions %etween diffe ent fa$ilies a$ong the datat7pes5 anges f o$ "12@V@W4D@V55G12@V@W4D@V5

AL?SQL

Conve sion !an %e done in two wa7s5 '/pli!it !onve sion I$pli!it !onve sion

Copy rights are reserved.

174

'+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$eH=(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* 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
AL?SQL

%lo!&5 6hen a va ia%le goes out of s!ope# the sto e the va ia%le5

engine will f ee the $e$o 7 used to

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 Uualif7 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

"" s!ope of a

Copy rights are reserved.

175
"""""""" D'CL(R'

% 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 ,,$7M%lo!&-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 "" nu$%e t7pe % is availa%le using ,,$7M%lo!&--5%
'ND.

""""""
'ND.

AL?SQL CONTROL STRUCTUR'S

Copy rights are reserved.

176

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' !tatement!; 'lsif ,condition1- then $e.uence o' !tatement!; RR 'lse $e.uence o' !tatement!; 'nd if. '/0
D'CL(R'

dno nu$%e (1).


)'8IN

sele!t deptno into dno f o$ dept whe e dna$e H =(CCOUNTIN8=. if dno H 23 then d%$sMoutput5putMline(=Lo!ation is elsif dno H 13 then d%$sMoutput5putMline(=Lo!ation is elsif dno H 43 then
D(LL(S=). N'6 *ORF=).

Copy rights are reserved.

177
d%$sMoutput5putMline(=Lo!ation is else d%$sMoutput5putMline(=Lo!ation is end if.
'ND. )OSTON=). C:IC(8O=).

Output0 Lo!ation is C(S' S7nta/0 Case te!t0variable 6hen value1 then !e.uence o' !tatement!; 6hen value2 then !e.uence o' !tatement!; RR 6hen valuen then !e.uence o' !tatement!; 'lse !e.uence o' !tatement!; 'nd !ase. '/0
D'CL(R' N'6 *ORF

dno nu$%e (1).


)'8IN

sele!t deptno into dno f o$ dept whe e dna$e H =(CCOUNTIN8=. !ase dno when 23 then d%$sMoutput5putMline(=Lo!ation is when 13 then d%$sMoutput5putMline(=Lo!ation is when 43 then d%$sMoutput5putMline(=Lo!ation is else d%$sMoutput5putMline(=Lo!ation is end !ase.
)OSTON=). C:IC(8O=). D(LL(S=). N'6 *ORF=).

Copy rights are reserved.

178
'ND.

Output0 Lo!ation is
N'6 *ORF

C(S' 6IT:OUT 'LS' S7nta/0 Case te!t0variable 6hen value1 then !e.uence o' !tatement!; 6hen value2 then !e.uence o' !tatement!; RR 6hen valuen then !e.uence o' !tatement!; 'nd !ase. '/0
D'CL(R'

dno nu$%e (1).


)'8IN

sele!t deptno into dno f o$ dept whe e dna$e H =(CCOUNTIN8=. !ase dno when 23 then d%$sMoutput5putMline(=Lo!ation is when 13 then d%$sMoutput5putMline(=Lo!ation is when 43 then d%$sMoutput5putMline(=Lo!ation is when @3 then d%$sMoutput5putMline(=Lo!ation is end !ase.
'ND. )OSTON=). C:IC(8O=). D(LL(S=). N'6 *ORF=).

Output0 Lo!ation is
N'6 *ORF

Copy rights are reserved.

179

L()'L'D C(S' S7nta/0 ,,label-Case te!t0variable 6hen value1 then !e.uence o' !tatement!; 6hen value2 then !e.uence o' !tatement!; RR 6hen valuen then !e.uence o' !tatement!; 'nd !ase. '/0
D'CL(R'

dno nu$%e (1).


)'8IN

sele!t deptno into dno f o$ dept whe e dna$e H =(CCOUNTIN8=. ,,$7M!ase-!ase dno when 23 then d%$sMoutput5putMline(=Lo!ation is when 13 then d%$sMoutput5putMline(=Lo!ation is when 43 then d%$sMoutput5putMline(=Lo!ation is when @3 then d%$sMoutput5putMline(=Lo!ation is end !ase $7M!ase.
'ND. )OSTON=). C:IC(8O=). D(LL(S=). N'6 *ORF=).

Output0 Lo!ation is S'(RC:'D C(S' S7nta/0


N'6 *ORF

Copy rights are reserved.

180
Case 6hen ,condition1- then !e.uence o' !tatement!; 6hen ,condition2- then !e.uence o' !tatement!; RR 6hen ,conditionn- then !e.uence o' !tatement!; 'nd !ase. '/0
D'CL(R'

dno nu$%e (1).


)'8IN

sele!t deptno into dno f o$ dept whe e dna$e H =(CCOUNTIN8=. !ase dno when dno H 23 then d%$sMoutput5putMline(=Lo!ation is when dno H 13 then d%$sMoutput5putMline(=Lo!ation is when dno H 43 then d%$sMoutput5putMline(=Lo!ation is when dno H @3 then d%$sMoutput5putMline(=Lo!ation is end !ase.
'ND. )OSTON=). C:IC(8O=). D(LL(S=). N'6 *ORF=).

Output0 Lo!ation is SIMAL' LOOA S7nta/0 Loop $e.uence o' !tatement!; '/it when ,condition-. 'nd loop. In the s7nta/ e/it when ,condition- is eUuivalent to
N'6 *ORF

Copy rights are reserved.

181
If ,condition- then '/it. 'nd if. '/0
D'CL(R'

i nu$%e 0H 2.
)'8IN

loop d%$sMoutput5putMline(=i H = XX i). i 0H i G 2. e/it when i - C. end loop.


'ND.

Output0 iH2 iH1 iH4 iH@ iHC 6:IL' LOOA S7nta/0 6hile ,!ondition- loop $e.uence o' !tatement!; 'nd loop. '/0
D'CL(R'

i nu$%e 0H 2.
)'8IN

6hile i ,H C loop d%$sMoutput5putMline(=i H = XX i). i 0H i G 2.

Copy rights are reserved.

182
end loop.
'ND.

Output0 iH2 iH1 iH4 iH@ iHC BOR LOOA S7nta/0 Bo ,loop_counter_variable- in low_bound55)i")_bound loop $e.uence o' !tatement!; 'nd loop. '/20
)'8IN

Bo i in 255C loop d%$sMoutput5putMline(=i H = XX i). end loop.


'ND.

Output0 iH2 iH1 iH4 iH@ iHC '/10


)'8IN

Bo i in eve se 255C loop d%$sMoutput5putMline(=i H = XX i). end loop.


'ND.

Copy rights are reserved.

183
Output0 iHC iH@ iH4 iH1 iH2 NULL ST(T'M'NT Usuall7 when 7ou w ite a state$ent in a p og a$# 7ou want it to do so$ething5 The e a e !ases# howeve # when 7ou want to tell the The
NULL AL?SQL

to do a%solutel7 nothing# and that is whe e

!o$es5 state$ent deos nothing e/!ept pass !ont ol to the ne/t e/e!uta%le state$ent5
NULL

NULL

*ou !an use

state$ent in the following situations5

I$p oving p og a$ eada%ilit75 So$eti$es# it is helpful to avoid an7 a$%iguit7 inhe ent in an not have to in!lude an !lause5
IB

state$ent that

doesn;t !ove all possi%le !ases5 Bo e/a$ple# when 7ou w ite an IB state$ent# 7ou do
'LS'

Nullif7ing a aised e/!eption5 6hen 7ou don;t want to w ite an7 spe!ial !ode to handle an e/!eption# 7ou !an use the NULL state$ent to $a&e su e that a aised e/!eption halts e/e!ution of the !u ent
AL?SQL

%lo!& %ut does not p opagate an7 e/!eptions to en!losing %lo!&s5

Using null afte a la%el5 In so$e !ases# 7ou !an pai state$ents5 Bo
NULL

with
8OTO

8OTO

to avoid having to e/e!ute additional p o!essing is eUui ed5


NULL

e/a$ple# I use a

state$ent to Uui!&l7 $ove to the end of $7

p og a$ if the state of $7 data indi!ates that no fu the

)e!ause I do not have to do an7thing at the te $ination of the p og a$# I pla!e a 'ven though deos nothing# it is still an e/e!uta%le state$ent5

state$ent afte the la%el %e!ause at least one e/e!uta%le state$ent is eUui ed the e5
NULL

8OTO (ND L()'LS

Copy rights are reserved.

184

S7nta/0 8oto label. 6he e label is a la%el defined in the state$ent identified %7 the la%el5 '/0
)'8IN

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

Bo i in 255C loop d%$sMoutput5putMline(=i H = XX i). if i H @ then goto e/itMloop. end if. end loop. ,,e/itMloop-Null.
'ND.

Output0 iH2 iH1 iH4 iH@


R'STRICTIONS ON 8OTO

It is illegal to % an!h into an inne %lo!&# loop5 (t least one e/e!uta%le state$ent $ust follow5 It is illegal to % an!h into an if state$ent5 It is illegal to % an!h 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

Copy rights are reserved.

185
A ag$as a e !o$pile di e!tives5 The7 se ve as inst u!tions to the !o$pile will a!t on the p ag$a du ing the !o$pilation of the %lo!&5 S7nta/0
AR8(M( AL?SQL

!o$pile 5 The

in!truction_to_compiler5

AL?SQL

offe s seve al p ag$as0


(UTONOMOUSMTR(NS(CTION '+C'ATIONMINIT R'STRICTMR'B'R'NC'S S'RI(LL*MR'US()L'

Copy rights are reserved.

186

SU)ARO8R(MS
AROC'DUR'S ( p o!edu e is a $odule that pe fo $s one o $o e a!tions5 S7nta/0 A o!edu e Ns!he$a5Oname N(parameter1 N#parameter2 RO)O Nauthid define X !u entMuse O is "" Nde!la ationsO )egin "" e/e!uta%le state$ents N'/!eption "" e/!eption handle sO 'nd Nna$eO. In the a%ove aut)id !lause defines whethe the p o!edu e will e/e!ute unde the autho it7 of the define of the p o!edu e o unde the autho it7 of the !u ent use 5 BUNCTIONS ( fun!tion is a $odule that etu ns a value5 S7nta/0 Bun!tion Ns!he$a5Oname N(parameter1 N#parameter2 RO)O

Copy rights are reserved.

187
Retu n etu nMdatat7pe Nauthid define X !u entMuse O Ndete $inisti!O Npa allelMena%leO is "" Nde!la ationsO )egin "" e/e!uta%le state$ents N'/!eption "" e/!eption handle sO 'nd Nna$eO. In the a%ove aut)id !lause defines whethe the p o!edu e will e/e!ute unde the autho it7 of the define of the p o!edu e o unde the autho it7 of the !u ent use 5 -etermini!tic !lause defines# an opti$iTation hint that lets the s7ste$ use a saved !op7 of the fun!tion;s etu n esult# if availa%le5 The Uuet7 opti$iTe !an !hoose whethe to use the saved !op7 o e"!all the fun!tion5

7arallel_enable !lause defines# an opti$iTation hint that ena%les the fun!tion to %e e/e!uted in pa allel when !alled f o$ within A(R(M'T'R MOD'S In (Default) Out In out
IN S'L'CT

state$ent5

In pa a$ete will a!t as pl9!.l con!tant5


OUT

Out pa a$ete will a!t as unintiali,ed variable5 *ou !annot p ovide a default value to an out pa a$ete 5 (n7 assign$ents $ade to out pa a$ete aised in the p og a$5 a e olled %a!& when an e/!eption is

Copy rights are reserved.

188

IN OUT

(n a!tual pa a$ete !o esponding to an out fo $al pa a$ete $ust %e a va ia%le5

In out pa a$ete will a!t as initiali,ed variable5 (n a!tual pa a$ete va ia%le5 !o esponding to an in out fo $al pa a$ete $ust %e a

D'B(ULT A(R(M'T'RS Default Aa a$ete s will not allow in the be"innin" and middle5 /ut and 5n /ut pa a$ete s !an not have default values5 '/0 p o!edu e p(a in nu$%e default C# % in nu$%e default D# ! in nu$%e default V) \ valid p o!edu e p(a in nu$%e # % in nu$%e default D# ! in nu$%e default V) \ valild p o!edu e p(a in nu$%e # % in nu$%e # ! in nu$%e default V) \ 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 NOT(TIONS Notations a e of two t7pes5 Aositional notation Na$e notation

6e !an !o$%ine positional and na$e notation %ut positional notation !an not %e followed %7 the na$e notation5 '/0 Suppose we have a p o!edu e p o!(a nu$%e #% nu$%e #! nu$%e ) and we have one

Copy rights are reserved.

189
anon7$ous %lo!& whi!h !ontains v2#v1# and v4. e/e! p o! (v2#v1#v4) e/e! p o! (aH-v2#%H-v1#!H-v4) "" Aositional notation "" Na$ed notation

SQLSQL-

BORM(L (ND (CTU(L A(R(M'T'RS Aa a$etes whi!h a e in !alling su%p og a$ a e actual parameter!5 Aa a$etes whi!h a e in !alled su%p og a$ a e 'ormal parameter!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 '/20
CR'(T' OR R'AL(C' AROC'DUR' S(MAL'(a

in nu$%e #% out nu$%e #! in out

nu$%e ) is
)'8IN

d%$sMoutput5putMline(=(fte !all=). d%$sMoutput5putMline(=a H = XX a XX= % H = XX % XX = ! H = XX !). % 0H 23. ! 0H 13. d%$sMoutput5putMline(=(fte assign$ent=). d%$sMoutput5putMline(=a H = XX a XX= % H = XX % XX = ! H = XX !).
'ND S(MAL'. D'CL(R'

v2 nu$%e 0H @. v1 nu$%e 0H C. v4 nu$%e 0H D.


)'8IN

d%$sMoutput5putMline(=)efo e !all=). d%$sMoutput5putMline(=v2 H = XX v2 XX = v1 H = XX v1 XX = v4 H = XX v4). sa$ple(v2#v1#v4). d%$sMoutput5putMline(=(fte !o$pletion of !all=). d%$sMoutput5putMline(=v2 H = XX v2 XX = v1 H = XX v1 XX = v4 H = XX v4).
'ND.

Copy rights are reserved.

190
Output0 )efo e !all v2 H @ v1 H C v4 H D (fte !all aH@%H !HD (fte assign$ent a H @ % H 23 ! H 13 (fte !o$pletion of !all v2 H @ v1 H 23 v4 H 13 '/10
CR'(T' OR R'AL(C' BUN(a

in nu$%e #% out nu$%e #! in out nu$%e ) etu n


IS

nu$%e
)'8IN

d%$sMoutput5putMline(=(fte !all=). d%$sMoutput5putMline(=a H = XX a XX = % H = XX % XX = ! H = XX !). d%$sMoutput5putMline(=)efo e assigne$ent Result H = XX (aEnvl(%#2)E!)). % 0H C. ! 0H V. d%$sMoutput5putMline(=(fte assign$ent=). d%$sMoutput5putMline(=a H = XX a XX = % H = XX % XX = ! H = XX !). etu n (aE%E!).
'ND BUN. D'CL(R'

v2 nu$%e 0H 2. v1 nu$%e 0H 1. v4 nu$%e 0H 4. v nu$%e .


)'8IN

d%$sMoutput5putMline(=)efo e !all=). d%$sMoutput5putMline(=v2 H = XX v2 XX = v1 H = XX v1 XX = v4 H = XX v4). v 0H fun(v2#v1#v4). d%$sMoutput5putMline(=(fte !all !o$pleted=). d%$sMoutput5putMline(=v2 H = XX v2 XX = v1 H = XX v1 XX = v4 H = XX v4). d%$sMoutput5putMline(=Result H = XX v).
'ND.

Copy rights are reserved.

191
Output0 )efo e !all v2 H 2 v1 H 1 v4 H 4 (fte !all aH2%H !H4 )efo e assigne$ent Result H 4 (fte assign$ent aH2%HC!HV (fte !all !o$pleted v2 H 2 v1 H C v4 H V Result H 4C
R'STRICTIONS ON BORM(L A(R(M'T'RS

)7 de!la ing with spe!ified siTe in a!tual pa a$ete s5 )7 de!la ing fo $al pa a$ete s with Lt7pe spe!ifie 5

USIN8 NOCOA* :ocopy is a hint# not a !o$$and5 This $eans that the !o$pile de!ide that it !an;t fulfill 7ou '/0
CR'(T' OR R'AL(C' AROC'DUR' AROC(a )'8IN """" 'ND AROC.

$ight silentl7

eUuest fo a nocopy pa a$ete 5

The !op7ing f o$ fo $al to a!tual !an %e est i!ted %7 issuing nocopy Uualifie 5 To pass the out and in out pa a$ete s %7 efe en!e use no!op7 Uualifie 5

in out no!op7 nu$%e ) IS

C(LL (ND '+'C Call is a S7nta/0 Call !ubpro"ram_name(Nar"ument_li!tO) Ninto )o!t_variableO. state$ent# whi!h !an %e used to e/e!ute su%p og a$s li&e e/e!5

SQL

Copy rights are reserved.

192

The pa antheses a e alwa7s eUui ed# even if the su%p og a$ ta&es no a gu$ents5 6e !an not use !all with out and in out pa a$ete s5

Call is a SQL state$ent# it is not valid inside a AL?SQL %lo!&. The INTO !lause is used fo the output va ia%les of fun!tions onl75
6e !an not use Ke/e!; with out o in out pa a$ete s5

'/e! is not valid inside a AL?SQL %lo!&.

'/20
CR'(T' OR R'AL(C' AROC IS )'8IN

d%$sMoutput5putMline(=hello wo ld=).
'ND AROC.

Output0
SQL-

!all p o!(). hello wo ld

'/10
CR'(T' OR R'AL(C' AROC(a )'8IN

in nu$%e #% in nu$%e )

IS

d%$sMoutput5putMline(=a H = XX a XX = % H = XX %).
'ND AROC.

Output0
SQL-

!all p o!(C#D). aHC%HD

'/40
CR'(T' OR R'AL(C' BUNCTION BUN R'TURN 9(RC:(R IS )'8IN

etu n =hello wo ld=.


'ND BUN.

Output0

Copy rights are reserved.

193
SQLSQLSQL-

va ia%le v va !ha (13) !all fun() into 0v. p int v hello wo ld

C(LL )* R'B'R'NC' (ND C(LL )* 9(LU' In pa a$ete s %7 default call by re'erence whe e as out and in out call by value5 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 passed %7 value it !opies the value of the a!tual pa a$ete to the fo $al pa a$ete 5 Call %7 efe en!e is faste than the !all %7 value %e!ause it avoids the !op7ing5

SU)ARO8R(MS O9'RLO(DIN8 Aossi%le with diffe ent nu$%e of pa a$ete s5 Aossi%le with diffe ent t7pes of data5 Aossi%le with sa$e t7pe with o%Je!ts5 Can not %e possi%le with diffe ent t7pes of $odes5 6e !an ove load lo!al su%p og a$s also5

'/0

SQLSQL-

! eate o ! eate o
D'CL(R'

epla!e t7pe t2 as o%Je!t(a nu$%e ).? epla!e t7pe t2 as o%Je!t(a nu$%e ).?

i t2 0H t2(C). J t1 0H t1(C).
AROC'DUR' A($ t2) IS )'8IN

d%$sMoutput5putMline(=a H = XX $5a).
'ND A. AROC'DUR' A(n t1) IS )'8IN

d%$sMoutput5putMline(=% H = XX n5%).
'ND A.

Copy rights are reserved.

194
AROC'DUR' ARODUCT(a nu$%e #% nu$%e ) IS )'8IN

d%$sMoutput5putMline(=A odu!t of a#% H = XX a E %).


'ND ARODUCT. AROC'DUR' ARODUCT(a nu$%e #% nu$%e #! nu$%e ) IS )'8IN

d%$sMoutput5putMline(=A odu!t of a#% H = XX a E % E !).


'ND ARODUCT. )'8IN

p(i). p(J). p odu!t(@#C). p odu!t(@#C#D).


'ND.

Output0 aHC %HC A odu!t of a#% H 13 A odu!t of a#% H 213


)'N'BITS OB O9'RLO(DIN8

Suppo ting $an7 data !o$%inations Bitting the p og a$ to the use 5

R'STRICTIONS ON O9'RLO(DIN8

Ove loaded p og a$s with pa a$ete lists that diffe onl7 %7 na$e $ust %e !alled using na$ed notation5 The pa a$ete $ode5 list of ove loaded p og a$s $ust diffe %7 $o e than pa a$ete

(ll of the ove loaded p og a$s $ust %e defined within the sa$e
%lo!&5 Ove loaded fun!tions $ust diffe %7 $o e than thei etu n t7pe5

AL?SQL

s!ope o

IMAORT(NT AOINTS ()OUT SU)ARO8R(MS

Copy rights are reserved.

195

6hen a sto ed su%p og a$ is ! eated# it is sto ed in the data dictionary5 The su%p og a$ is sto ed in !o$pile fo $ whi!h is &nown as p0code in addition to the sou !e te/t5

The p"!ode has all of the 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 e/e!uted5
AL?SQL

engine5

6hen the su%p og a$ is !alled# the p"!ode is ead f o$ the dis&# if ne!essa 7# and

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 Su%p og a$s !an %e local5
AL?SQL (LRU)

algo ith$5

Lo!al su%p og a$s $ust %e de!la ed in the de!la ative se!tion of


!alled f o$ the e/e!uta%le se!tion5 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. Lo!al su%p og a$s also !an have lo!al su%p og a$s5

%lo!& and

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 (!ubpro"ram_name5sal)5 Su%p og a$s !an %e invalidated5

AROC'DUR'S 9 BUNCTIONS A o!edu es $a7 etu n th ough out and in out pa a$ete s whe e as fun!tion $ust 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 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%$sMoutput whe e as p o!edu e !an not5 A o!edu e !all is a standalone e/e!uta%le state$ent whe e as fun!tion !all is a pa t of an e/e!uta%le state$ent5 e/e!uting p o!edu e whe e as we need to

Copy rights are reserved.

196

STOR'D 9 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 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)MSMS:(R'DMAOOL 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 who has

'/20
CR'(T' OR R'AL(C' AROC'DUR' A IS )'8IN

d%$sMoutput5putMline(=Sto ed su%p og a$=).


'ND.

Output0
SQL-

e/e! p

Sto ed su%p og a$ '/10


D'CL(R'

Copy rights are reserved.

197
AROC'DUR' A IS )'8IN

d%$sMoutput5putMline(=Lo!al su%p og a$=).


'ND. )'8IN

p.
'ND.

Output0 Lo!al 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%Je!ts5

If a su%p og a$ is invalidated# the


e!o$pile in the ne/t ti$e it is !alled5

AL?SQL

engine will auto$ati!all7 atte$pt to

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

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 A2aOR(CL'.


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%Je!ts is !he!&ed at unti$e5 6hen A2 is !alled# the e$ote data di!tiona 7 is Uue ied to dete $ine the status of A15

Copy rights are reserved.

198
A2 and A1 a e !o$pa ed to see it A2 needs to %e diffe ent $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%Je!ts a e !o$pa ed5 The la!t_ddl_time field of u!er_ob#ect! !ontains the ti$esta$p5 If the %ase o%Je!t has a newe ti$esta$p than the dependent o%Je!t# the dependent o%Je!t will %e e!o$piled5
ISSU'S 6IT: T:IS MOD'L

e!o$piled# the e a e two

If the o%Je!ts a e in diffe ent ti$e Tones# the !o$pa ison is invalid5
AL?SQL

6hen A2 is in a !lient side


the fo $s5 SI8N(TUR' MOD'L

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

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

In o de to use the signatu e $odel# the pa a$ete


%e set to
SI8N(TUR'5

R'MOT'MD'A'ND'NCI'SMMOD'

$ust

This is a pa a$ete in the data%ase initialiTation file5

T:R'' 6(*S OB S'TTIN8 T:IS MOD'

Copy rights are reserved.

199 (dd the line


sessions5 (lte s7ste$ set e$oteMdependen!iesM$ode H 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 R'MOT'MD'A'ND'NCI'SMMOD'HSI8N(TUR'

to the data%ase initialiTation file5


SI8N(TUR'

The ne/t ti$e the data%ase is sta ted# the $ode will %e set to

fo

all

p ivilege to issue this !o$$and5

(lte session set e$oteMdependen!iesM$ode H 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%$sMoutput5putMline(=B o$ p o!edu e p2=). p1.


'ND A2. AROC'DUR' A1 IS )'8IN

d%$sMoutput5putMline(=B o$ p o!edu e p1=).

Copy rights are reserved.

200
p4.
'ND A1. AROC'DUR' A4 IS )'8IN

d%$sMoutput5putMline(=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%$sMoutput5putMline(=B o$ p o!edu e p2=). p1.


'ND A2. AROC'DUR' A1 IS )'8IN

d%$sMoutput5putMline(=B o$ p o!edu e p1=). p4.


'ND A1. AROC'DUR' A4 IS )'8IN

Copy rights are reserved.

201
d%$sMoutput5putMline(=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


on p o!edu e to use ) with the following !o$$and5
SQL-

'+'CUT'5

If use ( had the p o!edu e !alled e$pMp o! then use ( g ants e/e!ute p ivilege 8 ant e/e!ute on e$pMp o! to use )5 '/e! use (5e$pMp o!

Then use ) !an un the p o!edu e %7 issuing


SQL-

use ( ! eated the following p o!edu e


CR'(T' OR R'AL(C' AROC'DUR' A 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.

use ( g anted e/e!ute p ivilege to use ) using


SQL-

g ant e/e!ute on p to use )

Copy rights are reserved.

202

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 5nvo er;! and -e'iner;! ri")t!. )7 default it will use the define ;s ights5
(UT:ID

(n invo&e ;s ights outine !an %e ! eated %7 using


use );s ta%le5

!lause to populate the

It is valid fo stand"alone su%p og a$s# pa!&age spe!ifi!ations# and o%Je!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'NTMUS'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

Copy rights are reserved.

203
Instead of !u entMuse of authid !lause# if 7ou use define then it will %e !alled define ; p o!edu e5
STOR'D SU)ARO8R(MS (ND ROL'S

we have two use s sa&eth and sudha in whi!h sa&eth has student ta%le and sudha does not5 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%$sMoutput5putMline(KNo H K 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&ethM ole. g ant all on student to sa&ethM ole. g ant sa&ethM 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

Copy rights are reserved.

204
fo v in ! loop d%$sMoutput5putMline(KNo H K XX v5no). end loop.
'ND A.

The a%ove !ode will aise e o instead of ! eating p o!edu e 5 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 )ut efe en!es in


set5

ights

outine# e/te nal

efe en!es in

SQL

state$ents will %e

esolved using the !alle ;s p ivilege set5


AL?SQL

state$ents a e still esolved unde the owne ;s p ivilege

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

Copy rights are reserved.

205

A(CF(8'S
( pac a"e is a !ontaine fo elated o%Je!ts5 It has spe!ifi!ation and %od75 'a!h of

the$ is sto ed sepa atel7 in data di!tiona 75 A(CF(8' S*NT(+ C eate o epla!e pa!&age ,pac a"e_name- is

"" pa!&age spe!ifi!ation in!ludes su%p og a$s signatu es# !u so s and glo%al o pu%li! va ia%les5 'nd ,pac a"e_name-. C eate o epla!e pa!&age %od7 ,pac a"e_name- is

"" pa!&age %od7 in!ludes %od7 fo all the su%p og a$s de!la ed in the spe!# p ivate 9a ia%les and !u so s5 )egin "" initialiTation se!tion '/!eption "" '/!eption handling se!iton 'nd ,pac a"e_name-.

Copy rights are reserved.

206

IMAORT(NT AOIN8S ()OUT A(CF(8'S The fi st ti$e a pa!&aged su%p og a$ is !alled o 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 initialiTation needs to %e the pa!&age %od7 afte all the o%Je!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 un the fi st ti$e the pa!&age is instantiated within a session5 This !an %e done %7 adding initialiTation se!tion to an7 efe en!e to a pa!&aged

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 *ou !an in!lude authid !lause inside the pa!&age spe! not in the %od75 The e/e!ution se!tion of a pa!&age is &now as initialiTation se!tion5 *ou !an have an e/!eption se!tion at the %otto$ of a pa!&age %od75 Aa!&ages su%p og a$s a e not invalidated5

COMAILIN8 A(CF(8'S

SQLSQLSQL-

(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%Je!ts and the pa!&age heade 5 The pa!&age heade does not depend on the pa!&age %od7# whi!h is an advantage of pa!&ages5 6e !an !hange the pa!&age %od7 with out !hanging the heade 5

Copy rights are reserved.

207
A(CF(8' RUNTIM' ST(T' 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 in %oth pa!&age spe! and %od7# '/a$ine the following pa!&age5
CR'(T' OR R'AL(C' A(CF(8' AF8 IS

AR(8M( S'RI(LL*MR'US()L'

p ag$a se iall7M eusa%le. p o!edu e e$pMp o!.


'ND AF8.

CR'(T' OR R'AL(C' A(CF(8' )OD* AF8 IS

p ag$a se iall7M eusa%le. !u so ! is sele!t ena$e f o$ e$p.


AROC'DUR' 'MAMAROC IS

vMena$e e$p5ena$eLt7pe. vMflag %oolean 0H t ue. vMnu$ ows nu$%e 0H 3.


)'8IN

if not !Lisopen then open !. end if. while vMflag loop fet!h ! into vMena$e. vMnu$ ows 0H vMnu$ ows G 2. if vMnu$ ows H C then vMflag 0H false. end if. d%$sMoutput5putMline(='na$e H = XX vMena$e). end loop.
'ND 'MAMAROC.

Copy rights are reserved.

208
'ND AF8.

SQL-

e/e! p&g5e$pMp o! 'na$e H 'na$e H 'na$e H 'na$e H


SMIT: (LL'N 6(RD

'na$e H >ON'S
M(RTIN

SQL-

e/e! p&g5e$pMp o! 'na$e H 'na$e H 'na$e H 'na$e H 'na$e H


SMIT: (LL'N 6(RD >ON'S M(RTIN

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 eusa%le ve sion esets the state of the !u so ea!h ti$e it was !alled5

NON S'RI(LL * R'US()L' A(CF(8'S

This is the default ve sion used %7 the o a!le# e/a$ine the following pa!&age5
CR'(T' OR R'AL(C' A(CF(8' AF8 IS

p o!edu e e$pMp o!.


'ND AF8.

CR'(T' OR R'AL(C' A(CF(8' )OD* AF8 IS

!u so ! is sele!t ena$e f o$ e$p.


AROC'DUR' 'MAMAROC IS

vMena$e e$p5ena$eLt7pe. vMflag %oolean 0H t ue. vMnu$ ows nu$%e 0H 3.


)'8IN

Copy rights are reserved.

209
if not !Lisopen then open !. end if. while vMflag loop fet!h ! into vMena$e. vMnu$ ows 0H vMnu$ ows G 2. if vMnu$ ows H C then vMflag 0H false. end if. d%$sMoutput5putMline(='na$e H = XX vMena$e). end loop.
'ND 'MAMAROC. 'ND AF8.

SQL-

e/e! p&g5e$pMp o! 'na$e H 'na$e H 'na$e H 'na$e H 'na$e H


SMIT: (LL'N 6(RD >ON'S M(RTIN

SQL-

e/e! p&g5e$pMp o! 'na$e H 'na$e H 'na$e H 'na$e H 'na$e H

)L(F' CL(RF SCOTT FIN8 TURN'R

The a%ove pa!&age displa7s the diffe ent output fo the !u so is not !losed5 )e!ause the non"se iall7 data%ase !alls5 eusa%le ve sion

ea!h e/e!ution even though ove

e$ains the state of the !u so

D'A'ND'NCI'S OB A(CF(8' RUNTIM' ST(T'

Copy rights are reserved.

210
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
CR'(T' OR R'AL(C' A(CF(8' AF8 IS

v nu$%e 0H C. p o!edu e p.
'ND AF8.

CR'(T' OR R'AL(C' A(CF(8' )OD* AF8 IS AROC'DUR' A IS )'8IN

d%$sMoutput5putMline(=v H = XX v). v 0H 23. d%$sMoutput5putMline(=v H = XX v).


'ND A. 'ND AF8.

Conne!t to se!ond session# un the following !ode5


)'8IN

p&g5p.
'ND.

The a%ove !ode wil wo &5 8o %a!& to fi st session and e! eate the pa!&age using ! eate5 Then !onne!t to se!ond session and un the following !ode again5
)'8IN

p&g5p.
'ND.

This a%ove !ode will not wo & %e!ause of the following5 The anon7$ous %lo!& depends on p&g5 This is !o$pile ti$e dependen!75 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

Copy rights are reserved.

211
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 AURIT* L'9'LS In gene al# !alls to su%p og a$s a e p o!edu al# the7 !annot %e !alled f o$ !an %e !alled du ing e/e!ution of a SQL state$ent5 Use "defined fun!tions a e !alled the sa$e wa7 as %uilt"in fun!tions %ut it $ust $eet diffe ent est i!tions5 These est i!tions a e defined in te $s of pu it7 levels5 The e a e fou t7pes of pu it7 levels5
6NDS RNDS 6NAS RNAS

SQL

state$ents5 :oweve # if a stand"alone o pa!&aged fun!tion $eets !e tain est i!tions# it

"" "" "" ""

6 ites No Data%ase State Reads No Data%ase State 6 ites No Aa!&age State Reads No Aa!&age State est i!tions# a use "defined fun!tion $ust also $eet the
SQL

In addition to the p e!eding

following eUui e$ents to %e !alled f o$ a

state$ent5

The fun!tion has to %e sto ed in the data%ase# eithe stand"alone o as pa t of a pa!&age5 The fun!tion !an ta&e onl7 in pa a$etes5
AL?SQL

The fo $al pa a$ete s $ust use onl7 data%ase t7pes# not


%oolean o e!o d5

t7pes su!h as

The etu n t7pe of the fun!tion $ust also %e a data%ase t7pe5 The fun!tion $ust not end the !u ent t ansa!tion with !o$$it o oll%a!& to a savepoint p io to the fun!tion e/e!ution5 It also $ust not issue an7 alte session o alte s7ste$ !o$$ands5 oll%a!&# o

Copy rights are reserved.

212
R'STRICTMR'B'R'NC'S

Bo

pa!&aged fun!tions# howeve # the

R'STRICTMR'B'R'NC'S

p ag$a is

eUui ed to spe!if7

the pu it7 level of a given fun!tion5 S7nta/0


AR(8M( R'STRICTMR'B'R'NC'S(!ubpro"ram_name N#RNDSO N#RNASO).

or pac a"e_name#

6NDS N#6NASO

'/0
CR'(T' OR R'AL(C' A(CF(8' AF8 IS

fun!tion fun2 etu n va !ha . p ag$a est i!tM efe en!es(fun2#wnds). fun!tion fun1 etu n va !ha . p ag$a est i!tM efe en!es(fun1#wnds).
'ND AF8.

CR'(T' OR R'AL(C' A(CF(8' )OD* AF8 IS BUNCTION BUN2 )'8IN

etu n va !ha

IS

update dept set deptno H 22. etu n =hello=.


'ND BUN2. BUNCTION BUN1 )'8IN

etu n va !ha

IS

update dept set dna$e H=aa=. etu n =hello=.


'ND BUN1. 'ND AF8.

The a%ove pa!&age %od7 will not ! eated# it will give the following e os5 ALS"33@C10 Su%p og a$ =BUN2= violates its asso!iated p ag$a ALS"33@C10 Su%p og a$ =BUN1= violates its asso!iated p ag$a
CR'(T' OR R'AL(C' A(CF(8' )OD* AF8 IS BUNCTION BUN2 )'8IN

etu n va !ha

IS

etu n =hello=.

Copy rights are reserved.

213
'ND BUN2. BUNCTION BUN1 )'8IN

etu n va !ha

IS

etu n =hello=.
'ND BUN1. 'ND AF8.

Now the pa!&age %od7 will %e ! eated5


D'B(ULT

If the e is no fo

R'STRICTMR'B'R'NC'S

p ag$a asso!iated with a given pa!&aged fun!tion# it

will not have an7 pu it7 level asse ted5 :oweve # 7ou !an !hange the default pu it7 level a pa!&age5 The
D'B(ULT

&e7wo d is used instead of the su%p og a$ na$e in the

p ag$a5 '/0
CR'(T' OR R'AL(C' A(CF(8' AF8 IS

p ag$a est i!tM efe en!es(default#wnds). fun!tion fun2 etu n va !ha . fun!tion fun1 etu n va !ha .
'ND AF8.

CR'(T' OR R'AL(C' A(CF(8' )OD* AF8 IS BUNCTION BUN2 )'8IN

etu n va !ha

IS

update dept set deptno H 22. etu n =hello=.


'ND BUN2. BUNCTION BUN1 )'8IN

etu n va !ha

IS

update dept set dna$e H=aa=. etu n =hello=.


'ND BUN1. 'ND AF8.

The a%ove pa!&age %od7 will not ! eated# it will give the following e os %e!ause the p ag$a will appl7 to all the fun!tions5

Copy rights are reserved.

214

ALS"33@C10 Su%p og a$ =BUN2= violates its asso!iated p ag$a ALS"33@C10 Su%p og a$ =BUN1= violates its asso!iated p ag$a
CR'(T' OR R'AL(C' A(CF(8' )OD* AF8 IS BUNCTION BUN2 )'8IN

etu n va !ha

IS

etu n =hello=.
'ND BUN2. BUNCTION BUN1 )'8IN

etu n va !ha

IS

etu n =hello=.
'ND BUN1. 'ND AF8.

Now the pa!&age %od7 will %e ! eated5

TRUST

If the

TRUST

&e7wo d is p esent# the

est i!tions listed in the p ag$a a e not enfo !ed5

Rathe # the7 a e t usted to %e t ue5 '/0


CR'(T' OR R'AL(C' A(CF(8' AF8 IS

fun!tion fun2 etu n va !ha . p ag$a est i!tM efe en!es(fun2#wnds#t ust). fun!tion fun1 etu n va !ha . p ag$a est i!tM efe en!es(fun1#wnds#t ust).
'ND AF8.

CR'(T' OR R'AL(C' A(CF(8' )OD* AF8 IS BUNCTION BUN2 )'8IN

etu n va !ha

IS

update dept set deptno H 22. etu n =hello=.


'ND BUN2.

Copy rights are reserved.

215
BUNCTION BUN1 )'8IN

etu n va !ha

IS

update dept set dna$e H=aa=. etu n =hello=.


'ND BUN1. 'ND AF8.

The a%ove pa!&age will %e ! eated su!!essfull75


IMAORT(NT AOINTS ()OUT R'STRICTMR'B'R'NC'S

This p ag$a !an appea an7whe e in the pa!&age spe!ifi!ation# afte the fun!tion de!la ation5 It !an appl7 to onl7 one fun!tion definition5 Bo ove load fun!tions# the p ag$a applies to the nea est definition p io to the A ag$a5 This p ag$a is eUui ed onl7 fo pa!&ages fun!tions not fo stand"alone fun!tions5 The A ag$a !an %e de!la ed onl7 inside the pa!&age spe!ifi!ation5 The p ag$a is !he!&ed at !o$pile ti$e# not unti$e5 It is possi%le to spe!if7 without an7 pu it7 levels when t ust o !o$%ination of default and t ust &e7wo ds a e p esent5

AINNIN8 IN T:' S:(R'D AOOL The !)ared pool is the po tion of the S8S that !ontains# a$ong othe things# the p"!ode of !o$piled su%p og a$s as the7 a e un5 The fi st ti$e a sto ed a sto e su%p og a$ is !alled# the p"!ode is loaded f o$ dis& into the sha ed pool5 On!e the o%Je!t is no longe efe en!ed# it is f ee to %e aged out5 O%Je!ts a e aged out of the sha ed pool using an
LRU(Least

Re!entl7 Used) algo ith$5 pa!&age allows 7ou to pin o%Je!ts in the sha ed pool5 6hen an

The

D)MSMS:(R'DMAOOL

o%Je!t is pinned# it will neve %e aged out until 7ou eUuest it# no $atte how full the pool gets o how often the o%Je!t is a!!essed5 This !an i$p ove pe fo $an!e# as it ta&es ti$e to eload a pa!&age f o$ dis&5 has fou p o!edu es

D)MSMS:(R'DMAOOL

Copy rights are reserved.

216

F''A UNF''A SIZ'S ()ORT'DMR'QU'STMT:R'S:OLD

F''A

The

D)MSMS:(R'DMAOOL5F''A

p o!edu e is used to pin o%Je!ts in the pool5

S7nta/0
AROC'DUR' F''A(ob#ect_name

va !ha 1#'la" !ha default KA;).

:e e the flag ep esents diffe ent t7pes of flag values fo diffe ent t7pes of o%Je!ts5 A Q R C T >S >C >R >D
UNF''A

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

Aa!&age# fun!tion o p o!edu e SeUuen!e T igge SQL Cu so O%Je!t t7pe >ava sou !e >ava !lass >ava esou !e >ava sha ed data

UNF''A

is the onl7 wa7 to e$ove a &ept o%Je!t f o$ the sha ed pool# without esta ting

the data%ase5 Fept o%Je!ts a e neve aged out auto$ati!all75 S7nta/0


AROC'DUR' UNF''A(ob#ect_name

va !ha 1# 'la" !ha default KA;).

SIZ'S

SIZ'S

will e!ho the !ontents of the sha ed pool to the s! een5

Copy rights are reserved.

217
S7nta/0
AROC'DUR' SIZ'S(min!i,e

nu$%e ).
SIZ'S

O%Je!ts with g eate than the min!i,e will %e etu ned5 the data5
()ORT'DMR'QU'STMT:R'S:OLD

uses

D)MSMOUTAUT

to etu n

6hen the data%ase dete $ines that the e is not enough $e$o 7 in the sha ed pool to satisf7 a given eUuest# it will %egin aging o%Je!ts out until the e is enough $e$o 75 It enough o%Je!ts a e aged out# this !an have a pe fo $an!e i$pa!t on othe sessions5 The S7nta/0
AROC'DUR' ()ORT'DMR'QU'STMT:R'S:OLD(t)re!)old_!i,e ()ORT'DMR'QU'STMT:R'S:OLD

data%ase

!an %e used to e$ed7 this5

nu$%e ).

On!e this p o!edu e is !alled# o a!le will not sta t aging o%Je!ts f o$ the pool unless at least t)re!)old_!i,e %7tes is needed5 D(T( MOD'L BOR SU)ARO8R(MS (ND A(CF(8'S US'RMO)>'CTS US'RMSOURC' US'RM'RRORS D)(MO)>'CTS D)(MSOURC' D)(M'RRORS (LLMO)>'CTS (LLMSOURC' (LLM'RRORS

Copy rights are reserved.

218

CURSORS
Cur!or is a pointe to $e$o 7 lo!ation whi!h is !alled as conte(t area whi!h !ontains the info $ation ne!essa 7 fo state$ent# a pointe p o!essing# in!luding the nu$%e of ows p o!essed %7 the to the pa sed ep esentation of the state$ent# and the active !et

whi!h is the set of ows etu ned %7 the Uue 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 deptL owt7pe is sele!t Ef o$ dept. In the a%ove

Copy rights are reserved.

219
:eade \ !u so !(dno in nu$%e ) etu n deptL owt7pe )od7 \ sele!t Ef o$ dept CURSOR T*A'S I$pli!it (SQL) '/pli!it Aa a$ete iTed !u so s R'B !u so s CURSOR ST(8'S Open Bet!h Close

CURSOR (TTRI)UT'S Lfound Lnotfound L ow!ount Lisopen L%ul&M ow!ount L%ul&Me/!eptions

CURSOR D'CL'R(TION S7nta/0 Cu so ,cur!or_name- is !elect !tatement. '/0 Cu so ! is sele!t Ef o$ dept. CURSOR LOOAS

Copy rights are reserved.

220


SIMAL' LOOA

Si$ple loop 6hile loop Bo loop

S7nta/0 Loop Bet!h ,cur!or_name- into ,record_variable-. '/it when ,cur!or_name- L notfound. ,!tatement!-. 'nd loop. '/0
D'CL(R'

!u so ! is sele!t E f o$ student. vMstud studentL owt7pe.


)'8IN

open !. loop fet!h ! into vMstud. e/it when !Lnotfound. d%$sMoutput5putMline(=Na$e H = XX vMstud5na$e). end loop. !lose !.
'ND.

Output0 Na$e H sa&eth Na$e H s inu Na$e H satish Na$e H sudha


6:IL' LOOA

S7nta/0

Copy rights are reserved.

221
6hile ,cur!or_name- L found loop Bet!h ,cur!or_name- nto ,record_variable-. ,!tatement!-. 'nd loop. '/0
D'CL(R'

!u so ! is sele!t E f o$ student. vMstud studentL owt7pe.


)'8IN

open !. fet!h ! into vMstud. while !Lfound loop fet!h ! into vMstud. d%$sMoutput5putMline(=Na$e H = XX vMstud5na$e). end loop. !lose !.
'ND.

Output0 Na$e H sa&eth Na$e H s inu Na$e H satish Na$e H sudha


BOR LOOA

S7nta/0 fo ,record_variable- in ,cur!or_name- loop ,!tatement!-. 'nd loop. '/0


D'CL(R'

!u so ! is sele!t E f o$ student.
)'8IN

fo vMstud in ! loop

Copy rights are reserved.

222
d%$sMoutput5putMline(=Na$e H = XX vMstud5na$e). end loop. 'ND. Output0 Na$e H sa&eth Na$e H s inu Na$e H satish Na$e H sudha A(R(M'T(RIZ'D CURSORS This was used when 7ou a e going to use the !u so in $o e than one pla!e with diffe ent values fo the sa$e whe e !lause5 '/0
D'CL(R'

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 H dno. vMdept deptL owt7pe.
)'8IN

open !(13). loop fet!h ! into vMdept. e/it when !Lnotfound. d%$sMoutput5putMline(=Dna$e H = XX vMdept5dna$e XX = Lo! H = XX vMdept5lo!). end loop. !lose !.
'ND.

Output0 Dna$e H Lo! H

R'S'(RC:

D(LL(S

Copy rights are reserved.

223

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 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 '/20
CR'(T' OR R'AL(C' A(CF(8' AF8 IS

*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

!u so ! etu n deptL owt7pe is sele!t E f o$ dept. p o!edu e p o! is


'ND AF8.

CR'(T' OR R'AL(C' A(FC(8' )OD* AF8 IS

!u so ! etu n deptL owt7pe is sele!t E f o$ dept.


AROC'DUR' AROC IS )'8IN

fo v in ! loop d%$sMoutput5putMline(=Deptno H = XX v5deptno XX = Dna$e H = XX v5dna$e XX = Lo! H = XX v5lo!). end loop.


'ND AROC. 'ND AF8.

Output0
SQL-

e/e! p&g5p o! Deptno H 23 Dna$e H Deptno H 13 Dna$e H Deptno H 43 Dna$e H Deptno H @3 Dna$e H
(CCOUNTIN8 R'S'(RC: S(L'S

Lo! H

N'6 *ORF

Lo! H

D(LL(S

Lo! H

C:IC(8O )OSTON

OA'R(TIONS

Lo! H

'/10
CR'(T' OR R'AL(C' A(FC(8' )OD* AF8 IS

Copy rights are reserved.

224
!u so ! etu n deptL owt7pe is sele!t E f o$ dept whe e deptno - 13.
AROC'DUR' AROC IS )'8IN

fo v in ! loop d%$sMoutput5putMline(=Deptno H = XX v5deptno XX = Dna$e H = XX v5dna$e XX = Lo! H = XX v5lo!). end loop.


'ND AROC. 'ND AF8.

Output0 e/e! p&g5p o! Deptno H 43 Dna$e H Deptno H @3 Dna$e H


S(L'S

SQL-

Lo! H

C:IC(8O )OSTON

OA'R(TIONS

Lo! H

R'B CURSORS (ND CURSOR 9(RI()L'S This is un!onst ained !u so use input5 Ref !u so s !an not %e !losed i$pli!itl75 Ref !u so with etu n t7pe is !alled !tron" cur!or5 Ref !u so with out etu n t7pe is !alled wea cur!or5 whi!h will etu n diffe ent t7pes depends upon the

*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 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 assigned !u so va ia%le goes out of s!ope5 va ia%le even though the

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 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

Copy rights are reserved.

225
*ou !an not use to anothe 5 '/0
CR'(T' OR R'AL(C' AROC'DUR' R'BMCURSOR(T()L'MN(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

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 eUualit7# ineUualit7 and nullit75

t7pe t is ef !u so . ! t. vMdept deptL owt7pe. t7pe is e!o d(ena$e e$p5ena$eLt7pe#Jo% e$p5Jo%Lt7pe#sal e$p5salLt7pe). vMe$p . vMstud student5na$eLt7pe.
)'8IN

if ta%leMna$e H

=D'AT=

then then then

open ! fo sele!t E f o$ dept. elsif ta%leMna$e H elsif ta%leMna$e H end if. loop if ta%leMna$e H
=D'AT= ='MA=

open ! fo sele!t ena$e#Jo%#sal f o$ e$p.


=STUD'NT=

open ! fo sele!t na$e f o$ student.

then

fet!h ! into vMdept. e/it when !Lnotfound. d%$sMoutput5putMline(=Deptno H = XX vMdept5deptno XX = Dna$e H = XX vMdept5dna$e elsif ta%leMna$e H
='MA=

XX = Lo! H = XX vMdept5lo!).

then

fet!h ! into vMe$p. e/it when !Lnotfound. d%$sMoutput5putMline(='na$e H = XX vMe$p5ena$e XX = >o% H = XX vMe$p5Jo% XX = Sal H = XX vMe$p5sal). elsif ta%leMna$e H
=STUD'NT=

then

fet!h ! into vMstud. e/it when !Lnotfound.

Copy rights are reserved.

226
d%$sMoutput5putMline(=Na$e H = XX vMstud). end if. end loop. !lose !.
'ND.

Output0
SQL-

e/e! efM!u so (=D'AT=) Lo! H

Deptno H 23 Dna$e H Deptno H 13 Dna$e H Deptno H 43 Dna$e H Deptno H @3 Dna$e H

(CCOUNTIN8 R'S'(RC: S(L'S

N'6 *ORF

Lo! H

D(LL(S

Lo! H

C:IC(8O

OA'R(TIONS

Lo! H

)OSTON

SQL-

e/e! efM!u so (='MA=) >o% H >o% H >o% H >o% H >o% H >o% H >o% H Sal H W33 Sal H 2D33 Sal H 21C3 Sal H 1SVC Sal H 21C3 Sal H 1WC3 Sal H 1@C3 Sal H C333 Sal H 2C33

'na$e H 'na$e H 'na$e H 'na$e H 'na$e H 'na$e H 'na$e H 'na$e H 'na$e H 'na$e H 'na$e H 'na$e H 'na$e H 'na$e H

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% H

S(L'SM(N

M(N(8'R M(N(8'R (N(L*ST

Sal H 4333

>o% H

AR'SID'NT

TURN'R (D(MS >(M'S BORD

>o% H

S(L'SM(N

>o% H >o% H

CL'RF CL'RF

Sal H 2233 Sal H SC3 Sal H 4333 Sal H 2433

>o% H

(N(L*ST CL'RF

MILL'R

>o% H

SQL-

e/e! efM!u so (=STUD'NT=) Na$e H sa&eth Na$e H s inu Na$e H satish

Copy rights are reserved.

227
Na$e H 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 !u so 5 Nested !u so !loses if 7ou !lose e/pli!itl75 Nested !u so Nested !u so 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 using the !u so e/p essions in d7na$i! SQL5
USIN8 N'ST'D CURSORS OR CURSOR '+AR'SSIONS

e/p ession f o$ the pa ent o

oute

!loses wheneve !loses wheneve

the oute

pa ent !u so

is e/e!uted again o

!losed o !an!eled5 an e/!eption is aised while fet!hing data f o$ a

e/p essions when

'/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 H

d5deptno) f o$ e$p e. t7pe t is ef !u so . !2 t. !1 t. v2 e$p5ena$eLt7pe. v1 dept5dna$eLt7pe.


)'8IN

open !. loop fet!h !2 into v2.

Copy rights are reserved.

228
e/it when !2Lnotfound. fet!h !1 into v1. e/it when !1Lnotfound. d%$sMoutput5putMline(='na$e H = XX v2 XX = Dna$e H = XX v1). end loop. end loop. !lose !.
'ND.

CURSOR CL(US'S Retu n Bo update 6he e !u ent of )ul& !olle!t

R'TURN

Cu so ! etu n deptL owt7pe is sele!t Ef o$ dept. O Cu so !2 is sele!t Ef o$ dept. Cu so ! etu n !2L owt7pe is sele!t Ef o$ dept. O T7pe t is e!o d(deptno dept5deptnoLt7pe# dna$e dept5dna$eLt7pe). Cu so ! etu n t is sele!t deptno# dna$e f o$ dept.
BOR UAD(T' (ND 6:'R' CURR'NT OB

No $all7# a sele!t ope ation will not ta&e an7 lo!&s on the ows %eing a!!essed5 This will allow othe sessions !onne!ted to the data%ase to !hange the data %eing sele!ted5 The esult set is still !onsistent5 (t open ti$e# when the a!tive set is dete $ined# o a!le ta&es a snapshot of the ta%le5 (n7 !hanges that have %een !o$$itted p io to this point a e efle!ted in the a!tive set5 (n7 !hanges $ade afte this point# even if the7 a e !o$$itted# a e not efle!ted unless the !u so is eopened# whi!h will evaluate the a!tive set again5

Copy rights are reserved.

229

:oweve # if the the

BOR UAD(T'

!aluse is pesent# e/!lusive ow lo!&s a e ta&en on the ows in olled %a!&5 If anothe ope ation

the a!tive set %efo e the open etu ns5 These lo!&s p event othe sessions f o$ !hanging ows in the a!tive set until the t ansa!tion is !o$$itted o session al ead7 has lo!&s on the ows in the a!tive set# then waiting pe iod5 The

S'L'CT R BOR UAD(T'

will wait fo these lo!&s to %e eleased %7 the othe session5 The e is no ti$e"out fo this
S'L'CTRBOR UAD(T' NO6(IT

will hang until the othe session eleases the lo!&5

To handle this situation# the S7nta/0

!lause is availa%le5

Sele!t Rf o$ R fo update of !olu$nMna$e Nwait nO. If the !u so is de!la ed with the !lause# the !lause !an

BOR UAD(T'

6:'R' CURR'NT OB

%e used in an update o delete state$ent5 S7nta/0 6he e !u ent of !u so . '/0


D'CL(R'

!u so ! is sele!t E f o$ dept fo update of dna$e.


)'8IN

fo v in ! loop update dept set dna$e H =aa= whe e !u ent of !. !o$$it. end loop.
'ND.

)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?sUl and sUl engines5 Redu!es the ove head of et ieving data5 *ou !an use %ul& !olle!t in %oth d7na$i! and stati! sUl5 *ou !an use %ul& !olle!t in sele!t# fet!h into and etu ning into !lauses5

Copy rights are reserved.

230
SQL engine auto$ati!all7 initialiTes and e/tends the !olle!tions 7ou the %ul& !olle!t !lause5 )ul& !olle!t ope ation e$pties the !olle!tion e/e!uting the Uue 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 efe en!e in

)ULF COLL'CT IN B'TC:

'/0
D'CL(R'

T7pe t is ta%le of deptL owt7pe. nt t. Cu so ! is sele!t Ef o$ dept.


)'8IN

Open !. Bet!h ! %ul& !olle!t into nt. Close !. Bo i in nt5fi st55nt5last loop d%$sMoutput5putMline(=Dna$e H = XX nt(i)5dna$e XX = Lo! H = XX nt(i)5lo!). end loop.
'ND.

Output0 Dna$e H Dna$e H Dna$e H Dna$e H


)ULF COLL'CT IN S'L'CT (CCOUNTIN8 R'S'(RC: S(L'S

Lo! H

N'6 *ORF

Lo! H

D(LL(S

Lo! H

C:IC(8O

OA'R(TIONS

Lo! H

)OSTON

'/0
D'CL(R'

T7pe t is ta%le of deptL owt7pe. Nt t.

Copy rights are reserved.

231
)'8IN

Sele!t E %ul& !olle!t into nt f o$ dept. fo i in nt5fi st55nt5last loop d%$sMoutput5putMline(=Dna$e H = XX nt(i)5dna$e XX = Lo! H = XX nt(i)5lo!). end loop.
'ND.

Output0 Dna$e H Dna$e H Dna$e H Dna$e H


LIMIT IN )ULF COLL'CT (CCOUNTIN8 R'S'(RC: S(L'S

Lo! H

N'6 *ORF

Lo! H

D(LL(S

Lo! H

C:IC(8O

OA'R(TIONS

Lo! H

)OSTON

*ou !an use this to li$it the nu$%e of ows to %e fet!hed5

'/0
D'CL(R'

T7pe t is ta%le of deptL owt7pe. nt t. Cu so ! is sele!t Ef o$ dept.


)'8IN

Open !. Bet!h ! %ul& !olle!t into nt li$it 1. Close !. Bo i in nt5fi st55nt5last loop d%$sMoutput5putMline(=Dna$e H = XX nt(i)5dna$e XX = Lo! H = XX nt(i)5lo!). end loop.
'ND.

Output0 Dna$e H Dna$e H


(CCOUNTIN8 R'S'(RC:

Lo! H

N'6 *ORF

Lo! H

D(LL(S

Copy rights are reserved.

232

MULTIAL' B'TC:'S IN INTO CL(US'

'/20
D'CL(R'

T7pe t is ta%le of dept5dna$eLt7pe. nt t. T7pe t2 is ta%le of dept5lo!Lt7pe. nt2 t. Cu so ! is sele!t dna$e#lo! f o$ dept.
)'8IN

Open !. Bet!h ! %ul& !olle!t into nt#nt2. Close !. Bo i in nt5fi st55nt5last loop d%$sMoutput5putMline(=Dna$e H = XX nt(i)). end loop. Bo i in nt25fi st55nt25last loop d%$sMoutput5putMline(=Lo! H = XX nt2(i)). end loop.
'ND.

Output0 Dna$e H Dna$e H Dna$e H Dna$e H Lo! H Lo! H Lo! H Lo! H '/10
D'CL(R' (CCOUNTIN8 R'S'(RC: S(L'S OA'R(TIONS

N'6 *ORF D(LL(S C:IC(8O )OSTON

t7pe t is ta%le of dept5dna$eLt7pe. t7pe t2 is ta%le of dept5lo!Lt7pe. nt t.

Copy rights are reserved.

233
nt2 t2.
)'8IN

Sele!t dna$e#lo! %ul& !olle!t into nt#nt2 f o$ dept. fo i in nt5fi st55nt5last loop d%$sMoutput5putMline(=Dna$e H = XX nt(i)). end loop. fo i in nt25fi st55nt25last loop d%$sMoutput5putMline(=Lo! H = XX nt2(i)). end loop.
'ND.

Output0 Dna$e H Dna$e H Dna$e H Dna$e H Lo! H Lo! H Lo! H Lo! H


(CCOUNTIN8 R'S'(RC: S(L'S OA'R(TIONS

N'6 *ORF D(LL(S C:IC(8O )OSTON

R'TURNIN8 CL(US' IN )ULF COLL'CT

*ou !an use this to etu n the p o!essed data to the ouput va ia%les o t7ped va ia%les5 '/0
D'CL(R'

t7pe t is ta%le of nu$%e (1). nt t 0H t(2#1#4#@). t7pe t2 is ta%le of va !ha (1). nt2 t2. t7pe t1 is ta%le of studentL owt7pe. nt1 t1.
)'8IN

sele!t na$e %ul& !olle!t into nt2 f o$ student. fo all v in nt25fi st55nt25last update student set no H nt(v) whe e na$e H nt2(v) etu ning

Copy rights are reserved.

234
no#na$e#$a &s %ul& !olle!t into nt1. fo v in nt15fi st55nt15last loop d%$sMoutput5putMline(=Ma &s H = XX nt1(v)). end loop.
'ND.

Output0 Ma &s H 233 Ma &s H 133 Ma &s H 433 Ma &s H @33 AOINTS TO R'M'M)'R 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 su%p og a$s !loses auto$ati!all7 when that %lo!& te $inates e/e!ution5 L%ul&M ow!ount and L%ul&Me/!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

Copy rights are reserved.

235

SQL IN AL?SQL
The onl7 state$ents allowed di e!tl7 in pl?sUl a e )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
DML

and

TCL5

)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

Copy rights are reserved.

236

AL?SQL

%7 default uses ea l7 %inding5

)inding also involves !he!&ing the data%ase fo pe $issions to a!!ess the o%Je!t Refe en!ed5

D*N(MIC SQL

If 7ou use

DDL

in pl?sUl it validates the pe $issions and e/isten!e if

eUui 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
SQL5

Two te!hniUues a e availa%le fo D7na$i! Native D7na$i! SQL D)MSMSQL pa!&age

USIN8 N(TI9' D*N(MIC SQL


USIN8 '+'CUT' IMM'DI(T'

'/0
)'8IN

'/e!ute i$$ediate K! eate ta%le student(no nu$%e (1)#na$e va !ha (23));. o '/e!ute i$$ediate (K! eate ta%le student(no nu$%e (1)#na$e va !ha (23));).
'ND.

USIN8 '+'CUT' IMM'DI(T' 6IT: AL?SQL 9(RI()L'S

'/0
D'CL(R'

v va !ha (233).
)'8IN

v 0H =! eate ta%le student(no nu$%e (1)#na$e va !ha (23))=. e/e!ute i$$ediate v.


'ND.

Copy rights are reserved.

237

USIN8 '+'CUT' IMM'DI(T' 6IT: )IND 9(RI()L'S (ND USIN8 CL(US'

'/0
D'CL(R'

v va !ha (233).
)'8IN

v 0H =inse t into student values(0v2#0v1#0v4)=. e/e!ute i$$ediate v using D#=f=#D33.


'ND.

'+'CUTIN8 QU'RI'S 6IT: OA'N BOR (ND USIN8 CL(US'

'/0
CR'(T' OR R'AL(C' AROC'DUR' A(s$a

&s in nu$%e )

IS

s va !ha (233) 0H =sele!t Ef o$ student whe e $a &s - 0$=. t7pe t is ef !u so . ! t. v studentL owt7pe.
)'8IN

open ! fo s using s$a &s. loop fet!h ! into v. e/it when !Lnotfound. d%$sMoutput5putMline(=Student Ma &s H = XX v5$a &s). end loop. !lose !.
'ND.

Output0
SQL-

e/e! p(233) Student Ma &s H 133 Student Ma &s H 433 Student Ma &s H @33

QU'RI'S 6IT: '+'CUT' IMM'DI(T'

Copy rights are reserved.

238

'/0
D'CL(R'

dMna$e dept5dna$eLt7pe. l! dept5lo!Lt7pe. v va !ha (233).


)'8IN

v 0H =sele!t dna$e f o$ dept whe e deptno H 23=. e/e!ute i$$ediate v into dMna$e. d%$sMoutput5putMline(=Dna$e H =XX dMna$e). v 0H =sele!t lo! f o$ dept whe e dna$e H 0dn=. e/e!ute i$$ediate v into l! using dMna$e. d%$sMoutput5putMline(=Lo! H = XX l!).
'ND.

Output0 Dna$e H (CCOUNTIN8 Lo! H N'6 *ORF


9(RI()L' N(M'S

'/0
D'CL(R'

Ma &s nu$%e (4) 0H 233.


)'8IN

Delete student whe e $a &s H $a &s.


'ND.

"" this will delete all the ows in the "" student ta%le

This !an %e avoided %7 using the la%eled %lo!&s5 ,,$7M%lo!&-D'CL(R'

Ma &s nu$%e (4) 0H 233.


)'8IN

Delete student whe e $a &s H $7M%lo!&5$a &s.


'ND.

"" delete ows whi!h has "" a $a &s of 233

Copy rights are reserved.

239

8'TTIN8 D(T( INTO AL?SQL 9(RI()L'S

'/0
D'CL(R'

92 nu$%e . 91 va !ha (1).


)'8IN

Sele!t no#na$e into v2#v1 f o$ student whe e $a &s H 233.


'ND.

DML (ND R'CORDS

'/0
CR'(T' OR R'AL(C' AROC'DUR' A(s )'8IN

ow in studentL owt7pe)

IS

inse t into student values s ow.


'ND A.

D'CL(R'

s studentL owt7pe.
)'8IN

s5no 0H 22. s5na$e 0H =aa=. s5$a &s 0H 233. p(s).


'ND.

R'CORD )(S'D INS'RTS

'/0
D'CL(R'

s ow studentL owt7pe.
)'8IN

s ow5no 0H V. s ow5na$e 0H =!!=. s ow5$a &s 0H C33. inse t into student values s ow.

Copy rights are reserved.

240
'ND.

R'CORD )(S'D UAD(T'S

'/0
D'CL(R'

s ow studentL owt7pe.
)'8IN

s ow5no 0H D. s ow5na$e 0H =!!=. s ow5$a &s 0H C33. update student set owHs ow whe e no H s ow5no.
'ND.

USIN8 R'CORDS 6IT: R'TURNIN8 CL(US'

'/0
D'CL(R'

s ow studentL owt7pe. s etu n studentL owt7pe.


)'8IN

s ow5no 0H W. s ow5na$e 0H =dd=. s ow5$a &s 0H C33. inse t into student values s ow etu ning no#na$e#$a &s into s etu n. d%$sMoutput5putMline(=No H = XX s etu n5no). d%$sMoutput5putMline(=No H = XX s etu n5na$e). d%$sMoutput5putMline(=No H = XX s etu n5$a &s).
'ND.

Output0 No H W No H dd No H C33 USIN8 D)MSMSQL A(CF(8'

Copy rights are reserved.

241
D)MSMSQL is used to e/e!ute d7na$i! SQL f o$ with in AL?SQL5 Unli&e native d7na$i! SQL# it is not %uilt di e!tl7 into the language# and thus is less effi!ient5 The D)MSMSQL pa!&age allows 7ou to di e!tl7 !ont ol the p o!essing of a state$ent within a !u so # with ope ations su!h as opening and !losing a !u so # pa sing a state$ent# %inding input va ia%le# and defining output va ia%les5 '/20
D'CL(R'

!u so Mid nu$%e . flag nu$%e . vMst$t va !ha (C3).


)'8IN

!u so Mid 0H d%$sMsUl5openM!u so . vMst$t 0H =! eate ta%le stud(sno nu$%e (1)#sna$e va !ha (23))=. d%$sMsUl5pa se(!u so Mid#vMst$t#d%$sMsUl5native). flag 0H d%$sMsUl5e/e!ute(!u so Mid). d%$sMsUl5!loseM!u so (!u so Mid). d%$sMoutput5putMline(=Ta%le ! eated=).
'ND.

Output0 Ta%le ! eated SQL- des! stud Na$e SNO SN(M' '/10
CR'(T' OR R'AL(C' AROC'DUR' D)MSMSQLMAROC(v2

Null[

T7pe NUM)'R(1) 9(RC:(R1(23)

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

student5noLt7pe#

v1 student5$a &sLt7pe) is !u so Mid nu$%e .

Copy rights are reserved.

242
flag nu$%e . vMupdate va !ha (C3).
)'8IN

!u so Mid 0H d%$sMsUl5openM!u so . vMupdate 0H =update student set $a &s H 0s$a &s whe e no H 0sno=. d%$sMsUl5pa se(!u so Mid#vMupdate#d%$sMsUl5native). d%$sMsUl5%indMva ia%le(!u so Mid#=0sno=#v2). d%$sMsUl5%indMva ia%le(!u so Mid#=0s$a &s=#v1). flag 0H d%$sMsUl5e/e!ute(!u so Mid). d%$sMsUl5!loseM!u so (!u so Mid).
'ND D)MSMSQLMAROC.

Output0 sele!t E f o$ student. NO 2 1 4 N( a % ! M(RFS 233 133 433 "" %efo e e/e!ution

SQL-

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

SQL-

e/e! d%$sMsUlMp o!(1#111) sele!t E f o$ student. NO 2 1 4 N( a % ! M(RFS 233 111 433 "" afte e/e!ution

SQL-

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

BOR(LL ST(T'M'NT This !an %e used to get the data f o$ the data%ase at on!e %7 edu!ting the nu$%e of !onte/t swit!hes whi!h is a t ansfe of !ont ol %etween
AL?SQL

and

SQL

engine5

Copy rights are reserved.

243

S7nta/0 Bo all inde/Mva in N 4ower_bound55upper_bound X Indi!es of inde(in"_collection X 9alues of inde(in"_collection O SQL state$ent.
BOR(LL 6IT: NON"S'QU'NTI(L (RR(*S

'/0
D'CL(R'

t7pe t is ta%le of student5noLt7pe inde/ %7 %ina 7Mintege . i%t t.


)'8IN

i%t(2) 0H 2. i%t(23) 0H 1. fo all i in i%t5fi st55i%t5last update student set $a &s H S33 whe e no H i%t(i).
'ND.

The a%ove p og a$ will give e o li&e Kele$ent at inde/ N1O does not e/ists5 *ou !an e!tif7 it in one of the two following wa7s5
US8(8' OB INDIC'S OB TO (9OID T:' ()O9' )':(9IOUR

This will %e used when 7ou have a !olle!tion whose defined ows spe!if7 whi!h ows in the %inding a a7 7ou would li&e to p o!essed5 '/0
D'CL(R'

t7pe t is ta%le of student5noLt7pe inde/ %7 %ina 7Mintege . i%t t. t7pe t2 is ta%le of %oolean inde/ %7 %ina 7Mintege . i%t2 t2.
)'8IN

i%t(2) 0H 2.

Copy rights are reserved.

244
i%t(23) 0H 1. i%t(233) 0H 4. i%t2(2) 0H t ue. i%t2(23) 0H t ue. i%t2(233) 0H t ue. fo all i in indi!es of i%t2 update student set $a &s H S33 whe e no H i%t(i).
'ND.

Ouput0
SQL-

sele!t E f o$ student "" %efo e e/e!ution NO 2 1 4 N( a % ! M(RFS 233 133 433

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

SQL-

sele!t E f o$ student "" afte e/e!ution NO 2 1 4 N( a % ! M(RFS S33 S33 S33

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

US8(8' OB 9(LU'S OB TO (9OID T:' ()O9' )':(9IOUR

This will %e used when 7ou have a !olle!tion of intege s whose !ontent identifies the position in the %inding a a7 that 7ou want to %e p o!essed %7 the '/0
D'CL(R' BOR(LL

state$ent5

t7pe t is ta%le of student5noLt7pe inde/ %7 %ina 7Mintege . i%t t. t7pe t2 is ta%le of plsMintege inde/ %7 %ina 7Mintege . i%t2 t2.

Copy rights are reserved.

245
)'8IN

i%t(2) 0H 2. i%t(23) 0H 1. i%t(233) 0H 4. i%t2(22) 0H 2. i%t2(2C) 0H 23. i%t2(2W) 0H 233. fo all i in values of i%t2 update student set $a &s H CDV whe e no H i%t(i).
'ND.

Ouput0
SQL-

sele!t E f o$ student "" %efo e e/e!ution NO 2 1 4 N( a % ! M(RFS 233 133 433

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

SQL-

sele!t E f o$ student "" afte e/e!ution NO 2 1 4 N( a % ! M(RFS S33 S33 S33

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

AOINTS ()OUT )ULF )INDS

Aassing the enti e


%ind5

AL?SQL

ta%le to the

SQL

engine in one step is &nown as %ul&

)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

Copy rights are reserved.

246

AOINTS ()OUT R'TURIN8 CL(US'

This will %e used onl7 with DML state$ents to etu n data into AL?SQL 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


will avoid a !all to
RD)MS

S'L'CT

using

R'TURNIN8

!lause we will get the data whi!h

&e nel5

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 siTe# a7 t7pe5 The $a/i$u$ siTe of the va a7 is 1 9a a7s Nested ta%les Inde/ " %7 ta%les ((sso!iate a a7s)

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 giga %7tes5

Copy rights are reserved.

247
S7nta/0 T7pe ,type_name& is va a7 X va 7ing a a7 (,limit&) of ,element_type&. '/20
D'CL(R'

t7pe t is va a7(23) of va !ha (1). va t 0H t(=a=#=%=#=!=#=d=). flag %oolean.


)'8IN

d%$sMoutput5putMline(=Li$it H = XX va5li$it). d%$sMoutput5putMline(=Count H = XX va5!ount). d%$sMoutput5putMline(=Bi st Inde/ H = XX va5fi st). d%$sMoutput5putMline(=Last Inde/ H = XX va5last). d%$sMoutput5putMline(=Ne/t Inde/ H = XX va5ne/t(1)). d%$sMoutput5putMline(=A evious Inde/ H = XX va5p io (4)). d%$sMoutput5putMline(=9(RR(* fo i in va5fi st55va5last loop d%$sMoutput5putMline(=vaN= XX i XX =O H = XX va(i)). end loop. flag 0H va5e/ists(4). if flag H t ue then d%$sMoutput5putMline(=Inde/ 4 e/ists with an ele$ent = XX va(4)). else d%$sMoutput5putMline(=Inde/ 4 does not e/ists=). end if. va5e/tend. d%$sMoutput5putMline(=(fte e/tend of one inde/# Count H = XX va5!ount). flag 0H va5e/ists(C). if flag H t ue then d%$sMoutput5putMline(=Inde/ C e/ists with an ele$ent = XX va(C)). else d%$sMoutput5putMline(=Inde/ C does not e/ists=). end if. flag 0H va5e/ists(D). if flag H t ue then d%$sMoutput5putMline(=Inde/ D e/ists with an ele$ent = XX va(D)).
'L'M'NTS=).

Copy rights are reserved.

248
else d%$sMoutput5putMline(=Inde/ D does not e/ists=). end if. va5e/tend(1). d%$sMoutput5putMline(=(fte e/tend of two inde/es# Count H = XX va5!ount). d%$sMoutput5putMline(=9(RR(* fo i in va5fi st55va5last loop d%$sMoutput5putMline(=vaN= XX i XX =O H = XX va(i)). end loop. va(C) 0H =e=. va(D) 0H =f=. va(V) 0H =g=. d%$sMoutput5putMline(=(BT'R (SSIN8NIN8 9(LU'S TO '+T'ND'D 'L'M'NTS#
9(RR(* 'L'M'NTS=). 'L'M'NTS=).

fo i in va5fi st55va5last loop d%$sMoutput5putMline(=vaN= XX i XX =O H = XX va(i)). end loop. va5e/tend(4#1). d%$sMoutput5putMline(=(fte e/tend of th ee inde/es# Count H = XX va5!ount). d%$sMoutput5putMline(=9(RR(* 'L'M'NTS=). fo i in va5fi st55va5last loop d%$sMoutput5putMline(=vaN= XX i XX =O H = XX va(i)). end loop. va5t i$. d%$sMoutput5putMline(=(fte t i$ of one inde/# Count H = XX va5!ount). va5t i$(4). d%$sMoutput5putMline(=(fte t i$ of th ee inde/s# Count H = XX va5!ount). d%$sMoutput5putMline(=(BT'R TRIM# 9(RR(* 'L'M'NTS=). fo i in va5fi st55va5last loop d%$sMoutput5putMline(=vaN= XX i XX =O H = XX va(i)). end loop. va5delete. d%$sMoutput5putMline(=(fte delete of enti e va a7# Count H = XX va5!ount).
'ND.

Output0

Copy rights are reserved.

249
Li$it H 23 Count H @ Bi st Inde/ H 2 Last Inde/ H @ Ne/t Inde/ H 4 A evious Inde/ H 1 9(RR(* 'L'M'NTS vaN2O H a vaN1O H % vaN4O H ! vaN@O H d Inde/ 4 e/ists with an ele$ent ! (fte e/tend of one inde/# Count H C Inde/ C e/ists with an ele$ent Inde/ D does not e/ists (fte e/tend of two inde/es# Count H V 9(RR(* 'L'M'NTS vaN2O H a vaN1O H % vaN4O H ! vaN@O H d vaNCO H vaNDO H vaNVO H (BT'R (SSIN8NIN8 9(LU'S TO '+T'ND'D 'L'M'NTS# 9(RR(* 'L'M'NTS vaN2O H a vaN1O H % vaN4O H ! vaN@O H d vaNCO H e vaNDO H f vaNVO H g (fte e/tend of th ee inde/es# Count H 23 9(RR(* 'L'M'NTS vaN2O H a

Copy rights are reserved.

250
vaN1O H % vaN4O H ! vaN@O H d vaNCO H e vaNDO H f vaNVO H g vaNWO H % vaNSO H % vaN23O H % (fte t i$ of one inde/# Count H S (fte t i$ of th ee inde/s# Count H D (BT'R TRIM# 9(RR(* 'L'M'NTS vaN2O H a vaN1O H % vaN4O H ! vaN@O H d vaNCO H e vaNDO H f (fte delete of enti e va a7# Count H 3 '/10
D'CL(R'

t7pe t is va a7(@) of studentL owt7pe. va t 0H t(null#null#null#null).


)'8IN

fo i in 255va5!ount loop sele!t E into va(i) f o$ student whe e sno H i. d%$sMoutput5putMline(=Sno H = XX va(i)5sno XX = Sna$e H = XX va(i)5sna$e). end loop.
'ND.

Output0 Sno H 2 Sna$e H sa&eth Sno H 1 Sna$e H s inu Sno H 4 Sna$e H div7a Sno H @ Sna$e H $anogni

Copy rights are reserved.

251

'/40
D'CL(R'

t7pe t is va a7(@) of student5s$a &sLt7pe. va t 0H 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 H i. d%$sMoutput5putMline(=S$a &s H = XX va(i)). end loop.
'ND.

Output0 S$a &s H 233 S$a &s H 133 S$a &s H 433 S$a &s H @33 '/@0
D'CL(R'

t7pe

is e!o d(!2 student5sna$eLt7pe#!1 student5s$a &sLt7pe).

t7pe t is va a7(@) of . va t 0H t(null#null#null#null).


)'8IN

fo i in 255va5!ount loop sele!t sna$e#s$a &s into va(i) f o$ student whe e sno H i. d%$sMoutput5putMline(=Sna$e H = XX va(i)5!2 XX = S$a &s H = XX va(i)5!1). end loop.
'ND.

Output0 Sna$e H sa&eth S$a &s H 233 Sna$e H s inu S$a &s H 133 Sna$e H div7a S$a &s H 433 Sna$e H $anogni S$a &s H @33 '/C0

Copy rights are reserved.

252
D'CL(R'

t7pe t is va a7(2) of add . va t 0H t(null). !u so ! is sele!t E f o$ e$plo7. i nu$%e 0H 2.


)'8IN

fo v in ! loop sele!t add ess into va(i) f o$ e$plo7 whe e ena$e H v5ena$e. d%$sMoutput5putMline(=:no H = XX va(i)5hno XX = Cit7 H = XX va(i)5!it7). end loop.
'ND.

Output0 :no H 22 Cit7 H h7d :no H 11 Cit7 H %ang :no H 44 Cit7 H &o!hi

'/D0
D'CL(R'

t7pe t is va a7(C) of va !ha (1). va2 t. va1 t 0H t().


)'8IN

if va2 is null then d%$sMoutput5putMline(=va2 is null=). else d%$sMoutput5putMline(=va2 is not null=). end if. if va1 is null then d%$sMoutput5putMline(=va1 is null=). else d%$sMoutput5putMline(=va1 is not null=). end if.
'ND.

Output0

Copy rights are reserved.

253
va2 is null va1 is not null

N'ST'D T()L'S ( nested ta%le is thought of a data%ase ta%le whi!h has no li$it on its siTe5 'le$ents a e inse ted into nested ta%le sta ting at inde/ 25 The $a/i$u$ siTe of the va a7 is 1 giga %7tes5 S7nta/0 T7pe ,type_name& is ta%le of ,table_type&. '/20
D'CL(R'

t7pe t is ta%le of va !ha (1). nt t 0H t(=a=#=%=#=!=#=d=). flag %oolean.


)'8IN

if nt5li$it is null then d%$sMoutput5putMline(=No li$it to Nested Ta%les=). else d%$sMoutput5putMline(=Li$it H = XX nt5li$it). end if. d%$sMoutput5putMline(=Count H = XX nt5!ount). d%$sMoutput5putMline(=Bi st Inde/ H = XX nt5fi st). d%$sMoutput5putMline(=Last Inde/ H = XX nt5last). d%$sMoutput5putMline(=Ne/t Inde/ H = XX nt5ne/t(1)). d%$sMoutput5putMline(=A evious Inde/ H = XX nt5p io (4)). d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=). fo i in 255nt5!ount loop d%$sMoutput5putMline(=ntN= XX i XX =O H = XX nt(i)). end loop. flag 0H nt5e/ists(4). if flag H t ue then d%$sMoutput5putMline(=Inde/ 4 e/ists with an ele$ent = XX nt(4)).

Copy rights are reserved.

254
else d%$sMoutput5putMline(=Inde/ 4 does not e/ists=). end if. nt5e/tend. d%$sMoutput5putMline(=(fte e/tend of one inde/# Count H = XX nt5!ount). flag 0H nt5e/ists(C). if flag H t ue then d%$sMoutput5putMline(=Inde/ C e/ists with an ele$ent = XX nt(C)). else d%$sMoutput5putMline(=Inde/ C does not e/ists=). end if. flag 0H nt5e/ists(D). if flag H t ue then d%$sMoutput5putMline(=Inde/ D e/ists with an ele$ent = XX nt(D)). else d%$sMoutput5putMline(=Inde/ D does not e/ists=). end if. nt5e/tend(1). d%$sMoutput5putMline(=(fte e/tend of two inde/es# Count H = XX nt5!ount). d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=). fo i in 255nt5!ount loop d%$sMoutput5putMline(=ntN= XX i XX =O H = XX nt(i)). end loop. nt(C) 0H =e=. nt(D) 0H =f=. nt(V) 0H =g=. d%$sMoutput5putMline(=(BT'R (SSIN8NIN8 9(LU'S TO '+T'ND'D 'L'M'NTS# N'ST'D
T()L' 'L'M'NTS=).

fo i in 255nt5!ount loop d%$sMoutput5putMline(=ntN= XX i XX =O H = XX nt(i)). end loop. nt5e/tend(C#1). d%$sMoutput5putMline(=(fte e/tend of five inde/es# Count H = XX nt5!ount). d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=). fo i in 255nt5!ount loop d%$sMoutput5putMline(=ntN= XX i XX =O H = XX nt(i)).

Copy rights are reserved.

255
end loop. nt5t i$. d%$sMoutput5putMline(=(fte t i$ of one inde/# Count H = XX nt5!ount). nt5t i$(4). d%$sMoutput5putMline(=(fte t i$ of th ee inde/s# Count H = XX nt5!ount). d%$sMoutput5putMline(=(BT'R TRIM# N'ST'D T()L' 'L'M'NTS=). fo i in 255nt5!ount loop d%$sMoutput5putMline(=ntN= XX i XX =O H = XX nt(i)). end loop. nt5delete(2). d%$sMoutput5putMline(=(fte delete of fi st inde/# Count H = XX nt5!ount). d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=). fo i in 155nt5!ountG2 loop d%$sMoutput5putMline(=ntN= XX i XX =O H = XX nt(i)). end loop. nt5delete(@). d%$sMoutput5putMline(=(fte delete of fou th inde/# Count H = XX nt5!ount). d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=). fo i in 1554 loop d%$sMoutput5putMline(=ntN= XX i XX =O H = XX nt(i)). end loop. fo i in C55nt5!ountG1 loop d%$sMoutput5putMline(=ntN= XX i XX =O H = XX nt(i)). end loop. nt5delete. d%$sMoutput5putMline(=(fte delete of enti e nested ta%le# Count H = XX nt5!ount).
'ND.

Output0 No li$it to Nested Ta%les Count H @ Bi st Inde/ H 2 Last Inde/ H @ Ne/t Inde/ H 4 A evious Inde/ H 1

Copy rights are reserved.

256
N'ST'D T()L' 'L'M'NTS ntN2O H a ntN1O H % ntN4O H ! ntN@O H d Inde/ 4 e/ists with an ele$ent ! (fte e/tend of one inde/# Count H C Inde/ C e/ists with an ele$ent Inde/ D does not e/ists (fte e/tend of two inde/es# Count H V N'ST'D T()L' 'L'M'NTS ntN2O H a ntN1O H % ntN4O H ! ntN@O H d ntNCO H ntNDO H ntNVO H (BT'R (SSIN8NIN8 9(LU'S TO '+T'ND'D 'L'M'NTS# N'ST'D T()L' 'L'M'NTS ntN2O H a ntN1O H % ntN4O H ! ntN@O H d ntNCO H e ntNDO H f ntNVO H g (fte e/tend of five inde/es# Count H 21 N'ST'D T()L' 'L'M'NTS ntN2O H a ntN1O H % ntN4O H ! ntN@O H d ntNCO H e ntNDO H f

Copy rights are reserved.

257
ntNVO H g ntNWO H % ntNSO H % ntN23O H % ntN22O H % ntN21O H % (fte t i$ of one inde/# Count H 22 (fte t i$ of th ee inde/s# Count H W (BT'R TRIM# N'ST'D T()L' 'L'M'NTS ntN2O H a ntN1O H % ntN4O H ! ntN@O H d ntNCO H e ntNDO H f ntNVO H g ntNWO H % (fte delete of fi st inde/# Count H V N'ST'D T()L' 'L'M'NTS ntN1O H % ntN4O H ! ntN@O H d ntNCO H e ntNDO H f ntNVO H g ntNWO H % (fte delete of fou th inde/# Count H D N'ST'D T()L' 'L'M'NTS ntN1O H % ntN4O H ! ntNCO H e ntNDO H f ntNVO H g ntNWO H % (fte delete of enti e nested ta%le# Count H 3

Copy rights are reserved.

258

'/10
D'CL(R'

t7pe t is ta%le of studentL owt7pe. nt t 0H t(null#null#null#null).


)'8IN

fo i in 255nt5!ount loop sele!t E into nt(i) f o$ student whe e sno H i. d%$sMoutput5putMline(=Sno H = XX nt(i)5sno XX = Sna$e H = XX nt(i)5sna$e). end loop.
'ND.

Output0 Sno H 2 Sna$e H sa&eth Sno H 1 Sna$e H s inu Sno H 4 Sna$e H div7a Sno H @ Sna$e H $anogni '/40
D'CL(R'

t7pe t is ta%le of student5s$a &sLt7pe. nt t 0H t(null#null#null#null).


)'8IN

fo i in 255nt5!ount loop sele!t s$a &s into nt(i) f o$ student whe e sno H i. d%$sMoutput5putMline(=S$a &s H = XX nt(i)). end loop.
'ND.

Output0 S$a &s H 233 S$a &s H 133 S$a &s H 433 S$a &s H @33 '/@0
D'CL(R'

Copy rights are reserved.

259
t7pe is e!o d(!2 student5sna$eLt7pe#!1 student5s$a &sLt7pe).

t7pe t is ta%le of . nt t 0H t(null#null#null#null).


)'8IN

fo i in 255nt5!ount loop sele!t sna$e#s$a &s into nt(i) f o$ student whe e sno H i. d%$sMoutput5putMline(=Sna$e H = XX nt(i)5!2 XX = S$a &s H = XX nt(i)5!1). end loop.
'ND.

Output0 Sna$e H sa&eth S$a &s H 233 Sna$e H s inu S$a &s H 133 Sna$e H div7a S$a &s H 433 Sna$e H $anogni S$a &s H @33

'/C0
D'CL(R'

t7pe t is ta%le of add . nt t 0H t(null). !u so ! is sele!t E f o$ e$plo7. i nu$%e 0H 2.


)'8IN

fo v in ! loop sele!t add ess into nt(i) f o$ e$plo7 whe e ena$e H v5ena$e. d%$sMoutput5putMline(=:no H = XX nt(i)5hno XX = Cit7 H = XX nt(i)5!it7). end loop.
'ND.

Output0 :no H 22 Cit7 H h7d :no H 11 Cit7 H %ang :no H 44 Cit7 H &o!hi

Copy rights are reserved.

260
'/D0
D'CL(R'

t7pe t is va a7(C) of va !ha (1). nt2 t. nt1 t 0H t().


)'8IN

if nt2 is null then d%$sMoutput5putMline(=nt2 is null=). else d%$sMoutput5putMline(=nt2 is not null=). end if. if nt1 is null then d%$sMoutput5putMline(=nt1 is null=). else d%$sMoutput5putMline(=nt1 is not null=). end if.
'ND.

Output0 nt2 is null nt1 is not null


S'T OA'R(TIONS IN N'ST'D T()L'S

*ou !an pe fo $ set ope ations in the nested ta%les5 *ou !an also pe fo $ eUualit7 !o$pa isions %etween nested ta%les5 Aossi%le ope ations a e
UNION UNION DISTINCT INT'RS'CT '+C'AT (

'/0

a!t li&e MINUS)

D'CL(R'

Copy rights are reserved.

261
t7pe t is ta%le of va !ha (1). nt2 t 0H t(=a=#=%=#=!=). nt1 t 0H t(=!=#=%=#=a=). nt4 t 0H t(=%=#=!=#=a=#=!=). nt@ t 0H t(=a=#=%=#=d=). ntC t.
)'8IN

ntC 0H set(nt2). d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=). fo i in ntC5fi st55ntC5last loop d%$sMoutput5putMline(=ntCN = XX i XX = O H = XX ntC(i)). end loop. ntC 0H set(nt4). d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=). fo i in ntC5fi st55ntC5last loop d%$sMoutput5putMline(=ntCN = XX i XX = O H = XX ntC(i)). end loop. ntC 0H nt2 $ultiset union nt@. d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=). fo i in ntC5fi st55ntC5last loop d%$sMoutput5putMline(=ntCN = XX i XX = O H = XX ntC(i)). end loop. ntC 0H nt2 $ultiset union nt4. d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=). fo i in ntC5fi st55ntC5last loop d%$sMoutput5putMline(=ntCN = XX i XX = O H = XX ntC(i)). end loop. ntC 0H nt2 $ultiset union distin!t nt4. d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=). fo i in ntC5fi st55ntC5last loop d%$sMoutput5putMline(=ntCN = XX i XX = O H = XX ntC(i)). end loop. ntC 0H nt2 $ultiset e/!ept nt@. d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=). fo i in ntC5fi st55ntC5last loop d%$sMoutput5putMline(=ntCN = XX i XX = O H = XX ntC(i)).

Copy rights are reserved.

262
end loop. ntC 0H nt@ $ultiset e/!ept nt2. d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=). fo i in ntC5fi st55ntC5last loop d%$sMoutput5putMline(=ntCN = XX i XX = O H = XX ntC(i)). end loop.
'ND.

Output0 N'ST'D T()L' 'L'M'NTS ntCN 2 O H a ntCN 1 O H % ntCN 4 O H ! N'ST'D T()L' 'L'M'NTS ntCN 2 O H % ntCN 1 O H ! ntCN 4 O H a N'ST'D T()L' 'L'M'NTS ntCN 2 O H a ntCN 1 O H % ntCN 4 O H ! ntCN @ O H a ntCN C O H % ntCN D O H d N'ST'D T()L' 'L'M'NTS ntCN 2 O H a ntCN 1 O H % ntCN 4 O H ! ntCN @ O H % ntCN C O H ! ntCN D O H a ntCN V O H ! N'ST'D T()L' 'L'M'NTS ntCN 2 O H a ntCN 1 O H %

Copy rights are reserved.

263
ntCN 4 O H ! N'ST'D T()L' 'L'M'NTS ntCN 2 O H ! N'ST'D T()L' 'L'M'NTS ntCN 2 O H d IND'+")* T()L'S (n inde/"%7 ta%le has no li$it on its siTe5 'le$ents a e inse ted into inde/"%7 ta%le whose inde/ $a7 sta t non"seUuentiall7 in!luding negative intege s5 S7nta/0 T7pe ,type_name& is ta%le of ,table_type& inde/ %7 %ina 7Mintege . '/0
D'CL(R'

t7pe t is ta%le of va !ha (1) inde/ %7 %ina 7Mintege . i%t t. flag %oolean.
)'8IN

i%t(2) 0H =a=. i%t("13) 0H =%=. i%t(43) 0H =!=. i%t(233) 0H =d=. if i%t5li$it is null then d%$sMoutput5putMline(=No li$it to Inde/ %7 Ta%les=). else d%$sMoutput5putMline(=Li$it H = XX i%t5li$it). end if. d%$sMoutput5putMline(=Count H = XX i%t5!ount). d%$sMoutput5putMline(=Bi st Inde/ H = XX i%t5fi st). d%$sMoutput5putMline(=Last Inde/ H = XX i%t5last). d%$sMoutput5putMline(=Ne/t Inde/ H = XX i%t5ne/t(1)). d%$sMoutput5putMline(=A evious Inde/ H = XX i%t5p io (4)). d%$sMoutput5putMline(=IND'+ )* T()L' 'L'M'NTS=). d%$sMoutput5putMline(=i%tN"13O H = XX i%t("13)).

Copy rights are reserved.

264
d%$sMoutput5putMline(=i%tN2O H = XX i%t(2)). d%$sMoutput5putMline(=i%tN43O H = XX i%t(43)). d%$sMoutput5putMline(=i%tN233O H = XX i%t(233)). flag 0H i%t5e/ists(43). if flag H t ue then d%$sMoutput5putMline(=Inde/ 43 e/ists with an ele$ent = XX i%t(43)). else d%$sMoutput5putMline(=Inde/ 43 does not e/ists=). end if. flag 0H i%t5e/ists(C3). if flag H t ue then d%$sMoutput5putMline(=Inde/ C3 e/ists with an ele$ent = XX i%t(43)). else d%$sMoutput5putMline(=Inde/ C3 does not e/ists=). end if. i%t5delete(2). d%$sMoutput5putMline(=(fte delete of fi st inde/# Count H = XX i%t5!ount). i%t5delete(43). d%$sMoutput5putMline(=(fte delete of inde/ thi t7# Count H = XX i%t5!ount). d%$sMoutput5putMline(=IND'+ )* T()L' 'L'M'NTS=). d%$sMoutput5putMline(=i%tN"13O H = XX i%t("13)). d%$sMoutput5putMline(=i%tN233O H = XX i%t(233)). i%t5delete. d%$sMoutput5putMline(=(fte delete of enti e inde/"%7 ta%le# Count H = XX i%t5!ount).
'ND.

Output0 No li$it to Inde/ %7 Ta%les Count H @ Bi st Inde/ H "13 Last Inde/ H 233 Ne/t Inde/ H 43 A evious Inde/ H 2 IND'+ )* T()L' 'L'M'NTS

Copy rights are reserved.

265
i%tN"13O H % i%tN2O H a i%tN43O H ! i%tN233O H d Inde/ 43 e/ists with an ele$ent ! Inde/ C3 does not e/ists (fte delete of fi st inde/# Count H 4 (fte delete of inde/ thi t7# Count H 1 IND'+ )* T()L' 'L'M'NTS i%tN"13O H % i%tN233O H d (fte delete of enti e inde/"%7 ta%le# Count H 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 initialiTed %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

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 &e7s !an %e positive o negative5

Refe en!ing none/istent ele$ents aises

SU)SCRIATM)'*ONDMCOUNT NOMD(T(MBOUND

in %oth nested

ta%les and va a7s# %ut in !ase of inde/"%7 ta%les ta%les5

aises5

Fe7s a e seUuential in %oth nested ta%les and va a7s# non"seUuential in inde/"%7 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

Copy rights are reserved.

266
Colle!tions of $o e than one di$ension whi!h is a !olle!tion of !olle!tions# &nown as $ultilevel !olle!tions5 S7nta/0 T7pe ,type_name1& is ta%le of ,table_type& inde/ %7 %ina 7Mintege . T7pe ,type_name2& is va a7(,limit-) X ta%le X of ,type_name1& < inde/ %7 %ina 7Mintege . '/20
D'CL(R'

t7pe t2 is ta%le of va !ha (1) inde/ %7 %ina 7Mintege . t7pe t1 is va a7(C) of t2. va t1 0H t1(). ! nu$%e 0H SV. flag %oolean.
)'8IN

va5e/tend(@). d%$sMoutput5putMline(=Count H = XX va5!ount). d%$sMoutput5putMline(=Li$it H = XX va5li$it). fo i in 255va5!ount loop fo J in 255va5!ount loop va(i)(J) 0H !h (!). ! 0H ! G 2. end loop. end loop. d%$sMoutput5putMline(=9(RR(* 'L'M'NTS=). fo i in 255va5!ount loop fo J in 255va5!ount loop d%$sMoutput5putMline(=vaN= XX i XX =ON= XX J XX =O H = XX va(i)(J)). end loop. end loop. d%$sMoutput5putMline(=Bi st inde/ H = XX va5fi st). d%$sMoutput5putMline(=Last inde/ H = XX va5last). d%$sMoutput5putMline(=Ne/t inde/ H = XX va5ne/t(1)). d%$sMoutput5putMline(=A evious inde/ H = XX va5p io (4)). flag 0H va5e/ists(1).

Copy rights are reserved.

267
if flag H t ue then d%$sMoutput5putMline(=Inde/ 1 e/ists=). else d%$sMoutput5putMline(=Inde/ 1 e/ists=). end if. va5e/tend. va(2)(C) 0H =U=. va(1)(C) 0H = =. va(4)(C) 0H =s=. va(@)(C) 0H =t=. va(C)(2) 0H =u=. va(C)(1) 0H =v=. va(C)(4) 0H =w=. va(C)(@) 0H =/=. va(C)(C) 0H =7=. d%$sMoutput5putMline(=(fte e/tend of one inde/# Count H = XX va5!ount). d%$sMoutput5putMline(=9(RR(* 'L'M'NTS=). fo i in 255va5!ount loop fo J in 255va5!ount loop d%$sMoutput5putMline(=vaN= XX i XX =ON= XX J XX =O H = XX va(i)(J)). end loop. end loop. va5t i$. d%$sMoutput5putMline(=(fte t i$ of one inde/# Count H = XX va5!ount). va5t i$(1). d%$sMoutput5putMline(=(fte t i$ of two inde/es# Count H = XX va5!ount). d%$sMoutput5putMline(=9(RR(* 'L'M'NTS=). fo i in 255va5!ount loop fo J in 255va5!ount loop d%$sMoutput5putMline(=vaN= XX i XX =ON= XX J XX =O H = XX va(i)(J)). end loop. end loop. va5delete. d%$sMoutput5putMline(=(fte delete of enti e va a7# Count H = XX va5!ount).
'ND.

Copy rights are reserved.

268
Output0 Count H @ Li$it H C 9(RR(* 'L'M'NTS vaN2ON2O H a vaN2ON1O H % vaN2ON4O H ! vaN2ON@O H d vaN1ON2O H e vaN1ON1O H f vaN1ON4O H g vaN1ON@O H h vaN4ON2O H i vaN4ON1O H J vaN4ON4O H & vaN4ON@O H l vaN@ON2O H $ vaN@ON1O H n vaN@ON4O H o vaN@ON@O H p Bi st inde/ H 2 Last inde/ H @ Ne/t inde/ H 4 A evious inde/ H 1 Inde/ 1 e/ists (fte e/tend of one inde/# Count H C 9(RR(* 'L'M'NTS vaN2ON2O H a vaN2ON1O H % vaN2ON4O H ! vaN2ON@O H d vaN2ONCO H U vaN1ON2O H e vaN1ON1O H f

Copy rights are reserved.

269
vaN1ON4O H g vaN1ON@O H h vaN1ONCO H vaN4ON2O H i vaN4ON1O H J vaN4ON4O H & vaN4ON@O H l vaN4ONCO H s vaN@ON2O H $ vaN@ON1O H n vaN@ON4O H o vaN@ON@O H p vaN@ONCO H t vaNCON2O H u vaNCON1O H v vaNCON4O H w vaNCON@O H / vaNCONCO H 7 (fte t i$ of one inde/# Count H @ (fte t i$ of two inde/es# Count H 1 9(RR(* 'L'M'NTS vaN2ON2O H a vaN2ON1O H % vaN1ON2O H e vaN1ON1O H f (fte delete of enti e va a7# Count H 3 '/10
D'CL(R'

t7pe t2 is ta%le of va !ha (1) inde/ %7 %ina 7Mintege . t7pe t1 is ta%le of t2. nt t1 0H t1(). ! nu$%e 0H DC. v nu$%e 0H 2. flag %oolean.
)'8IN

Copy rights are reserved.

270
nt5e/tend(@). d%$sMoutput5putMline(=Count H = XX nt5!ount). if nt5li$it is null then d%$sMoutput5putMline(=No li$it to Nested Ta%les=). else d%$sMoutput5putMline(=Li$it H = XX nt5li$it). end if. fo i in 255nt5!ount loop fo J in 255nt5!ount loop nt(i)(J) 0H !h (!). ! 0H ! G 2. if ! H S2 then ! 0H SV. end if. end loop. end loop. d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=). fo i in 255nt5!ount loop fo J in 255nt5!ount loop d%$sMoutput5putMline(=ntN= XX i XX =ON= XX J XX =O H = XX nt(i)(J)). end loop. end loop. d%$sMoutput5putMline(=Bi st inde/ H = XX nt5fi st). d%$sMoutput5putMline(=Last inde/ H = XX nt5last). d%$sMoutput5putMline(=Ne/t inde/ H = XX nt5ne/t(1)). d%$sMoutput5putMline(=A evious inde/ H = XX nt5p io (4)). flag 0H nt5e/ists(1). if flag H t ue then d%$sMoutput5putMline(=Inde/ 1 e/ists=). else d%$sMoutput5putMline(=Inde/ 1 e/ists=). end if. nt5e/tend(1). nt(2)(C) 0H =Q=. nt(2)(D) 0H =R=.

Copy rights are reserved.

271
nt(1)(C) 0H =S=. nt(1)(D) 0H =T=. nt(4)(C) 0H =U=. nt(4)(D) 0H =9=. nt(@)(C) 0H =6=. nt(@)(D) 0H =+=. nt(C)(2) 0H =*=. nt(C)(1) 0H =Z=. nt(C)(4) 0H =a=. nt(C)(@) 0H =%=. nt(C)(C) 0H =!=. nt(C)(D) 0H =d=. nt(D)(2) 0H =e=. nt(D)(1) 0H =f=. nt(D)(4) 0H =g=. nt(D)(@) 0H =h=. nt(D)(C) 0H =i=. nt(D)(D) 0H =J=. d%$sMoutput5putMline(=(fte e/tend of one inde/# Count H = XX nt5!ount). d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=). fo i in 255nt5!ount loop fo J in 255nt5!ount loop d%$sMoutput5putMline(=ntN= XX i XX =ON= XX J XX =O H = XX nt(i)(J)). end loop. end loop. nt5t i$. d%$sMoutput5putMline(=(fte t i$ of one inde/e# Count H = XX nt5!ount). nt5t i$(1). d%$sMoutput5putMline(=(fte t i$ of two inde/es# Count H = XX nt5!ount). d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=). fo i in 255nt5!ount loop fo J in 255nt5!ount loop d%$sMoutput5putMline(=ntN= XX i XX =ON= XX J XX =O H = XX nt(i)(J)). end loop. end loop.

Copy rights are reserved.

272
nt5delete(1). d%$sMoutput5putMline(=(fte delete of se!ond inde/# Count H = XX nt5!ount). d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=). loop e/it when v H @. fo J in 255nt5!ountG2 loop d%$sMoutput5putMline(=ntN= XX v XX =ON= XX J XX =O H = XX nt(v)(J)). end loop. v 0H v G 2. if vH 1 then v 0H 4. end if. end loop. nt5delete. d%$sMoutput5putMline(=(fte delete of enti e nested ta%le# Count H = XX nt5!ount).
'ND.

Output0 Count H @ No li$it to Nested Ta%les N'ST'D T()L' 'L'M'NTS ntN2ON2O H ( ntN2ON1O H ) ntN2ON4O H C ntN2ON@O H D ntN1ON2O H ' ntN1ON1O H B ntN1ON4O H 8 ntN1ON@O H : ntN4ON2O H I ntN4ON1O H > ntN4ON4O H F ntN4ON@O H L

Copy rights are reserved.

273
ntN@ON2O H M ntN@ON1O H N ntN@ON4O H O ntN@ON@O H A Bi st inde/ H 2 Last inde/ H @ Ne/t inde/ H 4 A evious inde/ H 1 Inde/ 1 e/ists (fte e/tend of one inde/# Count H D N'ST'D T()L' 'L'M'NTS ntN2ON2O H ( ntN2ON1O H ) ntN2ON4O H C ntN2ON@O H D ntN2ONCO H Q ntN2ONDO H R ntN1ON2O H ' ntN1ON1O H B ntN1ON4O H 8 ntN1ON@O H : ntN1ONCO H S ntN1ONDO H T ntN4ON2O H I ntN4ON1O H > ntN4ON4O H F ntN4ON@O H L ntN4ONCO H U ntN4ONDO H 9 ntN@ON2O H M ntN@ON1O H N ntN@ON4O H O ntN@ON@O H A ntN@ONCO H 6 ntN@ONDO H +

Copy rights are reserved.

274
ntNCON2O H * ntNCON1O H Z ntNCON4O H a ntNCON@O H % ntNCONCO H ! ntNCONDO H d ntNDON2O H e ntNDON1O H f ntNDON4O H g ntNDON@O H h ntNDONCO H i ntNDONDO H J (fte t i$ of one inde/e# Count H C (fte t i$ of two inde/es# Count H 4 N'ST'D T()L' 'L'M'NTS ntN2ON2O H ( ntN2ON1O H ) ntN2ON4O H C ntN1ON2O H ' ntN1ON1O H B ntN1ON4O H 8 ntN4ON2O H I ntN4ON1O H > ntN4ON4O H F (fte delete of se!ond inde/# Count H 1 N'ST'D T()L' 'L'M'NTS ntN2ON2O H ( ntN2ON1O H ) ntN2ON4O H C ntN4ON2O H I ntN4ON1O H > ntN4ON4O H F (fte delete of enti e nested ta%le# Count H 3 '/40
D'CL(R'

Copy rights are reserved.

275
t7pe t2 is ta%le of va !ha (1) inde/ %7 %ina 7Mintege . t7pe t1 is ta%le of t2 inde/ %7 %ina 7Mintege . i%t t1. flag %oolean.
)'8IN

d%$sMoutput5putMline(=Count H = XX i%t5!ount). if i%t5li$it is null then d%$sMoutput5putMline(=No li$it to Inde/"%7 Ta%les=). else d%$sMoutput5putMline(=Li$it H = XX i%t5li$it). end if. i%t(2)(2) 0H =a=. i%t(@)(C) 0H =%=. i%t(C)(2) 0H =!=. i%t(D)(1) 0H =d=. i%t(W)(4) 0H =e=. i%t(4)(@) 0H =f=. d%$sMoutput5putMline(=IND'+")* T()L' 'L'M'NTS=). d%$sMoutput5putMline(=i%t(N2ON2O H = XX i%t(2)(2)). d%$sMoutput5putMline(=i%t(N@ONCO H = XX i%t(@)(C)). d%$sMoutput5putMline(=i%t(NCON2O H = XX i%t(C)(2)). d%$sMoutput5putMline(=i%t(NDON1O H = XX i%t(D)(1)). d%$sMoutput5putMline(=i%t(NWON4O H = XX i%t(W)(4)). d%$sMoutput5putMline(=i%t(N4ON@O H = XX i%t(4)(@)). d%$sMoutput5putMline(=Bi st Inde/ H = XX i%t5fi st). d%$sMoutput5putMline(=Last Inde/ H = XX i%t5last). d%$sMoutput5putMline(=Ne/t Inde/ H = XX i%t5ne/t(4)). d%$sMoutput5putMline(=A io Inde/ H = XX i%t5p io (W)). i%t(2)(1) 0H =g=. i%t(2)(4) 0H =h=. i%t(2)(@) 0H =i=. i%t(2)(C) 0H =&=. i%t(2)(D) 0H =l=. i%t(2)(V) 0H =$=. i%t(2)(W) 0H =n=. d%$sMoutput5putMline(=Count H = XX i%t5!ount).

Copy rights are reserved.

276
d%$sMoutput5putMline(=IND'+")* T()L' 'L'M'NTS=). fo i in 255W loop d%$sMoutput5putMline(=i%tN2ON= XX i XX =O H = XX i%t(2)(i)). end loop. d%$sMoutput5putMline(=i%t(N@ONCO H = XX i%t(@)(C)). d%$sMoutput5putMline(=i%t(NCON2O H = XX i%t(C)(2)). d%$sMoutput5putMline(=i%t(NDON1O H = XX i%t(D)(1)). d%$sMoutput5putMline(=i%t(NWON4O H = XX i%t(W)(4)). d%$sMoutput5putMline(=i%t(N4ON@O H = XX i%t(4)(@)). flag 0H i%t5e/ists(4). if flag H t ue then d%$sMoutput5putMline(=Inde/ 4 e/ists=). else d%$sMoutput5putMline(=Inde/ 4 e/ists=). end if. i%t5delete(2). d%$sMoutput5putMline(=(fte delete of fi st inde/# Count H = XX i%t5!ount). i%t5delete(@). d%$sMoutput5putMline(=(fte delete of fou th inde/# Count H = XX i%t5!ount). d%$sMoutput5putMline(=IND'+")* T()L' 'L'M'NTS=). d%$sMoutput5putMline(=i%t(NCON2O H = XX i%t(C)(2)). d%$sMoutput5putMline(=i%t(NDON1O H = XX i%t(D)(1)). d%$sMoutput5putMline(=i%t(NWON4O H = XX i%t(W)(4)). d%$sMoutput5putMline(=i%t(N4ON@O H = XX i%t(4)(@)). i%t5delete. d%$sMoutput5putMline(=(fte delete of enti e inde/"%7 ta%le# Count H = XX i%t5!ount).
'ND.

Output0 Count H 3 No li$it to Inde/"%7 Ta%les IND'+")* T()L' 'L'M'NTS i%t(N2ON2O H a i%t(N@ONCO H % i%t(NCON2O H !

Copy rights are reserved.

277
i%t(NDON1O H d i%t(NWON4O H e i%t(N4ON@O H f Bi st Inde/ H 2 Last Inde/ H W Ne/t Inde/ H @ A io Inde/ H D Count H D IND'+")* T()L' 'L'M'NTS i%tN2ON2O H a i%tN2ON1O H g i%tN2ON4O H h i%tN2ON@O H i i%tN2ONCO H & i%tN2ONDO H l i%tN2ONVO H $ i%tN2ONWO H n i%t(N@ONCO H % i%t(NCON2O H ! i%t(NDON1O H d i%t(NWON4O H e i%t(N4ON@O H f Inde/ 4 e/ists (fte delete of fi st inde/# Count H C (fte delete of fou th inde/# Count H @ IND'+")* T()L' 'L'M'NTS i%t(NCON2O H ! i%t(NDON1O H d i%t(NWON4O H e i%t(N4ON@O H f (fte delete of enti e inde/"%7 ta%le# Count H 3 '/@0
D'CL(R'

t7pe t2 is ta%le of va !ha (1) inde/ %7 %ina 7Mintege . t7pe t1 is ta%le of t2 inde/ %7 %ina 7Mintege .

Copy rights are reserved.

278
t7pe t4 is ta%le of t1. nt t4 0H t4(). ! nu$%e 0H DC.
)'8IN

nt5e/tend(1). d%$sMoutput5putMline(=Count H = XX nt5!ount). fo i in 255nt5!ount loop fo J in 255nt5!ount loop fo & in 255nt5!ount loop nt(i)(J)(&) 0H !h (!). ! 0H ! G 2. end loop. end loop. end loop. d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=). fo i in 255nt5!ount loop fo J in 255nt5!ount loop fo & in 255nt5!ount loop d%$sMoutput5putMline(=ntN= XX i XX =ON= XX J XX =ON= XX & XX =O H = XX nt(i)(J)(&)). end loop. end loop. end loop.
'ND.

Output0 Count H 1 N'ST'D T()L' 'L'M'NTS ntN2ON2ON2O H ( ntN2ON2ON1O H ) ntN2ON1ON2O H C ntN2ON1ON1O H D ntN1ON2ON2O H ' ntN1ON2ON1O H B ntN1ON1ON2O H 8 ntN1ON1ON1O H :

Copy rights are reserved.

279

O)>'CTS US'D IN T:' '+(MAL'S

SQL-

sele!t E f o$ student. SN(M' sa&eth s inu div7a $anogni ! eate o SM(RFS 233 133 433 @33 epla!e t7pe add as o%Je!t(hno nu$%e (1)#!it7

SNO 2 1 4 @

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

SQL-

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' RanJit Satish S inu

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

Copy rights are reserved.

280

'RROR :(NDLIN8
AL?SQL i$ple$ents e o handling with e/!eptions and e/!eption handle s5 '/!eptions with 7ou own use "defined e o s5 )7 using

!an %e asso!iated with o a!le e o s o

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

Copy rights are reserved.

281
'/!eptions a e designed fo handling5 :(NDLIN8 '+C'ATIONS 6hen e/!eption is aised# !ont ol passes to the e/!eption se!tion of the %lo!&5 The so$e o all of the e/!eptions5 (n e/!eption un"ti$e e o handling# athe than !o$pile"ti$e e o

e/!eption se!tion !onsists of handle s fo o!!u s# and the e/!eption is aised5 S7nta/0
'+C'ATION

handle !ontains the !ode that is e/e!uted when the e o asso!iated with the e/!eption

6hen e/!eptionMna$e then SeUuen!eMofMstate$ents. 6hen e/!eptionMna$e then SeUuen!eMofMstate$ents. 6hen othe s then SeUuen!eMofMstate$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
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 de!la e the$ in the de!la ative se!ion5 '/20


D'CL(R'

a nu$%e . % va !ha (1). vM$a &s nu$%e .

Copy rights are reserved.

282
!u so ! is sele!t E f o$ student. t7pe t is va a7(4) of va !ha (1). va t 0H t(=a=#=%=). va2 t.
)'8IN "" NOMD(T(MBOUND )'8IN

sele!t s$a &s into vM$a &s f o$ student whe e sno H C3.
'+C'ATION

when noMdataMfound then d%$sMoutput5putMline(=Invalid student nu$%e =).


'ND. "" CURSORM(LR'(D*MOA'N )'8IN

open !. open !.
'+C'ATION

when !u so Mal ead7Mopen then d%$sMoutput5putMline(=Cu so is al ead7 opened=).


'ND. "" IN9(LIDMCURSOR )'8IN

!lose !. open !. !lose !. !lose !.


'+C'ATION

when invalidM!u so then d%$sMoutput5putMline(=Cu so is al ead7 !losed=).


'ND. "" TOOMM(N*MRO6S )'8IN

sele!t s$a &s into vM$a &s f o$ student whe e sno - 2.


'+C'ATION

when tooM$an7M ows then d%$sMoutput5putMline(=Too $an7 values a e !o$ing to $a &s va ia%le=).
'ND.

Copy rights are reserved.

283
"" Z'ROMDI9ID' )'8IN

a 0H C?3.
'+C'ATION

when Te oMdivide then d%$sMoutput5putMline(=Divided %7 Te o " invalid ope ation=).


'ND. "" 9(LU'M'RROR )'8IN

% 0H =sa&eth=.
'+C'ATION

when valueMe o then d%$sMoutput5putMline(=Invalid st ing length=).


'ND. "" IN9(LIDMNUM)'R )'8IN

inse t into student values(=a=#=s inu=#233).


'+C'ATION

when invalidMnu$%e then d%$sMoutput5putMline(=Invalid nu$%e =).


'ND. "" SU)SCRIATMOUTSID'MLIMIT )'8IN

va(@) 0H =!=.
'+C'ATION

when su%s! iptMoutsideMli$it then d%$sMoutput5putMline(=Inde/ is g eate than the li$it=).


'ND. "" SU)SCRIATM)'*ONDMCOUNT )'8IN

va(4) 0H =!=.
'+C'ATION

when su%s! iptM%e7ondM!ount then d%$sMoutput5putMline(=Inde/ is g eate than the !ount=).


'ND. "" COLL'CTIONMISMNULL )'8IN

va2(2) 0H =a=.
'+C'ATION

Copy rights are reserved.

284
when !olle!tionMisMnull then d%$sMoutput5putMline(=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 Te 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

'/10
D'CL(R'

! nu$%e .
)'8IN

! 0H C?3.
'+C'ATION

when Te oMdivide then d%$sMoutput5putMline(=Invalid Ope ation=). when othe s then d%$sMoutput5putMline(=B o$ OT:'RS handle 0 Invalid Ope ation=).
'ND.

Output0 Invalid Ope ation US'R"D'BIN'D '+C'ATIONS

Copy rights are reserved.

285

( 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%$sMoutput5putMline(=e is aised=).


'ND.

Output0 e is aised )ULIT"IN 'RROR BUNCTIONS


SQLCOD' (ND SQL'RRM

SQLCOD'

etu ns the !u ent e o

!ode# and

SQL'RRM

etu ns the !u ent e o

$essage te/t.

Bo use "defined e/!eption


e/!eptionQ5

SQLCOD'

etu ns 2 and

SQL'RRM

etu ns Puse "deifned

SQL'RRM wiil ta&e onl7 negative value e/!ept 2335 If an7 positive value othe than 233 etu ns non"o a!le e/!eption5

'/20
D'CL(R'

e e/!eption. vMdna$e va !ha (23).


)'8IN "" US'R"D'BIN'D '+C'ATION

Copy rights are reserved.

286
)'8IN

aise e.
'+C'ATION

when e then d%$sMoutput5putMline(SQLCOD' XX = = XX


'ND. "" AR'D'BIN'D '+C'ATION )'8IN SQL'RRM).

sele!t dna$e into vMdna$e f o$ dept whe e deptno H C3.


'+C'ATION

when noMdataMfound then d%$sMoutput5putMline(SQLCOD' XX = = XX


'ND. 'ND. SQL'RRM).

Output0 2 Use "Defined '/!eption 233 OR("32@340 no data found

'/10
)'8IN

d%$sMoutput5putMline(SQL'RRM(233)). d%$sMoutput5putMline(SQL'RRM(3)). d%$sMoutput5putMline(SQL'RRM(2)). d%$sMoutput5putMline(SQL'RRM("233)). d%$sMoutput5putMline(SQL'RRM("C33)). d%$sMoutput5putMline(SQL'RRM(133)). d%$sMoutput5putMline(SQL'RRM("S33)).


'ND.

Output0 OR("32@340 no data found OR("33330 no $al# su!!essful !o$pletion Use "Defined '/!eption OR("332330 no data found OR("33C330 Message C33 not found. p odu!tHRD)MS. fa!ilit7HOR( "1330 non"OR(CL' e/!eption

Copy rights are reserved.

287
OR("33S330 invalid SQL state$ent
D)MSMUTILIT*5BORM(TM'RRORMST(CF

The %uilt"in fun!tion# li&e


e o 5

SQL'RRM#

etu ns the $essage asso!iated with the !u ent

It diffe s f o$ SQL'RRM in two wa7s0


Its length is not est i!ted. it will etu n the full e o $essage st ing5 *ou !an not pass an e o !ode nu$%e to this fun!tion. it !annot %e used to etu n the $essage fo a ando$ e o !ode5 '/0
D'CL(R'

v nu$%e 0H =a%=.
)'8IN

null.
'+C'ATION

when othe s then d%$sMoutput5putMline(d%$sMutilit75fo $atMe o Msta!&).


'ND.

Output0 de!la e E '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 OR("3DC210 at line 1
D)MSMUTILIT*5BORM(TMC(LLMST(CF

This fun!tion
AL?SQL

etu ns a fo $atted st ing showing the e/e!ution !all sta!& inside 7ou t a!ing the e/e!tution of 7ou !ode5 *ou $a7 not use this fun!tion in

appli!ation5 Its usefulness is not est i!ted to e o $anage$ent. 7ou will also find

its hand7 fo

e/!eption %lo!&5 '/0

Copy rights are reserved.

288
)'8IN

d%$sMoutput5putMline(d%$sMutilit75fo $atM!allMsta!&).
'ND.

Output0 """"" AL?SQL Call Sta!& """"" O%Je!tMhandle DSVD3@VW


D)MSMUTILIT*5BORM(TM'RRORM)(CFTR(C'

lineMnu$%e 1

o%Je!tMna$e anon7$ous %lo!&

It displa7s the e/e!ution sta!& at the point whe e an e/!eption was aised5 Thus # 7ou !an !all this fun!tion with an e/!eption se!tion at the top level of 7ou sta!& and still find out whe e the e o was aised deep within the !all sta!&5 '/0
CR'(T' OR R'AL(C' AROC'DUR' A2 IS )'8IN

d%$sMoutput5putMline(=f o$ p o!edu e 2=). aise valueMe o .


'ND A2. CR'(T' OR R'AL(C' AROC'DUR' A1 IS )'8IN

d%$sMoutput5putMline(=f o$ p o!edu e 1=). p2.


'ND A1.

CR'(T' OR R'AL(C' AROC'DUR' A4 IS )'8IN

d%$sMoutput5putMline(=f o$ p o!edu e 4=). p1.


'+C'ATION

when othe s then d%$sMoutput5putMline(d%$sMutilit75fo $atMe o M%a!&t a!e).


'ND A4.

Output0
SQL-

e/e! p4

Copy rights are reserved.

289

f o$ p o!edu e 4 f o$ p o!edu e 1 f o$ p o!edu e 2 OR("3DC210 at bS(F'T:5A2b# line @ OR("3DC210 at bS(F'T:5A1b# line @ OR("3DC210 at bS(F'T:5A4b# line @ '+C'ATIONMINIT AR(8M( Using this 7ou !an asso!iate a na$ed e/!eption with a 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'ATIONMINIT(e(ception_name# OT:'RS

handle 5

oracle_error_number).

'/0
D'CL(R'

e e/!eption. p ag$a e/!eptionMinit(e#"2@VD). ! nu$%e .


)'8IN

! 0H C?3.
'+C'ATION

when e then d%$sMoutput5putMline(=Invalid Ope ation=).


'ND.

Output0 Invalid Ope ation R(IS'M(AALIC(TIONM'RROR *ou !an use this %uilt"in fun!tion to ! eate 7ou own e o $essages# whi!h !an %e $o e des! iptive than na$ed e/!eptions5 S7nta/0

Copy rights are reserved.

290
R(IS'M(AALIC(TIONM'RROR(error_number#

error_me!!a"e## N eep_error!_'la"O).
TRU'#

The )oolean pa a$ete

eep_error!_'la" is optional5 If it is aised5 If it is


B(LS'#

the new e o is added will

to the list of e o s al ead7

whi!h is default# the new e o

epla!e the !u ent list of e o s5 '/0


D'CL(R'

! nu$%e .
)'8IN

! 0H C?3.
'+C'ATION

when Te oMdivide then aiseMappli!ationMe o ("13111#=Invalid Ope ation=).


'ND.

Output0
D'CL(R'

E
'RROR

at line 20

OR("131110 Invalid Ope ation OR("3DC210 at line V '+C'ATION AROA(8(TION '/!eptions !an o!!u AL?SQL %lo!&5
'+C'ATION R(IS'D IN T:' '+'CU(T()L' S'CTION

in the de!la ative# the e/e!uta%le# o

the e/!eption se!tion of a

'/!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

Copy rights are reserved.

291
)'8IN

aise e.
'ND. '+C'ATION

when e then d%$sMoutput5putMline(=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) 0H =a%!d=.


)'8IN

d%$sMoutput5putMline(=:ello=).
'+C'ATION

when othe s then d%$sMoutput5putMline(=Invalid st ing length=).


'ND.

Copy rights are reserved.

292

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 OR("3DC210 at line 1 '/10
)'8IN D'CL(R'

! nu$%e (4) 0H =a%!d=.


)'8IN

d%$sMoutput5putMline(=:ello=).
'+C'ATION

when othe s then d%$sMoutput5putMline(=Invalid st ing length=).


'ND. '+C'ATION

when othe s then d%$sMoutput5putMline(=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%$sMoutput5putMline(=e2 is aised=).

Copy rights are reserved.

293
aise e1. when e1 then d%$sMoutput5putMline(=e1 is aised=).
'ND.

Output0 e2 is aised D'CL(R' E 'RROR at line 20 OR("3DC230 AL?SQL0 unhandled use "defined e/!eption OR("3DC210 at line S OR("3DC230 AL?SQL0 unhandled use "defined e/!eption '/10
D'CL(R'

e2 e/!eption. e1 e/!eption.
)'8IN )'8IN

aise e2.
'+C'ATION

when e2 then d%$sMoutput5putMline(=e2 is aised=). aise e1. when e1 then d%$sMoutput5putMline(=e1 is aised=).
'ND. '+C'ATION

when e1 then d%$sMoutput5putMline(=B o$ oute %lo!&0 e1 is aised=).


'ND.

Output0 e2 is aised B o$ oute %lo!&0 e1 is aised

Copy rights are reserved.

294
'/40
D'CL(R'

e e/!eption.
)'8IN

aise e.
'+C'ATION

when e then d%$sMoutput5putMline(=e is aised=). aise e.


'ND.

Output0 e is aised D'CL(R' E 'RROR at line 20 OR("3DC230 AL?SQL0 unhandled use "defined e/!eption OR("3DC210 at line W OR("3DC230 AL?SQL0 unhandled use "defined e/!eption

R'STRICTIONS *ou !an not pass e/!eption as an a gu$ent to a su%p og a$5

Copy rights are reserved.

295

D(T()(S' TRI88'RS
T igge s a e si$ila to p o!edu es o fun!tions in that the7 a e na$ed AL?SQL %lo!&s with de!la ative# e/e!uta%le# and e/!eption handling se!tions5 ( t igge is e/e!uted i$pli!itl7 wheneve the t igge ing event happens5 The a!t of e/e!uting a t igge is &nown as fi ing the t igge 5 R'STRICTIONS ON TRI88'R'S Li&e pa!&ages# t igge s $ust %e sto ed as stand"alone o%Je!ts in the data%ase and !annot %e lo!al to a %lo!& o pa!&age5 ( t igge does not a!!ept a gu$ents5

US' OB TRI88'RS

Copy rights are reserved.

296
Maintaining !o$ple/ integ it7 !onst aints not possi%le th ough de!la ative

!onst aints ena%le at ta%le ! eation5 (uditing info $ation in a ta%le %7 the$5 (uto$ati!all7 signaling othe !hages a e $ade to a ta%le5 Ae fo $ validation on !hanges %eing $ade to ta%les5 (uto$ate $aintenan!e of the data%ase5 p og a$s that a!tion needs to ta&e pla!e when e!o ding the !hanges $ade and who $ade

T*A'S OB TRI88'RS DML T igge s Instead of T igge s DDL T igge s S7ste$ T igge s Suspend T igge s

C(T'8ORI'S Ti$ing Level "" "" )efo e o (fte Row o State$ent

Row level t igge fi es on!e fo ea!h ow affe!ted %7 the t igge ing state$ent5 Row level t igge is identified %7 the
BOR '(C: RO6

!lause5

State$ent level t igge fi es on!e eithe %efo e o afte the state$ent5 DML TRI88'R S*NT(+ C eate o epla!e t igge ,tri""er_name-

^)efo e X afte _ ^inse t o update o delete_ on ,table_nameNBo ea!h owO

Copy rights are reserved.

297
N6hen (R)O NDe!la eO "" de!la ation )egin "" t igge %od7 N'/!eptionO "" e/!eption se!tion 'nd ,tri""er_name-. DML TRI88'RS ( t igge is fi ed on an
INS'RT# UAD(T'#

DML

D'L'T'

ope ation on a data%ase ta%le5 It !an

%e fi ed eithe %efo e o afte the state$ent e/e!utes# and !an %e fi ed on!e pe affe!ted ow# o on!e pe state$ent5 The !o$%ination of these fa!to s dete $ines the t7pes of the t igge s5 These a e a total of 21 possi%le t7pes (4 state$ents E 1 ti$ing E 1 levels)5
ST(T'M'NT L'9'L

State$ent level t igge fi es onl7 on!e5 '/0 ! eate ta%le state$entMlevel(!ount va !ha (C3)).

SQL-

CR'(T' OR R'AL(C' TRI88'R ST(T'M'NTML'9'LMTRI88'R

afte update on student


)'8IN

inse t into state$entMlevel values(=State$ent level fi ed=).


'ND ST(T'M'NTML'9'LMTRI88'R.

Output0
SQL-

update student set s$a &sHC33. 4 ows updated5

Copy rights are reserved.

298
SQL-

sele!t E f o$ state$entMlevel. COUNT """""""""""""""""""""""""""" State$ent level fi ed

RO6 L'9'L

Row level t igge fi es on!e fo ea!h ow affe!ted %7 the t igge ing state$ent5 '/0
SQL-

! eate ta%le owMlevel(!ount va !ha (C3)).

CR'(T' OR R'AL(C' TRI88'R RO6ML'9'LMTRI88'R

afte update on student


)'8IN

inse t into owMlevel values(=Row level fi ed=).


'ND RO6ML'9'LMTRI88'R.

Output0
SQL-

update student set s$a &sHC33. 4 ows updated5 sele!t E f o$ state$entMlevel. COUNT """""""""""""""""""""""""""" Row level fi ed Row level fi ed Row level fi ed

SQL-

ORD'R OB DML TRI88'R BIRIN8

Copy rights are reserved.

299
'/0 Suppose we have a follwing ta%le5 sele!t E f o$ student. NO N(M' 2 1 4 @ a % ! d M(RFS 233 133 433 @33 )efo e state$ent level )efo e ow level (fte ow level

(fte state$ent level

SQL-

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

SQL-

! eate ta%le fi ingMo de (o de va !ha (C3)).


CR'(T' OR R'AL(C' TRI88'R )'BOR'MST(T'M'NT

%efo e inse t on student


)'8IN

inse t into fi ingMo de values(=)efo e State$ent Level=).


'ND )'BOR'MST(T'M'NT. CR'(T' OR R'AL(C' TRI88'R )'BOR'MRO6

%efo e inse t on student fo ea!h ow


)'8IN

inse t into fi ingMo de values(=)efo e Row Level=).


'ND )'BOR'MRO6.

CR'(T' OR R'AL(C' TRI88'R (BT'RMST(T'M'NT

afte inse t on student


)'8IN

inse t into fi ingMo de values(=(fte State$ent Level=).


'ND (BT'RMST(T'M'NT.

Copy rights are reserved.

300

CR'(T' OR R'AL(C' TRI88'R (BT'RMRO6

afte inse t on student fo ea!h ow


)'8IN

inse t into fi ingMo de values(=(fte Row Level=).


'ND (BT'RMRO6.

Output0
SQL-

sele!t E f o$ fi ingMo de .

no ows sele!ted inse t into student values(C#=e=#C33).

SQL-

2 ow ! eated5 sele!t E f o$ fi ingMo de .

SQL-

ORD'R """""""""""""""""""""""""""""""""""""""""""""""""" )efo e State$ent Level )efo e Row Level (fte Row Level (fte State$ent Level sele!t E f o$ student. NO N(M' 2 1 4 @ a % ! d M(RFS 233 133 433 @33

SQL-

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

Copy rights are reserved.

301
C e C33

CORR'L(TION ID'NTIBI'RS IN RO6"L'9'L TRI88'RS

Inside the t igge # 7ou !an a!!ess the data in the ow that is !u entl7 %eing p o!essed5 This is a!!o$plished th ough two !o elation identifie s " 0old and 0new5 ( correlation identi'ier is a spe!ial &ind of
AL?SQL#

AL?SQL

%ind va ia%le5 The !olon in f ont of ea!h va ia%les5 The !o$pile will

indi!ates that the7 a e %ind va ia%les# in the sense of host va ia%les used in e$%edded and indi!ates that the7 a e not egula
AL?SQL AL?SQL

t eat the$ as e!o ds of t7pe T igge ingMta%leLRO6T*A'5 (lthough s7nta!ti!all7 the7 a e t eated as e!o ds# in ealit7 the7 a e not5 0old and 0new a e also &nown as p!eudorecord!# fo this eason5
TRI88'RIN8 ST(T'M'NT """""""""""""""""""""""""""""""""""""" INS'RT 0OLD """""""""""""""""""""""""""" 0N'6 """"""""""""""""""""""""""""""""""""""""""""""" NULL5

all fields a e

values that will %e inse ted 6hen the state$ent is !o$pleted5 new values that will %e updated when the state$ent is !o$pleted5 all fields a e
NULL5

UAD(T'

o iginal values fo the ow %efo e the update5 o iginal values %efo e the ow is deleted5

D'L'T'

'/0
SQL-

! eate ta%le $a &s(no nu$%e (1) oldM$a &s nu$%e (4)#newM$a &s nu$%e (4)).
CR'(T' OR R'AL(C' TRI88'R OLDMN'6

%efo e inse t o update o delete on student fo ea!h ow


)'8IN

inse t into $a &s values(0old5no#0old5$a &s#0new5$a &s).


'ND OLDMN'6.

Copy rights are reserved.

302

Output0
SQL-

sele!t E f o$ student. NO N(M' M(RFS

""""" """"""" """""""""" 2 1 4 @ C a % ! d e 233 133 433 @33 C33

SQL-

sele!t E f o$ $a &s.

no ows sele!ted inse t into student values(D#=f=#D33).

SQL-

2 ow ! eated5
SQL-

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

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

SQL-

sele!t E f o$ $a &s. NO OLDMM(RFS N'6MM(RFS """" """"""""""""""" """"""""""""""" D33

Copy rights are reserved.

303

SQL-

update student set $a &sHCCC whe e noHC.

2 ow updated5 sele!t E f o$ student. NO N(M' M(RFS """"" """"""" """""""""" 2 1 4 @ C D a % ! d e f 233 133 433 @33 CCC D33

SQL-

SQL-

sele!t E f o$ $a &s. NO OLDMM(RFS N'6MM(RFS D33 C C33 CCC

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

SQL-

delete student whe e no H 1.

2 ow deleted5 sele!t E f o$ student. NO N(M' 2 4 a ! M(RFS 233 433

SQL-

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

Copy rights are reserved.

304
@ C D d e f @33 CCC D33

SQL-

sele!t E f o$ $a &s. NO OLDMM(RFS N'6MM(RFS

""""" """""""""""""" """""""""""""""" D33 C 1


R'B'R'NCIN8 CL(US'

C33 133

CCC

If desi ed# 7ou !an use the

R'B'R'NCIN8

!lause to spe!if7 a diffe ent na$e fo


6:'N

0old ane

0new5 This !lause is found afte the t igge ing event# %efo e the S7nta/0
R'B'R'NCIN8

!lause5

Nold as oldMna$eO Nnew as newMna$eO

'/0
CR'(T' OR R'AL(C' TRI88'R R'B'R'NC'MTRI88'R

%efo e inse t o update o delete on student efe en!ing old as oldMstudent new as newMstudent fo ea!h ow
)'8IN

inse t into $a &s values(0oldMstudent5no#0oldMstudent5$a &s#0newMstudent5$a &s).


'ND R'B'R'NC'MTRI88'R. 6:'N CL(US'

6:'N

!lause is valid fo

ow"level t igge s onl75 If p esent# the t igge


6:'N

%od7 will %e !lause5

e/e!uted onl7 fo those ows that $eet the !ondition spe!ified %7 the S7nta/0
6:'N

tri""er_condition.

Copy rights are reserved.

305
6he e tri""er_condition is a )oolean e/p ession5 It will %e evaluated fo ea!h ow5 The =new and =old
R'B'R'NCIN8#

e!o ds !an %e

efe en!ed inside tri""er_condition as well# %ut li&e

the !olon is not used the e5 The !olon is onl7 valid in the t igge %od75

'/0
CR'(T' OR R'AL(C' TRI88'R 6:'NMTRI88'R

%efo e inse t o update o delete on student efe en!ing old as oldMstudent new as newMstudent fo ea!h ow when (newMstudent5$a &s - C33)
)'8IN

inse t into $a &s values(0oldMstudent5no#0oldMstudent5$a &s#0newMstudent5$a &s).


'ND 6:'NMTRI88'R. TRI88'R AR'DIC(T'S

The e a e th ee )oolean fun!tions that 7ou !an use to dete $ine what the ope ation is5 The p edi!ates a e
INS'RTIN8 UAD(TIN8 D'L'TIN8

'/0
SQL-

! eate ta%le p edi!ates(ope ation va !ha (13)).


CR'(T' OR R'AL(C' TRI88'R AR'DIC(T'MTRI88'R

%efo e inse t o update o delete on student


)'8IN

if inse ting then inse t into p edi!ates values(=Inse t=). elsif updating then inse t into p edi!ates values(=Update=). elsif deleting then inse t into p edi!ates values(=Delete=). end if.

Copy rights are reserved.

306
'ND AR'DIC(T'MTRI88'R.

Output0
SQL-

delete student whe e noH2.

2 ow deleted5 sele!t E f o$ p edi!ates. MS8 """"""""""""""" Delete inse t into student values(V#=g=#V33).

SQL-

SQL-

2 ow ! eated5 sele!t E f o$ p edi!ates. MS8 """"""""""""""" Delete Inse t update student set $a &s H VVV whe e noHV.

SQL-

SQL-

2 ow updated5 sele!t E f o$ p edi!ates. MS8 """"""""""""""" Delete Inse t Update

SQL-

Copy rights are reserved.

307
INST'(D"OB TRI88'RS Instead"of t igge s fi e instead of a ope ation5 (lso# instead"of t igge s !an %e defined

DML

onl7 on views5 Instead"of t igge s a e used in two !ases0 To allow a view that would othe wise not %e $odifia%le to %e $odified5 To $odif7 the !olu$ns of a nested ta%le !olu$n in a view5

'/0

SQL-

! eate view e$pMdept as sele!t e$pno#ena$e#Jo%#dna$e#lo!#sal#e5deptno f o$ e$p e# dept d whe e e5deptno H d5deptno.

CR'(T' OR R'AL(C' TRI88'R INST'(DMOBMTRI88'R

instead of inse t on e$pMdept


)'8IN

inse t into dept2 values(C3#= d=#=%ang=). inse t into e$p2(e$pno#ena$e#Jo%#sal#deptno)values(1111#=sa&eth=#=do!to =#W333#C3).


'ND INST'(DMOBMTRI88'R.

Output0 inse t into e$pMdept values(1111#=sa&eth=#=do!to =#W333#= d=#=%ang=#C3). sele!t E f o$ e$pMdept. 'N(M' SMIT: (LL'N 6(RD >ON'S )L(F' CL(RF SCOTT FIN8 >O) CL'RF S(L'SM(N S(L'SM(N M(N(8'R M(N(8'R M(N(8'R (N(L*ST AR'SID'NT S(L """"""""""" W33 2D33 21C3 1SVC 21C3 1WC3 1@C3 4333 C333 2C33 DN(M' R'S'(RC: S(L'S S(L'S R'S'(RC: S(L'S S(L'S R'S'(RC: (CCOUNTIN8 S(L'S LOC D(LL(S C:IC(8O C:IC(8O D(LL(S C:IC(8O C:IC(8O D(LL(S N'6 *ORF C:IC(8O D'ATNO """""""""" 13 43 43 13 43 43 23 13 23 43

SQLSQL-

'MANO V4DS V@SS VC12 VCDD VDC@ VDSW VVW1 VVWW VW4S VW@@

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

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

M(RTIN S(L'SM(N

(CCOUNTIN8 N'6 *ORF

TURN'R S(L'SM(N

Copy rights are reserved.

308
VWVD VS33 VS31 VS4@ 1111 (D(MS >(M'S BORD MILL'R sa&eth CL'RF CL'RF (N(L*ST CL'RF do!to 2233 SC3 4333 2433 W333 R'S'(RC: S(L'S R'S'(RC: (CCOUNTIN8 d D(LL(S C:IC(8O D(LL(S N'6 *ORF %ang 13 43 13 23 C3

SQL-

sele!t E f o$ dept. D'ATNO """""""""" 23 13 43 @3 C3 DN(M' """""""""""""""" (CCOUNTIN8 R'S'(RC: S(L'S OA'R(TIONS d LOC """"""""""" N'6 *ORF D(LL(S C:IC(8O )OSTON %ang

SQL-

sele!t E f o$ e$p. 'N(M' SMIT: (LL'N 6(RD >ON'S M(RTIN )L(F' CL(RF SCOTT FIN8 (D(MS >(M'S BORD >O) CL'RF S(L'SM(N S(L'SM(N M(N(8'R S(L'SM(N M(N(8'R M(N(8'R (N(L*ST AR'SID'NT VDSW VVWW VDSW VCDD CL'RF CL'RF (N(L*ST M8R VS31 VDSW VDSW VW4S VDSW VW4S VW4S VCDD :IR'D(T' 2 V"D'C"W3 13"B')"W2 11"B')"W2 31"(AR"W2 1W"S'A"W2 32"M(*"W2 3S">UN"W2 2S"(AR"WV 2V"NO9"W2 3W"S'A"W2 14"M(*"WV 34"D'C"W2 34"D'C"W2 S(L """"""""" W33 2D33 21C3 1SVC 21C3 1WC3 1@C3 4333 C333 2C33 2233 SC3 4333 3 2@33 433 C33 COMM D'ATNO 13 43 43 13 43 43 23 13 23 43 13 43 13

'MANO V4DS V@SS VC12 VCDD VDC@ VDSW VVW1 VVWW VW4S VW@@ VWVD VS33 VS31

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

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

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

TURN'R S(L'SM(N

Copy rights are reserved.

309
VS4@ 1111 DDL TRI88'RS O a!le allows 7ou to define t igge s that will fi e when Data Definition Language state$ents a e e/e!uted5 S7nta/0 C eate o epla!e t igge ,tri""er_nameMILL'R sa&eth CL'RF do!to VVW1 14">(N"W1 2433 W333 23 C3

^)efo e X afte _ ^DDL event_ on ^data%ase X s!he$a_ N6hen (R)O NDe!la eO "" de!la ation )egin "" t igge %od7 N'/!eptionO "" e/!eption se!tion 'nd ,tri""er_name-. '/0
SQL-

! eate ta%le $7Mo%Je!ts(o%JMna$e va !ha (23)#o%JMt7pe va !ha (23)#o%JMowne va !ha (23)#o%JMti$e date).

CR'(T' OR R'AL(C' TRI88'R CR'(T'MTRI88'R

afte ! eate on data%ase


)'8IN

inse t into $7Mo%Je!ts values(s7s5di!tiona 7Mo%JMna$e#s7s5di!tiona 7Mo%JMt7pe# s7s5di!tiona 7Mo%JMowne # s7sdate).


'ND CR'(T'MTRI88'R.

Output0 sele!t E f o$ $7Mo%Je!ts.

SQL-

Copy rights are reserved.

310

no ows sele!ted ! eate ta%le stud2(no nu$%e (1)). sele!t E f o$ $7Mo%Je!ts. O)>MT*A' T()L' O)>MO6N'R O)>MTIM' """""""""""" 12">UL"3V S*S

SQL-

SQL-

O)>MN(M' STUD2

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

SQL-

! eate seUuen!e ss. ! eate view studMview as sele!t E f o$ stud2. sele!t E f o$ $7Mo%Je!ts.

SQL-

SQL-

O)>MN(M' STUD2 SS STUDM9I'6


6:'N CL(US'

O)>MT*A' T()L'

O)>MO6N'R S*S S*S S*S

O)>MTIM' 12">UL"3V 12">UL"3V 12">UL"3V

"""""""""""""" """"""""""""" """""""""""""""" """"""""""""" S'QU'NC' 9I'6

If

6:'N

p esent# the t igge %od7 will %e e/e!uted onl7 fo those that $eet the !ondition
6:'N

spe!ified %7 the '/0

!lause5

CR'(T' OR R'AL(C' TRI88'R CR'(T'MTRI88'R

afte ! eate on data%ase when (s7s5di!tiona 7Mo%JMt7pe H KT()L';)


)'8IN

inse t into $7Mo%Je!ts values(s7s5di!tiona 7Mo%JMna$e#s7s5di!tiona 7Mo%JMt7pe# s7s5di!tiona 7Mo%JMowne # s7sdate).

Copy rights are reserved.

311
'ND CR'(T'MTRI88'R.

S*ST'M TRI88'RS S7ste$ t igge s will fi e wheneve p ivilege5


ST(RTUA S:UTDO6N LO8ON LO8OBB S'R9'R'RROR

data%ase"wide event o!!u s5 The following a e the 7ou need


(DMINIST'R D(T()(S' TRI88'R

data%ase event t igge s5 To ! eate s7ste$ t igge

S7nta/0 C eate o epla!e t igge ,tri""er_name-

^)efo e X afte _ ^Data%ase event_ on ^data%ase X s!he$a_ N6hen (R)O NDe!la eO "" de!la ation se!tion )egin "" t igge %od7 N'/!eptionO "" e/!eption se!tion 'nd ,tri""er_name-. '/0 ! eate ta%le use Mlogs(uMna$e va !ha (23)#logMti$e ti$esta$p).

SQL-

CR'(T' OR R'AL(C' TRI88'R (BT'RMLO8ON

afte logon on data%ase


)'8IN

inse t into use Mlogs values(use #!u entMti$esta$p).


'ND (BT'RMLO8ON.

Copy rights are reserved.

312
Output0 sele!t E f o$ use Mlogs. no ows sele!ted !onn sa&eth?sa&eth sele!t E f o$ use Mlogs. LO8MTIM' 11">UL"3V 2153V52452@3333 (M

SQL-

SQLSQL-

UMN(M' S(F'T:

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

SQLSQL-

!onn s7ste$?o a!le sele!t E f o$ use Mlogs.

UMN(M' S(F'T: S*ST'M

LO8MTIM' 11">UL"3V 2153V52452@3333 (M 11">UL"3V 2153V54@512W333 (M

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

SQLSQL-

!onn s!ott?tige sele!t E f o$ use Mlogs. LO8MTIM' 11">UL"3V 2153V52452@3333 (M 11">UL"3V 2153V54@512W333 (M 11">UL"3V 2153W5@453S4333 (M

UMN(M' S(F'T: S*ST'M SCOTT


S'R9'R'RROR

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

The

S'R9'R'RROR

event !an %e used to t a!& e o s that o!!u in the data%ase5 The e o


S'R9'RM'RROR

!ode is availa%le inside the t igge th ough the

att i%ute fun!tion5

Copy rights are reserved.

313
'/0 ! eate ta%le $7Me o s(e o M$sg va !ha (133)).

SQL-

CR'(T' OR R'AL(C' TRI88'R S'R9'RM'RRORMTRI88'R

afte se ve e o on data%ase
)'8IN

inse t into $7Me o s values(d%$sMutilit75fo $atMe o Msta!&).


'ND S'R9'RM'RRORMTRI88'R.

Output0 ! eate ta%le ss (no)). E 'RROR at line 20 OR("33S110 $issing o invalid option sele!t E f o$ $7Me o s.

SQL-

! eate ta%le ss (no))

SQL-

'RRORMMS8 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""" OR("33S110 $issing o invalid option inse t into student values(2#1#4). E 'RROR at line 20 OR("33S@10 ta%le o view does not e/ist sele!t E f o$ $7Me o s.

SQL-

inse t into student values(2#1#4)

SQL-

'RRORMMS8 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""" OR("33S110 $issing o invalid option OR("33S@10 ta%le o view does not e/ist
S'R9'RM'RROR (TTRI)UT' BUNCTION

Copy rights are reserved.

314

It ta&es a single nu$%e t7pe of a gu$ent and etu ns the e o at the position on the e o sta!& indi!ated %7 the a gu$ent5 The position 2 is the top of the sta!&5 '/0
CR'(T' OR R'AL(C' TRI88'R S'R9'RM'RRORMTRI88'R

afte se ve e o on data%ase
)'8IN

inse t into $7Me o s values(se ve Me o (2)).


'ND S'R9'RM'RRORMTRI88'R.

SUSA'ND TRI88'RS This will fi e wheneve a state$ent is suspended5 This $ight o!!u as the esult of a

spa!e issue su!h as e/!eeding an allo!ated ta%lepa!e Uuota5 This fun!tionalit7 !an %e used to add ess the p o%le$ and allow the ope atin to !ontinue5

S7nta/0 C eate o afte epla!e t igge ,tri""er_name-

suspend on ^data%ase X s!he$a_

N6hen (R)O NDe!la eO "" de!la ation se!tion )egin "" t igge %od7 N'/!eptionO "" e/!eption se!tion 'nd ,tri""er_name-. '/0 ! eate ta%lespa!e $7Mspa!e datafile =f0d$7Mfile5d%f= siTe 1$. ! eate ta%le student(sno nu$%e (1)#sna$e va !ha (23)) ta%lespa!e $7Mspa!e.

SQLSQL-

Copy rights are reserved.

315
CR'(T' OR R'AL(C' TRI88'R SUSA'NDMTRI88'R

afte suspend on data%ase


)'8IN

d%$sMoutput5putMline(K No oo$ to inse t in 7ou ta%lespa!e=).


'ND SUSA'NDMTRI88'R.

Output0 Inse t $o e ows in student ta%le then # 7ou will get No oo$ to inse t in 7ou ta%lespa!e (UTONOMOUS TR(NS(CTION A io to O a!leWi# the e was no wa7 in whi!h so$e SQL ope ations within a t ansa!tion !ould %e !o$$itted independent of the est of the ope ations5 O a!le allows this# howeve # th ough autonomou! tran!action!5 (n autonomou! tran!action is a t ansa!tion that is sta ted within the !onte/t of anothe t ansa!tion# &nown as pa ent t ansa!tion# %ut is independent of it5 The autono$ous t ansa!tion !an %e !o$$itted o ega dless ot the state of the pa ent t ansa!tion5 '/0
CR'(T' OR R'AL(C' TRI88'R (UTONOMOUSMTR(NS(CTIONMTRI88'R

olled %a!&

afte inse t on student


D'CL(R'

p ag$a autono$ousMt ansa!tion.


)'8IN

update student set $a &s H CCC. !o$$it.


'ND (UTONOMOUSMTR(NS(CTIONMTRI88'R.

Output0 sele!t E f o$ student. NO N( M(RFS

SQL-

Copy rights are reserved.

316
""""" """"" "" """""""""" 2 1 4 a % ! 222 111 433

SQL-

inse t into student values(@#=d=#@@@). sele!t E f o$ student. NO N( 2 1 4 @ a % ! d M(RFS CCC CCC CCC @@@

SQL-

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

R'STRICTIONS ON (UTONOMOUS TR(NS(CTION

If an autono$ous t ansa!tion atte$pts to a!!ess a t ansa!tion# a deadlo!& !an o!!u in 7ou p og a$5

esou !e held %7 the $ain

*ou !annot $a & all p og a$s in a pa!&age as autono$ous with a single

AR(8M(

de!la ation5 *ou $ust indi!ate autono$ous t ansa!tions e/pli!it7 in ea!h p og a$5

To e/it without e o s f o$ an autono$ous t ansa!tion p og a$ that has e/e!uted


at least one oll%a!&5
INS'RT

UAD(T'

D'L'T'#

7ou $ust pe fo $ an e/pli!it !o$$it o

The

COMMIT

and

ROLL)(CF

state$ents end the a!tive autono$ous t ansa!tion# %ut

the7 do not fo !e the te $ination of the autono$ous outine5 *ou !an have $ultiple
COMMIT

and?o

ROLL)(CF

state$ents inside 7ou autono$ous %lo!&5

*ou !an not oll%a!& to a savepoint set in the $ain t ansa!tion5


TR(NS(CTIONS

The

pa a$ete in the o a!le initialiTation file spe!ifies the $a/i$u$

nu$%e of t ansa!tions allowed !on!u entl7 in a session5 The default value is VC fo this# %ut 7ou !an in! ease the li$it5 MUT(TIN8 T()L'S

Copy rights are reserved.

317

The e a e est i!tions on the ta%les and !olu$ns that a t igge %od7 $a7 a!!ess5 In o de to define these ta%les5 ( $utating ta%le is ta%le that is !u entlt7 %eing $odified %7 a DML state$ent and the t igge event also DML state$ent5 ( $utating ta%le e o o!!u s when a ow"level t igge t ies to e/a$ine o !hange a ta%le that is al ead7 unde going !hange5 ( !onst aining ta%le is a ta%le that $ight need to %e ead f o$ fo a efe ential integ it7 !onst aint5 '/0
CR'(T' OR R'AL(C' TRI88'R MUT(TIN8MTRI88'R

est i!tions# it is ne!essa 7 to unde stand $utating and !onst aining

%efo e delete on student fo ea!h ow


D'CL(R'

!t nu$%e .
)'8IN

sele!t !ount(E) into !t f o$ student whe e no H 0old5no.


'ND MUT(TIN8MTRI88'R.

Output0 delete student whe e no H 2.

SQL-

delete student whe e no H 2 E 'RROR at line 20 OR("3@3S20 ta%le SCOTT5STUD'NT is $utating# t igge ?fun!tion $a7 not see it OR("3DC210 at bSCOTT5Tb# line @ OR("3@3WW0 e o du ing e/e!ution of t igge =SCOTT5T=
:O6 TO (9OID MUT(TIN8 T()L' 'RROR [

)7 using autono$ous t ansa!tion

Copy rights are reserved.

318
)7 using state$ent level t igge

Copy rights are reserved.

You might also like