Professional Documents
Culture Documents
1 1 1 1 1
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
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
1 1
a)
S7nta/0
'/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
%)
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-
'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)
!)
S7nta/0 inse t into ,table_name)(col1, col2, col3 Coln) values (value1, value2, value3 . Valuen). '/0
SQLSQL-
inse t into student (no# na$e) values (4# ;Ra$esh;). inse t into student (no# na$e) values (@# ;Madhu;).
d)
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-
'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(
'/0
SQL-
sele!t E f o$ student. NO N(M' """ """""" 2 1 2 1 4 @ C D Sudha Sað >agan Na en Ra$esh Madhu 9isu Rattu M(RFS """""""" 233 133 433 @33
SQL-
NO N(M' """ """""" 2 1 2 1 4 @ C D Sudha Sað >agan Na en Ra$esh Madhu 9isu Rattu
SQL-
1 2 1 4 @ C D
1 1
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
1 1 1 1 2
( ith$eti! ope ato s Co$pa ison ope ato s Logi!al ope ato s
2 3
2 2 2 3 4 3
a)
USIN8
null# not null li&e Logi!al ope ato s (nd O not "" lowest p e!eden!e
'/0
SQL-
SQL-
SQL-
M(RFS """"""""""
SQL-
SQL-
SQL-
SQL-
2 4 @ C D
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 ,conditionn-. '/0
SQL-
!)
USIN8 OR
This will gives the output when eithe of the !onditions %e!o$e t ue5
Na en
@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-
e)
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-
f)
USIN8 IN
This will gives the output %ased on the !olu$n and its list of values spe!ified5
'/0
SQL-
g)
USIN8 NOT IN
This will gives the output %ased on the !olu$n whi!h values a e not in the list of values spe!ified5
S7nta/0 sele!t E f o$ ,table_name- whe e ,col- not in ( value1, value2, value3 valuen).
'/0
SQL-
M(RFS """""""""
h)
USIN8 NULL
This will gives the output %ased on the null values in the spe!ified !olu$n5
'/0
SQL-
M(RFS """""""""
i)
This will gives the output %ased on the not null values in the spe!ified !olu$n5
'/0
SQL-
11
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
SQL-
ii) This will give the ows whose na$e sta t with KS;5
SQL-
iii) This will give the ows whose na$e ends with Kh;5
SQL-
sele!t E f o$ student whe e na$e li&e =Lh=. NO N(M' """ """"""" 1 4 Sað Ra$esh M(RFS """"""""" 133
i9) This will give the ows whose na$e;s se!ond lette sta t with Ka;5
SQL-
9) This will give the ows whose na$e;s thi d lette sta t with Kd;5
SQL-
9i) This will give the ows whose na$e;s se!ond lette sta t with Kt; f o$ ending5
SQL-
13
9ii) This will give the ows whose na$e;s thi d lette sta t with Ke; f o$ ending5
SQL-
9iii) This will give the ows whose na$e !otains 1 a;s5
SQL-
USIN8 ORD'R )*
)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-
NO N(M' """ """"""" 2 2 1 1 4 @ C D Sudha >agan Sað Na en Ra$esh Madhu 9isu Rattu
SQL-
NO N(M' """ """"""" D Rattu C 9isu @ Madhu 4 Ra$esh 1 Sað 1 Na en 2 Sudha 2 >agan
M(RFS """""""""
USIN8 DML
USIN8 UAD(T'
15
S7nta/0 Update ,table_name- set ,col1- H value2# ,col2- H value1 whe e ,condition-.
'/0
SQL-
If 7ou a e not spe!if7ing an7 !ondition this will update enti e ta%le5
SQLSQL-
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.
USIN8 D'L'T'
'/0
SQL-
delete student.
If 7ou a e not spe!if7ing an7 !ondition this will delete enti e ta%le5
SQL-
USIN8 DDL
USIN8 (LT'R
a)
(DDIN8 COLUMN
'/0
SQL-
%)
R'MO9IN8 COLUMN
'/0
SQL-
!)
d)
17
'/0
SQL-
d)
'/0
SQL-
e)
R'N(MIN8 COLUMN
'/0
SQL-
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-
USIN8 DROA
'/0
SQL-
d op ta%le student.
USIN8 R'N(M'
'/0
SQL-
USIN8 TCL
USIN8 COMMIT
1 1
a)
IMALICIT
I$pli!it '/pli!it
19
1
%)
'+ALICIT
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
1 1
S7nta/0 Roll o O
E 6hile p o!ess is going on# if suddenl7 powe goes then o a!le will oll%a!& the
*ou !an use savepoints to oll%a!& po tions of 7ou !u ent set of t ansa!tions5
'/0
SQLSQLSQL-
savepoint s2. inse t into student values(2# Ka;# 233). savepoint s1.
SQLSQLSQLSQLSQL-
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!&
SQL-
sele!t E f o$ student.
SQL-
SQL-
sele!t E f o$ student.
21
USIN8 DCL
DCL !o$$ands a e used to g anting and evo&ing the pe $issions5 USIN8 8R(NT This is used to g ant the p ivileges to othe use s5
'/0
SQLSQLSQL-
"" 7ou !an give individual p ivilege "" 7ou !an give set of p ivileges "" 7ou !an give all p ivileges
The sudha use has to use dot $ethod to a!!ess the o%Je!t5
SQL-
sele!t E f o$ sað5student.
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-
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
'/0
SQL-
"" 7ou !an evo&e individual "" 7ou !an evo&e set of
p ivilege
SQL-
p ivileges
SQL-
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-
23
SQL-
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
'/0
SQL-
COLUMN (LI(S'S
'/0
SQL-
SQL-
T()L' (LI(S'S
If 7ou a e using ta%le aliases 7ou !an use dot $ethod to the !olu$ns5
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
25
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
SQL-
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).
MULTIAL' INS'RTS
6e have ta%le !alled D'AT with the following !olu$ns and data
D'ATNO """""""" 23 13 43 @3
a)
SQL-
C eate ta%le student(no nu$%e (1)#na$e va !ha (1)#$a &s nu$%e (4)).
%)
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.
!)
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.
d)
SQL-
inse t all
27
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)
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.
inse ts @
f)
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)
g)
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)
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
29
i)
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.
EE *ou !an use $ulti ta%les with spe!ified fields# with dupli!ate !onditions# with fi st and else !lauses5
ows# with
BUNCTIONS
Bun!tions !an %e !atego iTed as follows5
1 1
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
1 1 1 1 1
Nu$e i! fun!tions St ing fun!tions Date fun!tions Mis!ellaneous fun!tions Conve sion fun!tions
NUM'RIC BUNCTIONS
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
(%s Sign SU t Mod Nvl Aowe '/p Ln Log Ceil Bloo Round T un& )itand 8 eatest Least
31
1
a)
()S
Coales!e
(%solute value is the $easu e of the $agnitude of value5 (%solute value is alwa7s a positive nu$%e 5
'/0
SQL-
()S(C) C
()S("C) C
"""""""""" """"""""""
%)
SI8N
'/0
SQL-
SI8N(C) """""""""" 2
!)
SQRT
S7nta/0 sU t (value)
'/0
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
'/0
SQL-
dual.
MOD(V#@) """""""""""" 4
e)
N9L
This will su%stitutes the spe!ified value in the pla!e of null values5
'/0
SQL-
sele!t E f o$ student.
33
SQL-
SQL-
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
'/0
SQL-
sele!t powe (1#C)# powe (3#3)# powe (2#2)# powe (null#null)# powe (1#" f o$ dual. AO6'R (1# C) AO6'R (3# 3) AO6'R (2# 2) AO6'R (NULL# NULL)
C)
AO6'R (1#"C)
g)
'+A
sele!t e/p (2)# e/p (1)# e/p (3)# e/p (null)# e/p ("1) f o$ dual.
'+A(2) """"""""
'+A(1) """""""""
15V2W1W2W4 V54WS3CD2
h)
LN
'/0
SQL-
LN (2) """"""" 3
LN (NULL) """"""""""""
(4) H 1353WCC4DS
(1353WCC4DS) H 4
35
i)
LO8
S7nta/0 log (23# value) "" he e value $ust %e g eate than Te o whi!h is positive onl75
'/0
SQL-
LN
(value) H
LO8
('+A(2)# value)
SQL-
LN (4) """""""
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
'/0
SQL-
sele!t !eil (C)# !eil (C52)# !eil ("C)# !eil ("C52)# !eil (3)# !eil (null) f o$
dual.
This will p odu!e a whole nu$%e that is less than o eUual to the spe!ified value5
'/0
SQL-
sele!t floo (C)# floo (C52)# floo ("C)# floo ( "C52)# floo (3)# floo (null) dual.
f o$
BLOOR (C) BLOOR (C52) BLOOR ("C) BLOOR ("C52) (NULL) """"""""""" """"""""""""" """""""""""""""" C l)
ROUND
""""""""""" 3
'/0
SQL-
f o$ dual.
37
ROUND (214514@C) ROUND (214514@C# 3) ROUND (214514@C# 1) ROUND (214514C@#1) """"""""""""""""""""" """"""""""""""""""""""" 214 21451@ 214 214514 """""""""""""""""""""""" """""""""""""""""""""""
SQL-
Round (214514@C#"@) f o$ dual. ROUND (214514@C#"2) ROUND (214514@C#"1) ROUND(214514@C#"4) ROUND(214514@C#"@) """""""""""""""""""""""" """"""""""""""""""""""""" """""""""""""""""""""""" """""""""""""""""""""""" 213 3 233 3
SQL-
ROUND (214#3) ROUND(214#2) ROUND(214#1) """"""""""""""""" """"""""""""""""" """""""""""""""" 214 214 214
SQL-
ROUND ("214# 3) ROUND ("214# 2) ROUND ("214#1) """""""""""""""""" """"""""""""""""" "214 "214 """"""""""""""""""" "214
SQL-
ound(
ROUND(214#"2) ROUND(214#"1) ROUND(214#"4) ROUND("214#"2) ROUND(" 214#"1) ROUND("214#"4) """"""""""""" """"""""""""" """"""""""""" """""""""""""" """""""""""""" """""""""""""""""""""""""" 213 233 3 "213 "233 3
ROUND(NULL#NULL) ROUND(3#3) ROUND(2#2) ROUND("2#"2) ROUND("1#"1) """"""""""""""""""""""" """""""""""""" """""""""""""" """""""""""""""" """""""""""""""" 3 $) 2 3 3
TRUNC
'/0
SQL-
dual.
SQL-
39
TRUNC(214514@C#"2) TRUNC(214514@C#"1) TRUNC(214514@C#"4) TRUNC(214514@C#"@) """""""""""""""""""""""" """""""""""""""""""""""" """""""""""""""""""""""" 213 233 3 3 """""""""""""""""""""""
SQL-
SQL-
SQL-
t un!(
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-
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
'/0
SQL-
sele!t %itand(1#4)# %itand(3#3)# %itand(2#2)# %itand(null#null)# %itand(" f o$ dual. )IT(ND(1#4) )IT(ND(3#3) )IT(ND(2#2) )IT(ND(NULL#NULL)
1#"4)
o)
8R'(T'ST
'/0
SQL-
41
1 1 1
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
'/0
SQL-
L'(ST(2#1#4) 2
L'(ST("2#"1#"4) "4
"""""""""""""""""""" """""""""""""""""""""""
4 4 4
U)
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
CO(L'SC'
'/0
SQL-
CO(L'SC'(2#1#4) CO(L'SC'(NULL#1#NULL#C)
""""""""""""""""""" 2
""""""""""""""""""""""""""""""" 1
STRIN8 BUNCTIONS
18 Init!ap 18 Uppe 18 Lowe 18 Length 18 Rpad 18 Lpad 18 Lt i$ 18 Rt i$ 18 T i$ 18 T anslate 18 Repla!e 18 Sounde/ 18 Con!at ( K XX K Con!atenation ope ato ) 18 (s!ii 18 Ch 18 Su%st 18 Inst 18 De!ode 18 8 eatest 18 Least 18 Coales!e
a)
INITC(A
43
'/0
SQL-
%)
UAA'R
'/0
SQL-
!)
LO6'R
'/0
SQL-
d)
L'N8T:
'/0
SQL-
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
'/0
SQL-
f)
LA(D
This will allows 7ou to pad the left side of a !olu$n with an7 set of !ha a!te s5 S7nta/0 lpad (!trin", len"t) *, paddin"_c)ar+)
'/0
45
SQL-
g)
LTRIM
This will t i$ off unwanted !ha a!te s f o$ the left end of st ing5
'/0
SQL-
SQL-
"" If 7ou haven;t spe!if7 an7 unwanted !ha a!te s it will displa7 enti e st ing5
h)
RTRIM
This will t i$ off unwanted !ha a!te s f o$ the ight end of st ing5 S7nta/0 t i$ (!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-
dual.
"" If 7ou haven;t spe!if7 an7 unwanted !ha a!te s it will displa7 enti e
i)
TRIM
This will t i$ off unwanted !ha a!te s f o$ the %oth sides of st ing5
'/0
SQL-
TRIM( """"" ndian sele!t t i$( leading=i= f o$ =indiani=) f o$ dual."" this will
SQL-
wo & as
LTRIM
TRIM(L """"""
47
ndiani
SQL-
RTRIM
J)
TR(NSL(T'
This will epla!e the set of !ha a!te s# !ha a!te %7 !ha a!te 5
'/0
SQL-
'/0
SQL-
R'AL(C'
R'AL(C'
""""""""""" """""""""""
+7dia
dia
l)
SOUND'+
This will %e used to find wo ds that sound li&e othe wo ds# e/!lusivel7 used in whe e !lause5 S7nta/0 sounde/ (!trin")
'/0
SQL-
S(L C33
D'ATNO 13
""""""""" """"""""""
$)
CONC(T
'/0
SQL-
If 7ou want to !o$%ine $o e than two st ings 7ou have to use !on!atenation ope ato (XX)5
SQL-
49
n)
(SCII
This will etu n the de!i$al ep esentation in the data%ase !ha a!te set of the fi st !ha a!te of the st ing5
'/0
SQL-
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-
C:R """"" a
p)
SU)STR
'/0
SQL-
1 1 1 1
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 1 O "V 4 M "D @ A "C C U "@ D T "4 V ' "1 W R "2
U)
INSTR
This will allows 7ou fo sea !hing th ough a st ing fo set of !ha a!te s5
51
'/0
SQL-
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
D'COD'
De!ode will a!t as value %7 value su%stitution5 Bo eve 7 value of field# it will !he!&s fo a $at!h in a se ies of if?then tests5
'/0
SQL-
S(L """"" C33 1C33 1333 4C33 4333 C333 @333 C333
D'COD' """"""""" Low Mediu$ Mediu$ Mediu$ Mediu$ :igh Mediu$ :igh
SQL-
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
3 3
s)
If all the pa a$ete s a e null then de!ode will displa7 nothing5 If all the pa a$ete s a e Te os then de!ode will displa7 Te o5
8R'(T'ST
'/0
SQL-
53
7 7
t)
L'(ST
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
'/0
SQL-
9 9
u)
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'
'/0
SQL-
D(T' BUNCTIONS
39 S7sdate 39 Cu entMdate 39 Cu entMti$esta$p 39 S7sti$esta$p 39 Lo!alti$esta$p 39 D%ti$eTone 39 Sessionti$eTone 39 ToM!ha 39 ToMdate 39 (ddM$onths 39 MonthsM%etween 39 Ne/tMda7 39 LastMda7 39 '/t a!t 39 8 eatest 39 Least 39 Round 39 T un! 39 NewMti$e 39 Coales!e
O a!le default date fo $at is !o$$and5
DD"MON"**5
6e !an !hange the default fo $at to ou desi ed fo $at %7 using the following
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'
55
%)
CURR'NTMD(T'
'/0
SQL-
!)
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-
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-
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-
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-
g)
S'SSIONTIM'ZON'
57
This will etu ns the value of the !u ent session;s ti$e Tone5
'/0
SQL-
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
D(T' BORM(TS
"" "" "" "" "" "" "" "" "" "" "" "" "" "" ""
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
"" "" "" "" "" "" "" "" "" "" "" "" "" "" o o o o
AM A5M )C )5C
Two digit 7ea f o$ ISO standa d Th ee digit 7ea f o$ ISO standa d Bou digit 7ea f o$ ISO standa d *ea with !o$$a Bull7 spelled out 7ea Centu 7 No of 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 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:
da7
T: SA SAT: T:SA
'/0
SQL-
SQL-
59
SQL-
SQL-
SQL-
SQL-
i)
TOMD(T'
'/0
SQL-
da7=)
"" If 7ou a e not using toM!ha o a!le will displa7 output in default date fo $at5 J)
(DDMMONT:S
'/0
SQL-
sele!t addM$onths(toMdate(=22"Jan"2SS3=#=dd"$on"7777=)# C) f o$
dual.
SQL-
dual.
61
1
&)
MONT:SM)'T6''N
'/0
SQL-
toMdate(=22"
toMdate(=22"
l)
N'+TMD(*
This will p odu!e ne/t da7 of the given da7 f o$ the spe!ified date5
'/0
SQL-
$)
L(STMD(*
'/0
SQL-
n)
'+TR(CT
S7nta/0 e/t a!t ((7ea X $onth X da7 X hou X $inute X se!ond)# date)
'/0
SQL-
63
o)
8R'(T'ST
'/0
SQL-
S3=#=dd"
p)
L'(ST
This will give the least date5 S7nta/0 least (date1, date2, date3 daten)
'/0
SQL-
$on"
U)
ROUND
Round will ounds the date to whi!h it was eUual to o g eate than the given date5
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
SUN T:U
"" ""
6'D SUN
65
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
1 1 1
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-
ound(toMdate(=22"$a "
SQL-
sele!t ound(toMdate(=22"Jan"3@=#=dd"$on"77=)#=$onth=)# Jan"3@=#=dd"$on"77=)#=$onth=) f o$ dual. ROUND(TOM ROUND(TOM """"""""""""" """"""""""""""" 32">(N"3@ 32"B')"3@
ound(toMdate(=2W"
SQL-
sele!t ound(toMdate(=1D"de!"3D=#=dd"$on"77=)#=da7=)# 3D=#=dd"$on"77=)#=da7=) f o$ dual. ROUND(TOM ROUND(TOM """""""""""""" """""""""""""" 1@"D'C"3D 42"D'C"3D
ound(toMdate(=1S"de!"
SQL-
sele!t toM!ha ( ound(toMdate(=1@"de!"3D=#=dd"$on"77=))# =dd $on 7777 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
1 1 1 1 1
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-
t un!(toMdate(=22"$a "
SQL-
sele!t t un!(toMdate(=22"Jan"3@=#=dd"$on"77=)#=$onth=)#
t un!(toMdate(=2W"Jan"
67
3@=#=dd"$on"77=)#=$onth=) f o$ dual.
SQL-
sele!t t un!(toMdate(=1D"de!"3D=#=dd"$on"77=)#=da7=)# 3D=#=dd"$on"77=)#=da7=) f o$ dual. TRUNC(TOM TRUNC(TOM """"""""""""" """""""""""""" 1@"D'C"3D 1@"D'C"3D
t un!(toMdate(=1S"de!"
SQL-
hh0$i0ss
s)
N'6MTIM'
TIM'ZON'S
(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-
a$=) f o$
SQL-
a$=) f o$
t)
CO(L'SC'
'/0
SQL-
$a "
69
MISC'LL(N'OUS BUNCTIONS
'/0
SQL-
%)
US'R
'/0
SQL-
!)
9SIZ'
'/0
SQL-
'/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
71
SQL-
R(NF(1SVC)6IT:IN8ROUA(ORD'R)*S(LD'SC) """"""""""""""""""""""""""""""""""""""""""""""""""""""""" @
d)
D'NS'MR(NF
D'NS'MR(NF(1SVC)6IT:IN8ROUA(ORD'R)*S(LD'SC) """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" 4
CON9'RSION BUNCTIONS
This will !onve t the %ina 7 value to its nu$e i!al eUuivalent5
'/0
SQL-
)INMTOMNUM(2#2#3) """""""""""""""""""""""" D
1 1
%)
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
d)
TOMNUM)'R
e)
TOMC:(R
f)
TOMD(T'
8ROUA BUNCTIONS
73
a)
SUM
This will give the su$ of the values of the spe!ified !olu$n5
'/0
SQL-
This will give the ave age of the values of the spe!ified !olu$n5
'/0
SQL-
!)
M(+
This will give the $a/i$u$ of the values of the spe!ified !olu$n5
'/0
SQL-
d)
MIN
This will give the $ini$u$ of the values of the spe!ified !olu$n5
'/0
SQL-
e)
COUNT
This will give the !ount of the values of the spe!ified !olu$n5
'/0
75
SQL-
COUNT(S(L) """""""""""""" 2@
COUNT(E) """""""""""" 2@
CONSTR(INTS
Const aints a e !atego iTed as follows5
1 1
UniUue
A i$a 7 &e7
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
2 2 2
"" 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
SQL-
! eate ta%le student(no nu$%e (1) not null# na$e va !ha (23)# $a &s nu$%e (4)).
SQL-
! eate ta%le student(no nu$%e (1) !onst aint nn not null# na$e $a &s nu$%e (4)).
va !ha (23)#
C:'CF
77
'/0
COLUMN L'9'L
SQL-
! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4) ($a &s - 433)).
!he!& ! 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)# ($a &s - 433)).
!he!& ! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# !onst aint !h !he!&($a &s - 433)).
SQL-
(LT'R L'9'L
SQLSQL-
alte ta%le student add !he!&($a &s-433). alte ta%le student add !onst aint !h !he!&($a &s-433).
UNIQU'
This is used to avoid dupli!ates %ut it allow nulls5 6e !an add this !onst aint in all th ee levels5
'/0
COLUMN L'9'L
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 $a &s nu$%e (4)).
SQL-
va !ha (23)#
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'*
1 1 1
'/0
This is used to avoid dupli!ates and nulls5 This will wo & as !o$%ination of uniUue and not null5 A i$a 7 &e7 alwa7s atta!hed to the pa ent ta%le5 6e !an add this !onst aint in all th ee levels5
COLUMN L'9'L
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 $a &s nu$%e (4)).
SQL-
va !ha (23)#
79
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'*
1 1 1
'/0
This is used to efe en!e the pa ent ta%le p i$a 7 &e7 !olu$n whi!h allows dupli!ates5 Bo eign &e7 alwa7s atta!hed to the !hild ta%le5 6e !an add this !onst aint in ta%le and alte levels onl75
T()L' L'9'L
SQL-
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)).
(LT'R L'9'L
SQL-
SQL- alte ta%le e$p add !onst aint f& fo eign &e7(deptno) efe en!es dept(deptno).
On!e the p i$a 7 &e7 and fo eign &e7 elationship has %een ! eated then 7ou !an not e$ove an7 pa ent e!o d if the dependent !hilds e/ists5
)7 using this !lause 7ou !an e$ove the pa ent e!o d even it !hilds e/ists5 )e!ause when eve 7ou e$ove pa ent e!o d o a!le auto$ati!all7 e$oves all its dependent e!o ds f o$ !hild ta%le# if this !lause is p esent while ! eating fo eign &e7 !onst aint5
'/0
T()L' L'9'L
SQL-
nu$%e (1)# p i$a 7 &e7(e$pno)# fo eign &e7(deptno) efe en!es dept(deptno) on delete !as!ade).
SQL-
nu$%e (1)# !onst aint p& p i$a 7 &e7(e$pno)# !onst aint f& fo eign &e7(deptno) efe en!es dept(deptno) on delete !as!ade).
(LT'R L'9'L
SQL-
alte ta%le e$p add fo eign &e7(deptno) efe en!es dept(deptno) on !as!ade.
delete alte ta%le e$p add !onst aint f& fo eign &e7(deptno) efe en!es
SQL-
81
( !o$posite &e7 !an %e defined on a !o$%ination of !olu$ns5 6e !an define !o$posite &e7s on entit7 integ it7 and efe ential integ it7 !onst aints5 Co$posite &e7 !an %e defined in ta%le and alte levels onl75
'/0
UNIQU' (T()L' L'9'L)
SQL-
! eate ta%le student(no nu$%e (1) # na$e va !ha (23)# $a &s nu$%e (4)# 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-
SQLSQL-
alte ta%le student add uniUue(no#na$e). alte ta%le student add !onst aint un uniUue(no#na$e).
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-
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).
SQL-
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)).
SQL-
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)).
BOR'I8N F'* ((LT'R L'9'L)
SQL-
alte ta%le e$p add fo eign &e7(deptno#dna$e) efe en!es alte ta%le e$p add !onst aint f& fo eign &e7(deptno#dna$e) efe en!es dept(deptno#dna$e).
dept(deptno#dna$e).
SQL-
D'B'RR()L' CONSTR(INTS
'a!h !onst aint has two additional att i%utes to suppo t defe ed !he!&ing of !onst aints5
1 1
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
SQL-
SQL-
defe ed.
83
SQL-
This will ena%le all the !onst aints violations at the ti$e of inse ting5
SQL-
This will ena%le all the !onst aints violations at the ti$e of !o$$it5
3 3 3 3
'N()L'
This will ena%le the !onst aint5 )efo e ena%le# the !onst aint will !he!& the e/isting data5
'/0
SQL-
DIS()L'
'/0
SQL-
'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-
DROA
'/0
SQL-
C(S'
Case is si$ila to de!ode %ut easie to unde stand while going th ough !oding
'/0
85
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 4133
C(S' """""""" low $ediu$ $ediu$ $ediu$ $ediu$ high $ediu$ high $ediu$ $ediu$ $ediu$ $ediu$ $ediu$ $ediu$
D'B(ULT
.e'ault !an %e !onside ed as a su%stitute %ehavio 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.
SQL-
NO 2 22
N(M' a %
"""""" """""""""
SQL-
SQL-
sele!t E f o$ student.
NO 2 22
N(M' a % C
"""""" """""""""
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
87
'/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-
SQL-
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=)).
SQL-
sele!t E f o$ student.
NO N(M' (DDR'SS(:NO# CIT*) """ """"""" """"""""""""""""""""""""" 2 1 4 a % ! (DDR(222# =h7d=) (DDR(111# =%ang=) (DDR(444# =delhi=)
SQL-
2 1 4
a % !
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
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 .
89
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 elates this o%Je!t to the e/isting ta%le 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?
! 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
4) Relate the o%Je!ts to the student ta%le %7 ! eating the o%Je!t view
SQL-
no#stud(na$e#add (hno#!it7)) f o$ student. @) Now 7ou !an inse t data into student ta%le in two wa7s a) )7 egula inse t
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.
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)).?
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.?
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.
"" :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
91
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 .? a7 in ta%le
%) 9a
SQLSQL-
1) Using va
SQL-
C eate ta%le student(no nu$%e (1)#na$e va !ha (23)#add ess va). Inse t into student values(2#;sudha;#va(add (222#;h7d;))).
SQL-
Inse t
into
student
"" 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-
"" Update and delete not possi%le in va a7s5 "" :e e we used ta%le fun!tion whi!h will ta&e the va a7 !olu$n as input fo p odu!ing output e/!luding va a7 and t7pes5
N'ST'D T()L'S ( nested ta%le is# as its na$e i$plies# a ta%le within a ta%le5 In this !ase it is a ta%le that is 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 .? 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
SQL-
Inse t
into
student
values
(1#;Jagan;#nt(add (222#;h7d;)#add (111#;%ang;))). @) Sele!ting data f o$ ta%le whi!h has nested ta%le
SQL-
Sele!t E f o$ student.
"" This will displa7 nested ta%le !olu$n data along with nested ta%le and adt.
93
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;)).
Update ta%le(sele!t add ess f o$ student whe e noH1) s set whe e s5hno H 111.
1 1 1 1 1 1
BL(S:)(CF QU'R*
Used to e!ove 75 et ieve the data whi!h has %een al ead7 !o$$itted with out going fo
1 1
'/0
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.
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
9a ia%le s nu$%e
95
'+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
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 ead and w ite p ivilege on the di e!to 75
'/0
SQLSQLSQLSQLSQLSQLSQL-
SUlplus s7ste$?$anage C eate di e!to 7 saðMdi as K?9isd%?visd%?S5153?e/te nal;. 8 ant all on di e!to 7 saðMdi to sað. Conn sað?sað Spool dept5lst Sele!t deptno XX K#; XX dna$e XX K#; XX lo! f o$ dept. Spool off
SQL-
C eate ta%le deptMe/t (deptno nu$%e (1)# Dna$e va !ha (2@)# Lo! va !ha (24)) O ganiTation e/te nal ( t7pe o a!leMloade Default di e!to 7 saðMdi (!!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;)).
SQL-
sele!t E f o$ deptMe/t.
This will ead f o$ dept5lst whi!h is a ope ating s7ste$ level file5
a) *ou !an not pe fo $ inse t# update# and delete ope ations b) Inde/ing not possi%le a) 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
a) *ou !an Join e/te nal ta%les to ea!h othe o to standa d ta%les
97
1 1 1
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
1 1
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'
1 1
'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 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)# va !ha (1)# vendo Madd ess va !ha (23)).?
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;). C eate ta%le o de s (o de Mno nu$%e (1)# vendo Minfo ef vendo Madt). O
@) ! eate anothe ta%le o de s whi!h holds the vendo Madt t7pe also5
SQL-
SQL-
C eate ta%le o de s (o de Mno nu$%e (1)# vendo Minfo ef vendo Madt owid).
with 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-
SQL-
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-
99
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-
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
This will give the !olu$ns data along with the t7pe5
R'B CONSTR(INTS
1 1
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
SQL-
C eate ta%le o de s (o de Mno nu$%e (1)# vendo Minfo ef vendo Madt s!ope vendo s). O C eate ta%le o de s (o de Mno nu$%e (1)# vendo Minfo ef vendo Madt efe en!es vendo s).
is
SQL-
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 aints5
)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
va !ha (1)#$a &s 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)). %) Inse t the e!o ds into %oth ta%les
SQLSQLSQLSQLSQL-
SQL-
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
! eate o
C eate o
C eate o
sele!t Ef o$ student1Mov.
101
SQL-
A(RTITIONS
( single logi!al ta%le !an %e split into a nu$%e ( non"pa titioned ta%le !an not %e pa titioned late 5 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
T*A'S
1 1 1 1
(D9(NT(8'S
Redu!ing downti$e fo s!heduled $aintenan!e# whi!h allows $aintenan!e ope ations to %e !a ied out on sele!ted pa titions while othe pa titions a e availa%le to use s5
1 1
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 fo va ious pu poses5 !on!u ent use of the va ious pa titions
1 1
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 datat7pes# LO) t7pes o o%Je!t t7pes5
long
aw
R(N8' A(RTITIONS
C eate ta%le student(no nu$%e (1)#na$e va !ha (1)) pa tition %7 (pa tition p2 values less than(23)# pa tition p1 values less than(13)#
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).
"" this will go to p2 "" this will go to p1 "" this will go to p4 "" this will go to p@
1 1 1 1 1 1 1
SQL-
e) (dding a pa tition (lte ta%le student add pa tition pC values less than(@3). f) D opping a pa tition
SQL-
g) Rena$ing a pa tition
103
SQL-
(lte ta%le student ena$e pa tition p4 to pD. (lte ta%le student t un!ate pa tition pD. (lte ta%le student split pa tition p1 at(2C) into (pa tition p12#pa tition
h) T un!ate a pa tition
SQL-
i) Splitting a pa tition
SQL-
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ðMts.
LIST A(RTITIONS
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 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@
8 8 8 8 8 8
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. (lte ta%le student t un!ate pa tition pD.
f) D opping a pa tition
SQL-
g) Rena$ing a pa tition
SQL-
h) T un!ate a pa tition
SQL-
i) '/!hanging a pa tition
SQL-
(lte ta%le student e/!hange pa tition p2 with ta%le student1. (lte ta%le student $ove pa tition p1 ta%lespa!e saðMts.
J) Moving a pa tition
SQL-
:(S: A(RTITIONS
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 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).
105
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-
Su%pa titions !lause is used %7 hash onl75 6e !an not ! eate su%pa titions with list and hash pa titions5
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 pa tition A2 \ S*SMSU)A2 S*SMSU)A1 S*SMSU)A4 A1 \ S*SMSU)A@ S*SMSU)AC S*SMSU)AD
ea!h
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).
(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
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-
p41).
D(T( MOD'L
2 2 2 2 2 2
107
2 2 2 2 2 2
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-
D'ATNO 23 13 43
"""""""""" """"""""""
SQL-
SUM(S(L) """""""""" 2433 1@C3 C333 D333 2S33 1SVC SC3 1WC3 CD33
:(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-
su$(sal) 4333.
D'ATNO 23 13 43
>O)
SQL-
sele!t deptno#Jo%#su$(sal) tsal f o$ e$p g oup %7 deptno#Jo% having 4333 o de %7 Jo%. D'ATNO >O) TS(L
su$(sal) -
109
AR'SID'NT S(L'SM(N
ORD'R OB '+'CUTION
1 1 1 1
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
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-
D'ATNO 23 23 23 23 13 13 13 13 43 43 43 43
>O) CL'RF
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 %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
SQL-
sele!t
de!ode(g ouping(deptno)#2#=(ll
111
23 13 13 13 13 43 43 43 43 (ll Depts
(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
WVC3 D333 2S33 1SVC 23WVC SC3 1WC3 CD33 S@33 1S31C
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
SQL-
23 13 13 13 13 43 43 43 43 (ll Depts (ll Depts (ll Depts (ll Depts (ll Depts (ll Depts
(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
WVC3 D333 2S33 1SVC 23WVC SC3 1WC3 CD33 S@33 D333 @2C3 W1VC C333 CD33 1S31C
S'T OA'R(TORS
T*A'S
113
1 1 1 1
UNION
This will !o$%ine the e!o ds of $ultiple ta%les having the sa$e st u!tu e5
'/0
SQL-
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-
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-
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-
115
9I'6S
( view is a data%ase o%Je!t that is a logi!al sa$e $anne as a ta%le5 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
( 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
1 1
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[
1 1 1
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
1 1 1 1
Read onl7 view 9iew with g oup %7 9iew with agg egate fun!tions 9iew with ownu$
1 1
'/0
SQL-
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
SQL-
deptno.
SQLSQLSQL-
C eate view stud as sele!t ownu$ no# na$e# $a &s f o$ student. C eate view student as sele!t Ef o$ student2 union sele!t Ef o$ student1. C eate view stud as sele!t distin!t no#na$e f o$ student.
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
3 4 5 5 5
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)
SQL-
117
"" On!e the %ase ta%le was ! eated then the view is validated5
SQL-
C eate view stud as sele!t Ef o$ student whe e $a &s H C33 with !he!& !onst aint C&.
option " Inse t possi%le with $a &s value as C33 " Update possi%le e/!luding $a &s !olu$n " Delete possi%le
DROAAIN8 9I'6S
SQL-
d op view deptMv.
D(T( MOD'L
a ta%le# view o
T*A'S
3 3
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
1 1
: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
119
SQLSQLSQL-
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 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 $e$o 7 fo faste a!!ess5 etains the$ in
'/0
SQL-
! eate seUuen!e s. ! eate seUuen!e s in! e$ent %7 23 sta t with 233 $invalue C $a/value
SQL-
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ð).
1 1
Initiall7 !u val is not defined and ne/tval is sta ting value5 (fte that ne/tval and !u val a e alwa7s eUual5
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'
1 1 1
'/0
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
SQLSQLSQL-
alte seUuen!e s $invalue C. alte seUuen!e s in! e$ent %7 1. alte seUuen!e s !a!he 23.
DROAAIN8 S'QU'NC'
SQL-
d op seUuen!e s.
>OINS
1 1 1
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 ta%le and so on5 esult with the ne/t
T*A'S
'Uui Join
121
1 1 1 1 1
Non"eUui Join Self Join Natu al Join C oss Join Oute Join Left oute Right oute Bull oute
1 1 1
SQL-
sele!t E f o$ dept.
SQL-
sele!t E f o$ e$p.
M8R
@@@
$adhu
enginee
111
@3
'QUI >OIN
'/0
SQL-
e5deptnoHd5deptno.
>O) anal7st
USIN8 CL(US'
SQL-
>O) anal7st
ON CL(US'
SQL-
sele!t
e$pno#ena$e#Jo%#dna$e#lo! 'MANO 222 444 'N(M' sað Jagan >O) anal7st $anage
f o$
Join
dept
123
111
sudha
!le &
fin
%ang
NON"'QUI >OIN
( Join whi!h !ontains an ope ato othe than KH; in the Joins !ondition5
'/0
SQL-
S'LB >OIN
'/0
SQL-
D'ATNO 23 @3 13 23
$anage enginee
'/0
SQL-
CROSS >OIN
'/0
SQL-
'MANO 'N(M' 222 111 444 @@@ 222 111 444 @@@ 222 111 444 @@@ sað sudha Jagan $adhu sað sudha Jagan $adhu sað sudha Jagan $adhu
LOC h7d h7d h7d h7d %ang %ang %ang %ang %o$%a7 %o$%a7 %o$%a7 %o$%a7
$anage enginee anal7st !le & $anage enginee anal7st !le & $anage enginee
OUT'R >OIN
125
Oute Join gives the non"$at!hing e!o ds along with $at!hing e!o ds5
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).
SQL-
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). O sele!t e$pno#ena$e#Jo%#dna$e#lo! f o$ e$p e#dept d whe e e5deptno(G) d5deptno.
SQL-
>O) anal7st
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-
INN'R >OIN
'/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
127
"""""""""" """""""""" """""""""" """""""""" """""""""" 222 444 111 sað Jagan sudha anal7st $anage !le & $&t $&t fin h7d h7d %ang
1 1 1
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
1 1 1 1
Single ow su%Uue ies Multi ow su%Uue ies Multiple su%Uue ies Co elated su%Uue ies
'/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).
>O) (N(L*ST
M8R VCDD
:IR'D(T' 2S"(AR"WV
S(L
COMM
D'ATNO 13 23
"""""""""" """""""""" """"""""" """""""""" """""""""""" """"""" """""""""" """""""""" 4333 AR'SID'NT 2V"NO9"W2 C333
VS31
BORD
(N(L*ST
VCDD
34"D'C"W2
4333
13
In $ulti ow su%Uue 7# it will etu n $o e than one value5 In su!h !ases we should in!lude ope ato s li&e an7# all# in o not in %etween the !o$pa ision ope ato and the su%Uue 75
'/0
SQL-
sele!t E f o$ e$p whe e sal - an7 (sele!t sal f o$ e$p whe e sal and @333).
%etween 1C33
>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
SQL-
sele!t E f o$ e$p whe e sal - all (sele!t sal f o$ e$p whe e sal %etween and @333).
1C33
'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-
129
>O)
M8R
S(L 4333
COMM
D'ATNO 13 13
"""""""""" """""""""" """"""""" """""""""" """""""""""" """"""" """""""""" """""""""" (N(L*ST VCDD (N(L*ST VCDD 4333
CORR'L(T'D SU)QU'RI'S
sele!t distin!t deptno f o$ e$p e whe e C ,H (sele!t !ount(ena$e) f o$ whe e e5deptno H deptno).
e$p
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
SQL-
D'ATNO """"""""" 13 43
COUNT(E) """""""""" C D
B o$ the a%ove Uue 7 !an 7ou want to displa7 the na$es of e$plo7ees[
SQL-
!ount(E)
no ows sele!ted
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
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
'N(M' (D(MS BORD >ON'S SCOTT SMIT: (LL'N )L(F' >(M'S M(RTIN TURN'R
"""""""""" """"""""""
131
43
6(RD
NOT '+ISTS
SQL-
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.
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 !onne!t %7 p io e$pnoH$g . 'N(M'XX=HH-=XXARIOR'N(M L'9'L
=FIN8=
"""""""""""""""""""""""""""""""""""" 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
"""""""" 2 1 4 @ 4 @ 1 4 4 4 4 4 1 4
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
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-
R(NF 2 1 4 @
133
M(RTIN MILL'R TURN'R (LL'N CL(RF )L(F' >ON'S SCOTT BORD FIN8
21C3 2433 2C33 2D33 1@C3 1WC3 1SVC 4333 4333 C333
C D V W S 23 22 21 24 2@
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 !on!u en!75 esou !e si$ultaneousl75 Lo!&s p ovides high deg ee of data
T*A'S
1 1
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 done %7 using sele!t55fo update !lause5 olled %a!&5 This !an %e
'/0
SQL-
( 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
1 1 1
S:(R' LOCF
( sha e lo!& lo!&s the ta%le allowing othe esou !e at the sa$e ti$e5
update o delete ows in a ta%le5 Multiple use s !an pla!e sha e lo!&s on the sa$e
'/0
SQL-
It lo!&s
use s to
p events the othe use s f o$ updating the ow that has %een lo!&ed5
'/0
SQL-
'+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
135
SQL-
NO6(IT
If one use 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 issues a !o$$it o nowait !lause in the lo!& ta%le !o$$and5 who has initiall7 lo!&ed the ta%le oll%a!& state$ent5 This dela7 !ould %e avoided %7 appending a
'/0
SQL-
D'(DLOCF
( deadlo!& o!!u s when two 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 !ontaining the value5 ows
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
1 1 1 1 1 1 1 1 1 1 1 1 1 1
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/ v Lo!al inde/ Lo!al p efi/ed Lo!al non"p efi/ed 8lo%al inde/ 8lo%al p efi/ed 8lo%al non"p efi/ed
UNIQU' IND'+
UniUue inde/es gua antee that no two ows of a ta%le have dupli!ate values in the !olu$ns that define the inde/5 UniUue inde/ is auto$ati!all7 ! eated when p i$a 7 &e7 o uniUue !onst aint is ! eated5
'/0
SQL-
NON"UNIQU' IND'+
137
Non"UniUue inde/es do not i$pose the a%ove est i!tion on the !olu$n values5
'/0
SQL-
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-
)ITM(A IND'+
This !an %e used fo low !a dinalit7 !olu$ns0 that is !olu$ns in whi!h the nu$%e of distin!t values is s$all when !o$pa ed to the nu$%e of the ows in the ta%le5
'/0
SQL-
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 need not %e adJa!ent !olu$ns of the ta%le5 in an7 o de and
'/0
SQL-
( 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-
6e !an &e7wo d5
e%uild a
'/0
SQL-
This will use esult of the fun!tion as &e7 instead of using !olu$n as the value fo the &e75
'/0
SQL-
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
139
'/0
SQL-
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 diffi!ult if all 7ou had availa%le was the standa d ope ato s5 )7 e/tending
SQL
elational
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 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#
CONT'+T
is suppo ted in o a!le Wi as well as o a!le Si5 (s of o a!le Si# 7ou !an use te/t inde/ fo fu the enhan!e 7ou te/t inde/ $anage$ent and Uue 7
the
CT+C(T
!apa%ilities5
1 1 1
The
CT+C(T
inde/ t7pes do not gene ate s!o e values du ing the te/t Uue ies5
*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
SQLSQL-
! 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.
T'+T QU'RI'S
On!e a te/t inde/ is ! eated on the info !olu$n of !apa%ilities in! ease d7na$i!all75
)OOFS
CONT(INS
fun!tion ta&es two pa a$ete s \ the !olu$n na$e and the sea !h st ing5
If 7ou ! eate a
C(TS'(RC:
CT+C(T
C(TS'(RC:
fun!tion in pla!e of
CONT(INS5
ta&es th ee pa a$ete s \ the !olu$n na$e# the sea !h st ing and the
inde/ set5
CONT(INS
C(TS'(RC:
141
the Uue 7 is p o!essed %7 o a!le te/t5 The e$ainde of the Uue 7 is p o!essed Just li&e a 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
SQLSQL-
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.
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
SQL-
sele!t title# s!o e(23) f o$ %oo&s whe e !ontains(info# Kp ope t7;# 23) - 3.
SQLSQL-
(ND
(ND
(ND
7ou !ould have used an a$pe sand(<)5 )efo e using this will not %e seen as pa t of a va ia%le
SQLSQLSQL-
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.
SQL-
(ND
ha vests
(ND
wo &e s;) - 3.
SQL-
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-
OR
ha vests;) - 3.
Instead of
OR
SQLSQL-
ha vests;) - 3.
X
ha vests;# null) - 3.
(CCUM(a!!u$ulate)
of the individual sea !hes and !o$pa es the a!!u$ulated s!o e to the th eshold
SQLSQL-
(CCUM
(CCUM
3.
Instead of
OR
SQLSQL-
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.
MINUS
ope ato
SQLSQL-
MINUS NOT
ha vests;) - 3.
ha vests;# null) - 3.
143
Instead of
MINUS
NOT
SQLSQL-
"
ha vests;) - 3.
]
ha vests;# null) - 3.
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
SQLSQL-
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.
*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
SQLSQL-
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.
SQL-
N'(R
ha vests;) - 3.
Instead of
N'(R
SQL-
In
CONT'+T
inde/ Uue ies# 7ou !an spe!if7 the $a/i$u$ nu$%e of wo ds %etween
SQL-
*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 availa%le5 egula te/t"st ing wild!a d p o!essing# two wild!a ds a e
L M
" "
pe !ent sign. $ultiple"!ha a!te wild!a d unde s!o e. single"!ha a!te wild!a d
SQLSQL-
sele!t E f o$ %oo&s whe e !ontains(info# Kwo &e L;) - 3. sele!t E f o$ %oo&s whe e !ontains(info# Kwo &MMM;) - 3.
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-
( 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 in the 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 etu n an7thing %e!ause its sea !h does not !ontain the wo d Kha dest;5
145
SQL-
It does# howeve # !ontains the wo d Kha vest;5 ( fuTT7 $at!h will etu n the %oo&s !ontaining the wo d Kha vest; even though Kha vest; has a diffe ent wo d ste$ thant the wo d used as the sea !h te $5
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
SQL-
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
To use the SOUND'+ option# 7ou $ust p e!ede the sea !h te $ with an e/!la$ation $a &(I)5
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
S*NCMIND'+
p o!edu e of the
CT+MDDL
pa!&age5
SQL-
e/e!
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
CT+MDDL
inde/es to it5 6hen 7ou ! eate a te/t inde/# 7ou !an then spe!if7 the inde/ set it
SQL-
e/e! CT+MDDL5CR'(T'MIND'+MS'T(K%oo&sMinde/Mset;).
SQL-
e/e!
CT+MDDL5(DDMIND'+(K%oo&sMinde/Mset;#
KtitleMinde/;).
Now ! eate a
CT+C(T
te/t inde/5 Spe!if7 !t/s7s5!t/!at as the inde/ t7pe# and list the
SQL-
! eate inde/ %oo&Minde/ on %oo&s(info) inde/t7pe is !t/s7s5!t/!at pa a$ete s(Kinde/ set %oo&sMinde/Mset;).
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-
A(RTITION IND'+
147
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
1 1
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
1 1
'/0
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
SQL-
8LO)(L IND'+'S
1 1 1 1 1
'/0
( 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 than lo!al
(pa titioned) inde/es5 *ou !annot ! eate glo%al inde/es fo hash pa titions o su%pa titions5
SQL-
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-
1 1
Inde/ pa titions !annot %e d opped $anuall75 The7 a e d opped i$pli!itl7 when the data the7 efe to is d opped f o$ the pa titioned ta%le5
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
D(T( MOD'L
1 1 1 1 1 1 1 1
149
1 1
US'RMA(RTMIND'+'S 9`O)>'CTMUS(8'
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
TTITL'
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'
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
SQLSQLSQLSQLSQL-
% 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=
151
SQL-
Output0
COMM
D'ATNO 23
MILL'R CL'RF
"""""""""" WVC3
EEEEEEEEEE su$
SMIT: BORD
CL'RF (N(L*ST
13
su$
43
EEEEEEEEEE su$
su$
1S31C
EE T:(NFQ EE
CL'(R
This will !lea the e/isting %uffe s o % ea& o !o$putations o !olu$ns fo $atting5
'/0
SQL-
!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'
SQL
state$ents5
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
153
SQL-
!?det?dept ?
2E sele!t E f o$ dept
SQL-
D'ATNO DN(M' 23 13 43 @3
LOC
"""""""""" """""""""""""""" """"""""""" (CCOUNTIN8 N'6 *ORF R'S'(RC: S(L'S OA'R(TIONS (LL(S C:IC(8O )OSTON
COLUMN
This will %e used to in! ease o de! ease the width of the ta%le !olu$ns5
'/0
SQLSQL-
S(9'
This will %e used to save 7ou !u ent SQL state$ent as SQL S! ipt file5
If 7ou want to save the filena$e with e/isting filena$e the 7ou have to use epla!e option5
'/0
SQL-
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
'/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
'/0
SQLSQL-
155
43 @3
S(L'S OA'R(TIONS
C:IC(8O )OSTON
SQLSQL-
SQL-
sele!t E f o$ dept.
SQL-
spool off
LIST
SQL
state$ent5
'/0
SQL-
sele!t
1 E 4 f o$ @ dept.
SQL-
list
2 sele!t 1 E 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
SQL
state$ent5
'/0
SQLSQL-
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 spa!e5
SQL
157
'/0
SQLSQL-
2E sele!t E
SQL-
2E sele!t E f o$ dept
SQL-
2E sele!t E f o$ dept
D'L'T'
SQL
state$ent lines5
'/0
SQL-
sele!t
list
del 2 list
2 E
del 1 list
del 1 @ list
2 E 1E -23
SQLSQL-
del list
2 E
9(RI()L'
'/0
SQLSQL-
va
ARINT
This will %e used to p int the output of the va ia%les that will %e de!la ed at level5
SQL
159
'/0
SQL-
p int deptMna$e
ST(RT
SQL
s! ipts5
'/0
SQLSQL-
sta t ss5sUl ass5sUl "" this will e/e!ute sUl s! ipt files onl75
:OST
OS
level f o$
SQL5
'/0
SQLSQL-
host host di
S:O6
Using this# 7ou !an see seve al !o$$ands that use the set !o$$and and status5
'/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
161
RUN
S7nta/0 Run X ?
'/0
SQLSQL-
un ?
STOR'
'/0
SQL-
sto e set $7Msettings5s!$d sto e set $7Msettings5!$d epla!e sto e set $7Msettings5!$d append
BOLDM(BT'R
'/0
SQL-
SQLSQLSQLSQL-
!ol dna$e foldMafte 2 !ol lo! foldMafte 2 set heading off sele!t E f o$ dept.
@3 OA'R(TIONS )OSTON
BOLDM)'BOR'
D'BIN'
This will give the list of all the va ia%les !u entl7 defined5
163
'/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 2352535153 \ A odu!tion 6ith the Aa titioning#
D'BIN' MSQLALUSMR'L'(S' H b2332333133b (C:(R) D'BIN' MOM9'RSION 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'
'/0
SQL-
A(8'SIZ'
S7nta/0
'/0
SQL-
set pagesiTe 43
D'SCRI)'
'/0
SQL-
A(US'
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 default this will %e off5 etu n &e75 )7
'/0
SQL-
set pause on
165
B''D)(CF
This will give the info $ation 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 C ows5
'/0
SQLSQL-
D'ATNO 23 13 43 @3
@ ows sele!ted5
:'(DIN8
If 7ou want to displa7 data without headings# then 7ou !an a!hieve with this5 )7 default heading is on5
'/0
SQLSQL-
23 13 43 @3
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
'/0
SQL-
set se ve output on
TIM'
This will %e used to displa7 the ti$e5 )7 default this will %e off5
'/0
SQL-
set ti$e on
SQL-
2S0CD044
TIMIN8
This will give the ti$e ta&en to e/e!ute the !u ent will %e off5
SQL
167
'/0
SQLSQL-
D'ATNO 23 13 43 @3
'lapsed0 3303303353D
SQLAROMAT
This will %e used to !hange the S7nta/0 Set sUlp o$pt ,prompt-
SQL
p o$pt5
'/0
SQL-
OR(CL'-
SQLC(S'
SQL
'/0
SQL-
SQLT'RMIN(TOR
SQL
'/0
SQLSQL-
D'BIN'
)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
'/0
SQL-inse
'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=)
SQL-
SQL-inse
169
value
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
'/0
SQL-
set newpage 23
The Te o value fo newpage does not p odu!e Te o %lan& lines instead it swit!hes to a 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
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
sele!t E f o$ dept.
D'ATNO 23 13 43
@3
OA'R(TIONS
)OSTON
SQLSQLSQL-
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
SQL
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
'/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
171
new
D'ATNO 23
DN(M'
LOC
SQLSQL-
LOC D(LL(S
ANO
This will give displa7s the page nu$%e s5 )7 default the value would %e Te o5
'/0
SQLSQLSQL-
!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.
3S">UN"W2
page
'MANO
'N(M'
D'ATNO 23 23 23
"""""""""" """""""""" """"""""""""""" """"""""" """"" """""""""" """""""""" VVW1 CL(RF VW4S FIN8 VS4@ MILL'R
SQLALUS
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
SA'CI(L BIL'S
LO8IN5sUl
SQLALUS
to define 7ou
eUui ed !o$$ands in a file na$ed login5sUl5 This is a spe!ial filena$e that wheneve
!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
!o$$ands and
SQL-
SQL
SQLALUS
p o$pt5
173
8LO8IN5sUl
LO8IN5sUl
SQLALUS
settings
IMAORT(NT QU'RI'S
1) To find the nth ow of a ta%le
SQL-
Sele!t Ef o$ e$p whe e owid H (sele!t $a/( owid) f o$ e$p whe e ,H @). O Sele!t Ef o$ e$p whe e ownu$ ,H @ $inus sele!t Ef o$ e$p whe e ,H 4.
ownu$
SQL-
ownu$
SQL-
Sele!t Ef o$ e$p whe e owid in (sele!t $a/( owid) f o$ e$p g oup %7 e$pno# ena$e# $g # Jo%# hi edate# !o$$# deptno# sal). O Sele!t e$pno#ena$e#sal#Jo%#hi edate#!o$$ # !ount(E) f o$ e$p g oup e$pno#ena$e#sal#Jo%#hi edate#!o$$ having !ount(E) -H2.
SQL-
%7
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).
2.
e$p).
6) 8etting e$plo7ee details of ea!h depa t$ent who is d awing $a/i$u$ sal[
SQL-
7) :ow to get nu$%e of e$plo7ees in ea!h depa t$ent # in whi!h depa t$ent
is having $o e than 1C33 e$plo7ees[
SQL-
-1C33.
175
Sele!t Ef o$ e$p whe e sal in (sele!t $a/(sal) f o$ (sele!t Ef o$ e$p %7 sal) whe e ownu$ ,H C).
o de
INTRODUCTION
C:(R(CT'RSTICS
1 1 1 1
: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
1
.
0ptimi-ed compiler
To !hange the opti$iTe settings fo the enti e data%ase# set the data%ase pa a$ete 3 2 1 " " "
ALSQLMOATIMIZ'ML'9'L5
O a!le
etains opti$iTe
settings on a $odule"%7"$odule %asis5 6hen 7ou $odule with nondefault settings# the settings will
R'US' S'TTIN8S5
e!o$pile a pa ti!ula
SQLSQL-
Compile1time warnin"!.
Sta ting with o a!le data%ase 23g !o$pile !an dete!t potential
elease 2 7ou !an ena%le additional p og a$s $o e o%ust5 The !ode# su!h as
177
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
the
!u ent session5
SQL-
Conditional compilation.
4 4 6
203455.
6hen handling an e o # how !an 7ou find the line nu$%e e o was o iginall7 aised[
on whi!h the
In ea lie
D)MSMUTILIT*5BORM(TM'RRORM)(CFTR(C'
fun!tion to o%tain
7 8
O a!le data%ase 23g suppo ts the use of egula e/p essions inside
AL?SQL
own
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'
p ovides $u!h
i$p oved fun!tionalit7 fo s!heduling and e/e!uting Jo%s defined via sto ed
D)MSMCR*ATO Offe s the a%ilit7 to en! 7pt and de! 7pt !o$$on o a!le datat7pe# in!luding
179
R(6s# )LO)s#
and
CLO)s5
D)MSM6(RNIN8 A ovides an
(AI
into the
AL?SQL
ead and !hange settings that !ont ol whi!h wa nings a e supp essed# displa7ed# o t eated 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
ST(ND(RD5
AL?SQL
p og a$ is %lo!&5 (ll
AL?SQL
p og a$s a e !o$posed of
)LOCF STRUCTUR'
De!la e "" de!la ative se!tion )egin "" e/e!uta%le se!tion '/!eption "" e/!eption se!tion 'nd.
)LOCF T*A'S
1 1
(NON*MOUS )LOCFS
'/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
181
TRI88'RS
AL?SQL
N'ST'D )LOCFS
( %lo!& !an %e nested within the e/e!uta%le o e/!eption se!tion of an oute %lo!&5
ID'NTIBI'RS
AL?SQL
o%Je!ts# su!h as va ia%les# !u so s# t7pes and signs# unde s!o es# and pound
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 signs onl75 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'
COMM'NTS
p og a$ $o e unde standa%le5
1 1
( 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
'/0
D'CL(R'
a nu$%e . % nu$%e 0H C.
183
! nu$%e default D.
CONST(NT D'CL'R(TIONS
CONST(NT
'/0
D'CL(R'
*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
1 1
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'
R'CORD (NC:ORIN8
1 1
ARO8R(MM'R"D'BIN'D T*A'S
AL?SQL
own su%t7pes o
aliases of p edefined datat7pes# so$eti$es efe ed to as a%st a!t datat7pes5 The e a e two &inds of su%t7pes5
1 1
CONSTR(IN'D SU)T*A'
( su%t7pe that est i!ts o !onst ains the values no $all7 allowd %7 the datat7pe itself5
In the a%ove de!la ation a va ia%le that is de!la ed as positive !an sto e onl7
185
ingege
g eate
than
Te o
even
though
%ina 7Mintege
anges
f o$
"12@V@W4D@V55G12@V@W4D@V5
UNCONSTR(IN'D SU)T*A'
AL?SQL
!an handle !onve sions %etween diffe ent fa$ilies a$ong the datat7pes5
1 1
'+ALICIT CON9'RSION
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
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
1 1
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
the %lo!&5 6hen a va ia%le goes out of s!ope# the $e$o 7 used to sto e the va ia%le5
The visi%ilit7 of a va ia%le is the po tion of the p og a$ whe e the va ia%le !an %e a!!essed without having to 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 """""""" D'CL(R'
"" s!ope of a
% nu$%e .
)'8IN
"" s!ope of %
"""""
'ND.
""""""
'ND.
'/10
D'CL(R'
a nu$%e . % nu$%e .
)'8IN
"" a # % availa%le he e
D'CL(R'
187
% !ha (23).
)'8IN
"""""
'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
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
1 1
If"then"else Case Case with no else La%eled !ase Sea !hed !ase
1 1 1
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'
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%$sMoutput5putMline(=Lo!ation is else d%$sMoutput5putMline(=Lo!ation is end if.
'ND. )OSTON=). C:IC(8O=). D(LL(S=). N'6 *ORF=).
Output0 Lo!ation is
N'6 *ORF
C(S'
189
S7nta/0 Case te!t1variable 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'
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.
'ND. )OSTON=). C:IC(8O=). D(LL(S=). N'6 *ORF=).
Output0 Lo!ation is
N'6 *ORF
S7nta/0
Case te!t1variable 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'
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
L()'L'D C(S'
S7nta/0 ,,label-Case te!t1variable 6hen value1 then !e/uence o' !tatement!; 6hen value2 then !e/uence o' !tatement!; RR
191
'/0
D'CL(R'
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
N'6 *ORF
S'(RC:'D C(S'
S7nta/0 Case 6hen ,condition1- then !e/uence o' !tatement!; 6hen ,condition2- then !e/uence o' !tatement!; RR 6hen ,conditionn- then !e/uence o' !tatement!; 'nd !ase.
'/0
D'CL(R'
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
N'6 *ORF
SIMAL' LOOA
S7nta/0 Loop $e/uence o' !tatement!; '/it when ,condition-. 'nd loop.
In the s7nta/ e/it when ,condition- is eUuivalent to If ,condition- then '/it. 'nd if.
'/0
D'CL(R'
i nu$%e 0H 2.
193
)'8IN
6:IL' LOOA
'/0
D'CL(R'
i nu$%e 0H 2.
)'8IN
iH@ iHC
BOR LOOA
'/20
)'8IN
'/10
)'8IN
195
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 and that is whe e the
NULL AL?SQL
to do a%solutel7 nothing#
!o$es5
The
NULL
state$ent deos nothing e/!ept pass !ont ol to the ne/t e/e!uta%le 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 7ou do 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#
'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 %lo!&s5
AL?SQL
with
8OTO 8OTO
the end of $7 p og a$ if the state of $7 data indi!ates that no fu the p o!essing is eUui ed5 )e!ause I do not have to do an7thing at the te $ination of the p og a$# I pla!e a e/e!uta%le state$ent is
NULL
deos nothing# it is
AL?SQL
angle % a!&ets5 6hen a goto state$ent is evaluated# !ont ol i$$ediatel7 passes to the state$ent identified %7 the la%el5 '/0
)'8IN
Bo i in 255C loop d%$sMoutput5putMline(=i H = XX i). if i H @ then goto e/itMloop. end if. end loop. ,,e/itMloop-Null.
'ND.
R'STRICTIONS ON 8OTO
1 1 1 1 1
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
AL?SQL
!o$pile 5
The !o$pile will a!t on the p ag$a du ing the !o$pilation of the %lo!&5
197
S7nta/0
AR8(M(
in!truction_to_compiler5
AL?SQL
1 1 1 1
SU)ARO8R(MS
AROC'DUR'S
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
S7nta/0 Bun!tion Ns!he$a5Oname N(parameter1 N#parameter2 RO)O Retu n etu nMdatat7pe Nauthid define X !u entMuse O Ndete $inisti!O Npa allelMena%leO is "" Nde!la ationsO )egin
199
"" 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
8arallel_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
S'L'CT
state$ent5
A(R(M'T'R MOD'S
1 1 1
IN
OUT
1 1 1 1
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 a e olled %a!& when an e/!eption is aised in the p og a$5 (n a!tual pa a$ete !o esponding to an out fo $al pa a$ete $ust %e a va ia%le5
IN OUT
5 5
In out pa a$ete will a!t as initiali-ed variable5 (n a!tual pa a$ete !o esponding to an in out fo $al pa a$ete $ust %e a va ia%le5
D'B(ULT A(R(M'T'RS
Default Aa a$ete s will not allow in the be"innin" and middle5 0ut and 6n 0ut 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 invalild p o!edu e p(a in nu$%e default C# % in nu$%e # ! in nu$%e ) \ invalild default C# % in nu$%e default D# ! in nu$%e ) \
NOT(TIONS
6e !an !o$%ine positional and na$e notation %ut positional notation !an not %e followed %7 the na$e notation5
'/0
201
Suppose we have a p o!edu e p o!(a nu$%e #% nu$%e #! nu$%e ) and we have one anon7$ous %lo!& whi!h !ontains v2#v1# and v4.
SQLSQL-
1 1 1
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
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'
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
etu n nu$%e
)'8IN IS
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'
203
)'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.
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
1 1
)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*
1 1 1
;ocopy is a hint# not a !o$$and5 This $eans that the !o$pile silentl7 de!ide that it !an;t fulfill 7ou eUuest fo a nocopy pa a$ete 5
$ight
To pass the out and in out pa a$ete s %7 efe en!e use no!op7 Uualifie 5
'/0
CR'(T' OR R'AL(C' AROC'DUR' AROC(a )'8IN """" 'ND AROC.
Call is a
SQL
7 7 7 7 7 7
6e !an not use !all with out and in out pa a$ete s5 Call is a The
SQL
AL?SQL
%lo!&.
INTO
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-
'/10
205
in nu$%e #% in nu$%e )
IS
d%$sMoutput5putMline(=a H = XX a XX = % H = XX %).
'ND AROC.
Output0
SQL-
'/40
CR'(T' OR R'AL(C' BUNCTION BUN R'TURN 9(RC:(R IS )'8IN
Output0
SQLSQLSQL-
4 4 4 4
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 !op7ing5 efe en!e is faste than the !all %7 value %e!ause it avoids the
SU)ARO8R(MS O9'RLO(DIN8
1 1 1 1
'/0
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
SQLSQL-
! eate o ! eate o
epla!e t7pe t2 as o%Je!t(a nu$%e ).? epla!e t7pe t2 as o%Je!t(a nu$%e ).?
D'CL(R'
i t2 0H t2(C). J t1 0H t1(C).
AROC'DUR' A($ t2) IS )'8IN
d%$sMoutput5putMline(=a H = XX $5a).
'ND A. AROC'DUR' A(n t1) IS )'8IN
d%$sMoutput5putMline(=% H = XX n5%).
'ND A. AROC'DUR' ARODUCT(a nu$%e #% nu$%e ) IS )'8IN
Output0 aHC
207
)'N'BITS OB O9'RLO(DIN8
R'STRICTIONS ON O9'RLO(DIN8
1 1 1 1
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 list of ove loaded p og a$s $ust diffe %7 $o e than
pa a$ete $ode5 (ll of the ove loaded p og a$s $ust %e defined within the sa$e s!ope o %lo!&5 Ove loaded fun!tions $ust diffe %7 $o e than thei etu n t7pe5
AL?SQL
8 8 8
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 p1code 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 engine5 eada%le %7
AL?SQL
8 8
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 as needed5
(S8()#
Li&e all of the !ontents of the sha ed pool# p"!ode is aged out of the sha ed pool a!!o ding to a least e!entl7 used
(LRU)
algo ith$5
8 8 8 8 8 8
Su%p og a$s !an %e local5 Lo!al su%p og a$s $ust %e de!la ed in the de!la ative se!tion of %lo!& and !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 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
AL?SQL
AROC'DUR'S 9 BUNCTIONS
13 A o!edu es $a7
13 A o!edu es !an not have etu n !lause whe e as fun!tions $ust5 13 6e !an use !all state$ent di e!tl7 fo e/e!uting p o!edu e whe e as we
need to de!la e a va ia%le in !ase of fun!tions5
13 Bun!tions !an use in sele!t state$ents whe e as p o!edu es !an not5 13 Bun!tions !an !all f o$ epo ts envi on$ent whe e as p o!edu es !an not5 13 6e !an use e/e! fo e/e!uting p o!edu es whe e as fun!tions !an not5 13 Bun!tion !an %e used in d%$sMoutput whe e as p o!edu e !an not5 13 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
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
209
ea!h ti$e5
Sto ed su%p og a$s !an %e !alled f o$ an7 %lo!& su%$itted %7 a use who has e/e!ute p ivileges on the su%p og a$5 Lo!al su%p og a$s !an %e !alled onl7 f o$ the %lo!& !ontaining the
su%p og a$5
)7 &eeping the sto ed su%p og a$ !ode sepa ate f o$ the !alling %lo!&# the !alling %lo!& is sho te and easie to unde stand5 The lo!al su%p og a$ and the !alling %lo!& a e one and the sa$e# whi!h !an
lead to pa t !onfusion5 If a !hange to the !alling %lo!& is $ade# the su%p og a$ will %e e!o$piled as of the e!o$pilation of the !ontaining %lo!&5
pinned
in
the
sha ed
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
4 4
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
'/20
CR'(T' OR R'AL(C' AROC'DUR' A IS )'8IN
Output0
SQL-
e/e! p
Sto ed su%p og a$
'/10
D'CL(R' AROC'DUR' A IS )'8IN
)'8IN
p.
'ND.
6 6
SQLSQL-
SU)ARO8R(MS D'A'ND'CI'S
8 8 8 8
( 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
AL?SQL
to e!o$pile in the ne/t ti$e it is !alled5 If we have two p o!edu es li&e A2 and A1 in whi!h A2 depends on A15 If we !o$pile A1 then A2 is invalidated5
1 1
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 %e!ause the data di!tiona 7 does not t a!& e$ote i$$ediatel7 dependen!ies5
1 1
unti$e5 6hen A2 is
!alled# the e$ote data di!tiona 7 is Uue ied to dete $ine the status of A15 A2 and A1 a e !o$pa ed to see it A2 needs to %e e!o$piled# the e a e two diffe ent $ethods of !o$pa ision Ti$esta$p Model
211
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
1 1
If the o%Je!ts a e in diffe ent ti$e Tones# the !o$pa ison is invalid5 6hen A2 is in a !lient side $a7 not possi%le to in!luded with the fo $s5
AL?SQL
SI8N(TUR' MOD'L
1 1 1 1
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 in
R'MOT'MD'A'ND'NCI'SMMOD'
$ust %e set to
SI8N(TUR'5
This is a pa a$ete
(dd
the
line
R'MOT'MD'A'ND'NCI'SMMOD'HSI8N(TUR'
to
the
data%ase
initialiTation file5 The ne/t ti$e the data%ase is sta ted# the $ode will %e set
to
SI8N(TUR'
fo all 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
(lte session set e$oteMdependen!iesM$ode H signatu e. This will onl7 affe!t 7ou session
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 $anuall75 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
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
213
p1.
'ND A2. AROC'DUR' A1 IS )'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
)'8IN
p2.
'ND.
'+'CUT' AR'9IL'8'
4 4
'+'CUT'5
If use ( had the p o!edu e !alled e$pMp o! then use ( g ants e/e!ute p ivilege on p o!edu e to use ) with the following !o$$and5
SQL-
!u so ! is sele!t Ef o$ student2.
)'8IN
215
If suppose use ) also having student1 ta%le then whi!h ta%le will populate whethe use (;s o use );s5
The answe is use (;s student1 ta%le onl7 %e!ause %7 default the 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
7 7 7
O a!le int odu!es 6nvo er<! and .e'iner<! ri")t!. )7 default it will use the define ;s ights5 (n invo&e ;s ights outine !an %e ! eated %7 using populate the use );s ta%le5
(UT:ID
!lause to
!u so is sele!t Ef o$ student2.
)'8IN
Then g ant e/e!ute p ivilege on p to use )5 '/e!uting the p o!edu e %7 use )# whi!h populates use );s ta%le5
The a%ove p o!edu e is !alled invo&e ;s p o!edu e5 Instead of !u entMuse of authid !lause# if 7ou use define then it will %e !alled define ; p o!edu e5
we have two use s sað and sudha in whi!h sað 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ð5 )efo e doing this sað $ust g ant the pe $issions on this ta%le to sudha5
SQLSQL-
!u so ! is sele!t Ef o$ sað5student.
)'8IN
If the sa$e p ivilege was g anted th ough a ole it wont ! eate the p o!edu e5
217
SQLSQLSQLSQL-
!onn sað?sað ! eate ole saðM ole. g ant all on student to saðM ole. g ant saðM ole to sudha.
!u so ! is sele!t Ef o$ sað5student.
)'8IN
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 i$$ediatel75
AL?SQL
1 1
In an invo&e ;s ights outine# e/te nal efe en!es in esolved using the !alle ;s p ivilege set5 )ut efe en!es in
AL?SQL
SQL
state$ents will %e
state$ents a e still
esolved unde
the owne ;s
p ivilege set5
the p ivilege set of the s!he$a that owns the t igge ing
This is also t ue fo
AL?SQL
the fun!tion will e/e!ute unde the p ivilege set of the view;s owne 5
A(CF(8'S
( pac a"e is a !ontaine fo elated o%Je!ts5 It has spe!ifi!ation and %od75 'a!h
A(CF(8' S*NT(+
C eate o glo%al o
"" pa!&age spe!ifi!ation in!ludes su%p og a$s signatu es# !u so s and pu%li! va ia%les5
219
C eate o p ivate
"" pa!&age %od7 in!ludes %od7 fo all the su%p og a$s de!la ed in the spe!# 9a ia%les and !u so s5 )egin "" initialiTation se!tion '/!eption "" '/!eption handling se!iton 'nd ,pac a"e_name-.
1 1
an7
efe en!e to a
pa!&aged va ia%le o t7pe is $ade# the pa!&age is instantiated5 'a!h session will have its own !op7 of pa!&aged va ia%les# ensu ing that two sessions e/e!uting su%p og a$s in the sa$e pa!&age use diffe ent $e$o 7 lo!ations5
instantiated within a session5 This !an %e done %7 adding initialiTation se!tion to the pa!&age %od7 afte all the o%Je!ts5
1 1 1 1 1 1 1 1
Aa!&ages a e sto ed in the data di!tiona 7 and !an not %e lo!al5 Aa!&aged su%p og a$s has an advantage ove stand alone su%p og a$5 6hen eve an7 efe en!e to pa!&age# the whole pa!&age p"!ode was sto ed in sha ed pool of
S8(5
Aa!&age $a7 have lo!al su%p og a$s5 *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
1 1 1
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
1 1 1
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
1 1
AR(8M(
221
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-
'na$e H >ON'S
M(RTIN
SQL-
1 1
The a%ove pa!&age displa7s the sa$e output fo though the !u so is not !losed5 )e!ause the se iall7 ti$e it was !alled5 eusa%le ve sion
ea!h
This is the default ve sion used %7 the o a!le# e/a$ine the following pa!&age5
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-
SQL-
e/e! p&g5e$pMp o!
'na$e H
)L(F'
223
1 1
The a%ove pa!&age displa7s the diffe ent output fo though the !u so is not !losed5 )e!ause the non"se iall7 ove data%ase !alls5 eusa%le ve sion
Dependen!ies !an e/ists %etween pa!&age state and anon7$ous %lo!&s5 '/a$ine the following p og a$
v nu$%e 0H C. p o!edu e p.
'ND AF8.
)'8IN
p&g5p.
'ND.
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.
1 1 1 1 1
The anon7$ous %lo!& depends on p&g5 This is !o$pile ti$e dependen!75 The e is also a unti$e dependen!7 on the pa!&aged va ia%les# sin!e ea!h session has its own !op7 of pa!&aged va ia%les5 Thus when p&g is e!o$piled the unti$e dependen!7 is followed# whi!h
invalidates the %lo!& and aises the o a!le e o 5 Runti$e dependen!ies e/ist onl7 on pa!&age state5 This in!ludes va ia%les and !u so s de!la ed in a pa!&age5 If the pa!&age had no glo%al va ia%les# the se!ond e/e!ution of the anon7$ous %lo!& would have su!!eeded5
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$ state$ents5 :oweve # if a stand"alone o est i!tions# it !an %e !alled du ing e/e!ution of a SQL state$ent5
SQL
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
225
RNAS
""
In addition to the p e!eding est i!tions# a use "defined fun!tion $ust also $eet the following eUui e$ents to %e !alled f o$ a
SQL
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 The fo $al pa a$ete s $ust use onl7 data%ase t7pes# not as %oolean o e!o d5 oll%a!&# 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 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
AL?SQL
t7pes su!h
R'STRICTMR'B'R'NC'S
Bo
R'STRICTMR'B'R'NC'S
p ag$a is
eUui ed to
S7nta/0
AR(8M( N#6NASO N#RNDSO N#RNASO) . R'STRICTMR'B'R'NC'S(!ubpro"ram_name
or
pac a"e_name#
6NDS
'/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.
etu n va !ha
IS
etu n va !ha
IS
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
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.
D'B(ULT
If the e is no
R'STRICTMR'B'R'NC'S
fun!tion# it will not have an7 pu it7 level asse ted5 :oweve # 7ou !an !hange the default pu it7 level fo a pa!&age5 The
D'B(ULT
227
'/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.
etu n va !ha
IS
etu n va !ha
IS
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
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
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.
TRUST
If the
TRUST
'/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.
etu n va !ha
IS
etu n va !ha
IS
229
the
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 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 eUui ed onl7 fo pa!&ages fun!tions not fo stand"alone
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 o%Je!t is no longe 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 efe en!ed# it is f ee to %e aged out5 O%Je!ts a e aged out of the
LRU(Least
The
D)MSMS:(R'DMAOOL
pa!&age allows 7ou to pin o%Je!ts in the sha ed pool5 6hen how often the o%Je!t is a!!essed5 This !an i$p ove
an o%Je!t is pinned# it will neve %e aged out until 7ou eUuest it# no $atte how full the pool gets o pe fo $an!e# as it ta&es ti$e to eload a pa!&age f o$ dis&5
D)MSMS:(R'DMAOOL
1 1 1 1
F''A
The
D)MSMS:(R'DMAOOL5F''A
S7nta/0
AROC'DUR' F''A(ob#ect_name
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
UNF''A
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
SIZ'S
SIZ'S
S7nta/0
AROC'DUR' SIZ'S(min!i-e
nu$%e ).
231
O%Je!ts with g eate than the min!i-e will %e etu ned5 etu n the data5
SIZ'S
uses
D)MSMOUTAUT
to
()ORT'DMR'QU'STMT:R'S:OLD
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 this5 data%ase sessions5 The
()ORT'DMR'QU'STMT:R'S:OLD
!an %e used to
e$ed7
S7nta/0
AROC'DUR' ()ORT'DMR'QU'STMT:R'S:OLD(t)re!)old_!i-e
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
1 1 1 1 1 1 1 1 1
CURSORS
Cur!or is a pointe to $e$o 7 lo!ation whi!h is !alled as conte(t area whi!h to the pa sed ep esentation of the
!ontains the info $ation ne!essa 7 fo p o!essing# in!luding the nu$%e of ows p o!essed %7 the state$ent# a pointe 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 :eade \ !u so !(dno in nu$%e ) etu n deptL owt7pe )od7 \ sele!t Ef o$ dept
233
CURSOR T*A'S
1 1
CURSOR ST(8'S
1 1 1
CURSOR (TTRI)UT'S
1 1 1 1 1 1
CURSOR D'CL'R(TION
CURSOR LOOAS
1 1 1
SIMAL' LOOA
S7nta/0 Loop Bet!h ,cur!or_name- into ,record_variable-. '/it when ,cur!or_name- L notfound. ,!tatement!-. 'nd loop.
'/0
D'CL(R'
open !. loop fet!h ! into vMstud. e/it when !Lnotfound. d%$sMoutput5putMline(=Na$e H = XX vMstud5na$e). end loop. !lose !.
'ND.
6:IL' LOOA
235
S7nta/0 6hile ,cur!or_name- L found loop Bet!h ,cur!or_name- nto ,record_variable-. ,!tatement!-. 'nd loop.
'/0
D'CL(R'
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
BOR LOOA
'/0
D'CL(R'
!u so ! is sele!t E f o$ student.
)'8IN
Output0
A(R(M'T(RIZ'D CURSORS
1 1 1 1
'/0
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 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
D'CL(R'
!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 !.
237
'ND.
Output0
Dna$e H
R'S'(RC:
Lo! H
D(LL(S
1 1 1 1 1
'/20
!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 *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
Output0
SQL-
e/e! p&g5p o!
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
Output0
SQL-
Lo! H
C:IC(8O )OSTON
OA'R(TIONS
Lo! H
1 1 1 1 1 1
This is un!onst ained !u so whi!h will etu n diffe ent t7pes depends upon the 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
*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
239
1 1
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 though the assigned !u so va ia%le goes out of s!ope5
va ia%le even
1 1 1 1 1 1 1
'/0
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 *ou !an not use se ve to anothe 5 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 e$ote p o!edu e !alls to pass !u so va ia%les f o$ one
if ta%leMna$e H
=D'AT=
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=
then
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. d%$sMoutput5putMline(=Na$e H = XX vMstud). end if. end loop. !lose !.
'ND.
Output0
SQL-
Lo! H
N'6 *ORF
Lo! H
D(LL(S
Lo! H
C:IC(8O
OA'R(TIONS
Lo! H
)OSTON
SQL-
CL'RF
Sal H W33 Sal H 2D33 Sal H 21C3 Sal H 1SVC Sal H 21C3 Sal H 1WC3 Sal H 1@C3
>o% H
S(L'SM(N
M(N(8'R M(N(8'R
241
SCOTT FIN8
>o% H
(N(L*ST
>o% H
AR'SID'NT
>o% H
S(L'SM(N
>o% H >o% H
CL'RF CL'RF
>o% H
(N(L*ST CL'RF
MILL'R
>o% H
SQL-
CURSOR '+AR'SSIONS
1 1 1 1 1
*ou !an use !u so e/p essions in e/pli!it !u so s5 *ou !an use !u so e/p essions in d7na$i! SQL5 *ou !an use !u so e/p essions in R'B !u so de!la ations and va ia%les5 *ou !an not use !u so e/p essions in i$pli!it !u so s5 O a!le opens the nested !u so defined %7 a !u so e/p ession i$pli!itl7 as soon as it fet!hes the data !ontaining the !u so e/p ession f o$ the pa ent o oute !u so 5
1 1 1 1 1 1
Nested !u so !loses if 7ou !lose e/pli!itl75 Nested !u so !loses wheneve the oute o pa ent !u so is e/e!uted again o !losed o !an!eled5 Nested !u so !loses wheneve an e/!eption is aised while fet!hing data
f o$ a pa ent !u so 5 Cu so e/p essions !an not %e used when de!la ing a view5 Cu so e/p essions !an %e used as an a gu$ent to ta%le fun!tion5 *ou !an not pe fo $ %ind and e/e!ute ope ations on !u so when using the !u so e/p essions in d7na$i! SQL5 e/p essions
'/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. 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
1 1 1 1
243
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.
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 to this point a e efle!ted in the a!tive set5 (n7 !hanges $ade dete $ined# o a!le ta&es a snapshot of the ta%le5 (n7 !hanges that have %een !o$$itted p io 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
:oweve # if the
BOR UAD(T'
ows in the a!tive set %efo e the open etu ns5 These lo!&s p event othe sessions f o$ !hanging the then 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# ope ation will wait fo these lo!&s to %e eleased %7 the
S'L'CTRBOR UAD(T'
othe session5 The e is no ti$e"out fo this waiting pe iod5 The will hang until the othe
NO6(IT
session
!lause is availa%le5
If the !u so
BOR UAD(T'
!lause# the
6:'R' CURR'NT OB
'/0
D'CL(R'
fo v in ! loop update dept set dna$e H =aa= whe e !u ent of !. !o$$it. end loop.
'ND.
)ULF COLL'CT
1 1 1 1 1 1 1 1 1 1 1
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 SQL engine auto$ati!all7 initialiTes and e/tends the !olle!tions 7ou efe en!e in the %ul& !olle!t !lause5 )ul& !olle!t ope ation e$pties the !olle!tion %efo e e/e!uting the Uue 75 *ou !an use the li$it !lause of %ul& !olle!t to 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 est i!t the no of ows efe en!ed in the into !lause
'/0
D'CL(R'
245
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.
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'
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.
Lo! H
N'6 *ORF
Lo! H
D(LL(S
Dna$e H Dna$e H
S(L'S
Lo! H
C:IC(8O
OA'R(TIONS
Lo! H
)OSTON
'/0
D'CL(R'
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.
Lo! H
N'6 *ORF
Lo! H
D(LL(S
'/20
D'CL(R'
247
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.
'/10
D'CL(R'
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.
t7ped
'/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 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.
249
AOINTS TO R'M'M)'R
1 1 1 1 1
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
SQL IN AL?SQL
The onl7 state$ents allowed di e!tl7 in pl?sUl a e
DML
and
TCL5
)INDIN8
)inding a va ia%le is the p o!ess of identif7ing the sto age lo!ation asso!iated with an identifie in the p og a$5
T7pes of %inding
1 1 3 3
)inding du ing the !o$piled phase is ea l7 %inding5 )inding du ing the unti$e phase is late %inding5
251
In ea l7 %inding !o$pile phase will ta&e longe %e!ause of %inding wo & %ut the '/e!ution is faste 5
6 6 6
In late %inding it will sho ten the !o$pile phase %ut lengthens the e/e!ution ti$e5
AL?SQL
D*N(MIC SQL
1 1 1
If 7ou use
DDL
eUui es du ing !o$pile ti$e whi!h $a&es invalid5 6e !an avoid this %7 using D7na$i!
SQL5
SQL5
1 1
'/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.
'/0
D'CL(R'
v va !ha (233).
)'8IN
'/0
D'CL(R'
v va !ha (233).
)'8IN
'/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.
253
!lose !.
'ND.
Output0
SQL-
e/e! p(233)
'/0
D'CL(R'
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.
'/0
D'CL(R'
,,$7M%lo!&-D'CL(R'
'ND.
'/0
D'CL(R'
'/0
CR'(T' OR R'AL(C' AROC'DUR' A(s )'8IN
ow in studentL owt7pe)
IS
D'CL(R'
s studentL owt7pe.
)'8IN
s5no 0H 22.
255
'/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.
'ND.
'/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.
'/0
D'CL(R'
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
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 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.
257
Output0
Ta%le ! eated
SQL- des! stud Na$e SNO SN(M' Null[ T7pe NUM)'R(1) 9(RC:(R1(23) """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""""""" """""""" """"""""
'/10
student5noLt7pe#
!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
SQL-
sele!t E f o$ student.
NO 2
N( a
M(RFS 233
1 4
% !
133 433
SQL-
SQL-
sele!t E f o$ student.
NO 2 1 4
N( a % !
BOR(LL ST(T'M'NT
This
edu!ting the
AL?SQL
and
SQL
S7nta/0 Bo all inde/Mva in N 5ower_bound55upper_bound X Indi!es of inde(in"_collection X 9alues of inde(in"_collection O SQL state$ent.
'/0
D'CL(R'
i%t(2) 0H 2. i%t(23) 0H 1.
259
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
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. 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-
NO
N(
M(RFS
"""""""""" """"""""""""
2 1 4
SQL-
a % !
NO 2 1 4
N( a % !
"""""""""" """"""""""""
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 state$ent5
BOR(LL
'/0
D'CL(R'
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.
)'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
261
SQL-
NO 2 1 4
N( a % !
"""""""""" """"""""""""
SQL-
NO 2 1 4
N( a % !
"""""""""" """"""""""""
1 1 1
AL?SQL
ta%le to the
SQL
)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
1 1
DML
state$ents to
AL?SQL
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
anothe
S'L'CT RD)MS
using
R'TURNIN8
&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
1 1 1
9(RR(*S
9a
a7s
( va a7 is datat7pe ve 7 si$ila to an a a75 ( va a7 has a fi/ed li$it on its siTe# spe!ified as pa t of the de!la ation5 'le$ents a e inse ted into va a7 sta ting at inde/ 2# up to $a/i$u$ lenth de!la ed in the va a7 t7pe5 The $a/i$u$ siTe of the va a7 is 1 giga %7tes5
'/20
D'CL(R'
263
)'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)). else d%$sMoutput5putMline(=Inde/ D does not e/ists=). end if. va5e/tend(1). d%$sMoutput5putMline(=(fte va5!ount). d%$sMoutput5putMline(=9(RR(* fo i in va5fi st55va5last loop
'L'M'NTS=). 'L'M'NTS=).
d%$sMoutput5putMline(=vaN= XX i XX =O H = XX va(i)). end loop. va(C) 0H =e=. va(D) 0H =f=. va(V) 0H =g=. d%$sMoutput5putMline(=(BT'R (SSIN8NIN8 9(LU'S TO '+T'ND'D 'L'M'NTS#
9(RR(* 'L'M'NTS=).
fo i in va5fi st55va5last loop d%$sMoutput5putMline(=vaN= XX i XX =O H = XX va(i)). end loop. va5e/tend(4#1). d%$sMoutput5putMline(=(fte 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 va5!ount).
'ND.
265
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 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'
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ð Sno H 1 Sna$e H s inu Sno H 4 Sna$e H div7a Sno H @ Sna$e H $anogni
'/40
267
D'CL(R'
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
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ð 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'
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'
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.
269
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
'/20
D'CL(R'
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)). else d%$sMoutput5putMline(=Inde/ 4 does not e/ists=). end if. nt5e/tend. d%$sMoutput5putMline(=(fte 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 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
N'ST'D T()L' 'L'M'NTS=). (SSIN8NIN8 9(LU'S TO '+T'ND'D 'L'M'NTS#
271
end loop. nt5e/tend(C#1). d%$sMoutput5putMline(=(fte 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. nt5t i$. d%$sMoutput5putMline(=(fte t i$ of one inde/# Count H = XX nt5!ount). nt5t i$(4). d%$sMoutput5putMline(=(fte 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 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 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. delete of fou th inde/# Count H = XX delete of fi st inde/# Count H = XX t i$ of th ee inde/s# Count H = XX e/tend of five inde/es# Count H = XX
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 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
273
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 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
'/10
D'CL(R'
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ð 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'
fo i in 255nt5!ount loop
275
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'
t7pe
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ð 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'
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
'/D0
D'CL(R'
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.
277
*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
1 1 1 1
'/0
D'CL(R'
t7pe t is ta%le of va !ha (1). nt2 t 0H t(=a=#=%=#=!=). nt1 t 0H t(=!=#=%=#=a=). nt4 t 0H t(=%=#=!=#=a=#=!=). nt@ t 0H t(=a=#=%=#=d=). ntC t.
)'8IN
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)). 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
279
ntCN 2 O H a ntCN 1 O H % ntCN 4 O H ! ntCN @ O H a ntCN C O H % ntCN D O H d N'ST'D T()L' 'L'M'NTS ntCN 2 O H a ntCN 1 O H % ntCN 4 O H ! ntCN @ O H % ntCN C O H ! ntCN D O H a ntCN V O H ! N'ST'D T()L' 'L'M'NTS ntCN 2 O H a ntCN 1 O H % ntCN 4 O H ! N'ST'D T()L' 'L'M'NTS ntCN 2 O H ! N'ST'D T()L' 'L'M'NTS ntCN 2 O H d
IND'+")* T()L'S
(n inde/"%7 ta%le has no li$it on its siTe5 'le$ents a e inse ted into inde/"%7 ta%le whose inde/ $a7 sta t non"seUuentiall7 in!luding negative intege s5
'/0
D'CL(R'
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)). 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 i%t5!ount). delete of fi st inde/# Count H = XX
281
i%t5delete(43). d%$sMoutput5putMline(=(fte 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 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
1 1 1 1 1 1
9a 9a
a7s has li$it# nested ta%les and inde/"%7 ta%les has no li$it5 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
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 aises5 aises
SU)SCRIATM)'*ONDMCOUNT
in %oth
NOMD(T(MBOUND
1 1 1 1
Fe7s a e seUuential in %oth nested ta%les and va a7s# non"seUuential in inde/"%7 ta%les5 Individual inde/es !an %e deleted in %oth nested ta%les and inde/"%7 ta%les# %ut in va a7s !an not5 Individual inde/es !an %e t i$$ed in %oth nested ta%les and va a7s# %ut in inde/"%7 ta%les !an not5 Individual inde/es !an %e e/tended in %oth nested ta%les and va a7s# %ut in inde/"%7 ta%les !an not5
MULTIL'9'L COLL'CTIONS
Colle!tions of $o e than one di$ension whi!h is a !olle!tion of !olle!tions# &nown as $ultilevel !olle!tions5
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'
283
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). 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 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 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 va5!ount).
'ND.
Output0
285
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 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
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.
287
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=. 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 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 nt5!ount). d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=). fo i in 255nt5!ount loop fo J in 255nt5!ount loop d%$sMoutput5putMline(=ntN= XX i XX =ON= XX J XX =O H = XX nt(i)(J)). end loop. end loop. nt5delete(1). d%$sMoutput5putMline(=(fte nt5!ount). d%$sMoutput5putMline(=N'ST'D T()L' 'L'M'NTS=). loop delete of se!ond inde/# Count H = XX t i$ of two inde/es# Count H = XX e/tend of one inde/# Count H = XX
289
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 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 + ntNCON2O H * ntNCON1O H Z ntNCON4O H a
291
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'
t7pe t2 is ta%le of va !ha (1) inde/ %7 %ina 7Mintege . t7pe t1 is ta%le of t2 inde/ %7 %ina 7Mintege .
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). d%$sMoutput5putMline(=IND'+")* T()L' 'L'M'NTS=). fo i in 255W loop
293
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 i%t5!ount). i%t5delete(@). d%$sMoutput5putMline(=(fte 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.
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 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
295
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 . 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.
SQL-
sele!t E f o$ student.
SNO 2 1 4 @
SQL-
! eate o
va !ha (23)).?
SQL-
sele!t E f o$ e$plo7.
297
'RROR :(NDLIN8
AL?SQL i$ple$ents e o handling with e/!eptions and e/!eption handle s5 with 7ou own use "defined AL?SQL
e o s5 )7 using e/!eptions and e/!eption handle s# 7ou !an $a&e 7ou du ing e/e!ution5
'RROR T*A'S
1 1
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 athe
engine
whi!h
!an
p og a$$ati!all7 aise and !aught %7 e/!eption handle s5 '/!eptions a e designed fo e o handling5 un"ti$e e o handling# than !o$pile"ti$e
:(NDLIN8 '+C'ATIONS
6hen e/!eption is aised# !ont ol passes to the e/!eption se!tion of the %lo!&5 The e/!eption se!tion !onsists of handle s fo e/!eption handle so$e o all of the e/!eptions5 (n asso!iated !ontains the !ode that is e/e!uted when the e o
S7nta/0
'+C'ATION
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
3 3
AR'D'BIN'D '+C'ATIONS
299
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
'/20
D'CL(R'
a nu$%e . % va !ha (1). vM$a &s nu$%e . !u so ! is sele!t E f o$ student. t7pe t is va a7(4) of va !ha (1). va t 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
open !. open !.
'+C'ATION
when tooM$an7M ows then d%$sMoutput5putMline(=Too $an7 values a e !o$ing to $a &s va ia%le=).
'ND. "" Z'ROMDI9ID' )'8IN
a 0H C?3.
'+C'ATION
% 0H =sað=.
'+C'ATION
va(@) 0H =!=.
'+C'ATION
301
va(4) 0H =!=.
'+C'ATION
va2(2) 0H =a=.
'+C'ATION
""
'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.
US'R"D'BIN'D '+C'ATIONS
defined e/!eptions a e de!la ed in the de!la ative se!ion of a AL?SQL %lo!&5 >ust
'+C'ATION
R(ISIN8 '+C'ATIONS
'/0
D'CL(R'
e e/!eption.
)'8IN
aise e.
'+C'ATION
303
1 1 1
SQLCOD'
!ode# and
SQL'RRM
etu ns 2 and
SQL'RRM
deifned e/!eptionQ5 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'
aise e.
'+C'ATION
'/10
)'8IN
d%$sMoutput5putMline(SQL'RRM(233)).
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 OR("33S330 invalid SQL state$ent
D)MSMUTILIT*5BORM(TM'RRORMST(CF
1 1 1 1
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
Output0 de!la e
305
E 'RROR at line 20 OR("3DC310 AL?SQL0 nu$e i! o !onve sion e o OR("3DC210 at line 1 value e o 0 !ha a!te to nu$%e
D)MSMUTILIT*5BORM(TMC(LLMST(CF
will also find its hand7 fo t a!ing the e/e!tution of 7ou !ode5 *ou $a7 not use this fun!tion in e/!eption %lo!&5
'/0
)'8IN
d%$sMoutput5putMline(d%$sMutilit75fo $atM!allMsta!&).
'ND.
Output0 """"" AL?SQL Call Sta!& """"" O%Je!tMhandle DSVD3@VW lineMnu$%e 1 o%Je!tMna$e anon7$ous %lo!&
D)MSMUTILIT*5BORM(TM'RRORM)(CFTR(C'
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
)'8IN
Output0
SQL-
e/e! p4
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 handle 5 spe!ifi!all7# athe than via an
OT:'RS
S7nta/0
AR(8M( '+C'ATIONMINIT(e(ception_name#
oracle_error_number).
'/0
D'CL(R'
e e/!eption.
307
! 0H C?3.
'+C'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
R(IS'M(AALIC(TIONM'RROR(error_number#
error_me!!a"e##
N eep_error!_'la"O).
eep_error!_'la" is optional5 If it is
B(LS'#
TRU'#
the new e o is
added to the list of e o s al ead7 aised5 If it is e o will epla!e the !u ent list of e o s5
'/0
D'CL(R'
! nu$%e .
)'8IN
! 0H C?3.
'+C'ATION
Output0
D'CL(R'
E
'RROR
at line 20
'+C'ATION AROA(8(TION
'/!eptions !an o!!u in the de!la ative# the e/e!uta%le# o the e/!eption se!tion of a AL?SQL %lo!&5
'/!eptions aised in e/e!uata%le se!tion !an %e handled in !u ent %lo!& o oute %lo!&5
'/20
D'CL(R'
e e/!eption.
)'8IN )'8IN
aise e.
'ND. '+C'ATION
Output0 e is aised
'/10
D'CL(R'
e e/!eption.
)'8IN )'8IN
309
aise e.
'ND. 'ND.
Output0
'RROR
at line 20
'/!eptions aised in the de!la ative se!ion $ust %e handled in the oute %lo!&5
'/20
D'CL(R'
d%$sMoutput5putMline(=:ello=).
'+C'ATION
Output0
'RROR
'/10
)'8IN D'CL(R'
d%$sMoutput5putMline(=:ello=).
'+C'ATION
'/!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=). 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
311
'/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
'/40
D'CL(R'
e e/!eption.
)'8IN
aise e.
'+C'ATION
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
313
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
1 1
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
1 1 1
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 $ade the$5 (uto$ati!all7 signaling othe p og a$s that a!tion needs to ta&e pla!e e!o ding the !hanges $ade and who
1 1
T*A'S OB S
1 1 1 1 1
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
"" ""
fi es on!e fo
ea!h
C eate o
^)efo e X afte _ ^Inse t o update o delete_ on ,table nameNBo ea!h owO N6hen (R)O NDe!la eO "" De!la ation
315
)egin "" T igge %od7 N'/!eptionO "" '/!eption se!tion 'nd ,tri""er name-.
DML TRI88'RS
DML
t igge is fi ed on an
INS'RT# UAD(T'#
D'L'T'S
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
SQL-
Output0
SQL-
4 ows updated5
SQL-
sele!t E f o$ state$entMlevel.
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-
Output0
SQL-
4 ows updated5
SQL-
sele!t E f o$ state$entMlevel.
317
1 1 1 1
'/0
SQL-
sele!t E f o$ student.
NO N(M' 2 1 4 @ a % ! d
SQL-
)'8IN
Output0
SQL-
sele!t E f o$ fi ingMo de .
no ows sele!ted
SQL-
2 ow ! eated5
SQL-
sele!t E f o$ fi ingMo de .
ORD'R """""""""""""""""""""""""""""""""""""""""""""""""" )efo e State$ent Level )efo e Row Level (fte Row Level (fte State$ent Level
SQL-
sele!t E f o$ student.
NO N(M'
M(RFS
319
2 1 4 @ C
a % ! d e
Inside the t igge # 7ou !an a!!ess the data in the 0new5
p o!essed5 This is a!!o$plished th ough two !o elation identifie s " 0old and
AL?SQL
of ea!h indi!ates that the7 a e %ind va ia%les# in the sense of host va ia%les used and indi!ates that the7 a e not egula
AL?SQL
T igge ingMta%leLRO6T*A'5
e!o ds# in
0OLD
0N'6 """"""""""""""""""""""""""""
""""""""""""""""""""""""""""""""""""""""""""""" INS'RT
all fields a e
NULL5
!o$pleted5
UAD(T'
all fields a e
NULL5
'/0
SQL-
! eate ta%le $a &s(no nu$%e (1) oldM$a &s nu$%e (4)#newM$a &s nu$%e (4)).
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
SQL-
2 ow ! eated5
SQL-
sele!t E f o$ student.
NO N(M'
M(RFS
321
2 1 4 @ C D
a % ! d e f
SQL-
sele!t E f o$ $a &s.
SQL-
2 ow updated5
SQL-
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-
sele!t E f o$ $a &s.
NO
OLDMM(RFS
N'6MM(RFS
SQL-
2 ow deleted5
SQL-
sele!t E f o$ student.
NO N(M' 2 4 @ C D a ! d e f
SQL-
sele!t E f o$ $a &s.
R'B'R'NCIN8 CL(US'
R'B'R'NCIN8
ane 0new5 This !lause is found afte the t igge ing event# %efo e the
!lause5
S7nta/0
R'B'R'NCIN8
'/0
CR'(T' OR R'AL(C' TRI88'R R'B'R'NC'MTRI88'R
323
%efo e inse t o update o delete on student efe en!ing old as oldMstudent new as newMstudent fo ea!h ow
)'8IN
6:'N CL(US'
6:'N
!lause is valid fo
e/e!uted onl7 fo those ows that $eet the !ondition spe!ified %7 the
!lause5
S7nta/0
6:'N
tri""er_condition.
ea!h
ow5 The >new and >old e!o ds !an %e efe en!ed inside tri""er_condition as well# 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
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
1 1 1
'/0
SQL-
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.
'ND AR'DIC(T'MTRI88'R.
Output0
SQL-
2 ow deleted5
SQL-
sele!t E f o$ p edi!ates.
325
SQL-
2 ow ! eated5
SQL-
sele!t E f o$ p edi!ates.
SQL-
2 ow updated5
SQL-
sele!t E f o$ p edi!ates.
INST'(D"OB TRI88'RS
DML
1 1
'/0
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
SQL-
Output0
SQLSQL-
'MANO V4DS V@SS VC12 VCDD VDC@ VDSW VVW1 VVWW VW4S VW@@ VWVD VS33 VS31 VS4@ 1111
'N(M' SMIT: (LL'N 6(RD >ON'S )L(F' CL(RF SCOTT FIN8 (D(MS >(M'S BORD MILL'R sað
>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 CL'RF CL'RF (N(L*ST CL'RF do!to
S(L """"""""""" W33 2D33 21C3 1SVC 21C3 1WC3 1@C3 4333 C333 2C33 2233 SC3 4333 2433 W333
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 R'S'(RC: S(L'S R'S'(RC: (CCOUNTIN8 d
D'ATNO """""""""" 13 43 43 13 43 43 23 13 23 43 13 43 13 23 C3
""""""""""""" """""""""""""
M(RTIN S(L'SM(N
(CCOUNTIN8 N'6 *ORF N'6 *ORF C:IC(8O D(LL(S C:IC(8O D(LL(S N'6 *ORF %ang
TURN'R S(L'SM(N
SQL-
sele!t E f o$ dept.
327
D'ATNO """""""""" 23 13 43 @3 C3
SQL-
sele!t E f o$ e$p.
'MANO D'ATNO
'N(M'
>O)
M8R
:IR'D(T'
S(L """""""""
COMM """"""""" W33 433 C33 1SVC 2@33 1WC3 1@C3 4333 C333
"""""""""" """""""""" """""""""" V4DS 13 V@SS 43 VC12 43 VCDD 13 VDC@ 43 VDSW 43 VVW1 23 VVWW 13 VW4S FIN8 M(RTIN 6(RD (LL'N
SMIT:
>ON'S
31"(AR"W2 21C3
23 VW@@ 43 VWVD 13 VS33 43 VS31 13 VS4@ 23 1111 C3 sað do!to W333 MILL'R CL'RF VVW1 14">(N"W1 2433 BORD (N(L*ST VCDD 34"D'C"W2 4333 >(M'S CL'RF VDSW 34"D'C"W2 SC3 (D(MS CL'RF VVWW 14"M(*"WV 2233 TURN'R S(L'SM(N VDSW 3W"S'A"W2 2C33 3
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
^)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
329
inse t
into
$7Mo%Je!ts
Output0
SQL-
sele!t E f o$ $7Mo%Je!ts.
no ows sele!ted
SQL-
SQL-
sele!t E f o$ $7Mo%Je!ts.
O)>MN(M' STUD2
O)>MT*A' T()L'
SQL-
SQL-
SQL-
sele!t E f o$ $7Mo%Je!ts.
O)>MN(M'
O)>MT*A'
O)>MO6N'R
O)>MTIM'
STUD2 SS STUDM9I'6
6:'N CL(US'
If
6:'N
6:'N
'/0
CR'(T' OR R'AL(C' TRI88'R CR'(T'MTRI88'R
inse t
into
$7Mo%Je!ts
S*ST'M TRI88'RS
S7ste$ t igge s will fi e wheneve data%ase"wide event o!!u s5 The following a e the data%ase event t igge s5 To ! eate s7ste$ t igge
D(T()(S' TRI88'R
7ou need
(DMINIST'R
p ivilege5
1 1 1 1 1
S7nta/0
C eate o
331
^)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
SQL-
Output0
SQL-
no ows sele!ted
SQLSQL-
UMN(M' S(F'T:
"""""""""" """"""""""""""""""""""""""""""""""""""""""""""""
SQLSQL-
"""""""""" """"""""""""""""""""""""""""""""""""""""""""""""
SQLSQL-
"""""""""" """""""""""""""""""""""""""""""""""""""""""""""
S'R9'R'RROR
The e o '/0
S'R9'R'RROR
event !an %e used to t a!& e o s that o!!u in the data%ase5 The th ough the
S'R9'RM'RROR
att i%ute
fun!tion5
SQL-
afte se ve e o on data%ase
)'8IN
Output0
SQL-
333
SQL-
sele!t E f o$ $7Me o s.
SQL-
inse t into student values(2#1#4) 'RROR at line 20 OR("33S@10 ta%le o view does not e/ist
SQL-
sele!t E f o$ $7Me o s.
'RRORMMS8 """"""""""""""""""""""""""""""""""""""""""""""""""""""""""""" OR("33S110 $issing o invalid option OR("33S@10 ta%le o view does not e/ist
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
afte se ve e o on data%ase
)'8IN
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
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
SQL-
! 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
SQL-
$7Mspa!e.
Output0
335
(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 this# howeve # &nown as th ough pa ent autonomou! t ansa!tion# tran!action!5 %ut is (n autonomou! of it5 The
t ansa!tion !ould %e !o$$itted independent of the est of the ope ations5 O a!le allows tran!action is a t ansa!tion that is sta ted within the !onte/t of anothe t ansa!tion# independent autono$ous t ansa!tion !an %e !o$$itted o the pa ent t ansa!tion5 olled %a!& ega dless ot the state of
'/0
Output0
SQL-
sele!t E f o$ student.
NO N( 2 1 4 a % !
SQL-
SQL-
sele!t E f o$ student.
NO N( 2 1 4 @ a % ! d
1 1
If an autono$ous t ansa!tion atte$pts to a!!ess a $ain t ansa!tion# a deadlo!& !an o!!u in 7ou p og a$5
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 e/pli!it !o$$it o
INS'RT
UAD(T'
D'L'T'#
7ou $ust pe fo $ an
oll%a!&5 state$ents end the a!tive autono$ous t ansa!tion# state$ents inside 7ou autono$ous
The
COMMIT
and
ROLL)(CF
%ut the7 do not fo !e the te $ination of the autono$ous outine5 *ou !an have $ultiple %lo!&5
COMMIT
and?o
ROLL)(CF
1 1
*ou !an not oll%a!& to a savepoint set in the $ain t ansa!tion5 The
TR(NS(CTIONS
pa a$ete
$a/i$u$ nu$%e
default value is VC fo this# %ut 7ou !an in! ease the li$it5
MUT(TIN8 T()L'S
The e a e est i!tions on the ta%les and !olu$ns that a t igge %od7 $a7 a!!ess5
337
In o de to define these est i!tions# it is ne!essa 7 to unde stand $utating and !onst aining 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 integ it7 !onst aint5
efe ential
'/0
D'CL(R'
!t nu$%e .
)'8IN
Output0
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=
1 1
339