100% found this document useful (1 vote)
367 views173 pages

DDL DML DRL TCL DCL Create Table Syntax: © Copy Rights Are Reserved

4 INTRODUCTION SQL is divided into the ollo!in" Data De inition, Data &ani'#lation, transaction control and Data control. 9e have t!o methods to insert. -y val#e method -y address method.

Uploaded by

prakash_02009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
100% found this document useful (1 vote)
367 views173 pages

DDL DML DRL TCL DCL Create Table Syntax: © Copy Rights Are Reserved

4 INTRODUCTION SQL is divided into the ollo!in" Data De inition, Data &ani'#lation, transaction control and Data control. 9e have t!o methods to insert. -y val#e method -y address method.

Uploaded by

prakash_02009
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 173

1

4 INTRODUCTION [email protected]
SQL is divided into the ollo!in" Data De inition Lan"#a"e $DDL% Data &ani'#lation Lan"#a"e $D&L% Data Retrieval Lan"#a"e $DRL% Transaction Control Lan"#a"e $TCL% Data Control Lan"#a"e $DCL%

DDL (( create) alter) dro') tr#ncate) rename D&L (( insert) #'date) delete DRL (( select TCL (( commit) roll*ack) save 'oint DCL (( "rant) revoke CR+,T+ T,-L+ S.NT,/ Create ta*le 0table name1 $col1 datatype1, col2 datatype2 coln datatypen%2 +34 SQL1 create ta*le st#dent $no n#m*er $5%) name varchar $67%) marks n#m*er $8%%2 INS+RT This !ill *e #sed to insert the records into ta*le. 9e have t!o methods to insert. -y val#e method -y address method a% USIN: ;,LU+ &+T<OD Synta34 insert into 0table_name% val#es $value1, value2, value3 . Valuen%2 +34 SQL1 insert into st#dent val#es $6) =s#dha=) 677%2 SQL1 insert into st#dent val#es $5) =saketh=) 577%2 To insert a ne! record a"ain yo# have to ty'e entire insert command) i there are lot o Records this !ill *e di ic#lt. This !ill *e avoided *y #sin" address method. *%
USIN: ,DDR+SS &+T<OD

Synta34 insert into 0table_name% val#es (&col1, &col2, &col3 . &coln%2 This !ill 'rom't yo# or the val#es *#t or every insert yo# have to #se or!ard slash. +34
SQL1

insert into st#dent val#es $>no) ?>name?) >marks%2

+nter val#e or no4 6

Copy rights are reserved.

+nter val#e or name4 @a"an +nter val#e or marks4 877 old 64 insert into st#dent val#es$>no) ?>name?) >marks% ne! 64 insert into st#dent val#es$6) ?@a"an?) 877% A +nter val#e or no4 5 +nter val#e or name4 Naren +nter val#e or marks4 477 old 64 insert into st#dent val#es$>no) ?>name?) >marks% ne! 64 insert into st#dent val#es$5) ?Naren?) 477%
SQL1

c%

INS+RTIN: D,T, INTO SB+CICI+D COLU&NS USIN: ;,LU+ &+T<OD

Synta34 insert into 0table_name%$col1, col2, col3 Coln% val#es $value1, value2, value3 . Valuen%2 +34 SQL1 insert into st#dent $no) name% val#es $8) =Ramesh=%2 SQL1 insert into st#dent $no) name% val#es $4) =&adh#=%2 d%
INS+RTIN: D,T, INTO SB+CICI+D COLU&NS USIN: ,DDR+SS &+T<OD

Synta34 insert into 0table_name1 $col1, col2, col3 coln% val#es (&col1, &col2, &col3 . &coln%2 This !ill 'rom't yo# or the val#es *#t or every insert yo# have to #se or!ard slash. +34 insert into st#dent $no) name% val#es $>no) ?>name?%2 +nter val#e or no4 D +nter val#e or name4 ;is# old 64 insert into st#dent $no) name% val#es$>no) ?>name?% ne! 64 insert into st#dent $no) name% val#es$D) ?;is#?%
SQL1

A +nter val#e or no4 E +nter val#e or name4 Ratt# old 64 insert into st#dent $no) name% val#es$>no) ?>name?% ne! 64 insert into st#dent $no) name% val#es$E) ?Ratt#?%
SQL1

S+L+CTIN: D,T, Synta34 Select F rom 0table_name12 (( here F indicates all col#mns or Select col1, col2, coln rom 0table_name12 +34
SQL1

select F rom st#dent2 NO N,&+ &,RGS ((( (((((( (((((((( 6 S#dha 677 5 Saketh 577 6 @a"an 877 5 Naren 477 8 Ramesh 4 &adh#

Copy rights are reserved.

D E
SQL1

;is# Ratt#

select no) name) marks rom st#dent2 NO ((( 6 5 6 5 8 4 D E N,&+ (((((( S#dha Saketh @a"an Naren Ramesh &adh# ;is# Ratt# &,RGS (((((((( 677 577 877 477

SQL1

select no) name rom st#dent2 NO ((( 6 5 6 5 8 4 D E N,&+ ((((((( S#dha Saketh @a"an Naren Ramesh &adh# ;is# Ratt#

CONDITION,L S+L+CTIONS ,ND OB+R,TORS


9e have t!o cla#ses #sed in this 9here Order *y USIN: 9<+R+ Synta34 select F rom 0table_name1 !here 0condition12 the ollo!in" are the di erent ty'es o o'erators #sed in !here cla#se. ,rithmetic o'erators Com'arison o'erators Lo"ical o'erators ,rithmetic o'erators (( hi"hest 'recedence H) () F) A Com'arison o'erators I) JI) 1) 0) 1I) 0I) 01 *et!een) not *et!een in) not in n#ll) not n#ll like Lo"ical o'erators ,nd Or (( lo!est 'recedence not a%
USIN:

I) 1) 0) 1I) 0I) JI) 01

Copy rights are reserved.

+34
SQL1

4
select F rom st#dent !here no I 52 NO ((( 5 5 N,&+ ((((((( Saketh Naren &,RGS ((((((((( 577 477

SQL1

select F rom st#dent !here no 0 52 NO ((( 6 6 N,&+ ((((((( S#dha @a"an &,RGS (((((((((( 677 877

SQL1

select F rom st#dent !here no 1 52 NO N,&+ ((( ((((((( 8 Ramesh 4 &adh# D ;is# E Ratt# &,RGS ((((((((((

SQL1

select F rom st#dent !here no 0I 52 NO N,&+ ((( ((((((( 6 S#dha 5 Saketh 6 @a"an 5 Naren &,RGS (((((((((( 677 577 877 477

SQL1

select F rom st#dent !here no 1I 52 NO N,&+ ((( ((((((( 5 Saketh 5 Naren 8 Ramesh 4 &adh# D ;is# E Ratt# &,RGS ((((((((( 577 477

SQL1

select F rom st#dent !here no JI 52 NO N,&+ ((( ((((((( 6 S#dha 6 @a"an 8 Ramesh 4 &adh# D ;is# E Ratt# &,RGS (((((((((( 677 877

SQL1

select F rom st#dent !here no 01 52 NO N,&+ ((( ((((((( 6 S#dha &,RGS (((((((((( 677

Copy rights are reserved.

6 8 4 D E *%
USIN: ,ND

@a"an Ramesh &adh# ;is# Ratt#

877

This !ill "ives the o#t'#t !hen all the conditions *ecome tr#e. Synta34 select F rom 0table name1 !here 0condition11 and 0condition21 and .. 0conditionn12 +34
SQL1

select F rom st#dent !here no I 5 and marks 1I 5772 NO N,&+ ((( ((((((( 5 Saketh 5 Naren &,RGS (((((((( 577 477

c%

USIN: OR

This !ill "ive the o#t'#t !hen either o the conditions *ecomes tr#e. Synta34 select F rom 0table_name1 !here 0condition11 and 0condition21 or .. 0conditionn12 +34
SQL1

select F rom st#dent !here no I 5 or marks 1I 5772 NO N,&+ ((( ((((((( 5 Saketh 6 @a"an 5 Naren &,RGS ((((((((( 577 877 477

d%

USIN: -+T9++N

This !ill "ives the o#t'#t *ased on the col#mn and its lo!er *o#nd) #''er*o#nd. Synta34 select F rom 0table_name1 !here 0col1 *et!een 0lower bound1 and 0upper bound12 +34
SQL1

select F rom st#dent !here marks *et!een 577 and 4772 NO N,&+ ((( ((((((( 5 Saketh 6 @a"an 5 Naren &,RGS ((((((((( 577 877 477

e%

USIN: NOT -+T9++N

Copy rights are reserved.

This !ill "ives the o#t'#t *ased on the col#mn !hich val#es are not in its lo!er *o#nd) #''er*o#nd. Synta34 select F rom 0table_name1 !here 0col1 not *et!een 0lower bound1 and 0upper bound12 +34
SQL1

select F rom st#dent !here marks not *et!een 577 and 4772 NO N,&+ ((( ((((((( 6 S#dha &,RGS ((((((((( 677

USIN: IN

This !ill "ives the o#t'#t *ased on the col#mn and its list o val#es s'eci ied. Synta34 select F rom 0table_name1 !here 0col1 in $ value1, value2, value3 valuen%2 +34
SQL1

select F rom st#dent !here no in $6) 5) 8%2 NO N,&+ ((( ((((((( 6 S#dha 5 Saketh 6 @a"an 5 Naren 8 Ramesh &,RGS ((((((((( 677 577 877 477

"%

USIN: NOT IN

This !ill "ives the o#t'#t *ased on the col#mn !hich val#es are not in the list o val#es s'eci ied. Synta34 select F rom 0table_name1 !here 0col1 not in $ value1, value2, value3 valuen%2 +34
SQL1

select F rom st#dent !here no not in $6) 5) 8%2 NO N,&+ ((( ((((((( 4 &adh# D ;is# E Ratt# &,RGS (((((((((

h%

USIN: NULL

This !ill "ives the o#t'#t *ased on the n#ll val#es in the s'eci ied col#mn. Synta34 select F rom 0table_name1 !here 0col1 is n#ll2 +34

Copy rights are reserved.

SQL1

select F rom st#dent !here marks is n#ll2 NO N,&+ ((( ((((((( 8 Ramesh 4 &adh# D ;is# E Ratt# &,RGS (((((((((

i%

USIN: NOT NULL

This !ill "ives the o#t'#t *ased on the not n#ll val#es in the s'eci ied col#mn. Synta34 select F rom 0table_name1 !here 0col1 is not n#ll2 +34
SQL1

select F rom st#dent NO N,&+ ((( ((((((( 6 S#dha 5 Saketh 6 @a"an 5 Naren

!here marks is not n#ll2 &,RGS ((((((((( 677 577 877 477

K%

USIN: LIG+

This !ill *e #sed to search thro#"h the ro!s o data*ase col#mn *ased on the 'attern yo# s'eci y. Synta34 select F rom 0table_name1 !here 0col1 like 0pattern12 +34 i% This !ill "ive the ro!s !hose marks are 677.
SQL1

select F rom st#dent !here marks like 6772 NO N,&+ ((( ((((((( 6 S#dha &,RGS ((((((((( 677

ii% This !ill "ive the ro!s !hose name start !ith LS=.
SQL1

select F rom st#dent !here name like ?SM?2 NO N,&+ ((( ((((((( 6 S#dha 5 Saketh &,RGS ((((((((( 677 577

iii% This !ill "ive the ro!s !hose name ends !ith Lh=.
SQL1

select F rom st#dent !here name like ?Mh?2 NO N,&+ ((( ((((((( 5 Saketh 8 Ramesh &,RGS ((((((((( 577

Copy rights are reserved.

8
i;% This !ill "ive the ro!s !hose name=s second letter start !ith La=.
SQL1

select F rom st#dent !here name like ?NaM?2 NO N,&+ ((( ((((((( 5 Saketh 6 @a"an 5 Naren 8 Ramesh 4 &adh# E Ratt# &,RGS (((((((( 577 877 477

;% This !ill "ive the ro!s !hose name=s third letter start !ith Ld=.
SQL1

select F rom st#dent !here name like ?NNdM?2 NO N,&+ ((( ((((((( 6 S#dha 4 &adh# &,RGS ((((((((( 677

;i% This !ill "ive the ro!s !hose name=s second letter start !ith Lt= rom endin".
SQL1

select F rom st#dent !here name like ?MNtM?2 NO N,&+ ((( ((((((( 5 Saketh E Ratt# &,RGS ((((((((( 577

;ii% This !ill "ive the ro!s !hose name=s third letter start !ith Le= rom endin".
SQL1

select F rom st#dent !here name like ?MeNNM?2 NO N,&+ ((( ((((((( 5 Saketh 8 Ramesh &,RGS ((((((((( 577

;iii% This !ill "ive the ro!s !hose name cotains 5 a=s.
SQL1

select F rom st#dent !here name like ?MaM a M?2 NO N,&+ ((( ((((((( 6 @a"an &,RGS (((((((((( 877

F .o# have to s'eci y the 'atterns in like #sin" #nderscore $ N %. USIN: ORD+R -. This !ill *e #sed to orderin" the col#mns data $ascendin" or descendin"%. Synta34 Select F rom 0table_name1 order *y 0col1 desc2 -y de a#lt oracle !ill #se ascendin" order.

Copy rights are reserved.

I yo# !ant o#t'#t in descendin" order yo# have to #se desc key!ord a ter the col#mn. +34
SQL1

select F rom st#dent order *y no2 NO ((( 6 6 5 5 8 4 D E N,&+ ((((((( S#dha @a"an Saketh Naren Ramesh &adh# ;is# Ratt# &,RGS ((((((((( 677 877 577 477

SQL1

select F rom st#dent order *y no desc2 NO N,&+ ((( ((((((( E Ratt# D ;is# 4 &adh# 8 Ramesh 5 Saketh 5 Naren 6 S#dha 6 @a"an &,RGS (((((((((

577 477 677 877

USIN: D&L
USIN: UBD,T+ This can *e #sed to modi y the ta*le data. Synta34 U'date 0table_name1 set 0col11 I val#e6) 0col21 I val#e5 !here 0condition12 +34 #'date st#dent set marks I D772 I yo# are not s'eci yin" any condition this !ill #'date entire ta*le.
SQL1 SQL1 SQL1

#'date st#dent set marks I D77 !here no I 52 #'date st#dent set marks I D77) name I ?;en#? !here no I 62

USIN: D+L+T+ This can *e #sed to delete the ta*le data tem'orarily. Synta34 Delete 0table_name1 !here 0condition12 +34 delete st#dent2 I yo# are not s'eci yin" any condition this !ill delete entire ta*le.
SQL1 SQL1

delete st#dent !here no I 52

Copy rights are reserved.

10

USIN: DDL
USIN: ,LT+R This can *e #sed to add or remove col#mns and to modi y the 'recision o the dataty'e. a%
,DDIN: COLU&N

Synta34 +34 *%
SQL1

alter ta*le 0table_name1 add 0col datatype12 alter ta*le st#dent add sdo* date2

R+&O;IN: COLU&N

Synta34 alter ta*le 0table_name1 dro' 0col datatype12 +34


SQL1

alter ta*le st#dent dro' col#mn sdo*2

c%

INCR+,SIN: OR D+CR+,SIN: BR+CISION OC , COLU&N

Synta34 alter ta*le 0table_name1 modi y 0col datatype12 +34 SQL1 alter ta*le st#dent modi y marks n#m*er$D%2 F To decrease 'recision the col#mn sho#ld *e em'ty. d%
&,GIN: COLU&N UNUS+D

Synta34 alter ta*le 0table_name1 set #n#sed col#mn 0col12 +34 SQL1 alter ta*le st#dent set #n#sed col#mn marks2 +ven tho#"h the col#mn is #n#sed still it !ill occ#'y memory. d%
DROBBIN: UNUS+D COLU&NS

Synta34 alter ta*le 0table_name1 dro' #n#sed col#mns2 +34 alter ta*le st#dent dro' #n#sed col#mns2 F .o# can not dro' individ#al #n#sed col#mns o a ta*le.
SQL1

e%

R+N,&IN: COLU&N

Synta34 alter ta*le 0table_name1 rename col#mn 0old_col_name1 to 0new_col_name12 +34


SQL1

alter ta*le st#dent rename col#mn marks to smarks2

USIN: TRUNC,T+ This can *e #sed to delete the entire ta*le data 'ermanently. Synta34

Copy rights are reserved.

tr#ncate ta*le 0table_name12 +34


SQL1

11

tr#ncate ta*le st#dent2

USIN: DROB This !ill *e #sed to dro' the data*ase o*Kect2 Synta34 Dro' ta*le 0table_name12 +34
SQL1

dro' ta*le st#dent2

USIN: R+N,&+ This !ill *e #sed to rename the data*ase o*Kect2 Synta34 rename 0oldNtable_name1 to 0new_table_name12 +34
SQL1

rename st#dent to st#d2

U SI N : T CL
USIN: CO&&IT This !ill *e #sed to save the !ork. Commit is o t!o ty'es. Im'licit +3'licit a%
I&BLICIT

This !ill *e iss#ed *y oracle internally in t!o sit#ations. 9hen any DDL o'eration is 'er ormed. 9hen yo# are e3itin" rom SQL F BLUS. *%
+/BLICIT

This !ill *e iss#ed *y the #ser. Synta34 Commit or commit !ork2 F 9hen ever yo# committed then the transaction !as com'leted. USIN: ROLL-,CG This !ill #ndo the o'eration. This !ill *e a''lied in t!o methods. U'to 'revio#s commit U'to 'revio#s roll*ack

Copy rights are reserved.

12
Synta34 Roll or roll !ork2 Or Roll*ack or roll*ack !ork2 F 9hile 'rocess is "oin" on) i s#ddenly 'o!er "oes then oracle !ill roll*ack the transaction. USIN: S,;+BOINT .o# can #se save'oints to roll*ack 'ortions o yo#r c#rrent set o transactions. Synta34 Save'oint 0savepoint_name12 +34 save'oint s62 insert into st#dent val#es$6) La=) 677%2 SQL1 save'oint s52 SQL1 insert into st#dent val#es$5) L*=) 577%2 SQL1 save'oint s82 SQL1 insert into st#dent val#es$8) Lc=) 877%2 SQL1 save'oint s42 SQL1 insert into st#dent val#es$4) Ld=) 477%2
SQL1 SQL1

-e ore roll*ack
SQL1

select F rom st#dent2 NO N,&+ ((( ((((((( 6 a 5 * 8 c 4 d &,RGS (((((((((( 677 577 877 477

SQL1 SQL1

roll*ack to save'oint s82 Or roll*ack to s82

This !ill roll*ack last t!o records. select F rom st#dent2 NO N,&+ ((( ((((((( 6 a 5 * &,RGS (((((((((( 677 577

SQL1

USIN: DCL
DCL commands are #sed to "rantin" and revokin" the 'ermissions. USIN: :R,NT This is #sed to "rant the 'rivile"es to other #sers. Synta34 :rant 0privile es1 on 0ob!ect_name1 to 0user_name1 O!ith "rant o'tionP2

Copy rights are reserved.

+34
SQL1 SQL1

13

"rant select on st#dent to s#dha2 (( yo# can "ive individ#al 'rivile"e "rant select) insert on st#dent to s#dha2 (( yo# can "ive set o 'rivile"es SQL1 "rant all on st#dent to s#dha2 (( yo# can "ive all 'rivile"es The s#dha #ser has to #se dot method to access the o*Kect. SQL1 select F rom saketh.st#dent2 The s#dha #ser can not "rant 'ermission on st#dent ta*le to other #sers. To "et this ty'e o o'tion #se the ollo!in". SQL1 "rant all on st#dent to s#dha !ith "rant o'tion2 No! s#dha #ser also "rant 'ermissions on st#dent ta*le. USIN: R+;OG+ This is #sed to revoke the 'rivile"es rom the #sers to !hich yo# "ranted the 'rivile"es. Synta34 Revoke 0privile es1 on 0ob!ect_name1 rom 0user_name12 +34
SQL1 revoke select on st#dent orm s#dha2 (( yo# can revoke individ#al 'rivile"e SQL1 revoke select) insert on st#dent rom s#dha2 (( yo# can revoke set o 'rivile"es SQL1 revoke all on st#dent rom s#dha2 (( yo# can revoke all 'rivile"es

USIN: ,LI,S+S
CR+,T+ 9IT< S+L+CT 9e can create a ta*le #sin" e3istin" ta*le Oalon" !ith dataP. Synta34 Create ta*le 0new_table_name1 Ocol1, col2, col3 ... colnP as select F rom 0old_table_name12 +34 SQL1 create ta*le st#dent6 as select F rom st#dent2 Creatin" ta*le !ith yo#r o!n col#mn names. create ta*le st#dent5$sno) sname) smarks% as select F rom st#dent2 Creatin" ta*le !ith s'eci ied col#mns. SQL1 create ta*le st#dent8 as select no)name rom st#dent2 Creatin" ta*le !ith o#t ta*le data. create ta*le st#dent5$sno) sname) smarks% as select F rom st#dent !here 6 I 52 In the a*ove !here cla#se "ive any condition !hich does not satis y.
SQL1

SQL1

INS+RT 9IT< S+L+CT Usin" this !e can insert e3istin" ta*le data to another ta*le in a sin"le tri'. -#t the ta*le str#ct#re sho#ld *e same. Synta34

Copy rights are reserved.

Insert into 0table11 select F rom 0table212 +34


SQL1

14

insert into st#dent6 select F rom st#dent2

Insertin" data into s'eci ied col#mns SQL1 insert into st#dent6$no) name% select no) name rom st#dent2 COLU&N ,LI,S+S Synta34 "elect #or inal_col$ #alias_name$ %rom #tableNname12 +34
SQL1 SQL1

select no sno rom st#dent2 or select no QsnoR rom st#dent2

T,-L+ ,LI,S+S I yo# are #sin" ta*le aliases yo# can #se dot method to the col#mns. Synta34 Select 0alias_name1.0col11) 0alias_name1.0col21 S 0alias_name1.0coln1 rom 0table_name1 0alias_name12 +34
SQL1

select s.no) s.name rom st#dent s2

USIN: &+R:+
&+R:+ .o# can #se mer"e command to 'er orm insert and #'date in a sin"le command. +34
SQL1

&er"e into st#dent6 s6 Usin" $select FCrom st#dent5% s5 On$s6.noIs5.no% 9hen matched then U'date set marks I s5.marks 9hen not matched then Insert $s6.no)s6.name)s6.marks% ;al#es$s5.no)s5.name)s5.marks%2

In the a*ove the t!o ta*les are !ith the same str#ct#re *#t !e can mer"e di erent str#ct#red ta*les also *#t the dataty'e o the col#mns sho#ld match. ,ss#me that st#dent6 has col#mns like no)name)marks and st#dent5 has col#mns like no) name) hno) city. SQL1 &er"e into st#dent6 s6 Usin" $select FCrom st#dent5% s5 On$s6.noIs5.no% 9hen matched then

Copy rights are reserved.

U'date set marks I s5.hno 9hen not matched then Insert $s6.no)s6.name)s6.marks% ;al#es$s5.no)s5.name)s5.hno%2

15

&ULTIBL+ INS+RTS
9e have ta*le called D+BT !ith the ollo!in" col#mns and data D+BTNO (((((((( 67 57 87 47 a% DN,&+ (((((((( acco#ntin" research sales o'erations LOC (((( ne! york dallas Chica"o *oston

CR+,T+ STUD+NT T,-L+ SQL1

Create

ta*le

st#dent$no

n#m*er$5%)name

varchar$5%)marks

n#m*er$8%%2 *%
&ULTI INS+RT 9IT< ,LL CI+LDS SQL1

Insert all Into st#dent val#es$6)=a=)677% Into st#dent val#es$5)=*=)577% Into st#dent val#es$8)=c=)877% Select F rom de't !here de'tnoI672

(( This inserts 8 ro!s c%


&ULTI INS+RT 9IT< SB+CICI+D CI+LDS SQL1

insert all Into st#dent $no)name% val#es$4)=d=% Into st#dent$name)marks% val#es$=e=)477% Into st#dent val#es$8)=c=)877% Select F rom de't !here de'tnoI672

(( This inserts 8 ro!s d%


&ULTI INS+RT 9IT< DUBLIC,T+ RO9S SQL1

insert all Into st#dent val#es$6)=a=)677% Into st#dent val#es$5)=*=)577% Into st#dent val#es$8)=c=)877% Select F rom de't !here de'tno 1 672

(( This inserts T ro!s *eca#se in the select statement retrieves 8 records $8 inserts or each ro! retrieved% e%
&ULTI INS+RT 9IT< CONDITIONS -,S+D SQL1

Insert all 9hen de'tno 1 67 then Into st#dent6 val#es$6)=a=)677% 9hen dname I LS,L+S= then Into st#dent5 val#es$5)=*=)577%

Copy rights are reserved.

9hen loc I LN+9 .ORG= then Into st#dent8 val#es$8)=c=)877% Select F rom de't !here de'tno1672

16

(( This inserts 4 ro!s *eca#se the irst condition satis ied 8 times) second condition satis ied once and the last none. %
&ULTI INS+RT 9IT< CONDITIONS -,S+D ,ND +LS+ SQL1

Insert all 9hen de'tno 1 677 then Into st#dent6 val#es$6)=a=)677% 9hen dname I LS= then Into st#dent5 val#es$5)=*=)577% 9hen loc I LN+9 .ORG= then Into st#dent8 val#es$8)=c=)877% +lse Into st#dent val#es$4)=d=)477% Select F rom de't !here de'tno1672

(( This inserts 8 records *eca#se the else satis ied 8 times "%
&ULTI INS+RT 9IT< CONDITIONS -,S+D ,ND CIRST SQL1

Insert irst 9hen de'tno I 57 then Into st#dent6 val#es$6)=a=)677% 9hen dname I LR+S+,RC<= then Into st#dent5 val#es$5)=*=)577% 9hen loc I LN+9 .ORG= then Into st#dent8 val#es$8)=c=)877% Select F rom de't !here de'tnoI572

(( This inserts 6 record *eca#se the irst cla#se avoid to check the remainin" conditions once the condition is satis ied. h%
&ULTI INS+RT 9IT< CONDITIONS -,S+D) CIRST ,ND +LS+ SQL1

Insert irst 9hen de'tno I 87 then Into st#dent6 val#es$6)=a=)677% 9hen dname I LR= then Into st#dent5 val#es$5)=*=)577% 9hen loc I LN+9 .ORG= then Into st#dent8 val#es$8)=c=)877% +lse Into st#dent val#es$4)=d=)477% Select F rom de't !here de'tnoI572

(( This inserts 6 record *eca#se the else cla#se satis ied once

i%

&ULTI INS+RT 9IT< &ULTI-L+ T,-L+S SQL1

Insert all Into st#dent6 val#es$6)=a=)677% Into st#dent5 val#es$5)=*=)577%

Copy rights are reserved.

Into st#dent8 val#es$8)=c=)877% Select F rom de't !here de'tnoI672 (( This inserts 8 ro!s

17

FF .o# can #se m#lti ta*les !ith s'eci ied ields) !ith d#'licate ro!s) !ith conditions) !ith irst and else cla#ses.

CUNCTIONS
C#nctions can *e cate"oriUed as ollo!s. Sin"le ro! #nctions :ro#' #nctions

SIN:L+ RO9 CUNCTIONS Sin"le ro! #nctions can *e cate"oriUed into ive. These !ill *e a''lied or each ro! and 'rod#ces individ#al o#t'#t or each ro!. N#meric #nctions Strin" #nctions Date #nctions &iscellaneo#s #nctions Conversion #nctions

NU&+RIC CUNCTIONS

a%
,-S

,*s Si"n SVrt &od Nvl Bo!er +3' Ln Lo" Ceil Cloor Ro#nd Tr#nk -itand :reatest Least Coalesce

,*sol#te val#e is the meas#re o the ma"nit#de o val#e. ,*sol#te val#e is al!ays a 'ositive n#m*er. Synta34 a*s $value% +34
SQL1

select a*s$D%) a*s$(D%) a*s$7%) a*s$n#ll% rom d#al2

Copy rights are reserved.

,-S$D% ,-S$(D% (((((((((( (((((((((( D D *%


SI:N

,-S$7% ,-S$NULL% (((((((((( ((((((((((((( 7

18

Si"n "ives the si"n o a val#e. Synta34 si"n $value% +34


SQL1

select si"n$D%) si"n$(D%) si"n$7%) si"n$n#ll% rom d#al2 SI:N$D% SI:N$(D% (((((((((( (((((((((( 6 (6 SI:N$7% SI:N$NULL% (((((((((( (((((((((((((( 7

c%

SQRT

This !ill "ive the sV#are root o the "iven val#e. Synta34 sVrt $value% +34
SQL1

(( here val#e m#st *e 'ositive.

select sVrt$4%) sVrt$7%) sVrt$n#ll%) sVrt$6% rom d#al2 SQRT$4% (((((((((( 5 SQRT$7% SQRT$NULL% (((((((((( ((((((((((((((( 7 SQRT$6% (((((((((( 6

d%

&OD

This !ill "ive the remainder. Synta34 mod $value, divisor% +34
SQL1

rom d#al2

select mod$W)4%) mod$6)D%) mod$n#ll)n#ll%) mod$7)7%) mod$(W)4% &OD$W)4% &OD$6)D% &OD$NULL)NULL% (((((((((((( (((((((((( ((((((((((((((((((((( 8 6 &OD$7)7% &OD$(W)4% ((((((((((( ((((((((((((( 7 (8

e%

N;L

This !ill s#*stit#tes the s'eci ied val#e in the 'lace o n#ll val#es. Synta34 nvl $null_col, replacement_value% +34
SQL1

select F rom st#dent2 NO N,&+ ((( ((((((( 6 a 5 * 8 c &,RGS ((((((((( 677 577

(( here or 8rd ro! marks val#e is n#ll

SQL1

select no) name) nvl$marks)877% rom st#dent2

Copy rights are reserved.

19
NO N,&+ N;L$&,RGS)877% ((( ((((((( ((((((((((((((((((((( 6 a 677 5 * 577 8 c 877
SQL1

select nvl$6)5%) nvl$5)8%) nvl$4)8%) nvl$D)4% rom d#al2 N;L$6)5% (((((((((( 6 N;L$5)8% (((((((((( 5 N;L$4)8% (((((((((( 4 N;L$D)4% (((((((((( D

SQL1

select nvl$7)7%) nvl$6)6%) nvl$n#ll)n#ll%) nvl$4)4% rom d#al2 N;L$7)7% (((((((((( 7 N;L$6)6% N;L$n#ll)n#ll% N;L$4)4% (((((((((( ((((((((((((((((( (((((((((( 6 4

BO9+R

Bo!er is the a*ility to raise a val#e to a "iven e3'onent. Synta34 'o!er $value, e&ponent% select 'o!er$5)D%) 'o!er$7)7%) 'o!er$6)6%) 'o!er$n#ll)n#ll%) 'o!er$5)(D% rom d#al2
SQL1

+34

BO9+R$5)D% BO9+R$7)7% BO9+R$6)6% BO9+R$NULL)NULL% BO9+R$5)(D% (((((((((((((( (((((((((((((( ((((( ((((((((( ((((((((((((((((((((((( ((((((((((((((( 85 6 6 .7865D "%
+/B

This !ill raise e val#e to the "ive 'o!er. Synta34 e3' $value% +34
SQL1

select e3'$6%) e3'$5%) e3'$7%) e3'$n#ll%) e3'$(5% rom d#al2 +/B$6% +/B$5% +/B$7% +/B$NULL% (((((((( ((((((((( (((((((( ((((((((((((( 5.W6X5X6X8 W.8XT7DE6 6 +/B$(5% (((((((((( .68D88D5X8

h%

LN

This is *ased on nat#ral or *ase e lo"arithm. Synta34 ln $value% 'ositive only. +34
SQL1

(( here val#e m#st *e "reater than Uero !hich is

select ln$6%) ln$5%) ln$n#ll% rom d#al2 LN$6% LN$5% LN$NULL%

Copy rights are reserved.

((((((( ((((((( (((((((((((( 7 .ET864W6X6 Ln and +3' are reci'rocal to each other. $8% I 57.7XDD8ET LN $57.7XDD8ET% I 8
+/B

20

i%

LO:

This is *ased on 67 *ased lo"arithm. Synta34 lo" $67) value% (( here val#e m#st *e "reater than Uero !hich is 'ositive only. +34
SQL1

select lo"$67)677%) lo"$67)5%) lo"$67)6%) lo"$67)n#ll% rom d#al2 LO:$67)677% LO:$67)5% LO:$67)6% LO:$67)NULL% ((((((((((((((( ((((((((((( (((((((((((( ((((((((((((((((( 5 .87675TTTE 7

LN

$val#e% I

LO:

$+/B$6%) val#e%

SQL1

select ln$8%) lo"$e3'$6%)8% rom d#al2 LN$8% LO:$+/B$6%)8% ((((((( ((((((((((((((((( 6.7TXE655T 6.7TXE655T

K%

C+IL

This !ill 'rod#ce a !hole n#m*er that is "reater than or eV#al to the s'eci ied val#e. Synta34 ceil $value% +34
SQL1

d#al2

select ceil$D%) ceil$D.6%) ceil$(D%) ceil$ (D.6%) ceil$7%) ceil$n#ll% rom C+IL$D% C+IL$D.6% ((((((((( ((((((((((( D E C+IL$(D% C+IL$(D.6% (((((((((( (((((((((((( (D (D C+IL$7% C+IL$NULL% (((((((( (((((((((((((( 7

k%

CLOOR

This !ill 'rod#ce a !hole n#m*er that is less than or eV#al to the s'eci ied val#e. Synta34 loor $value% +34
SQL1

select loor$D%) loor$D.6%) loor$(D%) loor$ (D.6%) loor$7%) loor$n#ll%

rom d#al2 CLOOR$D% CLOOR$D.6% CLOOR$(D% CLOOR$(D.6% CLOOR$7% CLOOR$NULL% ((((((((((( ((((((((((((( (((((((((((( (((((((((((((( ((((((((((( (((((((((((((((( D D (D (E 7

Copy rights are reserved.

l%

21
ROUND

This !ill ro#nds n#m*ers to a "iven n#m*er o di"its o 'recision. Synta34 ro#nd $value, precision% +34
SQL1

rom d#al2

select ro#nd$658.584D%) ro#nd$658.584D)5%) ro#nd$658.58D4)5%

ROUND$658.584D% ROUND$658.584D)7% ROUND$658.584D)5% ROUND$658.58D4)5% ((((((((((((((((((((( (((((((((((((((((((((((( ((((((((((((((((((((((( ((((((((((((((((((((((( 658 658 658.58 658.54
SQL1

8%)

select ro#nd$658.584D)(6%) ro#nd$658.584D)(5%) ro#nd$658.584D)( ro#nd$658.584D)(4% rom d#al2

ROUND$658.584D)(6% ROUND$658.584D)(5% ROUND$658.584D)(8% ROUND$658.584D)(4% (((((((((((((((((((((((( ((((((((((((((((((((((((( (((((((((((((((((((((((( (((((((((((((((((((((((( 657 677 7 7
SQL1

select ro#nd$658)7%) ro#nd$658)6%) ro#nd$658)5% rom d#al2 ROUND$658)7% ROUND$658)6% ROUND$658)5% ((((((((((((((((( ((((((((((((((((( (((((((((((((((( 658 658 658

SQL1

select ro#nd$(658)7%) ro#nd$(658)6%) ro#nd$(658)5% rom d#al2 ROUND$(658)7% ROUND$(658)6% ROUND$(658)5% (((((((((((((((((( ((((((((((((((((( ((((((((((((((((((( (658 (658 (658

select ro#nd$658)(6%) ro#nd$658)(5%) ro#nd$658)(8%) ro#nd$(658)( 6%) ro#nd$(658)( 5%) ro#nd$(658)(8% rom d#al2
SQL1

ROUND$658)(6% ROUND$658)(5% ROUND$658)(8% ROUND$(658)(6% ROUND$(658)(5% ROUND$(658)(8% ((((((((((((( ((((((((((((( ((((((((((((( (((((((((((((( (((((((((((((( (((((((((((((( 657 677 7 (657 (677 7 SQL1 select ro#nd$n#ll)n#ll%) ro#nd$7)7%) ro#nd$6)6%) ro#nd$(6)(6%) ro#nd$(5)(5% rom d#al2 ROUND$NULL)NULL% ROUND$7)7% ROUND$6)6% ROUND$(6)(6% ROUND$(5)(5% ((((((((((((((((((((((( (((((((((((((( (((((((((((((( (((((((((((((((( (((((((((((((((( 7 6 7 7

Copy rights are reserved.

m%

22
TRUNC

This !ill tr#ncates or cho's o

di"its o 'recision rom a n#m*er.

Synta34 tr#nc $value, precision% +34


SQL1

d#al2

select tr#nc$658.584D%) tr#nc$658.584D)5%) tr#nc$658.58D4)5% rom TRUNC$658.584D% TRUNC$658.584D)5% TRUNC$658.58D4)5% ((((((((((((((((((((( ((((((((((((((((((((((( ((((((((((((((((((((((( 658 658.58 658.58

SQL1

select tr#nc$658.584D)(6%) tr#nc$658.584D)(5%) tr#nc$658.584D)(8%) tr#nc$658.584D)(4% rom d#al2

TRUNC$658.584D)(6% TRUNC$658.584D)(5% TRUNC$658.584D)(8% TRUNC$658.584D)(4% (((((((((((((((((((((((( (((((((((((((((((((((((( ((((((((((((((((((((((( (((((((((((((((((((((((( 657 677 7 7
SQL1

select tr#nc$658)7%) tr#nc$658)6%) tr#nc$658)5% rom d#al2 TRUNC$658)7% TRUNC$658)6% TRUNC$658)5% (((((((((((((((( (((((((((((((((( ((((((((((((((((( 658 658 658

SQL1

select tr#nc$(658)7%) tr#nc$(658)6%) tr#nc$(658)5% rom d#al2 TRUNC$(658)7% TRUNC$(658)6% TRUNC$(658)5% ((((((((((((((((( ((((((((((((((((( ((((((((((((((((( (658 (658 (658

SQL1 select tr#nc$658)(6%) tr#nc$658)(5%) tr#nc$658)(8%) tr#nc$(658)(6%) tr#nc$(658)5%) tr#nc$(658)(8% rom d#al2

TRUNC$658)(6% TRUNC$658)(5% TRUNC$658)(8% TRUNC$(658)(6% TRUNC$( 658)5% TRUNC$( 658)(8% ((((((((((((( ((((((((((((( ((((((((((((( (((((((((((((( ((((((((((((( (((((((((((((( 657 677 7 (657 (658 7
SQL1 select tr#nc$n#ll)n#ll%) tr#nc$7)7%) tr#nc$6)6%) tr#nc$(6)(6%) tr#nc$( 5)(5% rom d#al2

7 n%

TRUNC$NULL)NULL% TRUNC$7)7% TRUNC$6)6% TRUNC$(6)(6% TRUNC$(5)(5% ((((((((((((((((((((((( ((((((((((((( ((((((((((((( ((((((((((((((( (((((((((((((((( 7 6 7

-IT,ND

This !ill 'er orm *it!ise and o'eration. Synta34 *itand $value1, value2%

Copy rights are reserved.

23
SQL1 select *itand$5)8%) *itand$7)7%) *itand$6)6%) *itand$n#ll)n#ll%) *itand$(5)(8% rom d#al2 -IT,ND$5)8% -IT,ND$7)7% -IT,ND$6)6% -IT,ND$NULL)NULL% -IT,ND$(5)(8% (((((((((((((( ((((((((((((((( (((((((((((((( (((((((((((((((((((((((( ((((((((((((((((( 5 7 6 (4

+34

o%

:R+,T+ST

This !ill "ive the "reatest n#m*er. Synta34 "reatest $value1, value2, value3 valuen% +34
SQL1

select "reatest$6) 5) 8%) "reatest$(6) (5) (8% rom d#al2 :R+,T+ST$6)5)8% :R+,T+ST$(6)(5)(8% (((((((((((((((((((( ((((((((((((((((((((((( 8 (6

'%
L+,ST

I all the val#es are Ueros then it !ill dis'lay Uero. I all the 'arameters are n#lls then it !ill dis'lay nothin". I any o the 'arameters is n#ll it !ill dis'lay nothin".

This !ill "ive the least n#m*er. Synta34 least $value1, value2, value3 valuen% +34
SQL1

select least$6) 5) 8%) least$(6) (5) (8% rom d#al2

V%

L+,ST$6)5)8% L+,ST$(6)(5)(8% (((((((((((((((((((( ((((((((((((((((((((((( 6 (8 I all the val#es are Ueros then it !ill dis'lay Uero. I all the 'arameters are n#lls then it !ill dis'lay nothin". I any o the 'arameters is n#ll it !ill dis'lay nothin".

CO,L+SC+

This !ill ret#rn irst non(n#ll val#e. Synta34 coalesce $value1, value2, value3 valuen% +34
SQL1

select coalesce$6)5)8%) coalesce$n#ll)5)n#ll)D% rom d#al2 CO,L+SC+$6)5)8% CO,L+SC+$NULL)5)NULL)D% ((((((((((((((((((( ((((((((((((((((((((((((((((((( 6 5

STRIN: CUNCTIONS

Initca' U''er

Copy rights are reserved.

24
a%
INITC,B

Lo!er Len"th R'ad L'ad Ltrim Rtrim Trim Translate Re'lace So#nde3 Concat $ L YY L Concatenation o'erator% ,scii Chr S#*str Instr Decode :reatest Least Coalesce

This !ill ca'italiUe the initial letter o the strin". Synta34 initca' $strin % +34
SQL1

select initca'$?com'#ter?% rom d#al2 INITC,B ((((((((((( Com'#ter

*%

UBB+R

This !ill convert the strin" into #''ercase. Synta34 #''er $strin % +34
SQL1

select #''er$?com'#ter?% rom d#al2 UBB+R (((((((((((


CO&BUT+R

c%

LO9+R

This !ill convert the strin" into lo!ercase. Synta34 lo!er $strin % +34
SQL1

select lo!er$?CO&BUT+R?% rom d#al2

LO9+R ((((((((((( com'#ter d%


L+N:T<

Copy rights are reserved.

25
This !ill "ive len"th o the strin". Synta34 len"th $strin % +34
SQL1

select len"th$?com'#ter?% rom d#al2 L+N:T< ((((((((((( X

e%

RB,D

This !ill allo!s yo# to 'ad the ri"ht side o a col#mn !ith any set o characters. Synta34 r'ad $strin , len t' (, paddin _c'ar)% +34
SQL1

select r'ad$?com'#ter?)6D)?F?%) r'ad$?com'#ter?)6D)?FZ?% rom d#al2 RB,D$?CO&BUT+R? RB,D$?CO&BUT+R? (((((((((((((((((((((( (((((((((((((((((((((( com'#terFFFFFFF com'#terFZFZFZF

(( De a#lt 'addin" character !as *lank s'ace. %


LB,D

This !ill allo!s yo# to 'ad the le t side o a col#mn !ith any set o characters. Synta34 l'ad $strin , len t' (, paddin _c'ar)% +34
SQL1

select l'ad$?com'#ter?)6D)?F?%) l'ad$?com'#ter?)6D)?FZ?% rom d#al2 LB,D$?CO&BUT+R? LB,D$?CO&BUT+R? ((((((((((((((((((((( ((((((((((((((((((((( FFFFFFFcom'#ter FZFZFZFcom'#ter

(( De a#lt 'addin" character !as *lank s'ace. "%


LTRI&

This !ill trim o

#n!anted characters rom the le t end o strin". (,unwanted_c'ars)%

Synta34 ltrim $strin +34


SQL1

select ltrim$?com'#ter?)?co?%) ltrim$?com'#ter?)?com?% rom d#al2 LTRI&$ LTRI& (((((((( ((((((((( m'#ter '#ter

SQL1

d#al2

select ltrim$?com'#ter?)?'#ter?%) ltrim$?com'#ter?)?om'#ter?% rom LTRI&$?C LTRI&$?C (((((((((( (((((((((( com'#ter com'#ter

Copy rights are reserved.

26
strin". h% (( I yo# haven=t s'eci y any #n!anted characters it !ill dis'lay entire

RTRI&

This !ill trim o

#n!anted characters rom the ri"ht end o strin". (, unwanted_c'ars)%

Synta34 rtrim $strin +34


SQL1

select rtrim$?com'#ter?)?er?%) rtrim$?com'#ter?)?ter?% rom d#al2 RTRI&$ RTRI& (((((((( ((((((((( com'#t com'# select rtrim$?com'#ter?)?com'#t=%) rtrim$?com'#ter?)?com'#te?% rom

SQL1

d#al2 RTRI&$?C RTRI&$?C (((((((((( (((((((((( com'#ter com'#ter (( I yo# haven=t s'eci y any #n!anted characters it !ill dis'lay entire strin". i%
TRI&

This !ill trim o

#n!anted characters rom the *oth sides o strin".

Synta34 trim $unwanted_c'ars rom strin % +34


SQL1

select trim$ ?i? rom ?indiani?% rom d#al2 TRI&$ ((((( ndian

SQL1

select trim$ leadin"?i? rom ?indiani?% rom d#al2 TRI&$L (((((( ndiani select trim$ trailin"?i? rom ?indiani?% rom d#al2 TRI&$T (((((( Indian

(( this !ill !ork

as

LTRI&

SQL1

(( this !ill !ork

as

RTRI&

K%

TR,NSL,T+

This !ill re'lace the set o characters) character *y character. Synta34 translate $strin , old_c'ars, new_c'ars% +34 SQL1 select translate$?india?)?in?)?3y?% rom d#al2

Copy rights are reserved.

TR,NS (((((((( 3yd3a k%


R+BL,C+

27

This !ill re'lace the set o characters) strin" *y strin". Synta34 re'lace $strin , old_c'ars (, new_c'ars)% +34 SQL1 select re'lace$?india?)?in?)?3y?%) re'lace$Lindia=)=in=% rom d#al2 R+BL,C+ R+BL,C+ ((((((((((( ((((((((((( /ydia dia

l%

SOUND+/

This !ill *e #sed to ind !ords that so#nd like other !ords) e3cl#sively #sed in !here cla#se. Synta34 so#nde3 $strin % +34
SQL1

select F rom em' !here so#nde3$ename% I so#nde3$?S&IT?%2 @O((((( CL+RG &:R <IR+D,T+ ((((( (((((((((((( WT75 6W(D+C(X7 S,L D+BTNO ((((((((( (((((((((( D77 57

+&BNO +N,&+ (((((((( (((((((( W8ET S&IT< m%


CONC,T

This !ill *e #sed to com*ine t!o strin"s only. Synta34 concat $strin 1, strin 2% +34
SQL1

select concat$?com'#ter?)? o'erator?% rom d#al2 CONC,T$?CO&BUT+R? ((((((((((((((((((((((((( com'#ter o'erator

I yo# !ant to com*ine more than t!o strin"s yo# have to #se concatenation o'erator $YY%.
SQL1

select ?ho!? YY ? are? YY ? yo#? rom d#al2 ?<O9?YY?,R+ ((((((((((((((( ho! are yo#

n%

,SCII

This !ill ret#rn the decimal re'resentation in the data*ase character set o the irst character o the strin". Synta34 ascii $strin % +34
SQL1

select ascii$?a?%) ascii$?a''le?% rom d#al2 ,SCII$?,?% ,SCII$?,BBL+?%

Copy rights are reserved.

(((((((((((( (((((((((((((((((( TW TW o%
C<R

28

This !ill ret#rn the character havin" the *inary eV#ivalent to the strin" in either the data*ase character set or the national character set. Synta34 chr $number% +34
SQL1

select chr$TW% rom d#al2 C<R ((((( a

'%

SU-STR

This !ill *e #sed to e3tract s#*strin"s. Synta34 s#*str $strin , start_c'r_count (, no_o%_c'ars)% +34
SQL1 select s#*str$?com'#ter?)5%) s#*str$?com'#ter?)5)D%) s#*str$?com'#ter?)8)W% rom d#al2

SU-STR$ SU-ST SU-STR (((((((((( ((((((( (((((((( om'#ter om'#t m'#ter I no_o%_c'ars 'arameter is ne"ative then it !ill dis'lay nothin". I *oth 'arameters e3ce't strin are n#ll or Ueros then it !ill dis'lay nothin". I no_o%_c'ars 'arameter is "reater than the len"th o the strin" then it i"nores and calc#lates *ased on the or"inal strin" len"th. I start_c'r_count is ne"ative then it !ill e3tract the s#*strin" rom ri"ht end. 6 C (X 5 O (W 8 & (E 4 B (D D U (4 E T (8 W + (5 X R (6

V%

INSTR

This !ill allo!s yo# or searchin" thro#"h a strin" or set o characters. Synta34 instr $strin , searc'_str (, start_c'r_count (, occurrence) )% +34 SQL1 select instr$?in ormation?)?o?)4)6%) instr$?in ormation?)?o?)4)5% rom d#al2 INSTR$?INCOR&,TION?)?O?)4)6% INSTR$?INCOR&,TION?)?O?)4)5% (((((((((((((((((((((((((((((((((((( ((((((((((((((((((((((((((((((((((((( 4 67

Copy rights are reserved.

29
I yo# are not s'eci yin" start_c'r_count and occurrence then it !ill start search rom the *e"innin" and inds irst occ#rrence only. I *oth 'arameters start_c'r_count and occurrence are n#ll) it !ill dis'lay nothin".

r%

D+COD+

Decode !ill act as val#e *y val#e s#*stit#tion. Cor every val#e o ield) it !ill checks or a match in a series o i Athen tests. Synta34 decode $value, i%1, t'en1, i%2, t'en2, . else%2 +34
SQL1

select sal) decode$sal)D77)?Lo!?)D777)?<i"h?)?&edi#m?% rom em'2 S,L ((((( D77 5D77 5777 8D77 8777 D777 4777 D777 6X77 6577 5777 5W77 5577 8577 D+COD+ ((((((((( Lo! &edi#m &edi#m &edi#m &edi#m <i"h &edi#m <i"h &edi#m &edi#m &edi#m &edi#m &edi#m &edi#m

SQL1

select decode$6)6)8%) decode$6)5)8)4)4)E% rom d#al2 D+COD+$6)6)8% D+COD+$6)5)8)4)4)E% ((((((((((((((((( (((((((((((((((((((((((( 8 E

s%

I the n#m*er o 'arameters are odd and di erent then decode !ill dis'lay nothin". I the n#m*er o 'arameters are even and di erent then decode !ill dis'lay last val#e. I all the 'arameters are n#ll then decode !ill dis'lay nothin". I all the 'arameters are Ueros then decode !ill dis'lay Uero.

:R+,T+ST

This !ill "ive the "reatest strin". Synta34 "reatest $strn 1, strin 2, strin 3 strin n% +34
SQL1

d#al2

select "reatest$?a?) ?*?) ?c?%) "reatest$?satish?)?srin#?)?saketh?% rom

:R+,T :R+,T ((((((( (((((((

Copy rights are reserved.

c t%
L+,ST

srin#

30

I all the 'arameters are n#lls then it !ill dis'lay nothin". I any o the 'arameters is n#ll it !ill dis'lay nothin".

This !ill "ive the least strin". Synta34 "reatest $strn 1, strin 2, strin 3 strin n% +34
SQL1

select least$?a?) ?*?) ?c?%) least$?satish?)?srin#?)?saketh?% rom d#al2 L+,ST L+,ST ((((((( ((((((( a saketh

#%

I all the 'arameters are n#lls then it !ill dis'lay nothin". I any o the 'arameters is n#ll it !ill dis'lay nothin".

CO,L+SC+

This !ill "ives the irst non(n#ll strin". Synta34 coalesce $strn 1, strin 2, strin 3 strin n% +34
SQL1

select coalesce$?a?)?*?)?c?%) coalesce$n#ll)?a?)n#ll)?*?% rom d#al2 CO,L+SC+ CO,L+SC+ ((((((((((( ((((((((((( a a

D,T+ CUNCTIONS

Sysdate C#rrentNdate C#rrentNtimestam' Systimestam' Localtimestam' D*timeUone SessiontimeUone ToNchar ToNdate ,ddNmonths &onthsN*et!een Ne3tNday LastNday +3tract :reatest Least Ro#nd Tr#nc Ne!Ntime Coalesce

Oracle de a#lt date ormat is DD(&ON(... 9e can chan"e the de a#lt ormat to o#r desired ormat *y #sin" the ollo!in" command.

Copy rights are reserved.

SQL1

alter session set nlsNdateN ormat I LDD(&ONT<(....=2 -#t this !ill e3'ire once the session !as closed.

31

a%

S.SD,T+

This !ill "ive the c#rrent date and time. +34 SQL1 select sysdate rom d#al2 S.SD,T+ ((((((((((( 54(D+C(7E *%
CURR+NTND,T+

This !ill ret#rns the c#rrent date in the session=s timeUone. +34
SQL1

select c#rrentNdate rom d#al2 CURR+NTND,T+ (((((((((((((((((( 54(D+C(7E

c%

CURR+NTNTI&+ST,&B

This !ill ret#rns the c#rrent timestam' !ith the active time Uone in ormation. +34
SQL1

select c#rrentNtimestam' rom d#al2 CURR+NTNTI&+ST,&B ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( 54(D+C(7E 78.45.46.8X88ET ,& H7D487

d%

S.STI&+ST,&B

This !ill ret#rns the system date) incl#din" ractional seconds and time Uone o the data*ase. +34
SQL1

select systimestam' rom d#al2 S.STI&+ST,&B ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( 54(D+C(7E 78.4T.86.X877TT ,& H7D487

e%

LOC,LTI&+ST,&B

This !ill ret#rns local timestam' in the active time Uone in ormation) !ith no time Uone in ormation sho!n. +34
SQL1

select localtimestam' rom d#al2 LOC,LTI&+ST,&B ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( 54(D+C(7E 78.44.6X.D75XW4 ,&

Copy rights are reserved.

32
D-TI&+[ON+

This !ill ret#rns the c#rrent data*ase time Uone in UTC ormat. $Coordinated Universal Time% +34
SQL1

select d*timeUone rom d#al2 D-TI&+[ON+ ((((((((((((((( (7W477

"%

S+SSIONTI&+[ON+

This !ill ret#rns the val#e o the c#rrent session=s time Uone. +34
SQL1

select sessiontimeUone rom d#al2 S+SSIONTI&+[ON+ ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( H7D487

h%

TONC<,R

This !ill *e #sed to e3tract vario#s date ormats. The availa*le date ormats as ollo!s. Synta34 toNchar $date) %ormat%
D,T+ COR&,TS D DD DDD && &ON &ONT< R& D. D,. . .. ... .... S.... I I. I.. I... .) ... .+,R CC Q 9 99 I9 << &I SS

((

(( No o days in !eek (( No o days in month (( No o days in year (( No o month (( Three letter a**reviation o month (( C#lly s'elled o#t month (( Roman n#meral month (( Three letter a**reviated day (( C#lly s'elled o#t day (( Last one di"it o the year (( Last t!o di"its o the year (( Last three di"its o the year (( C#ll o#r di"it year Si"ned year (( One di"it year rom ISO standard (( T!o di"it year rom ISO standard (( Three di"it year rom ISO standard (( Co#r di"it year rom ISO standard (( .ear !ith comma (( C#lly s'elled o#t year (( Cent#ry (( No o V#arters (( No o !eeks in month (( No o !eeks in year (( No o !eeks in year rom ISO standard (( <o#rs (( &in#tes (( Seconds

Copy rights are reserved.

CC

or B& or B.& ,D or -C ,.D or -.C


,& ,.& C&

or day
T< SB SBT< T<SB

(( (( (( (( (( (( (( (( (( ((

Cractional seconds Dis'lays ,& or B& de'endin" #'on time o day Dis'lays ,.& or B.& de'endin" #'on time o day Dis'lays ,D or -C de'endin" #'on the date Dis'lays ,D or -C de'endin" #'on the date Bre i3 to month or day) s#''resses 'addin" o month

33

S# i3 to a n#m*er s# i3 to a n#m*er to *e s'elled o#t S# i3 com*ination o T< and SB to *e *oth s'elled o#t same as SBT<

+34
SQL1

select toNchar$sysdate)?dd month yyyy hh4mi4ss am dy?% rom d#al2 TONC<,R$S.SD,T+)?DD &ONT< ....<<4&I (((((((((((((((((((((((((((((((((((((((((((((((((((( 54 decem*er 577E 75478458 'm s#n

SQL1

select toNchar$sysdate)?dd month year?% rom d#al2 TONC<,R$S.SD,T+)?DD&ONT<.+,R?% ((((((((((((((((((((((((((((((((((((((((((((((((((((((( 54 decem*er t!o tho#sand si3

SQL1

select toNchar$sysdate)?dd mmonth year?% rom d#al2 TONC<,R$S.SD,T+)?DD C&&ONT< .+,R?% ((((((((((((((((((((((((((((((((((((((((((((((((((((((( 54 decem*er t!o tho#sand si3

SQL1

select toNchar$sysdate)?ddth DDT<?% rom d#al2 TONC<,R$S (((((((((((( 54th 54T< select toNchar$sysdate)?dds'th DDSBT<?% rom d#al2 TONC<,R$S.SD,T+)?DDSBT<DDSBT< (((((((((((((((((((((((((((((((((((((((((( t!enty( o#rth T9+NT.(COURT<

SQL1

SQL1

select toNchar$sysdate)?dds' Dds' DDSB ?% rom d#al2 TONC<,R$S.SD,T+)?DDSBDDSBDDSB?% (((((((((((((((((((((((((((((((((((((((((((((((( t!enty( o#r T!enty(Co#r T9+NT.(COUR

i%

TOND,T+

This !ill *e #sed to convert the strin" into data ormat. Synta34 toNdate $date%
SQL1 select toNchar$toNdate$?54AdecA577E?)?ddAmonAyyyy?%) ?dd F month F day?% rom d#al2

+34

TONC<,R$TOND,T+$?54AD+CA57

Copy rights are reserved.

(((((((((((((((((((((((((( 54 F decem*er F S#nday (( I yo# are not #sin" toNchar oracle !ill dis'lay o#t'#t in de a#lt date ormat. K%
,DDN&ONT<S

34

This !ill add the s'eci ied months to the "iven date. Synta34 addNmonths $date, no_o%_mont's% +34
SQL1

d#al2

select addNmonths$toNdate$?66(Kan(6TT7?)?dd(mon(yyyy?%) D% rom ,DDN&ONT<S (((((((((((((((( 66(@UN(T7

SQL1

select addNmonths$toNdate$?66(Kan(6TT7?)?dd(mon(yyyy?%) (D% rom ,DDN&ONT< ((((((((((((((( 66(,U:(XT

d#al2

k%

I no_o%_mont's is Uero then it !ill dis'lay the same date. I no_o%_mont's is n#ll then it !ill dis'lay nothin".

&ONT<SN-+T9++N

This !ill "ive di erence o months *et!een t!o dates. Synta34 monthsN*et!een $date1, date2% +34
SQL1 select monthsN*et!een$toNdate$?66(a#"(6TT7?)?dd(mon(yyyy?%) toNdate$?66(Kan( 6TT7?)?dd(mon(yyyy?%% rom d#al2

&ONT<SN-+T9++N$TOND,T+$?66(,U:(6TT7?)?DD(&ON( ....?%)TOND,T+$?66(@,N(6TT7?)?DD(&ON(....?%% (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( ((((((((( W SQL1 select monthsN*et!een$toNdate$?66(Kan(6TT7?)?dd(mon(yyyy?%) toNdate$?66(a#"( 6TT7?)?dd(mon(yyyy?%% rom d#al2 &ONT<SN-+T9++N$TOND,T+$?66(@,N(6TT7?)?DD(&ON( ....?%)TOND,T+$?66(,U:(6TT7?)?DD(&ON(....?%% (((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( ((((((((((( (W l%
N+/TND,.

This !ill 'rod#ce ne3t day o the "iven day rom the s'eci ied date. Synta34 ne3tNday $date, day%

Copy rights are reserved.

35
+34
SQL1

select ne3tNday$toNdate$?54(dec(577E?)?dd(mon(yyyy?%)?s#n?% rom N+/TND,.$ ((((((((((((( 86(D+C(7E

d#al2

(( I the day 'arameter is n#ll then it !ill dis'lay nothin". m%


L,STND,.

This !ill 'rod#ce last day o the "iven date. Synta34 lastNday $date% +34
SQL1

select lastNday$toNdate$?54(dec(577E?)?dd(mon(yyyy?%)?s#n?% rom L,STND,.$ ((((((((((((( 86(D+C(7E

d#al2

n%

+/TR,CT

This is #sed to e3tract a 'ortion o the date val#e. Synta34 e3tract $$year Y month Y day Y ho#r Y min#te Y second%) date% +34 SQL1 select e3tract$year rom sysdate% rom d#al2 +/TR,CT$.+,RCRO&S.SD,T+% (((((((((((((((((((((((((((((((((((( 577E (( .o# can e3tract only one val#e at a time. o%
:R+,T+ST

This !ill "ive the "reatest date. Synta34 "reatest $date1, date2, date3 daten%
SQL1 select "reatest$toNdate$?66(Kan(T7?)?dd(mon(yy?%)toNdate$?66(mar( T7?)?dd(mon( yy?%)toNdate$?66(a'r(T7?)?dd(mon(yy?%% rom d#al2

+34

:R+,T+ST$ ((((((((((((( 66(,BR(T7 '%


L+,ST

This !ill "ive the least date. Synta34 least $date1, date2, date3 daten% +34
SQL1 select least$toNdate$?66(Kan(T7?)?dd(mon(yy?%)toNdate$?66(mar( T7?)?dd(mon(

Copy rights are reserved.

yy?%)toNdate$?66(a'r(T7?)?dd(mon(yy?%% rom d#al2 L+,ST$ ((((((((((((( 66(@,N(T7 V%


ROUND

36

Ro#nd !ill ro#nds the date to !hich it !as eV#al to or "reater than the "iven date. Synta34 ro#nd $date, (day Y month Y year*% I the second 'arameter !as year then ro#nd !ill checks the month o the "iven date in the ollo!in" ran"es.
@,N @UL (( (( @UN D+C

I the month alls *et!een @,N and @UN then it ret#rns the irst day o the c#rrent year. I the month alls *et!een @UL and D+C then it ret#rns the irst day o the ne3t year. I the second 'arameter !as mont' then ro#nd !ill checks the day o the "iven date in the ollo!in" ran"es. 6 6E (( (( 6D 86

I the day alls *et!een 6 and 6D then it ret#rns the irst day o the c#rrent month. I the day alls *et!een 6E and 86 then it ret#rns the irst day o the ne3t month. I the second 'arameter !as day then ro#nd !ill checks the !eek day o the "iven date in the ollo!in" ran"es.
SUN T<U (( (( 9+D SUN

I the !eek day alls *et!een SUN and 9+D then it ret#rns the 'revio#s s#nday. I the !eekday alls *et!een T<U and SUN then it ret#rns the ne3t s#nday. I the second 'arameter !as n#ll then it ret#rns nothin". I the yo# are not s'eci yin" the second 'arameter then ro#nd !ill resets the time to the *e"inin" o the c#rrent day in case o #ser s'eci ied date. I the yo# are not s'eci yin" the second 'arameter then ro#nd !ill resets the time to the *e"inin" o the ne3t day in case o sysdate.
SQL1 select ro#nd$toNdate$?54(dec(74?)?dd(mon(yy?%)?year?%) ro#nd$toNdate$?66(mar( 7E?)?dd(mon(yy?%)?year?% rom d#al2

+34

ROUND$TON ROUND$TON

Copy rights are reserved.

(((((((((((( ((((((((((((((( 76(@,N(7D 76(@,N(7E SQL1 select ro#nd$toNdate$?66(Kan(74?)?dd(mon(yy?%)?month?%) ro#nd$toNdate$?6X(Kan( 74?)?dd(mon(yy?%)?month?% rom d#al2 ROUND$TON ROUND$TON ((((((((((((( ((((((((((((((( 76(@,N(74 76(C+-(74
SQL1 select ro#nd$toNdate$?5E(dec(7E?)?dd(mon(yy?%)?day?%) ro#nd$toNdate$?5T(dec( 7E?)?dd(mon(yy?%)?day?% rom d#al2

37

ROUND$TON ROUND$TON (((((((((((((( (((((((((((((( 54(D+C(7E 86(D+C(7E


SQL1 select toNchar$ro#nd$toNdate$?54(dec(7E?)?dd(mon(yy?%%) ?dd mon yyyy hh4mi4ss am?% rom d#al2

TONC<,R$ROUND$TOND,T+$? ((((((((((((((((((((((((((((((((( 54 dec 577E 65477477 am r%


TRUNC

Tr#nc !ill cho's o date.

the date to !hich it !as eV#al to or less than the "iven

Synta34 tr#nc $date, (day Y month Y year*% I the second 'arameter !as year then it al!ays ret#rns the irst day o the c#rrent year. I the second 'arameter !as mont' then it al!ays ret#rns the irst day o the c#rrent month. I the second 'arameter !as day then it al!ays ret#rns the 'revio#s s#nday. I the second 'arameter !as n#ll then it ret#rns nothin". I the yo# are not s'eci yin" the second 'arameter then tr#nk !ill resets the time to the *e"inin" o the c#rrent day.

+34
SQL1 select tr#nc$toNdate$?54(dec(74?)?dd(mon(yy?%)?year?%) tr#nc$toNdate$?66(mar( 7E?)?dd(mon(yy?%)?year?% rom d#al2

TRUNC$TON TRUNC$TON ((((((((((((( (((((((((((((( 76(@,N(74 76(@,N(7E


SQL1 select tr#nc$toNdate$?66(Kan(74?)?dd(mon(yy?%)?month?%) tr#nc$toNdate$?6X(Kan( 74?)?dd(mon(yy?%)?month?% rom d#al2

TRUNC$TON TRUNC$TON ((((((((((((( ((((((((((((( 76(@,N(74 76(@,N(74

Copy rights are reserved.

select tr#nc$toNdate$?5E(dec(7E?)?dd(mon(yy?%)?day?%) tr#nc$toNdate$?5T(dec(7E?)?dd( mon(yy?%)?day?% rom d#al2


SQL1

38

TRUNC$TON TRUNC$TON ((((((((((((( (((((((((((((( 54(D+C(7E 54(D+C(7E


SQL1 select toNchar$tr#nc$toNdate$?54(dec(7E?)?dd(mon(yy?%%) ?dd mon yyyy hh4mi4ss am?% rom d#al2

TONC<,R$TRUNC$TOND,T+$? ((((((((((((((((((((((((((((((((( 54 dec 577E 65477477 am s%


N+9NTI&+

This !ill "ive the desired timeUone=s date and time. Synta34 ne!Ntime $date, current_time+one, desired_time+one% ,vaila*le timeUones are as ollo!s.
TI&+[ON+S ,STA,DT -STA-DT CSTACDT +STA+DT :&T <STA<DT &STA&DT NST BSTABDT .STA.DT

(( (( (( (( (( (( (( (( (( ((

,tlantic standardAday li"ht time -erin" standardAday li"ht time Central standardAday li"ht time +astern standardAday li"ht time :reen!ich mean time ,laska(<a!aii standardAday li"ht time &o#ntain standardAday li"ht time Ne! o#ndland standard time Baci ic standardAday li"ht time .#kon standardAday li"ht time

SQL1 select toNchar$ne!Ntime$sysdate)?"mt?)?yst?%)?dd mon yyyy hh4mi4ss am?% rom d#al2

+34

TONC<,R$N+9NTI&+$S.SD,T ((((((((((((((((((((((((((((((((((( 54 dec 577E 754D6457 'm


SQL1 select toNchar$ne!Ntime$sysdate)?"mt?)?est?%)?dd mon yyyy hh4mi4ss am?% rom d#al2 TONC<,R$N+9NTI&+$S.SD,T ((((((((((((((((((((((( 54 dec 577E 7E4D645E 'm

t%

CO,L+SC+

This !ill "ive the irst non(n#ll date. Synta34 coalesce $date1, date2, date3 daten%
SQL1 select coalesce$?65(Kan(T7?)?68(Kan(TT?%) coalesce$n#ll)?65(Kan( T7?)?58(mar(TX?)n#ll% rom d#al2

+34

Copy rights are reserved.

39
CO,L+SC+$ CO,L+SC+$ ((((((((((((( (((((((((((( 65(Kan(T7 65(Kan(T7

&ISC+LL,N+OUS CUNCTIONS

a%
UID

Uid User ;siUe Rank DenseNrank

This !ill ret#rns the inte"er val#e corres'ondin" to the #ser c#rrently lo""ed in. +34
SQL1

select #id rom d#al2 UID (((((((((( 86T

*%

US+R

This !ill ret#rns the lo"in=s #ser name. +34


SQL1

select #ser rom d#al2 US+R (((((((((((((((( S,G+T<

c%

;SI[+

This !ill ret#rns the n#m*er o *ytes in the e3'ression. +34


SQL1

select vsiUe$658%) vsiUe$?com'#ter?%) vsiUe$?65(Kan(T7?% rom d#al2 ;SI[+$658% ;SI[+$?CO&BUT+R?% ;SI[+$?65(@,N(T7?% ((((((((((((( ((((((((((((((((((((((( (((((((((((((((((((((( 8 X T

d%

R,NG

This !ill "ive the non(seV#ential rankin". +34


SQL1

select ro!n#m)sal rom $select sal rom em' order *y sal desc%2 RO9NU& S,L (((((((((( (((((((((( 6 D777 5 8777 8 8777

Copy rights are reserved.

4 D E W X T 67 66 65 68 64 6D
SQL1

5TWD 5XD7 54D7 6E77 6D77 6877 65D7 65D7 6677 6777 TD7 X77

40

select rank$5TWD% !ithin "ro#'$order *y sal desc% rom em'2 R,NG$5TWD%9IT<IN:ROUB$ORD+R-.S,LD+SC% ((((((((((((((((((((((((((((((((((((((((((((((((((((((((( 4

d%

D+NS+NR,NG

This !ill "ive the seV#ential rankin". +34


SQL1

select denseNrank$5TWD% !ithin "ro#'$order *y sal desc% rom em'2 D+NS+NR,NG$5TWD%9IT<IN:ROUB$ORD+R-.S,LD+SC% ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( 8

CON;+RSION CUNCTIONS

a%
-INNTONNU&

-inNtoNn#m Chartoro!id Ro!idtochar ToNn#m*er ToNchar ToNdate

This !ill convert the *inary val#e to its n#merical eV#ivalent. Synta34 *inNtoNn#m$ binary_bits% +34
SQL1

select *inNtoNn#m$6)6)7% rom d#al2

*%

-INNTONNU&$6)6)7% (((((((((((((((((((((((( E I all the *its are Uero then it 'rod#ces Uero. I all the *its are n#ll then it 'rod#ces an error.

C<,RTORO9ID

This !ill convert a character strin" to act like an internal oracle ro! identi ier or ro!id. c%
RO9IDTOC<,R

This !ill convert an internal oracle ro! identi ier or ro!id to character strin".

Copy rights are reserved.

d%

41
TONNU&-+R

This !ill convert a char or varchar to n#m*er. e%


TONC<,R

This !ill convert a n#m*er or date to character strin". %


TOND,T+

This !ill convert a n#m*er) char or varchar to a date. :ROUB CUNCTIONS S#m ,v" &a3 &in Co#nt

:ro#' #nctions !ill *e a''lied on all the ro!s *#t 'rod#ces sin"le o#t'#t. a%
SU&

This !ill "ive the s#m o the val#es o the s'eci ied col#mn. Synta34 s#m $column% +34
SQL1

select s#m$sal% rom em'2 SU&$S,L% (((((((((( 8XE77

*%

,;:

This !ill "ive the avera"e o the val#es o the s'eci ied col#mn. Synta34 av" $column% +34
SQL1

select av"$sal% rom em'2 ,;:$S,L% ((((((((((((((( 5WDW.645XE

c%

&,/

This !ill "ive the ma3im#m o the val#es o the s'eci ied col#mn. Synta34 ma3 $column% +34
SQL1

select ma3$sal% rom em'2 &,/$S,L% (((((((((( D777

Copy rights are reserved.

d%

42
&IN

This !ill "ive the minim#m o the val#es o the s'eci ied col#mn. Synta34 min $column% +34 SQL1 select min$sal% rom em'2 &IN$S,L% (((((((((( D77
COUNT

e%

This !ill "ive the co#nt o the val#es o the s'eci ied col#mn. Synta34 co#nt $column% +34
SQL1

select co#nt$sal%)co#nt$F% rom em'2 COUNT$S,L% COUNT$F% (((((((((((((( (((((((((((( 64 64

CONSTR,INTS
Constraints are cate"oriUed as ollo!s. Domain inte"rity constraints Not n#ll Check +ntity inte"rity constraints UniV#e Brimary key Re erential inte"rity constraints Corei"n key Constraints are al!ays attached to a col#mn not a ta*le. 9e can add constraints in three !ays. Col#mn level Ta*le level ,lter level (( alon" !ith the col#mn de inition (( a ter the ta*le de inition (( #sin" alter command

9hile addin" constraints yo# need not s'eci y the name *#t the ty'e only) oracle !ill internally name the constraint. I yo# !ant to "ive a name to the constraint) yo# have to #se the constraint cla#se. NOT NULL This is #sed to avoid n#ll val#es. 9e can add this constraint in col#mn level only. +34
SQL1 create ta*le st#dent$no n#m*er$5% not n#ll) name varchar$67%) marks n#m*er$8%%2 SQL1 create ta*le st#dent$no n#m*er$5% constraint nn not n#ll) name varchar$67%) marks n#m*er$8%%2

Copy rights are reserved.

C<+CG This is #sed to insert the val#es *ased on s'eci ied condition. 9e can add this constraint in all three levels. +34
COLU&N L+;+L

43

SQL1 create ta*le st#dent$no n#m*er$5% ) name varchar$67%) marks n#m*er$8% check $marks 1 877%%2 SQL1 create ta*le st#dent$no n#m*er$5% ) name varchar$67%) marks n#m*er$8% constraint ch check$marks 1 877%%2 T,-L+ L+;+L SQL1 create ta*le st#dent$no n#m*er$5% ) name varchar$67%) marks n#m*er$8%) check $marks 1 877%%2 SQL1 create ta*le st#dent$no n#m*er$5% ) name varchar$67%) marks n#m*er$8%) constraint ch check$marks 1 877%%2 ,LT+R L+;+L SQL1 SQL1

alter ta*le st#dent add check$marks1877%2 alter ta*le st#dent add constraint ch check$marks1877%2

UNIQU+ This is #sed to avoid d#'licates *#t it allo! n#lls. 9e can add this constraint in all three levels. +34
COLU&N L+;+L

SQL1 create ta*le st#dent$no n#m*er$5% #niV#e) name varchar$67%) marks n#m*er$8%%2 SQL1 create ta*le st#dent$no n#m*er$5% constraint #n #niV#e) name varchar$67%) marks n#m*er$8%%2 T,-L+ L+;+L SQL1 create ta*le st#dent$no n#m*er$5% ) name varchar$67%) marks n#m*er$8%) #niV#e$no%%2 SQL1 create ta*le st#dent$no n#m*er$5% ) name varchar$67%) marks n#m*er$8%) constraint #n #niV#e$no%%2 ,LT+R L+;+L SQL1 SQL1

alter ta*le st#dent add #niV#e$no%2 alter ta*le st#dent add constraint #n #niV#e$no%2

BRI&,R. G+. This is #sed to avoid d#'licates and n#lls. This !ill !ork as com*ination o #niV#e and not n#ll. Brimary key al!ays attached to the 'arent ta*le.

Copy rights are reserved.

9e can add this constraint in all three levels. +34


COLU&N L+;+L SQL1 create ta*le st#dent$no n#m*er$5% 'rimary key) name varchar$67%) marks n#m*er$8%%2 SQL1 create ta*le st#dent$no n#m*er$5% constraint 'k 'rimary key) name varchar$67%) marks n#m*er$8%%2 T,-L+ L+;+L SQL1 create ta*le st#dent$no n#m*er$5% ) name varchar$67%) marks n#m*er$8%) 'rimary key$no%%2 SQL1 create ta*le st#dent$no n#m*er$5% ) name varchar$67%) marks n#m*er$8%) constraint 'k 'rimary key$no%%2 ,LT+R L+;+L SQL1 SQL1

44

alter ta*le st#dent add 'rimary key$no%2 alter ta*le st#dent add constraint 'k 'rimary key$no%2

COR+I:N G+. This is #sed to re erence the 'arent ta*le 'rimary key col#mn !hich allo!s d#'licates. Corei"n key al!ays attached to the child ta*le. 9e can add this constraint in ta*le and alter levels only. +34
T,-L+ L+;+L

SQL1 create ta*le em'$em'no n#m*er$5%) ename varchar$67%) de'tno n#m*er$5%) 'rimary key$em'no%) orei"n key$de'tno% re erences de't$de'tno%%2 SQL1 create ta*le em'$em'no n#m*er$5%) ename varchar$67%) de'tno n#m*er$5%) constraint 'k 'rimary key$em'no%) constraint k orei"n key$de'tno% re erences de't$de'tno%%2 ,LT+R L+;+L

alter ta*le em' add orei"n key$de'tno% re erences de't$de'tno%2 alter ta*le em' add constraint k orei"n key$de'tno% re erences de't$de'tno%2
SQL1 SQL1

Once the 'rimary key and orei"n key relationshi' has *een created then yo# can not remove any 'arent record i the de'endent childs e3ists. USIN: ON D+LT+ C,SC,D+ -y #sin" this cla#se yo# can remove the 'arent record even it childs e3ists. -eca#se !hen ever yo# remove 'arent record oracle a#tomatically removes all its de'endent records rom child ta*le) i this cla#se is 'resent !hile creatin" orei"n key constraint. +34
T,-L+ L+;+L

Copy rights are reserved.

45
SQL1 create ta*le em'$em'no n#m*er$5%) ename varchar$67%) de'tno n#m*er$5%) 'rimary key$em'no%) orei"n key$de'tno% re erences de't$de'tno% on delete cascade%2 SQL1 create ta*le em'$em'no n#m*er$5%) ename varchar$67%) de'tno n#m*er$5%) constraint 'k 'rimary key$em'no%) constraint k orei"n key$de'tno% re erences de't$de'tno% on delete cascade%2 ,LT+R L+;+L SQL1 alter ta*le em' add orei"n key$de'tno% re erences de't$de'tno% on delete cascade2 SQL1 alter ta*le em' add constraint k orei"n key$de'tno% re erences de't$de'tno% on delete cascade2

CO&BOSIT+ G+.S , com'osite key can *e de ined on a com*ination o col#mns. 9e can de ine com'osite keys on entity inte"rity and re erential inte"rity constraints. Com'osite key can *e de ined in ta*le and alter levels only. +34
UNIQU+ $T,-L+ L+;+L%

SQL1 create ta*le st#dent$no n#m*er$5% ) name varchar$67%) marks n#m*er$8%) #niV#e$no)name%%2 SQL1 create ta*le st#dent$no n#m*er$5% ) name varchar$67%) marks n#m*er$8%) constraint #n #niV#e$no)name%%2 UNIQU+ $,LT+R L+;+L% SQL1 SQL1

alter ta*le st#dent add #niV#e$no)name%2 alter ta*le st#dent add constraint #n #niV#e$no)name%2

BRI&,R. G+. $T,-L+ L+;+L% SQL1 create ta*le st#dent$no n#m*er$5% ) name varchar$67%) marks n#m*er$8%) 'rimary key$no)name%%2 SQL1 create ta*le st#dent$no n#m*er$5% ) name varchar$67%) marks n#m*er$8%) constraint 'k 'rimary key$no)name%%2 BRI&,R. G+. $,LT+R L+;+L% SQL1 SQL1

alter ta*le st#dent add 'rimary key$no)anme%2 alter ta*le st#dent add constraint 'k 'rimary key$no)name%2

COR+I:N G+. $T,-L+ L+;+L% SQL1 create ta*le em'$em'no n#m*er$5%) ename varchar$67%) de'tno n#m*er$5%) dname varchar$67%) 'rimary key$em'no%) orei"n key$de'tno)dname% re erences de't$de'tno)dname%%2

Copy rights are reserved.

create ta*le em'$em'no n#m*er$5%) ename varchar$67%) de'tno n#m*er$5%) dname varchar$67%) constraint 'k 'rimary key$em'no%) constraint k orei"n key$de'tno)dname% re erences de't$de'tno)dname%%2
SQL1 COR+I:N G+. $,LT+R L+;+L%

46

SQL1 alter ta*le em' add orei"n key$de'tno)dname% re erences de't$de'tno)dname%2 SQL1 alter ta*le em' add constraint k orei"n key$de'tno)dname% re erences de't$de'tno)dname%2

D+C+RR,-L+ CONSTR,INTS +ach constraint has t!o additional attri*#tes to s#''ort de erred checkin" o constraints. De erred initially immediate De erred initially de erred

De erred initially immediate checks or constraint violation at the time o insert. De erred initially de erred checks or constraint violation at the time o commit. +34
SQL1 create ta*le st#dent$no n#m*er$5%) name varchar$67%) marks n#m*er$8%) constraint #n #niV#e$no% de erred initially immediate%2 SQL1 create ta*le st#dent$no n#m*er$5%) name varchar$67%) marks n#m*er$8%) constraint #n #niV#e$no% de erred initially de erred%2 SQL1 alter ta*le st#dent add constraint #n #niV#e$no% de erra*le initially de erred2

set constraints all immediate2 This !ill ena*le all the constraints violations at the time o insertin". SQL1 set constraints all de erred2 This !ill ena*le all the constraints violations at the time o commit.
SQL1

OB+R,TIONS 9IT< CONSTR,INTS Bossi*le o'erations !ith constraints as ollo!s.


+N,-L+

+na*le Disa*le +n orce Dro'

This !ill ena*le the constraint. -e ore ena*le) the constraint !ill check the e3istin" data. +34
SQL1

alter ta*le st#dent ena*le constraint #n2

DIS,-L+

This !ill disa*le the constraint.

Copy rights are reserved.

+34
SQL1 +NCORC+

47
alter ta*le st#dent ena*le constraint #n2

This !ill en orce the constraint rather than ena*le or #t#re inserts or #'dates. This !ill not check or e3istin" data !hile en orcin" data. +34
SQL1 DROB

alter ta*le st#dent en orce constraint #n2

This !ill remove the constraint. +34 SQL1 alter ta*le st#dent dro' constraint #n2 Once the ta*le is dro''ed) constraints a#tomatically !ill dro'.

C,S+ ,ND D+C,ULT


C,S+ Case is similar to decode *#t easier to #nderstand !hile "oin" thro#"h codin" +34
SQL1

Select sal) Case sal 9hen D77 then Llo!= 9hen D777 then Lhi"h= +lse Lmedi#m= +nd case Crom em'2 S,L ((((( D77 5D77 5777 8D77 8777 D777 4777 D777 6X77 6577 5777 5W77 5577 8577 C,S+ (((((((( lo! medi#m medi#m medi#m medi#m hi"h medi#m hi"h medi#m medi#m medi#m medi#m medi#m medi#m

D+C,ULT ,e%ault can *e considered as a s#*stit#te *ehavior o not null constraint !hen a''lied to ne! ro!s *ein" entered into the ta*le. 9hen yo# de ine a col#mn !ith the de%ault key!ord ollo!ed *y a val#e) yo# are act#ally tellin" the data*ase that) on insert i a ro! !as not assi"ned a val#e or this col#mn) #se the de a#lt val#e that yo# have s'eci ied. De a#lt is a''lied only d#rin" insertion o ne! ro!s.

Copy rights are reserved.

48
+34
SQL1 SQL1 SQL1 SQL1

create ta*le st#dent$no n#m*er$5% de a#lt 66)name varchar$5%%2 insert into st#dent val#es$6)?a?%2 insert into st#dent$name% val#es$?*?%2 select F rom st#dent2 NO N,&+ (((((( ((((((((( 6 a 66 *

SQL1 SQL1

insert into st#dent val#es$n#ll) Lc=%2 select F rom st#dent2

NO N,&+ (((((( ((((((((( 6 a 66 * C (( De a#lt can not override n#lls.

,-STR,CT D,T, T.B+S


Some times yo# may !ant ty'e !hich holds all ty'es o data incl#din" n#m*ers) chars and s'ecial characters somethin" like this. .o# can not achieve this #sin" 're(de ined ty'es. .o# can de ine c#stom ty'es !hich holds yo#r desired data. +34 S#''ose in a ta*le !e have address col#mn !hich holds hno and city in ormation. 9e !ill de ine a c#stom ty'e !hich holds *oth n#meric as !ell as char data.
CR+,TIN: ,DT SQL1

create ty'e addr as o*Kect$hno n#m*er$8%)city varchar$67%%2 A

CR+,TIN: T,-L+ -,S+D ON ,DT SQL1

create ta*le st#dent$no n#m*er$5%)name varchar$5%)address addr%2

INS+RTIN: D,T, INTO ,DT T,-L+S SQL1 SQL1 SQL1

insert into st#dent val#es$6)?a?)addr$666)?hyd?%%2 insert into st#dent val#es$5)?*?)addr$555)?*an"?%%2 insert into st#dent val#es$8)?c?)addr$888)?delhi?%%2

S+L+CTIN: D,T, CRO& ,DT T,-L+S SQL1

select F rom st#dent2

NO N,&+ ,DDR+SS$<NO) CIT.% ((( ((((((( ((((((((((((((((((((((((( 6 a ,DDR$666) ?hyd?% 5 * ,DDR$555) ?*an"?% 8 c ,DDR$888) ?delhi?%

Copy rights are reserved.

SQL1

select no)name)s.address.hno)s.address.city rom st#dent s2

49

NO N,&+ ,DDR+SS.<NO ,DDR+SS.CIT. (((( ((((((( ((((((((((((((((( (((((((((((((((( 6 a 666 hyd 5 * 555 *an" 8 c 888 delhi
UBD,T+ 9IT< ,DT T,-L+S SQL1

#'date st#dent s set s.address.city I ?*om*ay? !here s.address.hno I select no)name)s.address.hno)s.address.city rom st#dent s2 NO N,&+ ,DDR+SS.<NO ,DDR+SS.CIT. (((( ((((((( ((((((((((((((((( (((((((((((((((( 6 a 666 hyd 5 * 555 *an" 8 c 888 *om*ay

8882
SQL1

D+L+T+ 9IT< ,DT T,-L+S SQL1 SQL1

delete st#dent s !here s.address.hno I 6662 select no)name)s.address.hno)s.address.city rom st#dent s2 NO N,&+ ,DDR+SS.<NO ,DDR+SS.CIT. (((( ((((((( ((((((((((((((((( (((((((((((((((( 5 * 555 *an" 8 c 888 *om*ay

DROBBIN: ,DT SQL1

dro' ty'e addr2

O-@+CT ;I+9S ,ND &+T<ODS


O-@+CT ;I+9S I yo# !ant to im'lement o*Kects !ith the e3istin" ta*le) o*Kect vie!s come into 'ict#re. .o# de ine the o*Kect and create a vie! !hich relates this o*Kect to the e3istin" ta*le nothin" *#t ob!ect view. O*Kect vie!s are #sed to relate the #ser de ined o*Kects to the e3istin" ta*le. +34 6% ,ss#me that the ta*le st#dent has already *een created !ith the ollo!in" col#mns SQL1 create ta*le st#dent$no n#m*er$5%)name varchar$67%)hno n#m*er$8%)city varchar$67%%2 5% Create the ollo!in" ty'es SQL1 create ty'e addr as o*Kect$hno n#m*er$5%)city varchar$67%%2A SQL1 create ty'e st#d as o*Kect$name varchar$67%)address addr%2A 8% Relate the o*Kects to the st#dent ta*le *y creatin" the o*Kect vie! SQL1 create vie! st#dentNov$no)st#dNin o% as select no)st#d$name)addr$hno)city%% rom st#dent2 4% No! yo# can insert data into st#dent ta*le in t!o !ays a% -y re"#lar insert SQL1 Insert into st#dent val#es$6)=s#dha=)666)=hyd=%2 *% -y #sin" o*Kect vie!

Copy rights are reserved.

SQL1

Insert into st#dentNov val#es$6)st#d$Ls#dha=)addr$666)=hyd=%%%2

50

&+T<ODS .o# can de ine methods !hich are nothin" *#t #nctions in ty'es and a''ly in the ta*les !hich holds the ty'es2 +34 6% De inin" methods in ty'es SQL1 Create ty'e st#d as o*Kect$name varchar$67%)marks n#m*er$8%) &em*er #nction makrsN $marks in n#m*er% ret#rn n#m*er) Bra"ma restrictNre erences$marksN )!nds)rnds)!n's) n's%%2A 5% De inin" ty'e *ody SQL1 Create ty'e *ody st#d as &em*er #nction marksN $marks in n#m*er% ret#rn n#m*er is -e"in Ret#rn $marksH677%2 +nd marksN 2 +nd2A 8% Create a ta*le #sin" st#d ty'e SQL1 Create ta*le st#dent$no n#m*er$5%)in o st#d%2 4% Insert some data into st#dent ta*le SQL1 Insert into st#dent val#es$6)st#d$Ls#dha=)677%%2 D% Usin" method in select SQL1 Select s.in o.marksN $s.in o.marks% rom st#dent s2 (( <ere !e are #sin" the 'ra"ma restrictNre erences to avoid the !rites to the data*ase.

;,RR,.S ,ND N+ST+D T,-L+S


;,RR,.S , varyin" array allo!s yo# to store re'eatin" attri*#tes o a record in a sin"le ro! *#t !ith limit. +34 6% 9e can create varrays #sin" oracle ty'es as !ell as #ser de ined ty'es. a% ;array #sin" 're(de ined ty'es SQL1 Create ty'e va as varray$D% o varchar$67%2A *% ;arrays #sin" #ser de ined ty'es SQL1 Create ty'e addr as o*Kect$hno n#m*er$8%)city varchar$67%%2A SQL1 Create ty'e va as varray$D% o addr2A 5% Usin" varray in ta*le SQL1 Create ta*le st#dent$no n#m*er$5%)name varchar$67%)address va%2 8% Insertin" val#es into varray ta*le SQL1 Insert into st#dent val#es$6)=s#dha=)va$addr$666)=hyd=%%%2 SQL1 Insert into st#dent val#es$5)=Ka"an=)va$addr$666)=hyd=%)addr$555)=*an"=%%%2 4% Selectin" data rom varray ta*le SQL1 Select F rom st#dent2 (( This !ill dis'lay varray col#mn data alon" !ith varray and adt2 SQL1 Select no)name) s.F rom st#dent s6) ta*le$s6.address% s2 (( This !ill dis'lay in "eneral ormat D% Instead o s.F yo# can s'eci y the col#mns in varray SQL1 Select no)name) s.hno)s.city rom st#dent s6)ta*le$s6.address% s2 (( U'date and delete not 'ossi*le in varrays. (( <ere !e #sed ta*le #nction !hich !ill take the varray col#mn as in'#t or 'rod#cin" o#t'#t e3cl#din" varray and ty'es.

Copy rights are reserved.

51
N+ST+D T,-L+S , nested ta*le is) as its name im'lies) a ta*le !ithin a ta*le. In this case it is a ta*le that is re'resented as a col#mn !ithin another ta*le. Nested ta*le has the same e ect o varrays *#t has no limit. +34 6% 9e can create nested ta*les #sin" oracle ty'es and #ser de ined ty'es !hich has no limit a% Nested ta*les #sin" 're(de ined ty'es SQL1 Create ty'e nt as ta*le o varchar$67%2A *% Nested ta*les #sin" #ser de ined ty'es SQL1 Create ty'e addr as o*Kect$hno n#m*er$8%)city varchar$67%%2A SQL1 Create ty'e nt as ta*le o addr2A 5% Usin" nested ta*le in ta*le SQL1 Create ta*le st#dent$no n#m*er$5%)name varchar$67%)address nt% nested ta*le address store as st#dentNtem'2 8% Insertin" val#es into ta*le !hich has nested ta*le SQL1 Insert into st#dent val#es $6)=s#dha=)nt$addr$666)=hyd=%%%2 SQL1 Insert into st#dent val#es $5)=Ka"an=)nt$addr$666)=hyd=%)addr$555)=*an"=%%%2 4% Selectin" data rom ta*le !hich has nested ta*le SQL1 Select F rom st#dent2 (( This !ill dis'lay nested ta*le col#mn data alon" !ith nested ta*le and
SQL1

adt2

Select no)name) s.F rom st#dent s6) ta*le$s6.address% s2 (( This !ill dis'lay in "eneral ormat D% Instead o s.F yo# can s'eci y the col#mns in nested ta*le SQL1 Select no)name) s.hno)s.city rom st#dent s6)ta*le$s6.address% s2 E% Insertin" nested ta*le data to the e3istin" ro! SQL1 Insert into ta*le$select address rom st#dent !here noI6% val#es$addr$DDD)=chennai=%%2 W% U'date in nested ta*les SQL1 U'date ta*le$select address rom st#dent !here noI5% s set s.cityI=*om*ay= !here s.hno I 5552 X% Delete in nested ta*le SQL1 Delete ta*le$select address rom st#dent !here noI8% s !here s.hnoI8882 D,T, &OD+L ,LLNCOLLNT.B+S ,LLNT.B+S D-,NCOLLNT.B+S D-,NT.B+S US+RNCOLLNT.B+S US+RNT.B+S

CL,S<-,CG QU+R.
Used to retrieve the data !hich has *een already committed !ith o#t "oin" or recovery. Clash*acks are o t!o ty'es Time *ase lash*ack SCN *ased lash*ack $SCN stands or System Chan"e N#m*er%

Copy rights are reserved.

52
+34 6% Usin" time *ased lash*ack a% SQL1 Select F rom st#dent2 (( This !ill dis'lay all the ro!s *% SQL1 Delete st#dent2 c% SQL1 Commit2 (( this !ill commit the !ork. d% SQL1 Select F rom st#dent2 (( <ere it !ill dis'lay nothin" e% Then e3ec#te the ollo!in" 'roced#res SQL1 +3ec d*msN lash*ack.ena*leNatNtime$sysdate(5A6447% % SQL1 Select F rom st#dent2 (( <ere it !ill dis'lay the lost data (( The lost data !ill come *#t the c#rrent system time !as #sed "% SQL1 +3ec d*msN lash*ack.disa*le (( <ere !e have to disa*le the lash*ack to ena*le it a"ain 5% Usin" SCN *ased lash*ack a% Declare a varia*le to store SCN SQL1 ;aria*le s n#m*er *% :et the SCN SQL1 +3ec 4s 4I e3ec d*msN lash*ack."etNsystemNchan"eNn#m*er c% To see the SCN SQL1 Brint s d% Then e3ec#te the ollo!in" 'roced#res SQL1 +3ec d*msN lash*ack.ena*leNatNsystemNchan"eNn#m*er$4s% SQL1 +3ec d*msN lash*ack.disa*le

+/T+RN,L T,-L+S
.o# can #ser e3ternal ta*le eat#re to access e3ternal iles as i they are ta*les inside the data*ase. 9hen yo# create an e3ternal ta*le) yo# de ine its str#ct#re and location !ith in oracle. 9hen yo# V#ery the ta*le) oracle reads the e3ternal ta*le and ret#rns the res#lts K#st as i the data had *een stored !ith in the data*ase. ,CC+SSIN: +/T+RN,L T,-L+ D,T, To access e3ternal iles rom !ithin oracle) yo# m#st irst #se the create directory command to de ine a directory o*Kect 'ointin" to the e3ternal ile location Users !ho !ill access the e3ternal iles m#st have the read and !rite 'rivile"e on the directory. +34
CR+,TIN: DIR+CTOR. ,ND OS L+;+L CIL+ SQL1 SQL1 SQL1 SQL1 SQL1 SQL1 SQL1

SVl'l#s systemAmana"er Create directory sakethNdir as LA;isd*Avisd*AT.5.7Ae3ternal=2 :rant all on directory sakethNdir to saketh2 Conn sakethAsaketh S'ool de't.lst Select de'tno YY L)= YY dname YY L)= YY loc rom de't2 S'ool o

CR+,TIN: +/T+RN,L T,-L+

Copy rights are reserved.

53
SQL1

Create ta*le de'tNe3t $de'tno n#m*er$5%) Dname varchar$64%) Loc varchar$68%% Or"aniUation e3ternal $ ty'e oracleNloader De a#lt directory sakethNdir ,ccess 'arameters $ records delimited *y ne!line Cields terminated *y Q)R $ de'tno n#m*er$5%) Dname varchar$64%) Loc varchar$68%%% Location $LA;isd*Avisd*AT.5.7Ade't.lst=%%2

S+L+CTIN: D,T, CRO& +/T+RN,L T,-L+

select F rom de'tNe3t2 This !ill read rom de't.lst !hich is a o'eratin" system level ile.
SQL1 LI&IT,TIONS ON +/T+RN,L T,-L+S

a% .o# can not 'er orm insert) #'date) and delete o'erations a% Inde3in" not 'ossi*le *% Constraints not 'ossi*le
-+N+CITS OC +/T+RN,L T,-L+S

a% Q#eries o e3ternal ta*les com'lete very V#ickly even tho#"h a #ll ta*le scan id reV#ired !ith each access *% .o# can Koin e3ternal ta*les to each other or to standard ta*les

R+C D+R+C ;,LU+


R+C The re #nction allo!s re erencin" o e3istin" ro! o*Kects. +ach o the ro! o*Kects has an o*Kect id val#e assi"ned to it. The o*Kect id assi"ned can *e seen *y #sin" re #nction.

D+R+C The dere #nction 'er orms o''osite action. It takes a re erence val#e o o*Kect id and ret#rns the val#e o the ro! o*Kects.

;,LU+ +ven tho#"h the 'rimary ta*le is o*Kect ta*le) still it dis'lays the ro!s in "eneral ormat. To dis'lay the entire str#ct#re o the o*Kect) this !ill *e #sed.

+34 6% create vendotNadt ty'e SQL1 Create ty'e vendorNadt as o*Kect $vendorNcode n#m*er$5%) vendorNname varchar$5%) vendorNaddress varchar$67%%2A 5% create o*Kect ta*les vendors and vendors6 SQL1 Create ta*le vendors o vendorNadt2 SQL1 Create ta*le vendors6 o vendorNadt2 8% insert the data into o*Kect ta*les

Copy rights are reserved.

insert into vendors val#es$6) La=) Lhyd=%2 SQL1 insert into vendors val#es$5) L*=) L*an"=%2 SQL1 insert into vendors6 val#es$8) Lc=) Ldelhi=%2 SQL1 insert into vendors6 val#es$4) Ld=) Lchennai=%2 4% create another ta*le orders !hich holds the vendorNadt ty'e also. SQL1 Create ta*le orders $orderNno n#m*er$5%) vendorNin o re vendorNadt%2 Or SQL1 Create ta*le orders $orderNno n#m*er$5%) vendorNin o re vendorNadt !ith ro!id%2
SQL1

54

D% insert the data into orders ta*le The vendorNin o col#mn in the ollo!in" synta3es !ill store o*Kect id o any ta*le !hich is re erenced *y vendorNadt o*Kect $ *oth vendors and vendors6%.
SQL1 insert into orders val#es$66)$select re $v% rom vendors v !here vendorNcode I 6%%2 SQL1 insert into orders val#es$65)$select re $v% rom vendors v !here vendorNcode I 5%%2 SQL1 insert into orders val#es$68)$select re $v6% rom vendors6 v6 !here vendorNcode I 6%%2 SQL1 insert into orders val#es$64)$select re $v6% rom vendors6 v6 !here vendorNcode I 6%%2 E% To see the o*Kect ids o vendor ta*le SQL1 Select re $;% rom vendors v2 W% I yo# see the vendorNin o o orders it !ill sho! only the o*Kect ids not the val#es) to see the val#es SQL1 Select dere $o.vendorNin o% rom orders o2 X% +ven tho#"h the vendors ta*le is o*Kect ta*le it !ill not sho! the adt alon" !ith data) to see the data alon" !ith the adt SQL1Select F rom vendors2 This !ill "ive the data !itho#t adt. SQL1Select val#e$v% rom vendors v2 This !ill "ive the col#mns data alon" !ih the ty'e.

R+C CONSTR,INTS Re can also acts as constraint. +ven tho#"h vendors6 also holdin" vendorNadt) the orders ta*le !ill store the o*Kect ids o vendors only *eca#se it is constrained to that ta*le only. The vendorNin o col#mn in the ollo!in" synta3es !ill store o*Kect ids o vendors only. Create ta*le orders $orderNno n#m*er$5%) vendorNin o re vendorNadt sco'e is vendors%2 Or SQL1 Create ta*le orders $orderNno n#m*er$5%) vendorNin o re vendorNadt constraint k re erences vendors%2
SQL1

O-@+CT ;I+9S 9IT< R+C+R+NC+S


Copy rights are reserved.

To im'lement the o*Kects and the re constraints to the e3istin" ta*les) !hat !e can do\ Sim'ly dro' the *oth ta*les and recreate !ith o*Kects and re constrains.

55

-#t yo# can achieve this !ith o#t dro''in" the ta*les and !itho#t losin" the data *y creatin" o*Kect vie!s !ith re erences. +34 a% Create the ollo!in" ta*les SQL1 Create ta*le st#dent6$no n#m*er$5% 'rimary key)name varchar$5%)marks n#m*er$8%%2 SQL1 Create ta*le st#dent5$no n#m*er$5% 'rimary key)hno n#m*er$8%)city varchar$67%)id n#m*er$5%) orei"n Gey$id% re erences st#dent6$no%%2 *% Insert the records into *oth ta*les SQL1 insert into st#dent6$6)=a=)677%2 SQL1 insert into st#dent6$5)=*=)577%2 SQL1 insert into st#dent5$66)666)=hyd=)6%2 SQL1 insert into st#dent5$65)555)=*an"=)5%2 SQL1 insert into st#dent5$68)888)=*om*ay=)6%2 c% Create the ty'e SQL1 create or re'lace ty'e st#d as o*Kect$no n#m*er$5%)name varchar$5%)marks n#m*er$8%%2A d% :eneratin" OIDs SQL1 Create or re'lace vie! st#dent6Nov o st#d !ith o*Kect identi ier$or id% $no% as Select F rom St#dent62 e% :eneratin" re erences SQL1 Create or re'lace vie! st#dent5Nov as select no)hno)city)makeNre $st#dent6Nov)id% id rom St#dent52 d% Q#ery the ollo!in" SQL1 select F rom st#dent6Nov2 SQL1 select re $s% rom st#dent6Nov s2 SQL1 select val#es$s% rom st#dent6Nov2 SQ1 select F rom st#dent5Nov2 SQL1 select dere $s.id% rom st#dent5Nov s2

B,RTITIONS
, sin"le lo"ical ta*le can *e s'lit into a n#m*er o 'hysically se'arate 'ieces *ased on ran"es o key val#es. +ach o the 'arts o the ta*le is called a 'artition. , non('artitioned ta*le can not *e 'artitioned later. T.B+S Ran"e 'artitions List 'artitions <ash 'artitions S#* 'artitions

,D;,NT,:+S Red#cin" do!ntime or sched#led maintenance) !hich allo!s maintenance o'erations to *e carried o#t on selected 'artitions !hile other 'artitions are availa*le to #sers.

Copy rights are reserved.

56
Red#cin" do!ntime d#e to data ail#re) ail#re o a 'artic#lar 'artition !ill no !ay a ect other 'artitions. Bartition inde'endence allo!s or conc#rrent #se o the vario#s 'artitions or vario#s '#r'oses.

,D;,NT,:+S OC B,RTITIONS -. STORIN: T<+& IN DICC+R+NT T,-L+SB,C+S Red#ces the 'ossi*ility o data corr#'tion in m#lti'le 'artitions. -ack #' and recovery o each 'artition can *e done inde'endently.

DIS,D;,NT,:+S Bartitioned ta*les cannot contain any col#mns !ith lon" or lon" ra! dataty'es) LO- ty'es or o*Kect ty'es.

R,N:+ B,RTITIONS a% Creatin" ran"e 'artitioned ta*le SQL1 Create ta*le st#dent$no n#m*er$5%)name varchar$5%% 'artition *y ran"e$no% $'artition '6 val#es less than$67%) 'artition '5 val#es less than$57%) 'artition '8 val#es less than$87%)'artition '4 val#es less than$ma3val#e%%2 FF i yo# are #sin" ma3val#e or the last 'artition) yo# can not add a 'artition. *% Insertin" records into ran"e 'artitioned ta*le SQL1 Insert into st#dent val#es$6)=a=%2 (( this !ill "o to '6 SQL1 Insert into st#dent val#es$66)=*=%2 (( this !ill "o to '5 SQL1 Insert into st#dent val#es$56)=c=%2 (( this !ill "o to '8 SQL1 Insert into st#dent val#es$86)=d=%2 (( this !ill "o to '4 c% Retrievin" records rom ran"e 'artitioned ta*le SQL1 Select F rom st#dent2 SQL1 Select F rom st#dent 'artition$'6%2 d% Bossi*le o'erations !ith ran"e 'artitions ,dd Dro' Tr#ncate Rename S'lit &ove +3chan"e e% ,ddin" a 'artition SQL1 ,lter ta*le st#dent add 'artition 'D val#es less than$47%2 % Dro''in" a 'artition SQL1 ,lter ta*le st#dent dro' 'artition '42 "% Renamin" a 'artition SQL1 ,lter ta*le st#dent rename 'artition '8 to 'E2 h% Tr#ncate a 'artition SQL1 ,lter ta*le st#dent tr#ncate 'artition 'E2 i% S'littin" a 'artition SQL1 ,lter ta*le st#dent s'lit 'artition '5 at$6D% into $'artition '56)'artition '55%2 K% +3chan"in" a 'artition SQL1 ,lter ta*le st#dent e3chan"e 'artition '6 !ith ta*le st#dent52 k% &ovin" a 'artition SQL1 ,lter ta*le st#dent move 'artition '56 ta*les'ace sakethNts2 LIST B,RTITIONS

Copy rights are reserved.

a% Creatin" list 'artitioned ta*le SQL1 Create ta*le st#dent$no n#m*er$5%)name varchar$5%% 'artition *y list$no% $'artition '6 val#es$6)5)8)4)D%) 'artition '5 val#es$E)W)X)T)67%)'artition '8 val#es$66)65)68)64)6D%) 'artition '4 val#es$6E)6W)6X)6T)57%%2 *% Insertin" records into list 'artitioned ta*le SQL1 Insert into st#dent val#es$6)=a=%2 (( this !ill "o to '6 SQL1 Insert into st#dent val#es$E)=*=%2 (( this !ill "o to '5 SQL1 Insert into st#dent val#es$66)=c=%2 (( this !ill "o to '8 SQL1 Insert into st#dent val#es$6E)=d=%2 (( this !ill "o to '4 c% Retrievin" records rom list 'artitioned ta*le SQL1 Select F rom st#dent2 SQL1 Select F rom st#dent 'artition$'6%2 d% Bossi*le o'erations !ith list 'artitions ,dd Dro' Tr#ncate Rename &ove +3chan"e e% ,ddin" a 'artition SQL1 ,lter ta*le st#dent add 'artition 'D val#es$56)55)58)54)5D%2 % Dro''in" a 'artition SQL1 ,lter ta*le st#dent dro' 'artition '42 "% Renamin" a 'artition SQL1 ,lter ta*le st#dent rename 'artition '8 to 'E2 h% Tr#ncate a 'artition SQL1 ,lter ta*le st#dent tr#ncate 'artition 'E2 i% +3chan"in" a 'artition SQL1 ,lter ta*le st#dent e3chan"e 'artition '6 !ith ta*le st#dent52 K% &ovin" a 'artition SQL1 ,lter ta*le st#dent move 'artition '5 ta*les'ace sakethNts2 <,S< B,RTITIONS a% Creatin" hash 'artitioned ta*le SQL1 Create ta*le st#dent$no n#m*er$5%)name varchar$5%% 'artition *y hash$no% 'artitions D2 <ere oracle a#tomatically "ives 'artition names like S.SNB6 S.SNB5 S.SNB8 S.SNB4 S.SNBD *% Insertin" records into hash 'artitioned ta*le it !ill insert the records *ased on hash #nction calc#lated *y takin" the 'artition key SQL1 Insert into st#dent val#es$6)=a=%2 SQL1 Insert into st#dent val#es$E)=*=%2 SQL1 Insert into st#dent val#es$66)=c=%2 SQL1 Insert into st#dent val#es$6E)=d=%2 c% Retrievin" records rom hash 'artitioned ta*le SQL1 Select F rom st#dent2 SQL1 Select F rom st#dent 'artition$sysN'6%2 d% Bossi*le o'erations !ith hash 'artitions ,dd Tr#ncate Rename

57

Copy rights are reserved.

58
&ove +3chan"e e% ,ddin" a 'artition SQL1 ,lter ta*le st#dent add 'artition 'E 2 % Renamin" a 'artition SQL1 ,lter ta*le st#dent rename 'artition 'E to 'W2 "% Tr#ncate a 'artition SQL1 ,lter ta*le st#dent tr#ncate 'artition 'W2 h% +3chan"in" a 'artition SQL1 ,lter ta*le st#dent e3chan"e 'artition sysN'6 !ith ta*le st#dent52 i% &ovin" a 'artition SQL1 ,lter ta*le st#dent move 'artition sysN'5 ta*les'ace sakethNts2 SU-(B,RTITIONS 9IT< R,N:+ ,ND <,S< S#*'artitions cla#se is #sed *y hash only. 9e can not create s#*'artitions !ith list and hash 'artitions. a% Creatin" s#*'artitioned ta*le SQL1 Create ta*le st#dent$no n#m*er$5%)name varchar$5%)marks n#m*er$8%% Bartition *y ran"e$no% s#*'artition *y hash$name% s#*'artitions 8 $Bartition '6 val#es less than$67%)'artition '5 val#es less than$57%%2 This !ill create t!o 'artitions '6 and '5 !ith three s#*'artitions or each 'artition B6 ] S.SNSU-B6 S.SNSU-B5 S.SNSU-B8 B5 ] S.SNSU-B4 S.SNSU-BD S.SNSU-BE FF i yo# are #sin" ma3val#e or the last 'artition) yo# can not add a 'artition. *% Insertin" records into s#*'artitioned ta*le SQL1 Insert into st#dent val#es$6)=a=%2 (( this !ill "o to '6 SQL1 Insert into st#dent val#es$66)=*=%2 (( this !ill "o to '5 c% Retrievin" records rom s#*'artitioned ta*le SQL1 Select F rom st#dent2 SQL1 Select F rom st#dent 'artition$'6%2 SQL1 Select F rom st#dent s#*'artition$sysNs#*'6%2 d% Bossi*le o'erations !ith s#*'artitions ,dd Dro' Tr#ncate Rename S'lit e% ,ddin" a 'artition SQL1 ,lter ta*le st#dent add 'artition '8 val#es less than$87%2 % Dro''in" a 'artition SQL1 ,lter ta*le st#dent dro' 'artition '82 "% Renamin" a 'artition SQL1 ,lter ta*le st#dent rename 'artition '5 to '82 h% Tr#ncate a 'artition SQL1 ,lter ta*le st#dent tr#ncate 'artition '62 i% S'littin" a 'artition SQL1 ,lter ta*le st#dent s'lit 'artition '8 at$6D% into $'artition '86)'artition '85%2 D,T, &OD+L

Copy rights are reserved.

59
,LLNINDNB,RTITIONS ,LLNINDNSU-B,RTITIONS ,LLNT,-NB,RTITIONS ,LLNT,-NSU-B,RTITIONS D-,NINDNB,RTITIONS D-,NINDNSU-B,RTITIONS D-,NT,-NB,RTITIONS D-,NT,-NSU-B,RTITIONS US+RNINDNB,RTITIONS US+RNINDNSU-B,RTITIONS US+RNT,-NB,RTITIONS US+RNT,-NSU-B,RTITIONS

:ROUB -. ,ND <,;IN:


:ROUB -. Usin" "ro#' *y) !e can create "ro#'s o related in ormation. Col#mns #sed in select m#st *e #sed !ith "ro#' *y) other!ise it !as not a "ro#' *y e3'ression. +34
SQL1

select de'tno) s#m$sal% rom em' "ro#' *y de'tno2 D+BTNO SU&$S,L% (((((((((( (((((((((( 67 XWD7 57 67XWD 87 T477

SQL1

select de'tno)Ko*)s#m$sal% rom em' "ro#' *y de'tno)Ko*2 D+BTNO (((((((((( 67 67 67 57 57 57 87 87 87 @OSU&$S,L% ((((((((( (((((((((( CL+RG 6877 &,N,:+R 54D7 BR+SID+NT D777 ,N,L.ST E777 CL+RG 6T77 &,N,:+R 5TWD CL+RG TD7 &,N,:+R 5XD7 S,L+S&,N DE77

<,;IN: This !ill !ork as !here cla#se !hich can *e #sed only !ith "ro#' *y *eca#se o a*sence o !here cla#se in "ro#' *y. +34 SQL1 select de'tno)Ko*)s#m$sal% tsal rom em' "ro#' *y de'tno)Ko* havin" s#m$sal% 1 87772 D+BTNO @OTS,L (((((((((( ((((((((( (((((((((( 67 BR+SID+NT D777 57 ,N,L.ST E777 87 S,L+S&,N DE77

Copy rights are reserved.

select de'tno)Ko*)s#m$sal% tsal rom em' "ro#' *y de'tno)Ko* havin" s#m$sal% 1 8777 order *y Ko*2
SQL1

60

D+BTNO (((((((((( 57 67 87 ORD+R OC +/+CUTION

@OTS,L ((((((((( (((((((((( ,N,L.ST E777 BR+SID+NT D777 S,L+S&,N DE77

:ro#' the ro!s to"ether *ased on "ro#' *y cla#se. Calc#late the "ro#' #nctions or each "ro#'. Choose and eliminate the "ro#'s *ased on the havin" cla#se. Order the "ro#'s *ased on the s'eci ied col#mn.

ROLLUB :ROUBIN: CU-+


These are the enhancements to the "ro#' *y eat#re. USIN: ROLLUB This !ill "ive the salaries in each de'artment in each Ko* cate"ory alon" !ih the total salary or individ#al de'artments and the total salary o all the de'artments.
SQL1

Select de'tno)Ko*)s#m$sal% rom em' "ro#' *y roll#'$de'tno)Ko*%2 D+BTNO (((((((((( 67 67 67 67 57 57 57 57 87 87 87 87 @OSU&$S,L% ((((((((( (((((((((( CL+RG 6877 &,N,:+R 54D7 BR+SID+NT D777 XWD7 ,N,L.ST E777 CL+RG 6T77 &,N,:+R 5TWD 67XWD CL+RG TD7 &,N,:+R 5XD7 S,L+S&,N DE77 T477 5T75D

USIN: :ROUBIN: In the a*ove V#ery it !ill "ive the total salary o the individ#al de'artments *#t !ith a *lank in the Ko* col#mn and "ives the total salary o all the de'artments !ith *lanks in de'tno and Ko* col#mns. To re'lace these *lanks !ith yo#r desired strin" "ro#'in" !ill *e #sed SQL1 select decode$"ro#'in"$de'tno%)6)?,ll De'ts?)de'tno%)decode$"ro#'in"$Ko*%)6)?,ll Ko*s?)Ko*%)s#m$sal% rom em' "ro#' *y roll#'$de'tno)Ko*%2

Copy rights are reserved.

61
D+COD+$:ROUBIN:$D+BTNO%)6)?,LLD+BTS?)D+B D+COD+$:R SU&$S,L% ((((((((((((((((((((((((((((((((((( (((((((((((((((((((((((((((((((((( (((((((((((((( 67 CL+RG 6877 67 &,N,:+R 54D7 67 BR+SID+NT D777 67 ,ll Ko*s XWD7 57 ,N,L.ST E777 57 CL+RG 6T77 57 &,N,:+R 5TWD 57 ,ll Ko*s 67XWD 87 CL+RG TD7 87 &,N,:+R 5XD7 87 S,L+S&,N DE77 87 ,ll Ko*s T477 ,ll De'ts ,ll Ko*s 5T75D :ro#'in" !ill ret#rn 6 i the col#mn !hich is s'eci ied in the "ro#'in" #nction has *een #sed in roll#'. :ro#'in" !ill *e #sed in association !ith decode. USIN: CU-+ This !ill "ive the salaries in each de'artment in each Ko* cate"ory) the total salary or individ#al de'artments) the total salary o all the de'artments and the salaries in each Ko* cate"ory. select decode$"ro#'in"$de'tno%)6)=,ll De'ts=)de'tno%)decode$"ro#'in"$Ko*%)6)=,ll @o*s=)Ko*%)s#m$sal% rom em' "ro#' *y c#*e$de'tno)Ko*%2
SQL1

D+COD+$:ROUBIN:$D+BTNO%)6)?,LLD+BTS?)D+B D+COD+$:R ((((((((((((((((((((((((((((((((((( (((((((((((((((((((((((((((((((((((( 67 CL+RG 67 &,N,:+R 67 BR+SID+NT 67 ,ll @o*s 57 ,N,L.ST 57 CL+RG 57 &,N,:+R 57 ,ll @o*s 87 CL+RG 87 &,N,:+R 87 S,L+S&,N 87 ,ll @o*s ,ll De'ts ,N,L.ST ,ll De'ts CL+RG ,ll De'ts &,N,:+R ,ll De'ts BR+SID+NT ,ll De'ts S,L+S&,N ,ll De'ts ,ll @o*s

SU&$S,L% (((((((((((( 6877 54D7 D777 XWD7 E777 6T77 5TWD 67XWD TD7 5XD7 DE77 T477 E777 46D7 X5WD D777 DE77 5T75D

S+T OB+R,TORS
T.B+S Union

Copy rights are reserved.

62
Union all Intersect &in#s

UNION This !ill com*ine the records o m#lti'le ta*les havin" the same str#ct#re. +34 SQL1 select F rom st#dent6 #nion select F rom st#dent52 UNION ,LL This !ill com*ine the records o m#lti'le ta*les havin" the same str#ct#re *#t incl#din" d#'licates. +34
SQL1

select F rom st#dent6 #nion all select F rom st#dent52

INT+RS+CT This !ill "ive the common records o m#lti'le ta*les havin" the same str#ct#re. +34
SQL1

select F rom st#dent6 intersect select F rom st#dent52

&INUS This !ill "ive the records o a ta*le !hose records are not in other ta*les havin" the same str#ct#re. +34 SQL1 select F rom st#dent6 min#s select F rom st#dent52

;I+9S
, vie! is a data*ase o*Kect that is a lo"ical re'resentation o a ta*le. It is delivered rom a ta*le *#t has no stora"e o its o!n and o ten may *e #sed in the same manner as a ta*le. , vie! takes the o#t'#t o the V#ery and treats it as a ta*le) there ore a vie! can *e tho#"ht o as a stored V#ery or a virt#al ta*le. T.B+S Sim'le vie! Com'le3 vie! rom one ta*le !here as com'le3 vie! can *e

Sim'le vie! can *e created created rom m#lti'le ta*les. 9<. ;I+9S\

Brovides additional level o sec#rity *y restrictin" 'redetermined set o ro!s andAor col#mns o a ta*le. <ide the data com'le3ity. Sim'li y commands or the #ser.

access

to

;I+9S 9IT<OUT D&L Read only vie! ;ie! !ith "ro#' *y ;ie! !ith a""re"ate #nctions

Copy rights are reserved.

63
+34
SQL1 SQL1

;ie! !ith ro!n#m Bartition vie! ;ie! !ith distinct

Create vie! de'tNv as select F rom de't !ith read only2 Create vie! de'tNv as select de'tno) s#m$sal% tNsal rom em' "ro#' *y

de'tno2
SQL1

Create vie! st#d as select ro!n#m no) name) marks rom st#dent2 Create vie! st#dent as select F rom st#dent6 #nion select F rom st#dent52 SQL1 Create vie! st#d as select distinct no)name rom st#dent2 ;I+9S 9IT< D&L
SQL1

;ie! !ith not n#ll col#mn 'ossi*le

((

insert !ith o#t not n#ll col#mn not (( #'date not n#ll col#mn to n#ll is not

'ossi*le

(( delete 'ossi*le ;ie! !ith o#t not n#ll col#mn !hich !as in *ase ta*le (( insert not 'ossi*le (( #'date) delete 'ossi*le ;ie! !ith e3'ression (( insert ) #'date not 'ossi*le (( delete 'ossi*le ;ie! !ith #nctions $e3ce't a""re"ate% (( insert) #'date not 'ossi*le (( delete 'ossi*le ;ie! !as created *#t the #nderlyin" ta*le !as dro''ed then !e !ill "et the messa"e like Q vie! has errors R. ;ie! !as created *#t the *ase ta*le has *een altered *#t still the vie! !as !ith the initial de inition) !e have to re'lace the vie! to a ect the chan"es. Com'le3 vie! $vie! !ith more than one ta*le% (( insert not 'ossi*le (( #'date) delete 'ossi*le $not al!ays% CR+,TIN: ;I+9 9IT<OUT <,;IN: T<+ -,S+ T,-L+ Create orce vie! st#d as select FCrom st#dent2 (( Once the *ase ta*le !as created then the vie! is validated.
SQL1

;I+9 9IT< C<+CG OBTION CONSTR,INT Create vie! st#d as select F rom st#dent !here marks I D77 !ith check o'tion constraint Ck2 ( Insert 'ossi*le !ith marks val#e as D77 ( U'date 'ossi*le e3cl#din" marks col#mn ( Delete 'ossi*le
SQL1

DROBBIN: ;I+9S
SQL1

dro' vie! de'tNv2

S.NON.& ,ND S+QU+NC+


S.NON.& , synonym is a data*ase o*Kect) !hich is #sed as an alias or a ta*le) vie! or seV#ence.

Copy rights are reserved.

T.B+S

64

Brivate B#*lic Brivate synonym is availa*le to the 'artic#lar #ser !ho creates. B#*lic synonym is created *y D-, !hich is availa*le to all the #sers.
,D;,NT,:+S

<ide the name and o!ner o the o*Kect. Brovides location trans'arency or remote o*Kects o data*ase.

a distri*#ted

CR+,T+ ,ND DROB SQL1 SQL1 SQL1

create synonym s6 or em'2 create '#*lic synonym s5 or em'2 dro' synonym s62

S+QU+NC+ , seV#ence is a data*ase o*Kect) !hich can "enerate #niV#e) seV#ential inte"er val#es. It can *e #sed to a#tomatically "enerate 'rimary key or #niV#e key val#es. , seV#ence can *e either in an ascendin" or descendin" order. Synta34 Create seV#ence 0se-_name1 Oincrement *ty nP Ostart !ith nP Oma3val#e nP Ominval#e nP OcycleAnocycleP OcacheAnocacheP2 -y de al#lt the seV#ence starts !ith 6) increments *y 6 !ith minval#e o 6 and !ith nocycle) nocache. Cache o'tion 're(alloocates a set o seV#ence n#m*ers and retains them in memory or aster access. +34 create seV#ence s2 create seV#ence s increment *y 67 start !ith 677 minval#e D ma3val#e 577 cycle cache 572
SQL1 SQL1 USIN: S+QU+NC+ SQL1 SQL1

create ta*le st#dent$no n#m*er$5%)name varchar$67%%2 insert into st#dent val#es$s.ne3tval) Lsaketh=%2 Initially c#rrval is not de ined and ne3tval is startin" val#e. , ter that ne3tval and c#rrval are al!ays eV#al.

CR+,TIN: ,LB<,(NU&+RIC S+QU+NC+ SQL1 SQL1

create seV#ence s start !ith 6665842 Insert into st#dent val#es $s.ne3tval YY translate $s.ne3tval)=6584DEWXT7=)=a*cde "hiK=%%2

,LT+RIN: S+QU+NC+

9e can alter the seV#ence to 'er orm the ollo!in". Set or eliminate minval#e or ma3val#e. Chan"e the increment val#e. Chan"e the n#m*er o cached seV#ence n#m*ers.

Copy rights are reserved.

+34
SQL1 SQL1 SQL1

65
alter seV#ence s minval#e D2 alter seV#ence s increment *y 52 alter seV#ence s cache 672

DROBBIN: S+QU+NC+ SQL1

dro' seV#ence s2

@OINS
The '#r'ose o a Koin is to com*ine the data across ta*les. , Koin is act#ally 'er ormed *y the !here cla#se !hich com*ines the s'eci ied ro!s o ta*les. I a Koin involves in more than t!o ta*les then oracle Koins irst t!o ta*les *ased on the Koins condition and then com'ares the res#lt !ith the ne3t ta*le and so on. T.B+S +V#i Koin Non(eV#i Koin Sel Koin Nat#ral Koin Cross Koin O#ter Koin Le t o#ter Ri"ht o#ter C#ll o#ter Inner Koin Usin" cla#se On cla#se

,ss#me that !e have the ollo!in" ta*les. SQL1 select F rom de't2 D+BTNO DN,&+ LOC (((((( (((((((((( (((((((((( 67 mkt hyd 57 in *an" 87 hr *om*ay
SQL1

select F rom em'2 +N,&+ (((((((((( saketh s#dha Ka"an madh# @O&:R D+BTNO (((((((((( (((((((((( (((((((((( analyst 444 67 clerk 888 57 mana"er 666 67 en"ineer 555 47

+&BNO (((((((((( 666 555 888 444 +QUI @OIN

, Koin !hich contains an LI= o'erator in the Koins condition. +34


SQL1 select em'no)ename)Ko*)dname)loc e.de'tnoId.de'tno2

rom em' e)de't d !here LOC

+&BNO

+N,&+

@O-

DN,&+

Copy rights are reserved.

(((((((((( (((((((((( (((((((((( (((((((((( (((((((((( 666 saketh analyst mkt hyd 888 Ka"an mana"er mkt hyd 555 s#dha clerk in *an" USIN: CL,US+
SQL1

66

select em'no)ename)Ko* )dname)loc rom em' e Koin de't d #sin"$de'tno%2 +&BNO +N,&+ @O- DN,&+ LOC (((((((((( (((((((((( (((((((((( (((((((((( (((((((((( 666 saketh analyst mkt hyd 888 Ka"an mana"er mkt hyd 555 s#dha clerk in *an"

ON CL,US+ select em'no)ename)Ko*)dname)loc on$e.de'tnoId.de'tno%2


SQL1

rom

em'

Koin

de't

+&BNO +N,&+ @O- DN,&+ LOC (((((((((( (((((((((( (((((((((( (((((((((( (((((((((( 666 saketh analyst mkt hyd 888 Ka"an mana"er mkt hyd 555 s#dha clerk in *an" NON(+QUI @OIN , Koin !hich contains an o'erator other than LI= in the Koins condition. +34
SQL1 select em'no)ename)Ko*)dname)loc rom em' e)de't d !here e.de'tno 1 d.de'tno2

+&BNO +N,&+ @ODN,&+ LOC (((((((((( (((((((((( (((((((((( (((((((((( (((((((((( 555 s#dha clerk mkt hyd 444 madh# en"ineer mkt hyd 444 madh# en"ineer in *an" 444 madh# en"ineer hr *om*ay S+LC @OIN @oinin" the ta*le itsel is called sel Koin. +34
SQL1

select e6.em'no)e5.ename)e6.Ko*)e5.de'tno rom em' e6)em' e5 e6.em'noIe5.m"r2 +&BNO (((((((((( 666 555 888 444 +N,&+ @OD+BTNO (((((((((( (((((((((( (((((((((( Ka"an analyst 67 madh# clerk 47 s#dha mana"er 57 saketh en"ineer 67

!here

N,TUR,L @OIN

Nat#ral Koin com'ares all the common col#mns.

Copy rights are reserved.

67
+34
SQL1

select em'no)ename)Ko*)dname)loc rom em' nat#ral Koin de't2 +&BNO +N,&+ @ODN,&+ LOC (((((((((( (((((((((( (((((((((( (((((((((( (((((((((( 666 saketh analyst mkt hyd 888 Ka"an mana"er mkt hyd 555 s#dha clerk in *an"

CROSS @OIN This !ill "ives the cross 'rod#ct. +34


SQL1

select em'no)ename)Ko*)dname)loc rom em' cross Koin de't2 +&BNO +N,&+ @ODN,&+ LOC (((((((((( (((((((((( (((((((((( (((((((((( (((((((((( 666 saketh analyst mkt hyd 555 s#dha clerk mkt hyd 888 Ka"an mana"er mkt hyd 444 madh# en"ineer mkt hyd 666 saketh analyst in *an" 555 s#dha clerk in *an" 888 Ka"an mana"er in *an" 444 madh# en"ineer in *an" 666 saketh analyst hr *om*ay 555 s#dha clerk hr *om*ay 888 Ka"an mana"er hr *om*ay 444 madh# en"ineer hr *om*ay

OUT+R @OIN O#ter Koin "ives the non(matchin" records alon" !ith matchin" records.
L+CT OUT+R @OIN

This !ill dis'lay the all matchin" records and the records !hich are in le t hand side ta*le those that are not in ri"ht hand side ta*le. +34 select em'no)ename)Ko*)dname)loc rom em' e le t o#ter Koin de't d on$e.de'tnoId.de'tno%2 Or SQL1 select em'no)ename)Ko*)dname)loc rom em' e)de't d !here e.de'tnoId.de'tno$H%2
SQL1

+&BNO (((((((((( 666 888 555 444


RI:<T OUT+R @OIN

+N,&+ (((((((((( saketh Ka"an s#dha madh#

@ODN,&+ LOC (((((((((( (((((((((( (((((((((( analyst mkt hyd mana"er mkt hyd clerk in *an" en"ineer

This !ill dis'lay the all matchin" records and the records !hich are in ri"ht hand side ta*le those that are not in le t hand side ta*le.

Copy rights are reserved.

+34
SQL1

68

select em'no)ename)Ko*)dname)loc rom em' e ri"ht o#ter Koin de't d on$e.de'tnoId.de'tno%2 Or SQL1 select em'no)ename)Ko*)dname)loc rom em' e)de't d !here e.de'tno$H% I d.de'tno2 +&BNO (((((((((( 666 888 555
CULL OUT+R @OIN

+N,&+ @ODN,&+ LOC (((((((((( (((((((((( (((((((((( (((((((((( saketh analyst mkt hyd Ka"an mana"er mkt hyd s#dha clerk in *an" hr *om*ay

This !ill dis'lay the all matchin" records and the non(matchin" records rom *oth ta*les. +34
SQL1

select em'no)ename)Ko*)dname)loc rom em' e #ll o#ter Koin de't d on$e.de'tnoId.de'tno%2 +&BNO +N,&+ @ODN,&+ LOC (((((((((( (((((((((( (((((((((( (((((((((( (((((((((( 888 Ka"an mana"er mkt hyd 666 saketh analyst mkt hyd 555 s#dha clerk in *an" 444 madh# en"ineer hr *om*ay

INN+R @OIN This !ill dis'lay all the records that have matched. +34
SQL1 select em'no)ename)Ko*)dname)loc #sin"$de'tno%2

rom em' inner Koin de't

+&BNO (((((((((( 666 888 555

+N,&+ @ODN,&+ LOC (((((((((( (((((((((( (((((((((( (((((((((( saketh analyst mkt hyd Ka"an mana"er mkt hyd s#dha clerk in *an"

SU-QU+RI+S ,ND +/ISTS


SU-QU+RI+S Nestin" o V#eries) one !ithin the other is termed as a s#*V#ery. , statement containin" a s#*V#ery is called a 'arent V#ery. S#*V#eries are #sed to retrieve data rom ta*les that de'end on the val#es in the ta*le itsel .
T.B+S

Sin"le ro! s#*V#eries &#lti ro! s#*V#eries &#lti'le s#*V#eries

Copy rights are reserved.

69
Correlated s#*V#eries
SIN:L+ RO9 SU-QU+RI+S

In sin"le ro! s#*V#ery) it !ill ret#rn one val#e. +34


SQL1

select F rom em' !here sal 1 $select sal rom em' !here em'no I +&BNO +N,&+ @O&:R <IR+D,T+ S,L CO&&

WDEE%2 D+BTNO ((((((((((

(((((((((( (((((((((( ((((((((( (((((((((( (((((((((((( ((((((( (((((((((( WWXX WX8T WT75 SCOTT ,N,L.ST WDEE 6T(,BR(XW 8777 GIN: BR+SID+NT 6W(NO;(X6 D777 CORD ,N,L.ST WDEE 78(D+C(X6 8777 57 67 57

&ULTI RO9 SU-QU+RI+S

In m#lti ro! s#*V#ery) it !ill ret#rn more than one val#e. In s#ch cases !e sho#ld incl#de o'erators like any) all) in or not in *et!een the com'arision o'erator and the s#*V#ery.

+34

SQL1 select F rom em' !here sal 1 any $select sal rom em' !here sal *et!een 5D77 and 4777%2

((((((((((

+&BNO +N,&+ @O&:R <IR+D,T+ S,L CO&& D+BTNO (((((((((( (((((((((( ((((((((( (((((((((( ((((((((((( (((((((( (((((((((( WDEE WWXX WX8T WT75 @ON+S &,N,:+R WX8T 75(,BR(X6 5TWD SCOTT ,N,L.ST WDEE 6T(,BR(XW 8777 GIN: BR+SID+NT 6W(NO;(X6 D777 CORD ,N,L.ST WDEE 78(D+C(X6 8777 57 57 67 57

SQL1 select F rom em' !here sal 1 all $select sal rom em' !here sal *et!een 5D77 and 4777%2

+&BNO D+BTNO ((((((((((

+N,&+

@O-

&:R

<IR+D,T+

S,L

CO&&

(((((((((( (((((((((( ((((((((( (((((((((( ((((((((((((( (((((( (((((((((( WX8T GIN: BR+SID+NT 6W(NO;(X6 D777 67

&ULTIBL+ SU-QU+RI+S

There is no limit on the n#m*er o s#*V#eries incl#ded in a !here cla#se. It allo!s nestin" o a V#ery !ithin a s#*V#ery. +34
SQL1

select F rom em' !here sal I $select ma3$sal% rom em' !here sal 0 ma3$sal% rom em'%%2 +&BNO +N,&+ @O&:R <IR+D,T+ S,L CO&&

$select

D+BTNO

Copy rights are reserved.

(((((((((( (((((((((( ((((((((( (((((((((( (((((((((((( ((((((( (((((((((( (((((((((( WWXX WT75 SCOTT ,N,L.ST WDEE CORD ,N,L.ST WDEE 6T(,BR(XW 8777 78(D+C(X6 8777 57 57

70

CORR+L,T+D SU-QU+RI+S

, s#*V#ery is eval#ated once or the entire 'arent statement !here as a correlated s#*V#ery is eval#ated once or every ro! 'rocessed *y the 'arent statement. +34 SQL1 select distinct de'tno rom em' e !here D 0I $select co#nt$ename% rom em' !here e.de'tno I de'tno%2 D+BTNO (((((((((( 57 87 +/ISTS +3ists #nction is a test or e3istence. This is a lo"ical test or the ret#rn o ro!s rom a V#ery. +34 S#''ose !e !ant to dis'lay the de'artment n#m*ers !hich has more than 4 em'loyees. SQL1 select de'tno)co#nt$F% rom em' "ro#' *y de'tno havin" co#nt$F% 1 42 D+BTNO ((((((((( 57 87 COUNT$F% (((((((((( D E

Crom the a*ove V#ery can yo# !ant to dis'lay the names o em'loyees\ select de'tno)ename) co#nt$F% rom em' "ro#' *y de'tno)ename havin" co#nt$F% 1 42
SQL1

no ro!s selected The a*ove V#ery ret#rns nothin" *eca#se com*ination o de'tno and ename never ret#rn more than one co#nt. The sol#tion is to #se e3ists !hich ollo!s. select de'tno)ename rom em' e6 !here e3ists $select F rom em' e5 !here e6.de'tnoIe5.de'tno "ro#' *y e5.de'tno havin" co#nt$e5.ename% 1 4% order *y de'tno)ename2
SQL1

D+BTNO (((((((((( 57 57 57 57 57 87 87

+N,&+ (((((((((( ,D,&S CORD @ON+S SCOTT S&IT< ,LL+N -L,G+

Copy rights are reserved.

87 87 87 87 NOT +/ISTS

@,&+S &,RTIN TURN+R 9,RD

71

select de'tno)ename rom em' e6 !here not e3ists $select F rom em' e5 !here e6.de'tnoIe5.de'tno "ro#' *y e5.de'tno havin" co#nt$e5.ename% 1 4% order *y de'tno)ename2
SQL1

D+BTNO +N,&+ ((((((((( (((((((((( 67 CL,RG 67 GIN: 67 &ILL+R

9,LGUB TR++S ,ND INLIN+ ;I+9


9,LGUB TR++S Usin" hierarchical V#eries) yo# can retrieve data *ased on a nat#ral hierarchical relationshi' *et!een ro!s in a ta*le. <o!ever) !here a hierarchical relationshi' e3ists *et!een the ro!s o a ta*le) a 'rocess called tree !alkin" ena*les the hierarchy to *e constr#cted. +34
SQL1 select ename YY ?II1? YY 'rior ename) level rom em' start !ith ename I ?GIN:? connect *y 'rior em'noIm"r2

+N,&+YY?II1?YYBRIOR+N,& (((((((((((((((((((((((((((((((((((( GIN:II1 @ON+SII1GIN: SCOTTII1@ON+S ,D,&SII1SCOTT CORDII1@ON+S S&IT<II1CORD -L,G+II1GIN: ,LL+NII1-L,G+ 9,RDII1-L,G+ &,RTINII1-L,G+ TURN+RII1-L,G+ @,&+SII1-L,G+ CL,RGII1GIN: &ILL+RII1CL,RG

L+;+L (((((((( 6 5 8 4 8 4 5 8 8 8 8 8 5 8

In the a*ove Start !ith cla#se s'eci ies the root ro! o the ta*le. Level 'se#do col#mn "ives the 6 or root ) 5 or child and so on. Connect *y 'rior cla#se s'eci ies the col#mns !hich has 'arent(child relationshi'. INLIN+ ;I+9 OR TOB(N ,N,L.SIS In the select statement instead o ta*le name) re'lacin" the select statement is kno!n as inline vie!.

Copy rights are reserved.

72
+34
SQL1

Select ename) sal) ro!n#m rank rom $select F rom em' order *y sal%2 +N,&+ S,L R,NG (((((((((( (((((((((( (((((((((( S&IT< X77 6 @,&+S TD7 5 ,D,&S 6677 8 9,RD 65D7 4 &,RTIN 65D7 D &ILL+R 6877 E TURN+R 6D77 W ,LL+N 6E77 X CL,RG 54D7 T -L,G+ 5XD7 67 @ON+S 5TWD 66 SCOTT 8777 65 CORD 8777 68 GIN: D777 64

LOCGS
Locks are the mechanisms #sed to 'revent destr#ctive interaction *et!een #sers accessin" same reso#rce sim#ltaneo#sly. Locks 'rovides hi"h de"ree o data conc#rrency. T.B+S Ro! level locks Ta*le level locks

RO9 L+;+L LOCGS In the ro! level lock a ro! is locked e3cl#sively so that other cannot modi y the ro! #ntil the transaction holdin" the lock is committed or rolled *ack. This can *e done *y #sin" select.. or #'date cla#se. +34
SQL1

select F rom em' !here sal 1 8777 or #'date o comm.2

T,-L+ L+;+L LOCGS , ta*le level lock !ill 'rotect ta*le data there*y "#aranteein" data inte"rity !hen data is *ein" accessed conc#rrently *y m#lti'le #sers. , ta*le lock can *e held in several modes.
S<,R+ LOCG

Share lock Share #'date lock +3cl#sive lock

, share lock locks the ta*le allo!in" other #sers to only V#ery *#t not insert) #'date or delete ro!s in a ta*le. &#lti'le #sers can 'lace share locks on the same reso#rce at the same time. +34
SQL1

lock ta*le em' in share mode2

Copy rights are reserved.

S<,R+ UBD,T+ LOCG

73

It locks ro!s that are to *e #'dated in a ta*le. It 'ermits other #sers to conc#rrently V#ery) insert ) #'date or even lock other ro!s in the same ta*le. It 'revents the other #sers rom #'datin" the ro! that has *een locked. +34
SQL1

lock ta*le em' in share #'date mode2

+/CLUSI;+ LOCG

+3cl#sive lock is the most restrictive o ta*les locks. 9hen iss#ed *y any #ser) it allo!s the other #ser to only V#ery. It is similar to share lock *#t only one #ser can 'lace e3cl#sive lock on a ta*le at a time. +34
SQL1

lock ta*le em' in share e3cl#sive mode2

NO9,IT I one #ser locked the ta*le !itho#t no!ait then another #ser tryin" to lock the same ta*le then he has to !ait #ntil the #ser !ho has initially locked the ta*le iss#es a commit or roll*ack statement. This delay co#ld *e avoided *y a''endin" a no!ait cla#se in the lock ta*le command. +34
SQL1

lock ta*le em' in e3cl#sive mode no!ait.

D+,DLOCG , deadlock occ#rs !hen t!o #sers have a lock each on se'arate o*Kect) and they !ant to acV#ire a lock on the each other=s o*Kect. 9hen this ha''ens) the irst #ser has to !ait or the second #ser to release the lock) *#t the second #ser !ill not release it #ntil the lock on the irst #ser=s o*Kect is reed. In s#ch a case) oracle detects the deadlock a#tomatically and solves the 'ro*lem *y a*ortin" one o the t!o transactions.

IND+/+S
Inde3 is ty'ically a listin" o key!ords accom'anied *y the location o in ormation on a s#*Kect. 9e can create inde3es e3'licitly to s'eed #' SQL statement e3ec#tion on a ta*le. The inde3 'oints directly to the location o the ro!s containin" the val#e. 9<. IND+/+S\ Inde3es are most #se #l on lar"er ta*les) on col#mns that are likely to a''ear in !here cla#ses as sim'le eV#ality. T.B+S UniV#e inde3 Non(#niV#e inde3 -tree inde3 -itma' inde3 Com'osite inde3 Reverse key inde3 C#nction(*ased inde3 Descendin" inde3 Domain inde3 O*Kect inde3 Cl#ster inde3 Te3t inde3 Inde3 or"aniUed ta*le

Copy rights are reserved.

74
Bartition inde3 Local inde3 Local 're i3ed Local non('re i3ed :lo*al inde3 :lo*al 're i3ed :lo*al non('re i3ed

UNIQU+ IND+/ UniV#e inde3es "#arantee that no t!o ro!s o a ta*le have d#'licate val#es in the col#mns that de ine the inde3. UniV#e inde3 is a#tomatically created !hen 'rimary key or #niV#e constraint is created. +34
SQL1

create #niV#e inde3 st#dNind on st#dent$sno%2

NON(UNIQU+ IND+/ Non(UniV#e inde3es do not im'ose the a*ove restriction on the col#mn val#es. +34
SQL1

create inde3 st#dNind on st#dent$sno%2

-TR++ IND+/ or ,SC+NDIN: IND+/ The de a#lt ty'e o inde3 #sed in an oracle data*ase is the *tree inde3. , *tree inde3 is desi"ned to 'rovide *oth ra'id access to individ#al ro!s and V#ick access to "ro#'s o ro!s !ithin a ran"e. The *tree inde3 does this *y 'er ormin" a s#ccession o val#e com'arisons. +ach com'arison eliminates many o the ro!s. +34
SQL1

create inde3 st#dNind on st#dent$sno%2

-IT&,B IND+/ This can *e #sed or lo! cardinality col#mns4 that is col#mns in !hich the n#m*er o distinct val#es is snall !hen com'ared to the n#m*er o the ro!s in the ta*le. +34
SQL1

create *itma' inde3 st#dNind on st#dent$se3%2

CO&BOSIT+ IND+/ , com'osite inde3 also called a concatenated inde3 is an inde3 created on m#lti'le col#mns o a ta*le. Col#mns in a com'osite inde3 can a''ear in any order and need not *e adKacent col#mns o the ta*le. +34
SQL1

create *itma' inde3 st#dNind on st#dent$sno) sname%2

R+;+RS+ G+. IND+/ , reverse key inde3 !hen com'ared to standard inde3) reverses each *yte o the col#mn *ein" inde3ed !hile kee'in" the col#mn order. 9hen the col#mn is inde3ed in reverse mode then the col#mn val#es !ill *e stored in an inde3 in di erent *locks as the startin" val#e di ers. S#ch an arran"ement can hel'

Copy rights are reserved.

avoid 'er ormance de"radations in inde3es !here modi ications to the inde3 are concentrated on a small set o *locks. +34
SQL1

75

create inde3 st#dNind on st#dent$sno) reverse%2

9e can re*#ild a reverse key inde3 into normal inde3 #sin" the noreverse key!ord. +34
SQL1

alter inde3 st#dNind re*#ild noreverse2

CUNCTION -,S+D IND+/ This !ill #se res#lt o the #nction as key instead o #sin" col#mn as the val#e or the key. +34
SQL1

create inde3 st#dNind on st#dent$#''er$sname%%2

D+SC+NDIN: IND+/ The order #sed *y -(tree inde3es has *een ascendin" order. .o# can cate"oriUe data in -(tree inde3 in descendin" order as !ell. This eat#re can *e #se #l in a''lications !here sortin" o'erations are reV#ired. +34
SQL1

create inde3 st#dNind on st#dent$sno desc%2

T+/T IND+/ Q#eryin" te3t is di erent rom V#eryin" data *eca#se !ords have shades o meanin") relationshi's to other !ords) and o''osites. .o# may !ant to search or !ords that are near each other) or !ords that are related to thers. These V#eries !o#ld *e e3tremely di ic#lt i all yo# had availa*le !as the standard relational o'erators. -y e3tendin" SQL to incl#de te3t inde3es) oracle te3t 'ermits yo# to ask very com'le3 V#estions a*o#t the te3t. To #se oracle te3t) yo# need to create a te&t inde& on the col#mn in !hich the te3t is stored. Te3t inde3 is a collection o ta*les and inde3es that store in ormation a*o#t the te3t stored in the col#mn.
T.B+S

There are several di erent ty'es o inde3es availa*le in oracle Ti. The irst) CONT+/T is s#''orted in oracle Xi as !ell as oracle Ti. ,s o oracle Ti) yo# can #se the CT/C,T te3t inde3 o #rther enhance yo#r te3t inde3 mana"ement and V#ery ca'a*ilities.
CONT+/T CT/C,T CT/RUL+

The CT/C,T inde3 ty'e s#''orts the transactional synchroniUation o data *et!een the *ase ta*le and its te3t inde3. 9ith CONT+/T inde3es) yo# need to man#ally tell oracle to #'date the val#es in the te3t inde3 a ter data chan"es in *ase ta*le. CT/C,T inde3 ty'es do not "enerate score val#es d#rin" the te3t V#eries.
<O9 TO CR+,T+ T+/T IND+/\

Copy rights are reserved.

.o# can create a te3t inde3 via a s'ecial version o the create inde3 comman. Cor conte3t inde3) s'eci y the ct3sys.conte3t inde3 ty'e and or ct3cat inde3) s'eci y the ct3sys.ct3cat inde3 ty'e. +34 S#''ose yo# have a ta*le called -OOGS !ith the ollo!in" col#mns Title) ,#thor) In o.
SQL1 SQL1

76

create inde3 *ookNinde3 on *ooks$in o% inde3ty'e is ct3sys.conte3t2 create inde3 *ookNinde3 on *ooks$in o% inde3ty'e is ct3sys.ct3cat2

T+/T QU+RI+S

Once a te3t inde3 is created on the in o col#mn o ca'a*ilities increase dynamically.


CONT,INS > C,TS+,RC< CONT,INS

-OOGS

ta*le) te3t(searchin"

#nction takes t!o 'arameters ] the col#mn name and the search

strin". Synta34 Contains$inde&ed_column, searc'_str%2 I yo# create a CT/C,T inde3) #se the C,TS+,RC< #nction in 'lace o CONT,INS. C,TS+,RC< takes three 'arameters ] the col#mn name) the search strin" and the inde3 set. Synta34 Contains$inde&ed_column, searc'_str, inde&_set%2
<O9 , T+/T Q+UR. 9ORGS\

9hen a #nction s#ch as CONT,INS or C,TS+,RC< is #sed in V#ery) the te3t 'ortion o the V#ery is 'rocessed *y oracle te3t. The remainder o the V#ery is 'rocessed K#st like a re"#lar V#ery !ithin the data*ase. The res#lt o the te3t V#ery 'rocessin" and the re"#lar V#ery 'rocessin" are mer"ed to ret#rn a sin"le set o records to the #ser.
S+,RC<IN: COR ,N +/,CT &,TC< OC , 9ORD

The ollo!in" V#eries !ill search "reater than Uero.


SQL1 SQL1

or a !ord called L'r'erty= !hose score is

select F rom *ooks !here contains$in o) L'ro'erty=% 1 72 select F rom *ooks !here catsearch$in o) L'ro'erty=) n#ll% 1 72

S#''ose i yo# !ant to kno! the score o the L'ro'erty= in each *ook) i score val#es or individ#al searches ran"e rom 7 to 67 or each occ#rrence o the strin" !ithin the te3t then #se the score #nction.
SQL1

select title) score$67% rom *ooks !here contains$in o) L'ro'erty=) 67% 1 72

S+,RC<IN: COR ,N +/,CT &,TC< OC &ULTIBL+ 9ORDS

The ollo!in" V#eries !ill search or t!o !ords.


SQL1 SQL1

72

select F rom *ooks !here contains$in o) L'ro'erty ,ND harvests=% 1 72 select F rom *ooks !here catsearch$in o) L'ro'erty ,ND harvests=) n#ll% 1

Copy rights are reserved.

Instead o #sin" ,ND yo# co#ld hae #sed an am'ersand$>%. -e ore #sin" this method) set de ine o so the > character !ill not *e seen as 'art o a varia*le name. set de ine o select F rom *ooks !here contains$in o) L'ro'erty > harvests=% 1 72 SQL1 select F rom *ooks !here catsearch$in o) L'ro'erty harvests=) n#ll% 1 72
SQL1 SQL1

77

The ollo!in" V#eries !ill search or more than t!o !ords. select F rom *ooks !here contains$in o) L'ro'erty ,ND harvests ,ND !orkers=% 1 72 SQL1 select F rom *ooks !here catsearch$in o) L'ro'erty harvests !orkers=) n#ll% 1 72
SQL1

The ollo!in" V#eries !ill search or either o the t!o !ords.


SQL1

select F rom *ooks !here contains$in o) L'ro'erty

OR

harvests=% 1 72

Instead o OR yo# can #se a vertical line $Y%. SQL1 select F rom *ooks !here contains$in o) L'ro'erty Y harvests=% 1 72 SQL1 select F rom *ooks !here catsearch$in o) L'ro'erty Y harvests=) n#ll% 1 72 In the ollo!in" V#eries the ,CCU&$acc#m#late% o'erator adds to"ether the scores o the individ#al searches and com'ares the acc#m#lated score to the threshold val#e.
SQL1 SQL1

select F rom *ooks !here contains$in o) L'ro'erty ,CCU& harvests=% 1 72 select F rom *ooks !here catsearch$in o) L'ro'erty ,CCU& harvests=) n#ll% yo# can #se a comma$)%.

1 72 Instead o
SQL1 SQL1 OR

select F rom *ooks !here contains$in o) L'ro'erty ) harvests=% 1 72 select F rom *ooks !here catsearch$in o) L'ro'erty ) harvests=) n#ll% 1 72

In the ollo!in" V#eries the &INUS o'erator s#*tracts the score o the second term=s search rom the score o the irst term=s search.
SQL1 SQL1

select F rom *ooks !here contains$in o) L'ro'erty &INUS harvests=% 1 72 select F rom *ooks !here catsearch$in o) L'ro'erty NOT harvests=) n#ll% 1 yo# can #se ] and instead o yo# can #se ^.

72 Instead o
SQL1 SQL1 &INUS NOT

select F rom *ooks !here contains$in o) L'ro'erty ( harvests=% 1 72 select F rom *ooks !here catsearch$in o) L'ro'erty ^ harvests=) n#ll% 1 72

S+,RC<IN: COR ,N +/,CT &,TC< OC , B<R,S+

The ollo!in" V#eries !ill search or the 'hrase. I the search 'hrase incl#des a reserved !ord !ithin oracle te3t) the yo# m#st #se c#rly *races $_`% to enclose te3t.
SQL1

select F rom *ooks !here contains$in o) Ltransactions _and` inances=% 1

72

select F rom *ooks !here catsearch$in o) Ltransactions _and` inances=) n#ll% 1 72


SQL1

.o# can enclose the entire 'hrase !ithin c#rly *races) in !hich case any reserved !ords !ithin the 'hrase !ill *e treated as 'art o the search criteria.

Copy rights are reserved.

SQL1

select F rom *ooks !here contains$in o) L_transactions and inances`=% 1

78

72

select F rom *ooks !here catsearch$in o) L_transactions and inances`=) n#ll% 1 72


SQL1 S+,RC<IN: COR 9ORDS T<,T ,R+ N+,R +,C< OT<+R

The ollo!in" V#eries !ill search or the !ords that are in *et!een the search terms.
SQL1

select F rom *ooks !here contains$in o) L!orkers


N+,R

N+,R

harvests=% 1 72

Instead o
SQL1

yo# can #se 2.

select F rom *ooks !here contains$in o) L!orkers 2 harvests=% 1 72 !ords

In CONT+/T inde3 V#eries) yo# can s'eci y the ma3im#m n#m*er o *et!een the search terms.
SQL1

72

select F rom *ooks !here contains$in o) LN+,R$$!orkers) harvests%)67%= 1

USIN: 9ILDC,RDS DURIN: S+,RC<+S

.o# can #se !ildcards to e3'and the list o valid search terms #sed d#rin" yo#r V#ery. @#st as in re"#lar te3t(strin" !ildcard 'rocessin") t!o !ildcards are availa*le. M N
SQL1 SQL1

( (

'ercent si"n2 m#lti'le(character !ildcard #nderscore2 sin"le(character !ildcard

select F rom *ooks !here contains$in o) L!orkerM=% 1 72 select F rom *ooks !here contains$in o) L!orkNNN=% 1 72

S+,RC<IN: COR 9ORDS T<,T S<,R+ T<+ S,&+ ST+&

Rather e3'and the list Sam'le Blay


SQL1

than #sin" !ildcards) yo# can #se stem(e3'ansion ca'a*ilities to the list o te3t strin"s. :iven the Lstem= o a !ord) oracle !ill e3'and o !ords to search or to incl#de all !ords havin" the same stem. e3'ansions are sho! here. 'lays 'layin" 'layed 'lay #l

select F rom *ooks !here contains$in o) Lamana"e=% 1 72

S+,RC<IN: COR CU[[. &,TC<+S

, #UUy match e3'ands the s'eci ied search term to incl#de !ords that are s'elled similarly *#t that do not necessarily have the same !ord stem. C#UUy matches are most hel' #l !hen the te3t contains miss'ellin"s. The miss'ellin"s can *e either in the searched te3t or in the search strin" s'eci ied *y the #ser d#rin" the V#ery. The ollo!in" V#eries !ill not ret#rn anythin" *eca#se its search does not contain the !ord Lhardest=.
SQL1

select F rom *ooks !here contains$in o) Lhardest=% 1 72

It does) ho!ever) contains the !ord Lharvest=. , #UUy match !ill ret#rn the *ooks containin" the !ord Lharvest= even tho#"h Lharvest= has a di erent !ord stem thant the !ord #sed as the search term.

Copy rights are reserved.

To #se a #UUy match) 'recede the search term !ith a V#estion mark) !ith no s'ace *et!een the V#estion mark and the *e"innin" o the search term.
SQL1

79

select F rom *ooks !here contains$in o) L\hardest=% 1 72

S+,RC<IN: COR 9ORDS T<,T SOUND LIG+ OT<+R 9ORDS

SOUND+/) e3'ands search terms *ased on ho! the !ord so#nds. The SOUND+/ e3'ansion method #ses the same te3t(matchin" lo"ic availa*le via the SOUND+/ #nction in SQL. To #se the SOUND+/ o'tion) yo# m#st 'recede the search term !ith an e3clamation mark$J%.
SQL1

select F rom *ooks !here contains$in o) LJ"rate=% 1 72

IND+/ S.NC<RONI[,TION

9hen #sin" CONT+/T inde3es) yo# need to mana"e the te3t inde3 contents2 the te3t inde3es are not #'dated !hen the *ase ta*le is #'dated. 9hen the ta*le !as #'dated) its te3t inde3 is o#t o sync !ith the *ase ta*le. To sync o the inde3) e3ec#te the S.NCNIND+/ 'roced#re o the CT/NDDL 'acka"e.
SQL1

e3ec

CT/NDDL.S.NCNIND+/$L*ookNinde3=%2

IND+/ S+TS

<istorically) 'ro*lems !ith V#eries o te3t inde3es have occ#rred !hen other criteria are #sed alon"side te3t searches as 'art o the !here cla#se. To im'rove the mi3ed V#ery ca'a*ility) oracle eat#res inde3 sets. The inde3es !ithin the inde3 set may *e str#ct#red relational col#mns or on te3t col#mns. To create an inde3 set) #se the CT/NDDL 'acka"e to create the inde3 set and add inde3es to it. 9hen yo# create a te3t inde3) yo# can then s'eci y the inde3 set it *elon"s to.
SQL1

e3ec CT/NDDL.CR+,T+NIND+/NS+T$L*ooksNinde3Nset=%2

The add non(te3t inde3es.


SQL1

e3ec

CT/NDDL.,DDNIND+/$L*ooksNinde3Nset=)

LtitleNinde3=%2

No! create a CT/C,T te3t inde3. S'eci y ct3sys.ct3cat as the inde3 ty'e) and list the inde3 set in the 'arameters cla#se. create inde3 *ookNinde3 on *ooks$in o% inde3ty'e is ct3sys.ct3cat 'arameters$Linde3 set *ooksNinde3Nset=%2
SQL1

IND+/(OR:,NI[+D T,-L+ ,n inde3(or"aniUed ta*le kee's its data sorted accordin" to the 'rimary key col#mn val#es or the ta*le. Inde3(or"aniUed ta*les store their data as i the entire ta*le !as stored in an inde3. ,n inde3(or"aniUed ta*le allo!s yo# to store the entire ta*le=s data in an inde3. +34 SQL1 create ta*le st#dent $sno n#m*er$5%)sname varchar$67%)smarks n#m*er$8% constraint 'k 'rimary key$sno% or"aniUation inde32 B,RTITION IND+/

Copy rights are reserved.

Similar to 'artitionin" ta*les) oracle allo!s yo# to 'artition inde3es too. Like ta*le 'artitions) inde3 'artitions co#ld *e in di erent ta*les'aces.
LOC,L IND+/+S

80

+34

Local key!ord tells oracle to create a se'arte inde3 or each 'artition. In the local 're i3ed inde3 the 'artition key is s'eci ied on the le t 're i3. 9hen the #nderlyin" ta*le is 'artitioned *aes on) say t!o col#mns then the inde3 can *e 're i3ed on the irst col#mn s'eci ied. Local 're i3ed inde3es can *e #niV#e or non #niV#e. Local inde3es may *e easier to mana"e than "lo*al inde3es. create inde3 st#dNinde3 on st#dent$sno% local2

SQL1

:LO-,L IND+/+S

+34

, "lo*al inde3 may contain val#es rom m#lti'le 'artitions. ,n inde3 is "lo*al 're i3ed i it is 'artitioned on the le t 're i3 o the inde3 col#mns. The "lo*al cla#se allo!s yo# to create a non('artitioned inde3. :lo*al inde3es may 'er orm #niV#eness checks aster than local $'artitioned% inde3es. .o# cannot create "lo*al inde3es or hash 'artitions or s#*'artitions. create inde3 st#dNinde3 on st#dent$sno% "lo*al2

SQL1

Similar to ta*le 'artitions) it is 'ossi*le to move them rom one device to another. -#t #nlike ta*le 'artitions) movement o inde3 'artitions reV#ires individ#al reconstr#ction o the inde3 or each 'artition $only in the case o "lo*al inde3%. +34
SQL1

alter inde3 st#dNind re*#ild 'artition '5

Inde3 'artitions cannot *e dro''ed man#ally. They are dro''ed im'licitly !hen the data they re er to is dro''ed rom the 'artitioned ta*le.

&ONITORIN: US+ OC IND+/+S Once yo# t#rned on the monitorin" the #se o !hether the ta*le is hittin" the inde3 or not. inde3es) then !e can check

To monitor the #se o inde3 #se the oll!in" synta3. Synta34 alter inde3 inde&_name monitorin" #sa"e2 then check or the details in ;aO-@+CTNUS,:+ vie!. I yo# !ant to sto' monitorin" #se the ollo!in". Synta34 alter inde3 inde&_name nomonitorin" #sa"e2 D,T, &OD+L ,LLNIND+/+S

Copy rights are reserved.

81
D-,NIND+/+S US+RNIND+/+S ,LLNIND(COLU&NS D-,(INDNCOLU&NS US+RNINDNCOLU&NS ,LLNB,RTNIND+/+S D-,NB,RTNIND+/+S US+RNB,RTNIND+/+S ;aO-@+CTNUS,:+

SQLFBLUS CO&&N,NDS
These commands does not reV#ire statement terminator and a''lica*le to the sessions ) those !ill *e a#tomatically cleared !hen session !as closed. -R+,G This !ill *e #sed to *reak#' the data de'endin" on the "ro#'in". Synta34 -reak or *re Oon 0col#mn_name1 on re'ortP CO&BUT+ This !ill *e #sed to 'er orm "ro#' #nctions on the data. Synta34 Com'#te or com' O"ro#'N #nction o column_name on breakin _column_name or re'ortP TTITL+ This !ill "ive the to' title or yo#r re'ort. .o# can on or o the ttitle.

Synta34 Ttitle or ttit Ole t Y center Y ri"htP title_name ski' n ot'er_c'aracters Ttitle or ttit Oon or o P -TITL+ This !ill "ive the *ottom title or yo#r re'ort. .o# can on or o the *title.

Synta34 -title or *tit Ole t Y center Y ri"htP title_name ski' n ot'er_c'aracters -title or *tit Oon or o P +34 SQL1 *re on de'tno ski' 6 on re'ort SQL1 com' s#m o sal on de'tno SQL1 com' s#m o sal on re'ort SQL1 ttitle center ?+&BLO.++ D+T,ILS? ski'6 center ?((((((((((((((((? SQL1 *title center ?FF T<,NGQ FF? SQL1 select F rom em' order *y de'tno2 O#t'#t4 +&BLO.++ D+T,ILS (((((((((((((((((((((((

Copy rights are reserved.

82
+&BNO (((((((((( WWX5 WX8T WT84 FFFFFFFFFF W8ET WXWE WT75 WWXX WDEE FFFFFFFFFF W4TT WETX WED4 WT77 WX44 WD56 FFFFFFFFFF s#m FF T<,NGQ FF CL+,R This !ill clear the e3istin" *# ers or *reak or com'#tations or col#mns ormattin". Synta34 Clear or cle *# er Y *re Y com' Y col2 +34 clear *# er -# er cleared SQL1 clear *re -reaks cleared SQL1 clear com' Com'#tes cleared SQL1 clear col Col#mns cleared
SQL1

+N,&+ @O&:R <IR+D,T+ S,L CO&& D+BTNO (((((((((( ((((((((( ((((((( (((((((((((((( (((((((( (((((((((( (((((((((( CL,RG &,N,:+R WX8T 7T(@UN(X6 54D7 67 GIN: BR+SID+NT 6W(NO;(X6 D777 &ILL+R CL+RG WWX5 58(@,N(X5 6877 (((((((((( XWD7 S&IT< CL+RG ,D,&S CL+RG CORD ,N,L.ST SCOTT ,N,L.ST @ON+S &,N,:+R WT75 6W(D+C(X7 WWXX 58(&,.(XW WDEE 78(D+C(X6 WDEE 6T(,BR(XW WX8T 75(,BR(X6 X77 6677 8777 8777 5TWD (((((((((( 67XWD ,LL+N S,L+S&,N -L,G+ &,N,:+R &,RTIN S,L+S&,N @,&+S CL+RG TURN+R S,L+S&,N 9,RD S,L+S&,N WETX 57(C+-(X6 WX8T 76(&,.(X6 WETX 5X(S+B(X6 WETX 78(D+C(X6 WETX 7X(S+B(X6 WETX 55(C+-(X6 6E77 5XD7 65D7 TD7 6D77 65D7 (((((((((( T477 (((((((((( 5T75D 877 6477 7 D77 s#m s#m 57

s#m 87

C<,N:+ This !ill *e #sed to re'lace any strin"s in Synta34


SQL

statements.

Chan"e or cAold_strin Anew_strin re'eats many times then new_strin re'laces the irst strin"

I the old_strin only.

Copy rights are reserved.

+34 select F rom det2 select F rom det F +RROR at line 64 OR,(77T454 ta*le or vie! does not e3ist
SQL1

83

cAdetAde't 6F select F rom de't SQL1 A


SQL1

D+BTNO DN,&+ (((((((((( (((((((((((((((( 67 ,CCOUNTIN: 57 R+S+,RC< 87 S,L+S 47 OB+R,TIONS COLU&N

LOC ((((((((((( N+9 .ORG ,LL,S C<IC,:O -OSTON

This !ill *e #sed to increase or decrease the !idth o the ta*le col#mns. Synta34 Col#mn or col 0column_name1 ormat 0num_%ormatYte&t_%ormat1 +34
SQL1 SQL1

col de'tno ormat TTT col dname ormat a67

S,;+ This !ill *e #sed to save yo#r c#rrent SQL statement as SQL Scri't ile. Synta34 Save or sav 0%ile_name1.Oe3tensionP re'lace or re' I yo# !ant to save the ilename !ith e3istin" ilename the yo# have to #se re'lace o'tion. -y de a#lt it !ill take s-l as the e3tension. +34 SQL1 save ss Created ile ss.sVl SQL1 save ss re'lace 9rote ile ss.sVl +/+CUT+ This !ill *e #sed to e3ec#te stored s#*'ro"rams or 'acka"ed s#*'ro"rams. Synta34 +3ec#te or e3ec 0subpro ram_name1 +34
SQL1

e3ec sam'leN'roc

SBOOL This !ill record the data !hen yo# s'ool on) #'to !hen yo# say s'ool o . -y de a#lt it !ill "ive lst as e3tension. Synta34

Copy rights are reserved.

S'ool on Y o +34
SQL1 SQL1

Y o#t Y 0%ile_name1.O+3tensionP

84

s'ool on select F rom de't2 D+BTNO DN,&+ LOC ((((((((( (((((((((((((( (((((((((( 67 ,CCOUNTIN: N+9 .ORG 57 R+S+,RC< D,LL,S 87 S,L+S C<IC,:O 47 OB+R,TIONS -OSTON

SQL1 SQL1

s'ool o ed on.lst
SQL1

select F rom de't2

D+BTNO DN,&+ LOC ((((((((( (((((((((((((( (((((((((( 67 ,CCOUNTIN: N+9 .ORG 57 R+S+,RC< D,LL,S 87 S,L+S C<IC,:O 47 OB+R,TIONS -OSTON
SQL1

s'ool o

LIST This !ill "ive the c#rrent


SQL

statement.

Synta34 List or li Ostart_line_numberP Oend_line_numberP +34 select 5 F 8 rom 4 de't2 SQL1 list 6 select 5 F 8 rom 4F de't SQL1 list 6 6F select SQL1 list 8 8F rom
SQL1 SQL1

list 6 8 6 select 5 F 8F rom

INBUT This !ill insert the ne! line to the c#rrent Synta34 In'#t or in 0strin 1
SQL

statement.

Copy rights are reserved.

+34 select F list 6F select F SQL1 in'#t rom de't SQL1 list 6 select F 5F rom de't
SQL1 SQL1

85

,BB+ND This !ill adds a ne! strin" to the e3istin" strin" in the any s'ace. Synta34 ,''end or a'' 0strin 1 +34 select F list 6F select F SQL1 a''end rom de't 6F select F rom de't
SQL1 SQL1 SQL1 SQL

statement !itho#t

list 6F select F rom de't

D+L+T+ This !ill delete the c#rrent


SQL

statement lines.

Synta34 Delete or del 0start_line_number1 O0end_line_number1P +34 select F rom de't !here de'tno 1672 SQL1 list 6 select 5 F 8 rom 4 de't D !here E de'tno WF 167 SQL1 del 6 SQL1 list 6 F 5 rom 8 de't 4 !here D de'tno EF 167 SQL1 del 5 SQL1 list
SQL1

5 8 4 D E W

Copy rights are reserved.

6 F 5 de't 8 !here 4 de'tno DF 167 SQL1 del 5 4 SQL1 list 6 F 5F 167 SQL1 del SQL1 list 6 F ;,RI,-L+ This !ill *e #sed to declare a varia*le. Synta34 ;aria*le or var 0variable_name1 0variable_type1 +34
SQL1 SQL1

86

var de'tNname varchar$6D% select dname into de'tNname rom de't !here de'tno I 672

BRINT This !ill *e #sed to 'rint the o#t'#t o the varia*les that !ill *e declared at level. Synta34 Brint 0variable_name1 +34
SQL1 SQL

'rint de'tNname D+BTNN,&+ (((((((((((((( ,CCOUNTIN:

ST,RT This !ill *e #sed to e3ec#te


SQL

scri'ts.

Synta34 start 0 ilename_name1.sVl +34


SQL1 SQL1

start ss.sVl @ss.sVl

(( this !ill e3ec#te sVl scri't iles only.

<OST This !ill *e #sed to interact !ith the Synta34 <ost OoperationP +34
SQL1 SQL1 OS

level rom

SQL.

host host dir

Copy rights are reserved.

S<O9 Usin" this) yo# can see several commands that #se the set command and stat#s. Synta34 Sho! all Y 0set_command1 +34 sho! all a''in o is OCC and set to bSQLFBl#sb arraysiUe 6D a#tocommit OCC a#to'rint OCC a#torecovery OCC a#totrace OCC *lockterminator b.b $he3 5e% *title OCC and is the irst e! characters o the ne3t S+L+CT statement cmdse' OCC colse' b b com'ati*ility version N,TI;+ concat b.b $he3 5e% co'ycommit 7 COB.T.B+C<+CG is ON de ine b>b $he3 5E% descri*e D+BT< 6 LIN+NU& OCC IND+NT ON echo OCC edit ile ba iedt.*# b em*edded OCC esca'e OCC C++D-,CG ON or E or more ro!s la""er OCC l#sh ON
SQL1

87

sho veri y veri y OCC


SQL1

RUN This !ill r#ns the command in the *# er. Synta34 R#n Y A +34
SQL1 SQL1

r#n A

STOR+ This !ill save all the set command stat#ses in a ile. Synta34 Store set 0 ilename1.Oe3tensionP OcreateP Y Ore'laceP Y Oa''endP +34 store set myNsettin"s.scmd Created ile myNsettin"s.scmd SQL1 store set myNsettin"s.cmd re'lace 9rote ile myNsettin"s.cmd SQL1 store set myNsettin"s.cmd a''end ,''ended ile to myNsettin"s.cmd
SQL1

Copy rights are reserved.

88
COLDN,CT+R This !ill old the col#mns one a ter the other. Synta34 Col#mn 0column_name1 oldNa ter Ono_o%_linesP +34
SQL1 SQL1 SQL1 SQL1 SQL1

col de'tno oldNa ter 6 col dname oldNa ter 6 col loc oldNa ter 6 set headin" o select F rom de't2 67 ,CCOUNTIN: N+9 .ORG 57 R+S+,RC< D,LL,S 87 S,L+S C<IC,:O 47 OB+R,TIONS -OSTON

COLDN-+COR+ This !ill old the col#mns one *e ore the other. Synta34 Col#mn 0column_name1 oldN*e ore Ono_o%_linesP D+CIN+ This !ill "ive the list o all the varia*les c#rrently de ined. Synta34 De ine Ovaria*leNnameP +34 de ine D+CIN+ ND,T+ I b6E(&,.(7Wb $C<,R% D+CIN+ NCONN+CTNID+NTICI+R I boracleb $C<,R% D+CIN+ NUS+R I bSCOTTb $C<,R% D+CIN+ NBRI;IL+:+ I bb $C<,R% D+CIN+ NSQLBLUSNR+L+,S+ I b6776777577b $C<,R% D+CIN+ N+DITOR I bNote'adb $C<,R% D+CIN+ NON;+RSION I bOracle Data*ase 67" +nter'rise +dition Release 67.6.7.5.7 ] Brod#ction 9ith the Bartitionin") OL,B and Data &inin" o'tionsb $C<,R% D+CIN+ NONR+L+,S+ I b6776777577b $C<,R%
SQL1

S+T CO&&,NDS
Copy rights are reserved.

89
These commands does not reV#ire statement terminator and a''lica*le to the sessions ) those !ill *e a#tomatically cleared !hen session !as closed. LIN+SI[+ This !ill *e #sed to set the linesiUe. De a#lt linesiUe is X7. Synta34 Set linesiUe 0value1 +34
SQL1

set linesiUe 677

B,:+SI[+ This !ill *e #sed to set the 'a"esiUe. De a#lt 'a"esiUe is 64. Synta34 Set 'a"esiUe 0value1 +34
SQL1

set 'a"esiUe 87

D+SCRI-+ This !ill *e #sed to see the o*Kect=s str#ct#re. Synta34 Descri*e or desc 0ob!ect_name1 +34
SQL1

desc de't

Name N#ll\ Ty'e ((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((((( ((((((((((((((((((((( D+BTNO NOT NULL NU&-+R$5% DN,&+ ;,RC<,R5$64% LOC ;,RC<,R5$68% B,US+ 9hen the dis'layed data contains h#ndreds or tho#sands o lines) !hen yo# select it then it !ill a#tomatically scrolls and dis'lays the last 'a"e data. To 'revent this yo# can #se this 'a#se o'tion. -y #sin" this it !ill dis'lay the data corres'oindin" to the 'a"esiUe !ith a *reak !hich !ill contin#e *y hittin" the ret#rn key. -y de a#lt this !ill *e o . Synta34 Set 'a#se on Y o +34
SQL1

set 'a#se on

C++D-,CG This !ill "ive the in ormation re"ardin" ho!many ro!s yo# selected the o*Kect. -y de a#lt the eed*ack messa"e !ill *e dis'layed) only !hen the o*Kect contains more than D ro!s.

Copy rights are reserved.

Synta34 Set eed*ack 0value1 +34


SQL1 SQL1

90

set eed*ack 4 select F rom de't2

D+BTNO DN,&+ LOC (((((((((( (((((((((((((( ((((((((((((( 67 ,CCOUNTIN: N+9 .ORG 57 R+S+,RC< D,LL,S 87 S,L+S C<IC,:O 47 OB+R,TIONS -OSTON 4 ro!s selected. <+,DIN: I yo# !ant to dis'lay data !itho#t headin"s) then yo# can achieve !ith this. -y de a#lt headin" is on. Synta34 Set headin" on Y o +34
SQL1 SQL1

set headin" o select F rom de't2 67 57 87 47 ,CCOUNTIN: R+S+,RC< S,L+S OB+R,TIONS N+9 .ORG D,LL,S C<IC,:O -OSTON

S+R;+ROUTBUT This !ill *e #sed to dis'lay the o#t'#t o the BLASQL 'ro"rams. -y de a#lt this !ill *e o . Synta34 Set servero#t'#t on Y o +34
SQL1

set servero#t'#t on

TI&+ This !ill *e #sed to dis'lay the time. -y de a#lt this !ill *e o . Synta34 Set time on Y o +34 set time on 6T4DE488 SQL1
SQL1

TI&IN: This !ill "ive the time taken to e3ec#te the c#rrent this !ill *e o . Synta34 Set timin" on Y o
SQL

statement. -y de a#lt

Copy rights are reserved.

91
+34
SQL1 SQL1

set timin" on select F rom de't2

D+BTNO DN,&+ LOC (((((((((( (((((((((((((( ((((((((((((( 67 ,CCOUNTIN: N+9 .ORG 57 R+S+,RC< D,LL,S 87 S,L+S C<IC,:O 47 OB+R,TIONS -OSTON +la'sed4 77477477.7E SQLBRO&BT This !ill *e #sed to chan"e the Synta34 Set sVl'rom't 0prompt1 +34
SQL1 set OR,CL+1 SQL

'rom't.

sVl'rom't ?OR,CL+1?

SQLC,S+ This !ill *e #sed to chan"e the case o the is mi3ed. Synta34 Set sVlcase #''er Y mi3ed Y lo!er +34
SQL1 SQL

statements. -y de a#lt the case

set sVlcase #''er

SQLT+R&IN,TOR This !ill *e #sed to chan"e the terminator o the terminator is 2.


SQL

statements. -y de a#lt the

Synta34 Set sVlterminator 0termination_c'aracter1 +34


SQL1 SQL1

set sVlterminator 4 select F rom de't4

D+CIN+ -y de a#lt i the > character inds then it !ill treat as *ind varia*le and ask or the in'#t. S#''ose yo#r !ant to treat it as a normal character !hile insertin" data) then yo# can 'revent this *y #sin" the de ine o'tion. -y de a#lt this !ill *e on Synta34 Set de ine on Y o +34
SQL1insert

into de't val#es$D7)?R>D?)?<.D?%2

Copy rights are reserved.

+nter val#e or d4 old 64 insert into de't val#es$D7)?R>D?)?<.D?% ne! 64 INS+RT INTO D+BT ;,LU+S$D7)?R?)?<.D?%
SQL1 SQL1insert

92

val#e

set de ine o into de't val#es$D7)?R>D?)?<.D?%2

(( here it !on=t ask or

N+9B,:+ This !ill sho!s ho! many *lank lines !ill *e le t *e ore the re'ort. -y de a#lt it !ill leave one *lank line. Synta34 Set ne!'a"e 0value1 +34
SQL1

set ne!'a"e 67

The Uero val#e or ne!'a"e does not 'rod#ce Uero *lank lines instead it s!itches to a s'ecial 'ro'erty !hich 'rod#ces a to'(o ( orm character $he3 68% K#st *e ore the date on each 'a"e. &ost modern 'rinters res'ond to this *y movin" immediately to the to' o the ne3t 'a"e) !here the 'ritin" o the re'ort !ill *e"in. <+,DS+B This allo! yo# to indicate !here yo# !ant to *reak a 'a"e title or a col#mn headin" that r#ns lon"er than one line. The de a#lt headin" se'arator is vertical *ar $Y%. Synta34 Set headse' 0separation_c'ar1 +34
SQL1

select F rom de't2

D+BTNO DN,&+ LOC (((((((((( (((((((((((((( ((((((((((((( 67 ,CCOUNTIN: N+9 .ORG 57 R+S+,RC< D,LL,S 87 S,L+S C<IC,:O 47 OB+R,TIONS -OSTON
SQL1 SQL1 SQL1

set headset' J col dname headin" ?D+B,RT&+NT J N,&+? A LOC (((((((((( N+9 .ORG D,LL,S C<IC,:O -OSTON

D+B,RT&+NT D+BTNO N,&+ (((((((((( ((((((((((((((((( 67 ,CCOUNTIN: 57 R+S+,RC< 87 S,L+S 47 OB+R,TIONS +C<O

9hen #sin" a *ind varia*le) the this is o .

SQL

statement is maintained *y echo. -y de a#lt

Copy rights are reserved.

93
Synta34 Set echo on Y o ;+RIC. 9hen #sin" a *ind varia*le) the old and ne! statements !ill *e maintained *y veri y. -y de a#lt this is on. Synta34 Set veri y on Y o +34

select F rom de't !here de'tno I >dno2 +nter val#e or dno4 67 old 64 select F rom de't !here de'tno I >dno ne! 64 select F rom de't !here de'tno I 67
SQL1

D+BTNO DN,&+ LOC (((((((((( (((((((((((((((( ((((((((((( 67 ,CCOUNTIN: N+9 .ORG set veri y o select F rom de't !here de'tno I >dno2 +nter val#e or dno4 57
SQL1 SQL1

D+BTNO DN,&+ LOC (((((((((( ((((((((((((( ((((((((((( 57 R+S+,RC< D,LL,S BNO This !ill "ive dis'lays the 'a"e n#m*ers. -y de a#lt the val#e !o#ld *e Uero. +34
SQL1 SQL1 SQL1

col hiredate ne!Nval#e 3today no'rint ormat a6 tr#nc ttitle le t 3today ri"ht ?'a"e? sVl.'no select F rom em' !here de'tno I 672 'a"e 6

7T(@UN(X6

+&BNO +N,&+ @O&:R S,L CO&& D+BTNO (((((((((( (((((((((( ((((((((((((((( ((((((((( ((((( (((((((((( (((((((((( WWX5 CL,RG &,N,:+R WX8T 54D7 67 WX8T GIN: BR+SID+NT D777 67 WT84 &ILL+R CL+RG WWX5 6877 67 In the a*ove no'rint tells SQLBLUS not to dis'lay this col#mn !hen it 'rints the res#lts o the SQL statement. Dates that have *een re ormatted *y TONC<,R "et a de a#lt !idth o a*o#t 677 characters. -y chan"in" the ormat to a6 tr#nc) yo# minimiUe this e ect. N+9N;,LU+ inserts contents o the col#mn retrieved *y the SQL statement into a varia*le called 3today.

SB+CI,L CIL+S
LO:IN.sVl

Copy rights are reserved.

I yo# !o#ld like SQLBLUS to de ine yo#r o!n environmental settin"s) '#t all the reV#ired commands in a ile named lo"in.sVl. This is a s'ecial ilename that SQLBLUS al!ays looks or !henever it starts #'. I it inds lo"in.sVl) it e3ec#tes any commands in it as i yo# had entered then *y hand. .o# can '#t any command in lo"in.sVl that yo# can #se in SQLBLUS) incl#din" SQLBLUS commands and SQL statements. ,ll ot them e3ec#ted *e ore SQLBLUS "ives yo# the SQL1 'rom't. :LO:IN.sVl This is #sed in the same !ays as settin"s or all #sers o a data*ase.
LO:IN.sVl

94

*#t to esta*lish de a#lt

SQLBLUS

I&B QU+RI+S
6% To ind the nth ro! o a ta*le Select F rom em' !here ro!id I $select ma3$ro!id% rom em' !here ro!n#m 0I 4%2 Or SQL1 Select F rom em' !here ro!n#m 0I 4 min#s select F rom em' !here ro!n#m 0I 82
SQL1

5% To ind d#'licate ro!s Select F rom em' !here ro!id in $select ma3$ro!id% rom em' "ro#' *y em'no) ename) m"r) Ko*) hiredate) comm) de'tno) sal%2 Or SQL1 Select em'no)ename)sal)Ko*)hiredate)comm ) co#nt$F% rom em' "ro#' *y em'no)ename)sal)Ko*)hiredate)comm havin" co#nt$F% 1I62
SQL1

8% To delete d#'licate ro!s


SQL1

Delete em' !here ro!id in $select ma3$ro!id% rom em' "ro#' *y em'no)ename)m"r)Ko*)hiredate)sal)comm)de'tno%2

4% To ind the co#nt o d#'licate ro!s


SQL1

Select ename) co#nt$F% rom em' "ro#' *y ename havin" co#nt$F%

1I 62 D% <o! to dis'lay alternative ro!s in a ta*le\


SQL1 select F rom em' !here $ro!id)7% in $select ro!id)mod$ro!n#m)5% rom em'%2

E% :ettin" em'loyee details o each de'artment !ho is dra!in" ma3im#m sal\


SQL1

select F rom em' !here $de'tno)sal% in $ select de'tno)ma3$sal% rom em' "ro#' *y de'tno%2

W% <o! to "et n#m*er o em'loyees in each de'artment ) in !hich de'artment is havin" more than 5D77 em'loyees\
SQL1

15D772

Select de'tno)co#nt$F% rom em' "ro#' *y de'tno havin" co#nt$F%

Copy rights are reserved.

X% To reset the time to the *e"innin" o the day Select toNchar$tr#nc$sysdate%)=dd(mon(yyyy hh4mi4ss am=% rom

95

SQL1

d#al2

T% To ind nth ma3im#m sal


SQL1 Select F rom em' !here sal in $select ma3$sal% rom $select F rom em' order *y sal% !here ro!n#m 0I D%2

INTRODUCTION
C<,R,CT+RSTICS <i"hly str#ct#red) reada*le and accessi*le lan"#a"e. Standard and Brota*le lan"#a"e. +m*edded lan"#a"e. Im'roved e3ec#tion a#thority.

67" C+,TUR+S . .ptimi+ed compiler To chan"e the o'timiUer settin"s or the entire data*ase) set the data*ase 'arameter BLSQLNOBTI&I[+NL+;+L. ;alid settin"s are as ollo!s 7 ( No o'timiUation 6 ( &oderate o'timiUation 5 ( ,""ressive o'timiUation These settin"s are also modi ia*le or the c#rrent session. SQL1 alter session set 'lsVlNo'timUeNlevelI52 Oracle retains o'timiUer settin"s on a mod#le(*y(mod#le *asis. 9hen yo# recom'ile a 'artic#lar mod#le !ith nonde a#lt settin"s) the settin"s !ill stick allo!in" yo# to recom'ile later on #sin" R+US+ S+TTIN:S.
SQL1 SQL1

,lter 'roced#re 'roc com'ile 'lsVlNo'timiUeNlevelI62 ,lter 'roced#re 'roc com'ile re#se settin"s2

Compile/time warnin s. Startin" !ith oracle data*ase 67" release 6 yo# can ena*le additional com'ile(time !arnin"s to hel' make yo#r 'ro"rams more ro*#st. The com'iler can detect 'otential r#ntime 'ro*lems !ith yo#r code) s#ch as identi yin" lines o code that !ill never *e r#n. This 'rocess) also kno!n as lint c'eckin . To ena*le these !arnin"s o the entire data*ase) set the data*ase 'arameter BLSQLN9,RNIN:S. These settin"s are also modi ia*le or the c#rrent session. alter session set 'lsVlN!arnin"s I Lena*le4all=2 The a*ove can *e achieved #sin" the *#ilt(in 'acka"e
SQL1 D-&SN9,RNIN:.

Conditional compilation.

Copy rights are reserved.

96
Conditional com'ilation allo!s the com'iler to allo! to com'ile selected 'arts o a 'ro"ram *ased on conditions yo# 'rovide !ith the aIC directive.

"upport %or non/se-uential collections in 0.1233.


4mproved datatype support. 5acktrace an e&ception to its line number. 9hen handlin" an error) ho! can yo# ind the line n#m*er on !hich the error !as ori"inally raised\ In earlier release) the only !ay to do this !as allo! yo# e3ce'tion to "o #nhandled and then vie! the #ll error trace stack. No! yo# can call D-&SNUTILIT..COR&,TN+RRORN-,CGTR,C+ #nction to o*tain that stack and mani'#late it 'ro"rammatically !ithin yo#r 'ro"ram. "et operators %or nested tables. "upport %or re ular e&pressions. Oracle data*ase 67" s#''orts the #se o BLASQL code via o#r ne! *#ilt(in #nctions.
R+:+/BNLIG+ R+:+/BNINSTR R+:+/BNSU-STR R+:+/BNR+BL,C+

re"#lar e3'ressions inside

6ro rammer/de%ined -uotin

mec'anism.

Startin" !ith oracle data*ase 67" release 6) yo# can de ine yo#r o!n V#otin" mechanism or strin" literals in *oth SQL and BLASQL. Use the characters V=$V ollo!ed *y a sin"le V#ote% to note the 'ro"rammer(de ined deliemeter or yo# strin" literal. +34
D+CL,R+ -+:IN

v varchar$67% 4I ?com'#ter?2 d*msNo#t'#t.'#tNline$V?Fv I F? YY v%2 d*msNo#t'#t.'#tNline$V?av I a? YY v%2

+ND2

O#t'#t4 v I com'#ter v I com'#ter 7any new built/in packa es. D-&SNSC<+DUL+R Re'resents a maKor #'date to D-&SN@O-. D-&SNSC<+DUL+R 'rovides m#ch im'roved #nctionality or sched#lin" and e3ec#tin" Ko*s de ined via ced#res. D-&SNCR.BTO O ers the a*ility to encry't and decry't common oracle dataty'e) incl#din" R,9s) -LO-s) and CLO-s. It also 'rovides "lo*aliUation s#''ort or encry'tin" data across di erent charactersets.

Copy rights are reserved.

D-&SN&ONITOR Brovides an ,BI to control additional tracin" and statistics "atherin" o sessions.

97

D-&SN9,RNIN: Brovides an ,BI into the BLASQL com'iler !arnin"s mod#le) allo!in" yo# to read and chan"e settin"s that control !hich !arnin"s are s#''ressed) dis'layed) or treated as errors. ST,ND,RD B,CG,:+ Oracle has de ined in this s'ecial 'acka"e. Oracle de ines V#ite a e! identi iers in this 'acka"e) incl#din" *#ilt(in e3ce'tions) #nctions and s#*ty'es. .o# can re erence the *#ilt(in orm *y 're i3in" it !ith ST,ND,RD. The *asic #nit in any BLASQL 'ro"ram is *lock. ,ll BLASQL 'ro"rams are com'osed o *locks !hich can occ#r seV#entially or nested. -LOCG STRUCTUR+ Declare (( declarative section -e"in (( e3ec#ta*le section +3ce'tion (( e3ce'tion section +nd2 In the a*ove declarative and e3ce'tiona sections are o'tional. -LOCG T.B+S ,nonymo#s *locks Named *locks La*eled *locks S#*'ro"rams Tri""ers

,NON.&OUS -LOCGS

,nonymo#s *locks im'lies *asic *lock str#ct#re. +34


-+:IN +ND2

D*msNo#t'#t.'#tNline$L&y irst 'ro"ram=%4

L,-+L+D -LOCGS

La*eled *locks are anonymo#s *locks !ith a la*el !hich "ives a name to the *lock. +34 00myN*loock11
-+:IN +ND2

D*msNo#t'#t.'#tNline$L&y irst 'ro"ram=%4

Copy rights are reserved.

SU-BRO:R,&S

98

S#*'ro"rams are 'roced#res and #nctions. They can *e stored in the data*ase as stand(alone o*Kects) as 'art o 'acka"e or as methods o an o*Kect ty'e.
TRI::+RS

Tri""ers consists o a the data*ase. N+ST+D -LOCGS

BLASQL

*lock that is associated !ith an event that occ#r in

, *lock can *e nested !ithin the e3ec#ta*le or e3ce'tion section o an o#ter *lock. ID+NTICI+RS Identi iers are #sed to name BLASQL o*Kects) s#ch as varia*les) c#rsors) ty'es and s#*'ro"rams. Identi iers consists o a letter) o'tionally ollo!ed *y any seV#ence o characters) incl#din" letters) n#m*ers) dollar si"ns) #nderscores) and 'o#nd si"ns only. The ma3im#m len"th or an identi ier is 87 characters. QUOT+D ID+NTICI+RS I yo# !ant to make an identi ier case sensitive) incl#de characters s#ch as s'aces or #se a reserved !ord) yo# can enclose the identi ier in do#*le V#otation marks. +34
D+CL,R+

-+:IN

bab n#m*er 4I D2 b,b n#m*er 4I E2 d*msNo#t'#t.'#tNline$?a I ? YY a%2 d*msNo#t'#t.'#tNline$?, I ? YY ,%2

O#t'#t4

+ND2

aIE ,IE CO&&+NTS Comments im'rove reada*ility and make yo#r 'ro"ram more #nderstanda*le. They are i"nored *y the BLASQL com'iler. There are t!o ty'es o comments availa*le. Sin"le line comments &#ltiline comments

SIN:L+ LIN+ CO&&+NTS

, sin"le(line comment can start any 'oint on a line !ith t!o dashes and contin#es #ntil the end o the line. +34
-+:IN

D*msNo#t'#t.'#tNline$Lhello=%2

(( sam'le 'ro"ram

+ND2 &ULTILIN+ CO&&+NTS

&#ltiline comments start !ith the AF delimiter and ends !ith FA delimiter.

Copy rights are reserved.

+34
-+:IN +ND2

99
D*msNo#t'#t.'#tNline$Lhello=%2 AF sam'le 'ro"ram FA

;,RI,-L+ D+CL+R,TIONS ;aria*les can *e declared in declarative section o the *lock2 +34
D+CL,R+

a n#m*er2 * n#m*er 4I D2 c n#m*er de a#lt E2 CONST,NT D+CL+R,TIONS To declare a constant) yo# incl#de the a de a#lt val#e. +34
D+CL,R+ CONST,NT

key!ord) and yo# m#st s#''ly

* constant n#m*er 4I D2 c constant n#m*er de a#lt E2

NOT NULL CL,US+ .o# can also s'eci y that the varia*le m#st *e not n#ll. +34
D+CL,R+

* constant n#m*er not n#ll4I D2 c n#m*er not n#ll de a#lt E2 ,NC<OR+D D+CL+R,TIONS
BLASQL

o ers t!o kinds o achorin". Scalar anchorin" Record anchorin"

SC,L,R ,NC<ORIN:

Use the MT.B+ attri*#te to de ine yo#r varia*le *ased on ta*le=s col#mn o some other BLASQL scalar varia*le. +34
D+CL,R+

dno de't.de'tnoMty'e2 S#*ty'e tNn#m*er is n#m*er2 a tNn#m*er2 S#*ty'e tNsno is st#dent.snoMty'e2 ;Nsno tNsno2
R+CORD ,NC<ORIN:

Use the MRO9T.B+ attri*#te to de ine yo#r record str#ct#re *ased on a ta*le. +34
D+CL,R+

;Nde't de'tMro!ty'e2
-+N+CITS OC ,NC<OR+D D+CL,R,TIONS

Copy rights are reserved.

100
SynchroniUation !ith data*ase col#mns. NormaliUation o local varia*les.

BRO:R,&&+R(D+CIN+D T.B+S 9ith the SU-T.B+ statement) BLASQL allo!s yo# to de ine yo#r o!n s#*ty'es or aliases o 'rede ined dataty'es) sometimes re erred to as a*stract dataty'es. There are t!o kinds o s#*ty'es. Constrained Unconstrained
CONSTR,IN+D SU-T.B+

, s#*ty'e that restricts or constrains the val#es normally allo!d *y the dataty'e itsel . +34 S#*ty'e 'ositive is *inaryNinte"er ran"e 6..564W4X8E4W2 In the a*ove declaration a varia*le that is declared as 'ositive can store only in"e"er "reater than Uero even tho#"h *inaryNinte"er ran"es rom (564W4X8E4W..H564W4X8E4W. UNCONSTR,IN+D SU-T.B+ , s#*ty'e that does not restrict the val#es o the ori"inal dataty'e in varia*les declared !ith the s#*ty'e. +34 S#*ty'e loat is n#m*er2 D,T,T.B+ CON;+RSIONS can handle conversions *et!een di erent amilies amon" the dataty'es. Conversion can *e done in t!o !ays.
BLASQL

+3'licit conversion Im'licit conversion

+/BLICIT CON;+RSION

This can *e done #sin" the *#ilt(in #nctions availa*le.


I&BLICIT CON;+RSION

BLASQL !ill a#tomatically convert *et!een dataty'e amilies !hen 'ossi*le. +34
D+CL,R+ -+:IN +ND2

a varchar$67%2 select de'tno into a rom de't !here dnameI?,CCOUNTIN:?2

In the a*ove varia*le a is char ty'e and de'tno is n#m*er ty'e even tho#"h) oracle !ill a#tomatically converts the n#meric data into char ty'e assi"ns to the varia*le.
BLASQL

can a#tomatically convert *et!een Characters and n#m*ers Characters and dates

Copy rights are reserved.

;,RI,-L+ SCOB+ ,ND ;ISI-ILIT.

101

The sco'e o a varia*le is the 'ortion o the 'ro"ram in !hich the varia*le can *e accessed. Cor BLASQL varia*les) this is rom the varia*le declaration #ntil the end o the *lock. 9hen a varia*le "oes o#t o sco'e) the BLASQL en"ine !ill ree the memory #sed to store the varia*le. The visi*ility o a varia*le is the 'ortion o the 'ro"ram !here the varia*le can *e accessed !itho#t havin" to V#ali y the re erence. The visi*ility is al!ays !ithin the sco'e. I it is o#t o sco'e) it is not visi*le. +364
D+CL,R+ -+:IN (((((((( D+CL,R+ -+:IN

a n#m*er2

(( sco'e o a

* n#m*er2

(( sco'e o *

(((((
+ND2

((((((
+ND2

+354

D+CL,R+

a n#m*er2 * n#m*er2
-+:IN

(( a ) * availa*le here * char$67%2 (( a and char ty'e * is availa*le here

D+CL,R+ -+:IN +ND2 +ND2

(((((

+384

00myN*lock11
D+CL,R+

-+:IN

a n#m*er2 * n#m*er2
D+CL,R+ -+:IN

(( a ) * availa*le here * char$67%2 (( a and char ty'e * is availa*le here (( n#m*er ty'e * is availa*le #sin" 00myN*lock11.*

+ND2 +ND2

((((((

BLASQL CONTROL STRUCTUR+S has a variety o control str#ct#res that allo! yo# to control the *ehavio#r o the *lock as it r#ns. These str#ct#res incl#de conditional statements and loo's.
BLASQL

I (then(else Case Case !ith no else La*eled case Searched case

Copy rights are reserved.

102
Sim'le loo' 9hile loo' Cor loo' :oto and La*els

IC(T<+N(+LS+ Synta34 I 0condition11 then "e-uence o% statements8 +lsi 0condition11 then "e-uence o% statements8 SS +lse "e-uence o% statements8 +nd i 2 +34
D+CL,R+ -+:IN

dno n#m*er$5%2 select de'tno into dno rom de't !here dname I ?,CCOUNTIN:?2 i dno I 67 then d*msNo#t'#t.'#tNline$?Location is N+9 .ORG?%2 elsi dno I 57 then d*msNo#t'#t.'#tNline$?Location is D,LL,S?%2 elsi dno I 87 then d*msNo#t'#t.'#tNline$?Location is C<IC,:O?%2 else d*msNo#t'#t.'#tNline$?Location is -OSTON?%2 end i 2

+ND2

O#t'#t4 Location is C,S+ Synta34 Case test/variable 9hen value1 then se-uence o% statements8 9hen value2 then se-uence o% statements8 SS 9hen valuen then se-uence o% statements8 +lse se-uence o% statements8 +nd case2 +34
D+CL,R+ -+:IN N+9 .ORG

dno n#m*er$5%2 select de'tno into dno rom de't !here dname I ?,CCOUNTIN:?2 case dno !hen 67 then d*msNo#t'#t.'#tNline$?Location is N+9 .ORG?%2 !hen 57 then d*msNo#t'#t.'#tNline$?Location is D,LL,S?%2 !hen 87 then d*msNo#t'#t.'#tNline$?Location is C<IC,:O?%2 else

Copy rights are reserved.

d*msNo#t'#t.'#tNline$?Location is
+ND2

-OSTON?%2

103

end case2

O#t'#t4 Location is
N+9 .ORG

C,S+ 9IT<OUT +LS+ Synta34 Case test/variable 9hen value1 then se-uence o% statements8 9hen value2 then se-uence o% statements8 SS 9hen valuen then se-uence o% statements8 +nd case2 +34
D+CL,R+ -+:IN

dno n#m*er$5%2 select de'tno into dno rom de't !here dname I ?,CCOUNTIN:?2 case dno !hen 67 then d*msNo#t'#t.'#tNline$?Location is N+9 .ORG?%2 !hen 57 then d*msNo#t'#t.'#tNline$?Location is D,LL,S?%2 !hen 87 then d*msNo#t'#t.'#tNline$?Location is C<IC,:O?%2 !hen 47 then d*msNo#t'#t.'#tNline$?Location is -OSTON?%2 end case2

+ND2

O#t'#t4 Location is L,-+L+D C,S+ Synta34 00label11 Case test/variable 9hen value1 then se-uence o% statements8 9hen value2 then se-uence o% statements8 SS 9hen valuen then se-uence o% statements8 +nd case2 +34
D+CL,R+ -+:IN N+9 .ORG

dno n#m*er$5%2 select de'tno into dno rom de't !here dname I ?,CCOUNTIN:?2 00myNcase11 case dno !hen 67 then d*msNo#t'#t.'#tNline$?Location is N+9 .ORG?%2 !hen 57 then d*msNo#t'#t.'#tNline$?Location is D,LL,S?%2 !hen 87 then

Copy rights are reserved.

+ND2

d*msNo#t'#t.'#tNline$?Location is !hen 47 then d*msNo#t'#t.'#tNline$?Location is end case myNcase2

C<IC,:O?%2 -OSTON?%2

104

O#t'#t4 Location is S+,RC<+D C,S+ Synta34 Case 9hen 0condition11 then se-uence o% statements8 9hen 0condition21 then se-uence o% statements8 SS 9hen 0conditionn1 then se-uence o% statements8 +nd case2 +34
D+CL,R+ -+:IN N+9 .ORG

dno n#m*er$5%2 select de'tno into dno rom de't !here dname I ?,CCOUNTIN:?2 case dno !hen dno I 67 then d*msNo#t'#t.'#tNline$?Location is N+9 .ORG?%2 !hen dno I 57 then d*msNo#t'#t.'#tNline$?Location is D,LL,S?%2 !hen dno I 87 then d*msNo#t'#t.'#tNline$?Location is C<IC,:O?%2 !hen dno I 47 then d*msNo#t'#t.'#tNline$?Location is -OSTON?%2 end case2

+ND2

O#t'#t4 Location is SI&BL+ LOOB Synta34 Loo' "e-uence o% statements8 +3it !hen 0condition12 +nd loo'2 In the synta3 e3it !hen 0condition1 is eV#ivalent to I 0condition1 then +3it2 +nd i 2 +34
D+CL,R+ -+:IN N+9 .ORG

i n#m*er 4I 62 loo' d*msNo#t'#t.'#tNline$?i I ? YY i%2 i 4I i H 62 e3it !hen i 1 D2 end loo'2

+ND2

Copy rights are reserved.

105
O#t'#t4 i i i i i I I I I I 6 5 8 4 D

9<IL+ LOOB Synta34 9hile 0condition1 loo' "e-uence o% statements8 +nd loo'2 +34
D+CL,R+ -+:IN

i n#m*er 4I 62 9hile i 0I D loo' d*msNo#t'#t.'#tNline$?i I ? YY i%2 i 4I i H 62 end loo'2

+ND2

O#t'#t4

i i i i i

I I I I I

6 5 8 4 D

COR LOOB Synta34 Cor 0loop_counter_variable1 in low_bound..'i '_bound loo' "e-uence o% statements8 +nd loo'2 +364
-+:IN

+ND2

Cor i in 6..D loo' d*msNo#t'#t.'#tNline$?i I ? YY i%2 end loo'2 6 5 8 4 D

O#t'#t4

i i i i i

I I I I I

+354
-+:IN

Cor i in reverse 6..D loo' d*msNo#t'#t.'#tNline$?i I ? YY i%2 end loo'2

O#t'#t4

+ND2

iID iI4 iI8

Copy rights are reserved.

iI5 iI6 NULL ST,T+&+NT

106

Us#ally !hen yo# !rite a statement in a 'ro"ram) yo# !ant it to do somethin". There are cases) ho!ever) !hen yo# !ant to tell BLASQL to do a*sol#tely nothin") and that is !here the NULL comes. The NULL statement deos nothin" e3ce't 'ass control to the ne3t e3ec#ta*le statement. .o# can #se NULL statement in the ollo!in" sit#ations. Im'rovin" 'ro"ram reada*ility. Sometimes) it is hel' #l to avoid any am*i"#ity inherent in an IC statement that doesn=t cover all 'ossi*le cases. Cor e3am'le) !hen yo# !rite an IC statement) yo# do not have to incl#de an +LS+ cla#se. N#lli yin" a raised e3ce'tion. 9hen yo# don=t !ant to !rite any s'ecial code to handle an e3ce'tion) yo# can #se the NULL statement to make s#re that a raised e3ce'tion halts e3ec#tion o the c#rrent BLASQL *lock *#t does not 'ro'a"ate any e3ce'tions to enclosin" *locks. Usin" n#ll a ter a la*el. In some cases) yo# can 'air NULL !ith :OTO to avoid havin" to e3ec#te additional statements. Cor e3am'le) I #se a :OTO statement to V#ickly move to the end o my 'ro"ram i the state o my data indicates that no #rther 'rocessin" is reV#ired. -eca#se I do not have to do anythin" at the termination o the 'ro"ram) I 'lace a NULL statement a ter the la*el *eca#se at least one e3ec#ta*le statement is reV#ired there. +ven tho#"h NULL deos nothin") it is still an e3ec#ta*le statement.

:OTO ,ND L,-+LS Synta34 :oto label2 9here label is a la*el de ined in the BLASQL *lock. La*els are enclosed in do#*le an"le *rackets. 9hen a "oto statement is eval#ated) control immediately 'asses to the statement identi ied *y the la*el. +34
-+:IN

+ND2

Cor i in 6..D loo' d*msNo#t'#t.'#tNline$?i I ? YY i%2 i i I 4 then "oto e3itNloo'2 end i 2 end loo'2 00e3itNloo'11 N#ll2

O#t'#t4 i i i i I I I I 6 5 8 4

Copy rights are reserved.

R+STRICTIONS ON :OTO

107

It is ille"al to *ranch into an inner *lock) loo'. ,t least one e3ec#ta*le statement m#st ollo!. It is ille"al to *ranch into an i statement. It is ille"al to *ranch rom one i statement to another i statement. It is ille"al to *ranch rom e3ce'tion *lock to the c#rrent *lock.

BR,:&,S Bra"mas are com'iler directives. They serve as instr#ctions to the BLASQL com'iler. The com'iler !ill act on the 'ra"ma d#rin" the com'ilation o the *lock. Synta34
BR:,&, BLASQL

instruction_to_compiler.

o ers several 'ra"mas4


,UTONO&OUSNTR,NS,CTION +/C+BTIONNINIT R+STRICTNR+C+R+NC+S S+RI,LL.NR+US,-L+

SU-BRO:R,&S
BROC+DUR+S , 'roced#re is a mod#le that 'er orms one or more actions. Synta34 Broced#re Oschema.Pname O$parameter1 O)parameter2 SP%P Oa#thid de iner Y c#rrentN#serP is (( OdeclarationsP -e"in (( e3ec#ta*le statements O+3ce'tion (( e3ce'tion handlersP +nd OnameP2 In the a*ove aut'id cla#se de ines !hether the 'roced#re !ill e3ec#te #nder the a#thority o the de iner o the 'roced#re or #nder the a#thority o the c#rrent #ser. CUNCTIONS , #nction is a mod#le that ret#rns a val#e. Synta34 C#nction Oschema.Pname O$parameter1 O)parameter2 SP%P Ret#rn ret#rnNdataty'e Oa#thid de iner Y c#rrentN#serP OdeterministicP O'arallelNena*leP is (( OdeclarationsP -e"in (( e3ec#ta*le statements O+3ce'tion (( e3ce'tion handlersP +nd OnameP2

Copy rights are reserved.

In the a*ove aut'id cla#se de ines !hether the 'roced#re !ill e3ec#te #nder the a#thority o the de iner o the 'roced#re or #nder the a#thority o the c#rrent #ser.

108

,eterministic cla#se de ines) an o'timiUation hint that lets the system #se a saved co'y o the #nction=s ret#rn res#lt) i availa*le. The V#ety o'timiUer can choose !hether to #se the saved co'y or re(call the #nction. 6arallel_enable cla#se de ines) an o'timiUation hint that ena*les the #nction to *e e3ec#ted in 'arallel !hen called rom !ithin S+L+CT statement. B,R,&+T+R &OD+S In $De a#lt% O#t In o#t
IN

In 'arameter !ill act as pl9s-l constant.


OUT


IN OUT

O#t 'arameter !ill act as unintiali+ed variable. .o# cannot 'rovide a de a#lt val#e to an out 'arameter. ,ny assi"nments made to out 'arameter are rolled *ack !hen an e3ce'tion is raised in the 'ro"ram. ,n act#al 'arameter corres'ondin" to an out ormal 'arameter m#st *e a varia*le.

In o#t 'arameter !ill act as initiali+ed variable. ,n act#al 'arameter corres'ondin" to an in out ormal 'arameter m#st *e a varia*le.

D+C,ULT B,R,&+T+RS De a#lt Barameters !ill not allo! in the be innin and middle. .ut and 4n .ut 'arameters can not have de a#lt val#es. +34 'roced#re '$a in n#m*er de a#lt D) * in n#m*er de a#lt E) c in n#m*er de a#lt W% ] valid 'roced#re '$a in n#m*er) * in n#m*er de a#lt E) c in n#m*er de a#lt W% ] valild 'roced#re '$a in n#m*er) * in n#m*er) c in n#m*er de a#lt W% ] valild 'roced#re '$a in n#m*er) * in n#m*er de a#lt E) c in n#m*er% ] invalild 'roced#re '$a in n#m*er de a#lt D) * in n#m*er de a#lt E) c in n#m*er% ] invalild 'roced#re '$a in n#m*er de a#lt D) * in n#m*er) c in n#m*er% ] invalild NOT,TIONS Notations are o t!o ty'es. Bositional notation Name notation

9e can com*ine 'ositional and name notation *#t 'ositional notation can not *e ollo!ed *y the name notation.

Copy rights are reserved.

109
+34 S#''ose !e have a 'roced#re 'roc $a n#m*er n#m*er) c n#m*er% and !e have one ,nonymo#s *lock !hich contains v6) v5) and v82
SQL1 SQL1

e3ec 'roc $v6)v5)v8% e3ec 'roc $aI1v6)*I1v5)cI1v8%

(( Bositional notation (( Named notation

COR&,L ,ND ,CTU,L B,R,&+T+RS Barameters !hich are in callin" s#*'ro"ram are actual parameters. Barameters !hich are in called s#*'ro"ram are %ormal parameters. I any s#*'ro"ram !as called) once the call !as com'leted then the val#es o ormal Barameters are co'ied to the act#al 'arameters.

+364

CR+,T+ OR R+BL,C+ BROC+DUR+ S,&BL+ $a

in n#m*er)* o#t n#m*er)c in

o#t n#m*er% is
-+:IN

d*msNo#t'#t.'#tNline$?, ter call?%2 d*msNo#t'#t.'#tNline$?a I ? YY a YY? * I ? YY * YY ? c I ? YY c%2 * 4I 672 c 4I 572 d*msNo#t'#t.'#tNline$?, ter assi"nment?%2 d*msNo#t'#t.'#tNline$?a I ? YY a YY? * I ? YY * YY ? c I ? YY c%2
+ND S,&BL+2 D+CL,R+

v6 n#m*er 4I 42 v5 n#m*er 4I D2 v8 n#m*er 4I E2

-+:IN

d*msNo#t'#t.'#tNline$?-e ore call?%2 d*msNo#t'#t.'#tNline$?v6 I ? YY v6 YY ? v5 I ? YY v5 YY ? v8 I ? YY v8%2 sam'le$v6)v5)v8%2 d*msNo#t'#t.'#tNline$?, ter com'letion o call?%2 d*msNo#t'#t.'#tNline$?v6 I ? YY v6 YY ? v5 I ? YY v5 YY ? v8 I ? YY v8%2
+ND2

O#t'#t4

-e ore call v6 I 4 v5 I D v8 I E , ter call aI4*I cIE , ter assi"nment a I 4 * I 67 c I 57 , ter com'letion o call v6 I 4 v5 I 67 v8 I 57
CR+,T+ OR R+BL,C+ CUN$a

+354

in n#m*er)* o#t n#m*er)c in o#t n#m*er%

ret#rn n#m*er
-+:IN

IS

d*msNo#t'#t.'#tNline$?, ter call?%2 d*msNo#t'#t.'#tNline$?a I ? YY a YY ? * I ? YY * YY ? c I ? YY c%2 d*msNo#t'#t.'#tNline$?-e ore assi"nement Res#lt I ? YY $aFnvl$*)6%Fc%%2

Copy rights are reserved.

* 4I D2 c 4I W2 d*msNo#t'#t.'#tNline$?, ter assi"nment?%2 d*msNo#t'#t.'#tNline$?a I ? YY a YY ? * I ? YY * YY ? c I ? YY c%2 ret#rn $aF*Fc%2


+ND CUN2 D+CL,R+

110

-+:IN

v6 n#m*er 4I 62 v5 n#m*er 4I 52 v8 n#m*er 4I 82 v n#m*er2

d*msNo#t'#t.'#tNline$?-e ore call?%2 d*msNo#t'#t.'#tNline$?v6 I ? YY v6 YY ? v5 I ? YY v5 YY ? v8 I ? YY v8%2 v 4I #n$v6)v5)v8%2 d*msNo#t'#t.'#tNline$?, ter call com'leted?%2 d*msNo#t'#t.'#tNline$?v6 I ? YY v6 YY ? v5 I ? YY v5 YY ? v8 I ? YY v8%2 d*msNo#t'#t.'#tNline$?Res#lt I ? YY v%2
+ND2

O#t'#t4

-e ore call v6 I 6 v5 I 5 v8 I 8 , ter call aI6*I cI8 -e ore assi"nement Res#lt I 8 , ter assi"nment aI6*IDcIW , ter call com'leted v6 I 6 v5 I D v8 I W Res#lt I 8D

R+STRICTIONS ON COR&,L B,R,&+T+RS

-y declarin" !ith s'eci ied siUe in act#al 'arameters. -y declarin" ormal 'arameters !ith Mty'e s'eci ier.

USIN: NOCOB. +34 :ocopy is a hint) not a command. This means that the com'iler mi"ht silently decide that it can=t #l ill yo#r reV#est or a nocopy 'arameter. The co'yin" rom ormal to act#al can *e restricted *y iss#in" nocopy V#ali ier. To 'ass the o#t and in o#t 'arameters *y re erence #se noco'y V#ali ier.
CR+,T+ OR R+BL,C+ BROC+DUR+ BROC$a -+:IN (((( +ND BROC2

in o#t noco'y n#m*er% IS

C,LL ,ND +/+C Call is a


SQL

statement) !hich can *e #sed to e3ec#te s#*'ro"rams like e3ec.

Synta34 Call subpro ram_name$Oar ument_listP% Ointo 'ost_variableP2

Copy rights are reserved.

111
The 'arantheses are al!ays reV#ired) even i the s#*'ro"ram takes no ar"#ments. 9e can not #se call !ith out and in out 'arameters. Call is a SQL statement) it is not valid inside a BLASQL *lock2 The INTO cla#se is #sed or the o#t'#t varia*les o #nctions only. 9e can not #se Le3ec= !ith out or in out 'arameters. +3ec is not valid inside a BLASQL *lock2
CR+,T+ OR R+BL,C+ BROC IS -+:IN +ND BROC2 SQL1

+364

d*msNo#t'#t.'#tNline$?hello !orld?%2 call 'roc$%2 hello !orld in n#m*er)* in n#m*er%

O#t'#t4

+354
CR+,T+ OR R+BL,C+ BROC$a -+:IN +ND BROC2 IS

d*msNo#t'#t.'#tNline$?a I ? YY a YY ? * I ? YY *%2

O#t'#t4
SQL1

call 'roc$D)E%2 aID*IE

+384
CR+,T+ OR R+BL,C+ CUNCTION CUN R+TURN ;,RC<,R IS -+:IN +ND CUN2

ret#rn ?hello !orld?2

O#t'#t4 varia*le v varchar$57% call #n$% into 4v2 SQL1 'rint v hello !orld
SQL1 SQL1

C,LL -. R+C+R+NC+ ,ND C,LL -. ;,LU+ In 'arameters *y de a#lt call by re%erence !here as o#t and in o#t call by value. 9hen 'arameter 'assed *y re erence) a 'ointer to the act#al 'arameter is 'assed to the corres'ondin" ormal 'arameter. 9hen 'arameter 'assed *y val#e it co'ies the val#e o the act#al 'arameter to the ormal 'arameter. Call *y re erence is aster than the call *y val#e *eca#se it avoids the co'yin".

SU-BRO:R,&S O;+RLO,DIN: +34 Bossi*le !ith di erent n#m*er o 'arameters. Bossi*le !ith di erent ty'es o data. Bossi*le !ith same ty'e !ith o*Kects. Can not *e 'ossi*le !ith di erent ty'es o modes. 9e can overload local s#*'ro"rams also.

Copy rights are reserved.

SQL1 SQL1

create or re'lace ty'e t6 as o*Kect$a n#m*er%2A create or re'lace ty'e t6 as o*Kect$a n#m*er%2A
D+CL,R+

112

BROC+DUR+ B$m t6% IS -+:IN +ND B2 BROC+DUR+ B$n t5% IS -+:IN

i t6 4I t6$D%2 K t5 4I t5$D%2

d*msNo#t'#t.'#tNline$?a I ? YY m.a%2

d*msNo#t'#t.'#tNline$?* I ? YY n.*%2

+ND B2 BROC+DUR+ BRODUCT$a n#m*er)* n#m*er% IS -+:IN

d*msNo#t'#t.'#tNline$?Brod#ct o a)* I ? YY a F *%2

+ND BRODUCT2 BROC+DUR+ BRODUCT$a n#m*er)* n#m*er)c n#m*er% IS -+:IN +ND BRODUCT2 -+:IN

d*msNo#t'#t.'#tNline$?Brod#ct o a)* I ? YY a F * F c%2

'$i%2 '$K%2 'rod#ct$4)D%2 'rod#ct$4)D)E%2


+ND2

O#t'#t4 aID *ID Brod#ct o a)* I 57 Brod#ct o a)* I 657


-+N+CITS OC O;+RLO,DIN:

S#''ortin" many data com*inations Cittin" the 'ro"ram to the #ser.

R+STRICTIONS ON O;+RLO,DIN:

Overloaded 'ro"rams !ith 'arameter lists that di er only *y name m#st *e called #sin" named notation. The 'arameter list o overloaded 'ro"rams m#st di er *y more than 'arameter mode. ,ll o the overloaded 'ro"rams m#st *e de ined !ithin the same BLASQL sco'e or *lock. Overloaded #nctions m#st di er *y more than their ret#rn ty'e.

I&BORT,NT BOINTS ,-OUT SU-BRO:R,&S 9hen a stored s#*'ro"ram is created) it is stored in the data dictionary. The s#*'ro"ram is stored in com'ile orm !hich is kno!n as p/code in addition to the so#rce te3t. The '(code has all o the re erences in the s#*'ro"ram eval#ated) and the so#rce code is translated into a orm that is easily reada*le *y BLASQL en"ine. 9hen the s#*'ro"ram is called) the '(code is read rom the disk) i necessary) and e3ec#ted.

Copy rights are reserved.

113 Once it reads rom the disk) the '(code is stored in the shared 'ool
'ortion o the system "lo*al area $S:,%) !here it can *e accessed *y m#lti'le #sers as needed. Like all o the contents o the shared 'ool) '(code is a"ed o#t o the shared 'ool accordin" to a least recently #sed $LRU% al"orithm. S#*'ro"rams can *e local. Local s#*'ro"rams m#st *e declared in the declarative section o BLASQL *lock and called rom the e3ec#ta*le section. S#*'ro"rams can not have the declarative section se'arately. Stored s#*'ro"rams can have local s#*'ro"rams2 Local s#*'ro"rams also can have local s#*'ro"rams. I the s#*'ro"ram contains a varia*le !ith the same name as the col#mn name o the ta*le then #se the dot method to di erentiate $subpro ram_name.sal%. S#*'ro"rams can *e invalidated.

BROC+DUR+S ; CUNCTIONS Broced#res may ret#rn thro#"h o#t and in o#t 'arameters !here as #nction m#st ret#rn. Broced#res can not have ret#rn cla#se !here as #nctions m#st. 9e can #se call statement directly or e3ec#tin" 'roced#re !here as !e need to declare a varia*le in case o #nctions. C#nctions can #se in select statements !here as 'roced#res can not. C#nctions can call rom re'orts environment !here as 'roced#res can not. 9e can #se e3ec or e3ec#tin" 'roced#res !here as #nctions can not. C#nction can *e #sed in d*msNo#t'#t !here as 'roced#re can not. Broced#re call is a standalone e3ec#ta*le statement !here as #nction call is a 'art o an e3ec#ta*le statement.

STOR+D ; LOC,L SU-BRO:R,&S The stored s#*'ro"ram is stored in com'iled '(code in the data*ase) !hen the 'roced#re is called it does not have to *e com'iled. The local s#*'ro"ram is com'iled as 'art o its containin" *lock. I the containin" *lock is anonymo#s and is r#n m#lti'le times) the s#*'ro"ram has to *e com'iled each time. Stored s#*'ro"rams can *e called rom any *lock s#*mitted *y a #ser !ho has e3ec#te 'rivile"es on the s#*'ro"ram. Local s#*'ro"rams can *e called only rom the *lock containin" the s#*'ro"ram. -y kee'in" the stored s#*'ro"ram code se'arate rom the callin" *lock) the callin" *lock is shorter and easier to #nderstand. The local s#*'ro"ram and the callin" *lock are one and the same) !hich can lead to 'art con #sion. I a chan"e to the callin" *lock is made) the s#*'ro"ram !ill *e recom'iled as o the recom'ilation o the containin" *lock. The com'iled '(code can *e 'inned in the shared 'ool #sin" the D-&SNS<,R+DNBOOL Backa"e. This can im'rove 'er ormance. Local s#*'ro"rams cannot *e 'inned in the shared 'ool *y themselves. Stand alone stored s#*'ro"rams can not *e overloaded) *#t 'acka"ed s#*'ro"rams can *e overloaded !ithin the same 'acka"e. Local s#*'ro"rams can *e overloaded !ithin the same *lock.

Copy rights are reserved.

+364
CR+,T+ OR R+BL,C+ BROC+DUR+ B IS -+:IN +ND2

114

d*msNo#t'#t.'#tNline$?Stored s#*'ro"ram?%2

O#t'#t4 e3ec ' Stored s#*'ro"ram


SQL1

+354
D+CL,R+ BROC+DUR+ B IS -+:IN +ND2 -+:IN +ND2

d*msNo#t'#t.'#tNline$?Local s#*'ro"ram?%2

'2

O#t'#t4 Local s#*'ro"ram CO&BILIN: SU-BRO:R,&S

SQL1 SQL1

,lter 'roced#re B6 com'ile2 ,lter #nction C6 com'ile2

SU-BRO:R,&S D+B+ND+CI+S , stored s#*'ro"ram is marked as invalid in the data dictionary i it has com'ile errors. , stored s#*'ro"ram can also *ecome invalid i a DDL o'eration is 'er ormed on one o its de'endent o*Kects. I a s#*'ro"ram is invalidated) the BLASQL en"ine !ill a#tomatically attem't to recom'ile in the ne3t time it is called. I !e have t!o 'roced#res like B6 and B5 in !hich B6 de'ends on B5. I !e com'ile B5 then B6 is invalidated.

SU-BRO:R,&S D+B+ND+NCI+S IN R+&OT+ D,T,-,S+S

9e !ill call remote s#*'ro"ram #sin" connect strin" like B6@OR,CL+2


I !e have t!o 'roced#res like B6 and B5 in !hich B6 de'ends on B5 *#t B5 !as in remote data*ase. I !e com'ile B5 it !ill not invalidate B6 immediately *eca#se the data dictionary does not track remote de'endencies. Instead the validity o remote o*Kects is checked at r#ntime. 9hen B6 is called) the remote data dictionary is V#eried to determine the stat#s o B5. B6 and B5 are com'ared to see it B6 needs to *e recom'iled) there are t!o di erent methods o com'arision Timestam' &odel Si"nat#re &odel

TI&+ST,&B &OD+L This is the de a#lt model #sed *y oracle. 9ith this model) the timestam's o the last modi ications o o*Kects are the t!o

Copy rights are reserved.

com'ared. The last_ddl_time ield o user_ob!ects contains the timestam'. I the *ase o*Kect has a ne!er timestam' than the de'endent o*Kect) the de'endent o*Kect !ill *e recom'iled.
ISSU+S 9IT< T<IS &OD+L

115

I the o*Kects are in di erent time Uones) the com'arison is invalid. 9hen B6 is in a client side BLASQL en"ine s#ch as oracle orms) in this case it may not 'ossi*le to recom'ile B6) *eca#se the so#rce or it may not *e incl#ded !ith the orms.

SI:N,TUR+ &OD+L 9hen a 'roced#re is created) a si"nat#re is stored in the data dictionary in addition to the '(code. The si"nat#re encodes the ty'es and order o the 'arametes. 9hen B6 is com'iled the irst time) the si"nat#re o B5 is incl#ded. Th#s) B6 only needs to recom'iled !hen the si"nat#re o B5 chan"es. In order to #se the si"nat#re model) the 'arameter R+&OT+ND+B+ND+NCI+SN&OD+ m#st *e set to SI:N,TUR+. This is a 'arameter in the data*ase initialiUation ile.

T<R++ 9,.S OC S+TTIN: T<IS &OD+

,dd the line

R+&OT+ND+B+ND+NCI+SN&OD+ISI:N,TUR+ to the data*ase initialiUation ile. The ne3t time the data*ase is started) the mode !ill *e set to SI:N,TUR+ or all sessions. ,lter system set remoteNde'endenciesNmode I si"nat#re2 This !ill a ect the entire data*ase $all sessions% rom the time the statement is iss#ed. .o# m#st have the ,LT+R S.ST+& 'rivile"e to iss#e this command. ,lter session set remoteNde'endenciesNmode I si"nat#re2 This !ill only a ect yo#r session ISSU+S 9IT< T<IS &OD+L

Si"nat#res don=t "et modi ied i the de a#lt val#es o ormal 'arameters are chan"ed. S#''ose B5 has a de a#lt val#e or one o its 'arameters) and B6 is #sin" this de a#lt val#e. I the de a#lt in the s'eci ication or B5 is chan"ed) B6 !ill not *e recom'iled *y de a#lt. The old val#e or the de a#lt 'arameter !ill still *e #sed #ntil B6 is man#ally recom'iled. I B6 is callin" a 'acka"ed 'roced#re B5) and a ne! overloaded version o B5 is added to the remote 'acka"e) the si"nat#re is not chan"ed. B6 !ill still #se the old version $not the ne! overloaded one% #ntil B6 is recom'iled man#ally.

COR9,RD D+CL+R,TION -e ore "oin" to #se the 'roced#re in any other s#*'ro"ram or other *lock ) yo# m#st declare the 'rototy'e o the 'roced#re in declarative section. +364

Copy rights are reserved.

D+CL,R+ BROC+DUR+ B6 IS -+:IN

116

d*msNo#t'#t.'#tNline$?Crom 'roced#re '6?%2 '52


+ND B62 BROC+DUR+ B5 IS -+:IN

+ND B52 BROC+DUR+ B8 IS -+:IN +ND B82 -+:IN

d*msNo#t'#t.'#tNline$?Crom 'roced#re '5?%2 '82

d*msNo#t'#t.'#tNline$?Crom 'roced#re '8?%2

'62

+ND2

O#t'#t4

'52 F +RROR at line D4 OR,(7EDD74 line D) col#mn 64 BLS(778684 ?B5? not declared in this sco'e OR,(7EDD74 line D) col#mn 64 BLASQL4 Statement i"nored OR,(7EDD74 line 67) col#mn 64 BLS(778684 ?B8? not declared in this sco'e OR,(7EDD74 line 67) col#mn 64 BLASQL4 Statement i"nored
D+CL,R+ BROC+DUR+ B52 (( BROC+DUR+ B82 BROC+DUR+ B6 IS -+:IN

+354 or!ard declaration

+ND B62 BROC+DUR+ B5 IS -+:IN

d*msNo#t'#t.'#tNline$?Crom 'roced#re '6?%2 '52

d*msNo#t'#t.'#tNline$?Crom 'roced#re '5?%2 '82


+ND B52 BROC+DUR+ B8 IS -+:IN +ND B82 -+:IN +ND2

d*msNo#t'#t.'#tNline$?Crom 'roced#re '8?%2

'62

O#t'#t4 Crom 'roced#re '6 Crom 'roced#re '5 Crom 'roced#re '8 BRI;IL+:+S ,ND STOR+D SU-BRO:R,&S
+/+CUT+ BR+;IL+:+

Cor stored s#*'ro"rams and 'acka"es the relevant 'rivile"e is


I

+/+CUT+. #ser , had the 'roced#re called em'N'roc then #ser , "rants e3ec#te 'rivile"e on 'roced#re to #ser - !ith the ollo!in" command.

Copy rights are reserved.

:rant e3ec#te on em'N'roc to #ser -. Then #ser - can r#n the 'roced#re *y iss#in" SQL1 +3ec #ser ,.em'N'roc
SQL1

117

#ser, created the ollo!in" 'roced#re


CR+,T+ OR R+BL,C+ BROC+DUR+ B IS -+:IN

c#rsor is select F rom st#dent62 or v in c loo' insert into st#dent5 val#es$v.no)v.name)v.marks%2 end loo'2

+ND B2

#ser, "ranted e3ec#te 'rivile"e to #ser- #sin" SQL1 "rant e3ec#te on ' to #serThen #ser- e3ec#ted the 'roced#re SQL1 +3ec #ser,.' I s#''ose #ser- also havin" st#dent5 ta*le then !hich ta*le !ill 'o'#late !hether #ser,=s or #ser-=s. The ans!er is #ser,=s st#dent5 ta*le only *eca#se *y de a#lt the 'roced#re !ill e3ec#te #nder the 'rivli"e set o its o!ner. The a*ove 'roced#re is kno!n as de iner=s 'roced#re.
<O9 TO BOBUL,T+ US+R -=s T,-L+

Oracle introd#ces 4nvoker;s and ,e%iner;s ri 'ts. -y de a#lt it !ill #se the de iner=s ri"hts. ,n invoker=s ri"hts ro#tine can *e created *y #sin" ,UT<ID cla#se to 'o'#late the #ser-=s ta*le. It is valid or stand(alone s#*'ro"rams) 'acka"e s'eci ications) and o*Kect ty'e s'eci ications only. #ser, created the ollo!in" 'roced#re
CR+,T+ OR R+BL,C+ BROC+DUR+ B ,UT<ID CURR+NTNUS+R IS -+:IN

c#rsor is select F rom st#dent62 or v in c loo' insert into st#dent5 val#es$v.no)v.name)v.marks%2 end loo'2

+ND B2

Then "rant e3ec#te 'rivile"e on ' to #ser-. +3ec#tin" the 'roced#re *y #ser-) !hich 'o'#lates #ser-=s ta*le. The a*ove 'roced#re is called invoker=s 'roced#re. Instead o c#rrentN#ser o a#thid cla#se) i yo# #se de iner then it !ill *e called de iner= 'roced#re.
STOR+D SU-BRO:R,&S ,ND ROL+S

!e have t!o #sers saketh and s#dha in !hich saketh has st#dent ta*le and s#dha does not. S#dha is "oin" to create a 'roced#re *ased on st#dent ta*le o!ned *y saketh. -e ore doin" this saketh m#st "rant the 'ermissions on this ta*le to s#dha.

Copy rights are reserved.

118
conn sakethAsaketh "rant all on st#dent to s#dha2 then s#dha can create 'roced#re SQL1 conn s#dhaAs#dha
SQL1 SQL1 CR+,T+ OR R+BL,C+ BROC+DUR+ B IS -+:IN

c#rsor c is select F rom saketh.st#dent2 or v in c loo' d*msNo#t'#t.'#tNline$LNo I L YY v.no%2 end loo'2

+ND B2

here 'roced#re !ill *e created. I the same 'rivile"e !as "ranted thro#"h a role it !ont create the 'roced#re. +3amine the ollo!in" code conn sakethAsaketh create role sakethNrole2 SQL1 "rant all on st#dent to sakethNrole2 SQL1 "rant sakethNrole to s#dha2 then conn s#dhaAs#dha
SQL1 SQL1 CR+,T+ OR R+BL,C+ BROC+DUR+ B IS -+:IN

c#rsor c is select F rom saketh.st#dent2 or v in c loo' d*msNo#t'#t.'#tNline$LNo I L YY v.no%2 end loo'2

+ND B2

The a*ove code !ill raise error instead o creatin" 'roced#re . This is *eca#se o early *indin" !hich BLASQL #ses *y de a#lt in !hich re erences are eval#ated in com'ile time *#t !hen yo# are #sin" a role this !ill a ect immediately.
ISSU+S 9IT< IN;OG+R=S RI:<TS

In an invoker=s ri"hts ro#tine) e3ternal re erences in

SQL statements !ill *e resolved #sin" the caller=s 'rivile"e set. -#t re erences in BLASQL statements are still resolved #nder the o!ner=s 'rivile"e set.

TRI::+RS) ;I+9S ,ND IN;OG+R=S RI:<TS

, data*ase tri""er !ill al!ays *e e3ec#ted !ith de iner=s ri"hts and !ill e3ec#te #nder the 'rivile"e set o the schema that o!ns the tri""erin" ta*le. This is also tr#e or BLASQL #nction that is called rom a vie!. In this case) the #nction !ill e3ec#te #nder the 'rivile"e set o the vie!=s o!ner.

B,CG,:+S
, packa e is a container or related o*Kects. It has s'eci ication and *ody. +ach o them is stored se'arately in data dictionary.

Copy rights are reserved.

B,CG,:+ S.NT,/

119

Create or re'lace 'acka"e 0packa e_name1 is (( 'acka"e s'eci ication incl#des s#*'ro"rams si"nat#res) c#rsors and "lo*al or '#*lic varia*les. +nd 0packa e_name12 Create or re'lace 'acka"e *ody 0packa e_name1 is (( 'acka"e *ody incl#des *ody or all the s#*'ro"rams declared in the s'ec) 'rivate ;aria*les and c#rsors. -e"in (( initialiUation section +3ce'tion (( +3ce'tion handlin" seciton +nd 0packa e_name12 I&BORT,NT BOIN:S ,-OUT B,CG,:+S The irst time a 'acka"ed s#*'ro"ram is called or any re erence to a 'acka"ed varia*le or ty'e is made) the 'acka"e is instantiated. +ach session !ill have its o!n co'y o 'acka"ed varia*les) ens#rin" that t!o sessions e3ec#tin" s#*'ro"rams in the same 'acka"e #se di erent memory locations. In many cases initialiUation needs to *e r#n the irst time the 'acka"e is instantiated !ithin a session. This can *e done *y addin" initialiUation section to the 'acka"e *ody a ter all the o*Kects. Backa"es are stored in the data dictionary and can not *e local. Backa"ed s#*'ro"ram has an advanta"e over stand alone s#*'ro"ram. 9hen ever any re erence to 'acka"e) the !hole 'acka"e '(code !as stored in shared 'ool o S:,. Backa"e may have local s#*'ro"rams. .o# can incl#de a#thid cla#se inside the 'acka"e s'ec not in the *ody. The e3ec#tion section o a 'acka"e is kno! as initialiUation section. .o# can have an e3ce'tion section at the *ottom o a 'acka"e *ody. Backa"es s#*'ro"rams are not invalidated.

CO&BILIN: B,CG,:+S

SQL1 SQL1 SQL1

,lter 'acka"e BG: com'ile2 ,lter 'acka"e BG: com'ile s'eci ication2 ,lter 'acka"e BG: com'ile *ody2

B,CG,:+ D+B+ND+NCI+S The 'acka"e *ody de'ends on the some o*Kects and the 'acka"e header. The 'acka"e header does not de'end on the 'acka"e *ody) !hich is an advanta"e o 'acka"es. 9e can chan"e the 'acka"e *ody !ith o#t chan"in" the header.

B,CG,:+ RUNTI&+ ST,T+ Backa"e r#ntime state is di er or the ollo!in" 'acka"es. Serially re#sa*le 'acka"es Non serially re#sa*le 'acka"es
S+RI,LL. R+US,-L+ B,CG,:+S

Copy rights are reserved.

To

orce the oracle to #se serially re#sa*le version then incl#de BR,:&, S+RI,LL.NR+US,-L+ in *oth 'acka"e s'ec and *ody) +3amine the ollo!in" 'acka"e.
CR+,T+ OR R+BL,C+ B,CG,:+ BG: IS

120

'ra"ma seriallyNre#sa*le2 'roced#re em'N'roc2


+ND BG:2 CR+,T+ OR R+BL,C+ B,CG,:+ -OD. BG: IS

BROC+DUR+ +&BNBROC IS

'ra"ma seriallyNre#sa*le2 c#rsor c is select ename rom em'2 vNename em'.enameMty'e2 vN la" *oolean 4I tr#e2 vNn#mro!s n#m*er 4I 72

-+:IN

+ND +&BNBROC2 +ND BG:2 SQL1

i not cMiso'en then o'en c2 end i 2 !hile vN la" loo' etch c into vNename2 vNn#mro!s 4I vNn#mro!s H 62 i vNn#mro!s I D then vN la" 4I alse2 end i 2 d*msNo#t'#t.'#tNline$?+name I ? YY vNename%2 end loo'2

e3ec 'k".em'N'roc +name I S&IT< +name I ,LL+N +name I 9,RD +name I @ON+S +name I &,RTIN e3ec 'k".em'N'roc +name I S&IT< +name I ,LL+N +name I 9,RD +name I @ON+S +name I &,RTIN

SQL1

The a*ove 'acka"e dis'lays the same o#t'#t or each e3ec#tion even tho#"h the c#rsor is not closed. -eca#se the serially re#sa*le version resets the state o the c#rsor each time it !as called.

NON S+RI,LL . R+US,-L+ B,CG,:+S

This is the de a#lt version #sed *y the oracle) e3amine the ollo!in" 'acka"e.
CR+,T+ OR R+BL,C+ B,CG,:+ BG: IS

'roced#re em'N'roc2

+ND BG:2 CR+,T+ OR R+BL,C+ B,CG,:+ -OD. IS BROC+DUR+ +&BNBROC IS

c#rsor c is select ename rom em'2 vNename em'.enameMty'e2

Copy rights are reserved.

-+:IN

vN la" *oolean 4I tr#e2 vNn#mro!s n#m*er 4I 72

121

+ND +&BNBROC2 +ND BG:2 SQL1

i not cMiso'en then o'en c2 end i 2 !hile vN la" loo' etch c into vNename2 vNn#mro!s 4I vNn#mro!s H 62 i vNn#mro!s I D then vN la" 4I alse2 end i 2 d*msNo#t'#t.'#tNline$?+name I ? YY vNename%2 end loo'2

e3ec 'k".em'N'roc +name I S&IT< +name I ,LL+N +name I 9,RD +name I @ON+S +name I &,RTIN e3ec 'k".em'N'roc +name +name +name +name +name I I I I I
-L,G+ CL,RG SCOTT GIN: TURN+R

SQL1

The a*ove 'acka"e dis'lays the di erent o#t'#t or each e3ec#tion even tho#"h the c#rsor is not closed. -eca#se the non(serially re#sa*le version remains the state o the c#rsor over data*ase calls.

D+B+ND+NCI+S OC B,CG,:+ RUNTI&+ ST,T+

De'endencies can e3ists *et!een 'acka"e state and anonymo#s *locks. +3amine the ollo!in" 'ro"ram Create this 'acka"e in irst session
CR+,T+ OR R+BL,C+ B,CG,:+ BG: IS

+ND BG:2

v n#m*er 4I D2 'roced#re '2

CR+,T+ OR R+BL,C+ B,CG,:+ -OD. BG: IS BROC+DUR+ B IS -+:IN

d*msNo#t'#t.'#tNline$?v I ? YY v%2 v 4I 672 d*msNo#t'#t.'#tNline$?v I ? YY v%2


+ND B2 +ND BG:2

Connect to second session) r#n the ollo!in" code.


-+:IN +ND2

'k".'2

Copy rights are reserved.

The a*ove code !il !ork. :o *ack to irst session and recreate the 'acka"e #sin" create. Then connect to second session and r#n the ollo!in" code a"ain.
-+:IN +ND2

122

'k".'2

This a*ove code !ill not !ork *eca#se o the ollo!in". The anonymo#s *lock de'ends on 'k". This is com'ile time de'endency. There is also a r#ntime de'endency on the 'acka"ed varia*les) since each session has its o!n co'y o 'acka"ed varia*les. Th#s !hen 'k" is recom'iled the r#ntime de'endency is ollo!ed) !hich invalidates the *lock and raises the oracle error. R#ntime de'endencies e3ist only on 'acka"e state. This incl#des varia*les and c#rsors declared in a 'acka"e. I the 'acka"e had no "lo*al varia*les) the second e3ec#tion o the anonymo#s *lock !o#ld have s#cceeded.

BURIT. L+;+LS In "eneral) calls to s#*'ro"rams are 'roced#ral) they cannot *e called rom SQL statements. <o!ever) i a stand(alone or 'acka"ed #nction meets certain restrictions) it can *e called d#rin" e3ec#tion o a SQL statement. User(de ined #nctions are called the same !ay as *#ilt(in #nctions *#t it m#st meet di erent restrictions. These restrictions are de ined in terms o '#rity levels. There are o#r ty'es o '#rity levels.
9NDS RNDS 9NBS RNBS

(( (( (( ((

9rites No Data*ase State Reads No Data*ase State 9rites No Backa"e State Reads No Backa"e State

In addition to the 'recedin" restrictions) a #ser(de ined #nction m#st also meet the ollo!in" reV#irements to *e called rom a SQL statement. The #nction has to *e stored in the data*ase) either stand(alone or as 'art o a 'acka"e. The #nction can take only in 'arametes. The ormal 'arameters m#st #se only data*ase ty'es) not BLASQL ty'es s#ch as *oolean or record. The ret#rn ty'e o the #nction m#st also *e a data*ase ty'e. The #nction m#st not end the c#rrent transaction !ith commit or roll*ack) or roll*ack to a save'oint 'rior to the #nction e3ec#tion. It also m#st not iss#e any alter session or alter system commands.

R+STRICTNR+C+R+NC+S

Cor 'acka"ed #nctions) ho!ever) the R+STRICTNR+C+R+NC+S 'ra"ma is reV#ired to s'eci y the '#rity level o a "iven #nction. Synta34
BR,:&, R+STRICTNR+C+R+NC+S$subpro ram_name O)9NBSP O)RNDSP O)RNBSP%2

or packa e_name)

9NDS

Copy rights are reserved.

123
+34
CR+,T+ OR R+BL,C+ B,CG,:+ BG: IS

#nction #n6 ret#rn varchar2 'ra"ma restrictNre erences$ #n6)!nds%2 #nction #n5 ret#rn varchar2 'ra"ma restrictNre erences$ #n5)!nds%2
+ND BG:2 CR+,T+ OR R+BL,C+ B,CG,:+ -OD. BG: IS CUNCTION CUN6 ret#rn varchar IS -+:IN

+ND CUN62 CUNCTION CUN5 -+:IN

#'date de't set de'tno I 662 ret#rn ?hello?2 ret#rn varchar


IS

#'date de't set dname I?aa?2 ret#rn ?hello?2


+ND CUN52 +ND BG:2

The a*ove 'acka"e *ody !ill not created) it !ill "ive the ollo!in" erros. BLS(774D54 S#*'ro"ram ?CUN6? violates its associated 'ra"ma BLS(774D54 S#*'ro"ram ?CUN5? violates its associated 'ra"ma
CR+,T+ OR R+BL,C+ B,CG,:+ -OD. BG: IS CUNCTION CUN6 ret#rn varchar IS -+:IN

ret#rn ?hello?2

+ND CUN62 CUNCTION CUN5 -+:IN +ND CUN52 +ND BG:2

ret#rn varchar

IS

ret#rn ?hello?2

No! the 'acka"e *ody !ill *e created.


D+C,ULT

I there is no R+STRICTNR+C+R+NC+S 'ra"ma associated !ith a "iven 'acka"ed #nction) it !ill not have any '#rity level asserted. <o!ever) yo# can chan"e the de a#lt '#rity level or a 'acka"e. The D+C,ULT key!ord is #sed instead o the s#*'ro"ram name in the 'ra"ma. +34
CR+,T+ OR R+BL,C+ B,CG,:+ BG: IS

+ND BG:2

'ra"ma restrictNre erences$de a#lt)!nds%2 #nction #n6 ret#rn varchar2 #nction #n5 ret#rn varchar2

CR+,T+ OR R+BL,C+ B,CG,:+ -OD. BG: IS CUNCTION CUN6 ret#rn varchar IS -+:IN

#'date de't set de'tno I 662 ret#rn ?hello?2


+ND CUN62 CUNCTION CUN5 -+:IN

ret#rn varchar

IS

+ND CUN52 +ND BG:2

#'date de't set dname I?aa?2 ret#rn ?hello?2

Copy rights are reserved.

124
The a*ove 'acka"e *ody !ill not created) it !ill "ive the *eca#se the 'ra"ma !ill a''ly to all the #nctions. ollo!in" erros

BLS(774D54 S#*'ro"ram ?CUN6? violates its associated 'ra"ma BLS(774D54 S#*'ro"ram ?CUN5? violates its associated 'ra"ma
CR+,T+ OR R+BL,C+ B,CG,:+ -OD. BG: IS CUNCTION CUN6 ret#rn varchar IS -+:IN

ret#rn ?hello?2

+ND CUN62 CUNCTION CUN5 -+:IN +ND CUN52 +ND BG:2

ret#rn varchar

IS

ret#rn ?hello?2

No! the 'acka"e *ody !ill *e created.


TRUST

I the TRUST key!ord is 'resent) the restrictions listed in the 'ra"ma are not en orced. Rather) they are tr#sted to *e tr#e. +34
CR+,T+ OR R+BL,C+ B,CG,:+ BG: IS

+ND BG:2

#nction #n6 ret#rn varchar2 'ra"ma restrictNre erences$ #n6)!nds)tr#st%2 #nction #n5 ret#rn varchar2 'ra"ma restrictNre erences$ #n5)!nds)tr#st%2

CR+,T+ OR R+BL,C+ B,CG,:+ -OD. BG: IS CUNCTION CUN6 ret#rn varchar IS -+:IN

#'date de't set de'tno I 662 ret#rn ?hello?2


+ND CUN62 CUNCTION CUN5 -+:IN

ret#rn varchar

IS

+ND CUN52 +ND BG:2

#'date de't set dname I?aa?2 ret#rn ?hello?2

The a*ove 'acka"e !ill *e created s#ccess #lly.


I&BORT,NT BOINTS ,-OUT R+STRICTNR+C+R+NC+S

This 'ra"ma can a''ear any!here in the 'acka"e s'eci ication) a ter the #nction declaration. It can a''ly to only one #nction de inition. Cor overload #nctions) the 'ra"ma a''lies to the nearest de inition 'rior to the 'ra"ma. This 'ra"ma is reV#ired only or 'acka"es #nctions not or stand(alone #nctions. The Bra"ma can *e declared only inside the 'acka"e s'eci ication. The 'ra"ma is checked at com'ile time) not r#ntime. It is 'ossi*le to s'eci y !itho#t any '#rity levels !hen tr#st or com*ination o de a#lt and tr#st key!ords are 'resent.

Copy rights are reserved.

125
BINNIN: IN T<+ S<,R+D BOOL The s'ared pool is the 'ortion o the S:S that contains) amon" other thin"s) the '(code o com'iled s#*'ro"rams as they are r#n. The irst time a stored a store s#*'ro"ram is called) the '(code is loaded rom disk into the shared 'ool. Once the o*Kect is no lon"er re erenced) it is ree to *e a"ed o#t. O*Kects are a"ed o#t o the shared 'ool #sin" an LRU$Least Recently Used% al"orithm. The D-&SNS<,R+DNBOOL 'acka"e allo!s yo# to 'in o*Kects in the shared 'ool. 9hen an o*Kect is 'inned) it !ill never *e a"ed o#t #ntil yo# reV#est it) no matter ho! #ll the 'ool "ets or ho! o ten the o*Kect is accessed. This can im'rove 'er ormance) as it takes time to reload a 'acka"e rom disk.
D-&SNS<,R+DNBOOL G++B

has o#r 'roced#res

G++B UNG++B SI[+S ,-ORT+DNR+QU+STNT<R+S<OLD

The

D-&SNS<,R+DNBOOL.G++B

'roced#re is #sed to 'in o*Kects in the 'ool. varchar5)%la char de a#lt LB=%2 or di erent ty'es o

Synta34

BROC+DUR+ G++B$ob!ect_name

<ere the la" re'resents di erent ty'es o o*Kects. B Q R C T @S @C @R @D


UNG++B

la" val#es

(( (( (( (( (( (( (( (( ((

Backa"e) #nction or 'roced#re SeV#ence Tri""er SQL C#rsor O*Kect ty'e @ava so#rce @ava class @ava reso#rce @ava shared data

is the only !ay to remove a ke't o*Kect rom the shared 'ool) !itho#t restartin" the data*ase. Ge't o*Kects are never a"ed o#t a#tomatically.
UNG++B

Synta34
BROC+DUR+ UNG++B$ob!ect_name SI[+S SI[+S

varchar5) %la

char de a#lt LB=%2

!ill echo the contents o the shared 'ool to the screen.

Synta34

BROC+DUR+ SI[+S$minsi+e n#m*er%2 O*Kects !ith "reater than the minsi+e !ill *e ret#rned. to ret#rn the data. ,-ORT+DNR+QU+STNT<R+S<OLD

SI[+S

#ses

D-&SNOUTBUT

9hen the data*ase determines that there is not eno#"h memory in the shared 'ool to satis y a "iven reV#est) it !ill *e"in a"in" o*Kects o#t #ntil there is

Copy rights are reserved.

eno#"h memory. It eno#"h o*Kects are a"ed o#t) this can have a 'er ormance im'act on other data*ase sessions. The ,-ORT+DNR+QU+STNT<R+S<OLD can *e #sed to remedy this. Synta34
BROC+DUR+ ,-ORT+DNR+QU+STNT<R+S<OLD$t'res'old_si+e

126

n#m*er%2

Once this 'roced#re is called) oracle !ill not start a"in" o*Kects rom the 'ool #nless at least t'res'old_si+e *ytes is needed.

D,T, &OD+L COR SU-BRO:R,&S ,ND B,CG,:+S US+RNO-@+CTS US+RNSOURC+ US+RN+RRORS D-,NO-@+CTS D-,NSOURC+ D-,N+RRORS ,LLNO-@+CTS ,LLNSOURC+ ,LLN+RRORS

CURSORS
Cursor is a 'ointer to memory location !hich is called as conte&t area !hich contains the in ormation necessary or 'rocessin") incl#din" the n#m*er o ro!s 'rocessed *y the statement) a 'ointer to the 'arsed re'resentation o the statement) and the active set !hich is the set o ro!s ret#rned *y the V#ery. C#rsor contains t!o 'arts <eader -ody <eader incl#des c#rsor name) any 'arameters and the ty'e o loaded. -ody incl#des the select statement. +34 C#rsor c$dno in n#m*er% ret#rn de'tMro!ty'e is select F rom de't2 In the a*ove <eader ] c#rsor c$dno in n#m*er% ret#rn de'tMro!ty'e -ody ] select F rom de't CURSOR T.B+S Im'licit $SQL% +3'licit BarameteriUed c#rsors R+C c#rsors data *ein"

CURSOR ST,:+S

Copy rights are reserved.

127
O'en Cetch Close

CURSOR ,TTRI-UT+S M o#nd Mnot o#nd Mro!co#nt Miso'en M*#lkNro!co#nt M*#lkNe3ce'tions

CURSOR D+CL+R,TION Synta34 C#rsor 0cursor_name1 is select statement2 +34 C#rsor c is select F rom de't2 CURSOR LOOBS
SI&BL+ LOOB

Sim'le loo' 9hile loo' Cor loo'

Synta34 Loo' Cetch 0cursor_name1 into 0record_variable12 +3it !hen 0cursor_name1 M not o#nd2 0statements12 +nd loo'2 +34
D+CL,R+

-+:IN

c#rsor c is select F rom st#dent2 vNst#d st#dentMro!ty'e2 o'en c2 loo' etch c into vNst#d2 e3it !hen cMnot o#nd2 d*msNo#t'#t.'#tNline$?Name I ? YY vNst#d.name%2 end loo'2 close c2

+ND2

O#t'#t4 Name Name Name Name


9<IL+ LOOB

I I I I

saketh srin# satish s#dha

Synta34 9hile 0cursor_name1 M o#nd loo'

Copy rights are reserved.

Cetch 0cursor_name1 nto 0record_variable12 0statements12 +nd loo'2 +34


D+CL,R+

128

-+:IN

c#rsor c is select F rom st#dent2 vNst#d st#dentMro!ty'e2 o'en c2 etch c into vNst#d2 !hile cM o#nd loo' etch c into vNst#d2 d*msNo#t'#t.'#tNline$?Name I ? YY vNst#d.name%2 end loo'2 close c2 I I I I saketh srin# satish s#dha

+ND2

O#t'#t4 Name Name Name Name


COR LOOB

Synta34 or 0record_variable1 in 0cursor_name1 loo' 0statements12 +nd loo'2 +34


D+CL,R+ -+:IN

c#rsor c is select F rom st#dent2

or vNst#d in c loo' d*msNo#t'#t.'#tNline$?Name I ? YY vNst#d.name%2 end loo'2 +ND2 O#t'#t4 Name Name Name Name I I I I saketh srin# satish s#dha

B,R,&+T,RI[+D CURSORS +34 This !as #sed !hen yo# are "oin" to #se the c#rsor in more than one 'lace !ith di erent val#es or the same !here cla#se. C#rsor 'arameters m#st *e in mode. C#rsor 'arameters may have de a#lt val#es. The sco'e o c#rsor 'arameter is !ithin the select statement.

D+CL,R+

c#rsor c$dno in n#m*er% is select F rom de't !here de'tno I dno2 vNde't de'tMro!ty'e2
-+:IN

o'en c$57%2 loo' etch c into vNde't2 e3it !hen cMnot o#nd2

Copy rights are reserved.

+ND2

d*msNo#t'#t.'#tNline$?Dname I ? YY vNde't.dname YY ? Loc I ? YY vNde't.loc%2 end loo'2 close c2

129

O#t'#t4 Dname I

R+S+,RC<

Loc I

D,LL,S

B,CG,:+D CURSORS 9IT< <+,D+R IN SB+C ,ND -OD. IN B,CG,:+ -OD. +34
CR+,T+ OR R+BL,C+ B,CG,:+ BG: IS

c#rsors declared in 'acka"es !ill not close a#tomatically. In 'acka"ed c#rsors yo# can modi y the select statement !itho#t makin" any chan"es to the c#rsor header in the 'acka"e s'eci ication. Backa"ed c#rsors !ith m#st *e de ined in the 'acka"e *ody itsel ) and then #se it as "lo*al or the 'acka"e. .o# can not de ine the 'acka"ed c#rsor in any s#*'ro"rams. C#rsor declaration in 'acka"e !ith o#t *ody needs the ret#rn cla#se.

+ND BG:2

c#rsor c ret#rn de'tMro!ty'e is select F rom de't2 'roced#re 'roc is

CR+,T+ OR R+BL,C+ B,GC,:+ -OD. BG: IS BROC+DUR+ BROC IS -+:IN

c#rsor c ret#rn de'tMro!ty'e is select F rom de't2

or v in c loo' d*msNo#t'#t.'#tNline$?De'tno I ? YY v.de'tno YY ? Dname I ? YY v.dname YY ? Loc I ? YY v.loc%2 end loo'2


+ND BROC2 +ND BG:2

O#t'#t4
SQL1

CR+,T+ OR R+BL,C+ B,GC,:+ -OD. BG: IS

e3ec 'k".'roc De'tno I 67 Dname I ,CCOUNTIN: Loc I N+9 .ORG De'tno I 57 Dname I R+S+,RC< Loc I D,LL,S De'tno I 87 Dname I S,L+S Loc I C<IC,:O De'tno I 47 Dname I OB+R,TIONS Loc I -OSTON

c#rsor c ret#rn de'tMro!ty'e is select F rom de't !here de'tno 1 572


BROC+DUR+ BROC IS -+:IN

or v in c loo' d*msNo#t'#t.'#tNline$?De'tno I ? YY v.de'tno YY ? Dname I ? YY v.dname YY ? Loc I ? YY v.loc%2 end loo'2


+ND BROC2 +ND BG:2

O#t'#t4
SQL1

e3ec 'k".'roc De'tno I 87 Dname I S,L+S Loc I C<IC,:O De'tno I 47 Dname I OB+R,TIONS Loc I -OSTON

R+C CURSORS ,ND CURSOR ;,RI,-L+S

Copy rights are reserved.

130
This is #nconstrained c#rsor !hich !ill ret#rn di erent ty'es de'ends #'on the #ser in'#t. Re c#rsors can not *e closed im'licitly. Re c#rsor !ith ret#rn ty'e is called stron cursor. Re c#rsor !ith o#t ret#rn ty'e is called weak cursor. .o# can declare re c#rsor ty'e in 'acka"e s'ec as !ell as *ody. .o# can declare re c#rsor ty'es in local s#*'ro"rams or anonymo#s *locks. C#rsor varia*les can *e assi"ned rom one to another. .o# can declare a c#rsor varia*le in one sco'e and assi"n another c#rsor varia*le !ith di erent sco'e) then yo# can #se the c#rsor varia*le even tho#"h the assi"ned c#rsor varia*le "oes o#t o sco'e. C#rsor varia*les can *e 'assed as a 'arameters to the s#*'ro"rams. C#rsor varia*les modes are in or o#t or in o#t. C#rsor varia*les can not *e declared in 'acka"e s'ec and 'acka"e *ody $e3cl#din" s#*'ro"rams%. .o# can not #ser remote 'roced#re calls to 'ass c#rsor varia*les rom one server to another. C#rsor varia*les can not #se or #'date cla#se. .o# can not assi"n n#lls to c#rsor varia*les. .o# can not com'are c#rsor varia*les or eV#ality) ineV#ality and n#llity.
CR+,T+ OR R+BL,C+ BROC+DUR+ R+CNCURSOR$T,-L+NN,&+ IN ;,RC<,R% IS

+34

ty'e t is re c#rsor2 c t2 vNde't de'tMro!ty'e2 ty'e r is record$ename em'.enameMty'e)Ko* em'.Ko*Mty'e)sal em'.salMty'e%2 vNem' r2 vNst#d st#dent.nameMty'e2
-+:IN

i ta*leNname I ?D+BT? then o'en c or select F rom de't2 elsi ta*leNname I ?+&B? then o'en c or select ename)Ko*)sal rom em'2 elsi ta*leNname I ?STUD+NT? then o'en c or select name rom st#dent2 end i 2 loo' i ta*leNname I ?D+BT? then etch c into vNde't2 e3it !hen cMnot o#nd2 d*msNo#t'#t.'#tNline$?De'tno I ? YY vNde't.de'tno YY ? Dname I ? YY vNde't.dname YY ? Loc I ? YY vNde't.loc%2 elsi ta*leNname I ?+&B? then etch c into vNem'2 e3it !hen cMnot o#nd2 d*msNo#t'#t.'#tNline$?+name I ? YY vNem'.ename YY ? @o* I ? YY vNem'.Ko* YY ? Sal I ? YY vNem'.sal%2 elsi ta*leNname I ?STUD+NT? then etch c into vNst#d2 e3it !hen cMnot o#nd2 d*msNo#t'#t.'#tNline$?Name I ? YY vNst#d%2 end i 2 end loo'2 close c2
+ND2

Copy rights are reserved.

O#t'#t4
SQL1

131
e3ec re Nc#rsor$?D+BT?% I I I I 67 57 87 47 Dname Dname Dname Dname I I I I Loc I N+9 .ORG Loc I D,LL,S S,L+S Loc I C<IC,:O OB+R,TIONS Loc I -OSTON
,CCOUNTIN: R+S+,RC<

De'tno De'tno De'tno De'tno


SQL1

e3ec re Nc#rsor$?+&B?% I I I I I I I I I I I I I I @o* I CL+RG Sal I X77 @o* I S,L+S&,N Sal I 6E77 9,RD @o* I S,L+S&,N Sal I 65D7 @ON+S @o* I &,N,:+R Sal I 5TWD &,RTIN @o* I S,L+S&,N Sal I 65D7 -L,G+ @o* I &,N,:+R Sal I 5XD7 CL,RG @o* I &,N,:+R Sal I 54D7 SCOTT @o* I ,N,L.ST Sal I 8777 GIN: @o* I BR+SID+NT Sal I D777 TURN+R @o* I S,L+S&,N Sal I 6D77 ,D,&S @o* I CL+RG Sal I 6677 @,&+S @o* I CL+RG Sal I TD7 CORD @o* I ,N,L.ST Sal I 8777 &ILL+R @o* I CL+RG Sal I 6877
S&IT< ,LL+N

+name +name +name +name +name +name +name +name +name +name +name +name +name +name
SQL1

e3ec re Nc#rsor$?STUD+NT?% Name Name Name Name I I I I saketh srin# satish s#dha

CURSOR +/BR+SSIONS .o# can #se c#rsor e3'ressions in e3'licit c#rsors. .o# can #se c#rsor e3'ressions in dynamic SQL. .o# can #se c#rsor e3'ressions in R+C c#rsor declarations and varia*les. .o# can not #se c#rsor e3'ressions in im'licit c#rsors. Oracle o'ens the nested c#rsor de ined *y a c#rsor e3'ression im'licitly as soon as it etches the data containin" the c#rsor e3'ression rom the 'arent or o#ter c#rsor. Nested c#rsor closes i yo# close e3'licitly. Nested c#rsor closes !henever the o#ter or 'arent c#rsor is e3ec#ted a"ain or closed or canceled. Nested c#rsor closes !henever an e3ce'tion is raised !hile etchin" data rom a 'arent c#rsor. C#rsor e3'ressions can not *e #sed !hen declarin" a vie!. C#rsor e3'ressions can *e #sed as an ar"#ment to ta*le #nction. .o# can not 'er orm *ind and e3ec#te o'erations on c#rsor e3'ressions !hen #sin" the c#rsor e3'ressions in dynamic SQL.

USIN: N+ST+D CURSORS OR CURSOR +/BR+SSIONS

+34
D+CL,R+

c#rsor c is select ename)c#rsor$select dname rom de't d !here e.em'no I d.de'tno% rom em' e2 ty'e t is re c#rsor2 c6 t2 c5 t2 v6 em'.enameMty'e2

Copy rights are reserved.

v5 de't.dnameMty'e2
-+:IN

132

+ND2

o'en c2 loo' etch c6 into v62 e3it !hen c6Mnot o#nd2 etch c5 into v52 e3it !hen c5Mnot o#nd2 d*msNo#t'#t.'#tNline$?+name I ? YY v6 YY ? Dname I ? YY v5%2 end loo'2 end loo'2 close c2

CURSOR CL,US+S
R+TURN

Ret#rn Cor #'date 9here c#rrent o -#lk collect

C#rsor c ret#rn de'tMro!ty'e is select F rom de't2 Or C#rsor c6 is select F rom de't2 C#rsor c ret#rn c6Mro!ty'e is select F rom de't2 Or Ty'e t is record$de'tno de't.de'tnoMty'e) dname de't.dnameMty'e%2 C#rsor c ret#rn t is select de'tno) dname rom de't2

COR UBD,T+ ,ND 9<+R+ CURR+NT OC

Normally) a select o'eration !ill not take any locks on the ro!s *ein" accessed. This !ill allo! other sessions connected to the data*ase to chan"e the data *ein" selected. The res#lt set is still consistent. ,t o'en time) !hen the active set is determined) oracle takes a sna'shot o the ta*le. ,ny chan"es that have *een committed 'rior to this 'oint are re lected in the active set. ,ny chan"es made a ter this 'oint) even i they are committed) are not re lected #nless the c#rsor is reo'ened) !hich !ill eval#ate the active set a"ain. <o!ever) i the COR UBD,T+ cal#se is 'esent) e3cl#sive ro! locks are taken on the ro!s in the active set *e ore the o'en ret#rns. These locks 'revent other sessions rom chan"in" the ro!s in the active set #ntil the transaction is committed or rolled *ack. I another session already has locks on the ro!s in the active set) then S+L+CT S COR UBD,T+ o'eration !ill !ait or these locks to *e released *y the other session. There is no time(o#t or this !aitin" 'eriod. The S+L+CTSCOR UBD,T+ !ill han" #ntil the other session releases the lock. To handle this sit#ation) the NO9,IT cla#se is availa*le. Synta34 Select S rom S or #'date o col#mnNname O!ait nP2 I the c#rsor is declared !ith the COR UBD,T+ cla#se) the cla#se can *e #sed in an #'date or delete statement. Synta34 9here c#rrent o c#rsor2
9<+R+ CURR+NT OC

Copy rights are reserved.

+34
D+CL,R+ -+:IN

133
c#rsor c is select F rom de't or #'date o dname2 or v in c loo' #'date de't set dname I ?aa? !here c#rrent o c2 commit2 end loo'2

+ND2 -ULG COLL+CT

This is #sed or array etches 9ith this yo# can retrieve m#lti'le ro!s o data !ith a sin"le ro#ndtri'. This red#ces the n#m*er o conte3t s!itches *et!een the 'lAsVl and sVl en"ines. Red#ces the overhead o retrievin" data. .o# can #se *#lk collect in *oth dynamic and static sVl. .o# can #se *#lk collect in select) etch into and ret#rnin" into cla#ses. SQL en"ine a#tomatically initialiUes and e3tends the collections yo# re erence in the *#lk collect cla#se. -#lk collect o'eration em'ties the collection re erenced in the into cla#se *e ore e3ec#tin" the V#ery. .o# can #se the limit cla#se o *#lk collect to restrict the no o ro!s retrieved. .o# can etch into m#lti*le collections !ith one col#mn each. Usin" the ret#rnin" cla#se !e can ret#rn data to the another collection.

-ULG COLL+CT IN C+TC<

+34

D+CL,R+

-+:IN

Ty'e t is ta*le o de'tMro!ty'e2 nt t2 C#rsor c is select F rom de't2

O'en c2 Cetch c *#lk collect into nt2 Close c2 Cor i in nt. irst..nt.last loo' d*msNo#t'#t.'#tNline$?Dname I ? YY nt$i%.dname YY ? Loc I ? YY nt$i%.loc%2 end loo'2
+ND2

O#t'#t4

Dname Dname Dname Dname

I I I I

Loc I N+9 .ORG Loc I D,LL,S S,L+S Loc I C<IC,:O OB+R,TIONS Loc I -OSTON
,CCOUNTIN: R+S+,RC<

-ULG COLL+CT IN S+L+CT

+34

D+CL,R+

Ty'e t is ta*le o de'tMro!ty'e2 Nt t2 Select F *#lk collect into nt rom de't2 or i in nt. irst..nt.last loo' d*msNo#t'#t.'#tNline$?Dname I ? YY nt$i%.dname YY ? Loc I ? YY nt$i%.loc%2 end loo'2
+ND2 -+:IN

Copy rights are reserved.

134
O#t'#t4 Dname Dname Dname Dname I I I I Loc I N+9 .ORG Loc I D,LL,S S,L+S Loc I C<IC,:O OB+R,TIONS Loc I -OSTON
,CCOUNTIN: R+S+,RC<

LI&IT IN -ULG COLL+CT

.o# can #se this to limit the n#m*er o ro!s to *e etched. +34
D+CL,R+

-+:IN

Ty'e t is ta*le o de'tMro!ty'e2 nt t2 C#rsor c is select F rom de't2

O'en c2 Cetch c *#lk collect into nt limit 52 Close c2 Cor i in nt. irst..nt.last loo' d*msNo#t'#t.'#tNline$?Dname I ? YY nt$i%.dname YY ? Loc I ? YY nt$i%.loc%2 end loo'2
+ND2

O#t'#t4 Dname I Dname I


,CCOUNTIN: R+S+,RC<

Loc I N+9 .ORG Loc I D,LL,S

&ULTIBL+ C+TC<+S IN INTO CL,US+

+364
D+CL,R+

Ty'e t is ta*le o de't.dnameMty'e2 nt t2 Ty'e t6 is ta*le o de't.locMty'e2 nt6 t2 C#rsor c is select dname)loc rom de't2 O'en c2 Cetch c *#lk collect into nt)nt62 Close c2 Cor i in nt. irst..nt.last loo' d*msNo#t'#t.'#tNline$?Dname I ? YY nt$i%%2 end loo'2 Cor i in nt6. irst..nt6.last loo' d*msNo#t'#t.'#tNline$?Loc I ? YY nt6$i%%2 end loo'2

-+:IN

+ND2

O#t'#t4

Dname I ,CCOUNTIN: Dname I R+S+,RC< Dname I S,L+S Dname I OB+R,TIONS Loc I N+9 .ORG Loc I D,LL,S Loc I C<IC,:O Loc I -OSTON

+354

Copy rights are reserved.

D+CL,R+

135

ty'e t is ta*le o de't.dnameMty'e2 ty'e t6 is ta*le o de't.locMty'e2 nt t2 nt6 t62


-+:IN

Select dname)loc *#lk collect into nt)nt6 rom de't2 or i in nt. irst..nt.last loo' d*msNo#t'#t.'#tNline$?Dname I ? YY nt$i%%2 end loo'2 or i in nt6. irst..nt6.last loo' d*msNo#t'#t.'#tNline$?Loc I ? YY nt6$i%%2 end loo'2

+ND2

O#t'#t4

Dname I ,CCOUNTIN: Dname I R+S+,RC< Dname I S,L+S Dname I OB+R,TIONS Loc I N+9 .ORG Loc I D,LL,S Loc I C<IC,:O Loc I -OSTON

R+TURNIN: CL,US+ IN -ULG COLL+CT

.o# can #se this to ret#rn the 'rocessed data to the o#'#t varia*les or ty'ed varia*les. +34
D+CL,R+

-+:IN

ty'e t is ta*le o n#m*er$5%2 nt t 4I t$6)5)8)4%2 ty'e t6 is ta*le o varchar$5%2 nt6 t62 ty'e t5 is ta*le o st#dentMro!ty'e2 nt5 t52

select name *#lk collect into nt6 rom st#dent2 orall v in nt6. irst..nt6.last #'date st#dent set no I nt$v% !here name I nt6$v% ret#rnin" no)name)marks *#lk collect into nt52 or v in nt5. irst..nt5.last loo' d*msNo#t'#t.'#tNline$?&arks I ? YY nt5$v%%2 end loo'2
+ND2

O#t'#t4

&arks &arks &arks &arks

I I I I

677 577 877 477

BOINTS TO R+&+&-+R C#rsor name can *e #' to 87 characters in len"th. C#rsors declared in anonymo#s *locks or s#*'ro"rams closes a#tomatically !hen that *lock terminates e3ec#tion. M*#lkNro!co#nt and M*#lkNe3ce'tions can *e #sed only !ith orall constr#ct.

Copy rights are reserved.

136
C#rsor declarations may have e3'ressions !ith col#mn aliases. These e3'ressions are called virt#al col#mns or calc#lated col#mns.

SQL IN BLASQL
The only statements allo!ed directly in 'lAsVl are -INDIN: -indin" a varia*le is the 'rocess o identi yin" the stora"e location associated !ith an identi ier in the 'ro"ram. Ty'es o *indin" +arly *indin" Late *indin"
D&L

and

TCL.

-indin" d#rin" the com'iled 'hase is early *indin". -indin" d#rin" the r#ntime 'hase is late *indin". In early *indin" com'ile 'hase !ill take lon"er *eca#se o *indin" !ork *#t the e3ec#tion is aster. In late *indin" it !ill shorten the com'ile 'hase *#t len"thens the e3ec#tion time. BLASQL *y de a#lt #ses early *indin". -indin" also involves checkin" the data*ase or 'ermissions to access the o*Kect Re erenced. D.N,&IC SQL

I yo# #se

DDL in 'lAsVl it validates the 'ermissions and e3istence i reV#ires d#rin" com'ile time !hich makes invalid. 9e can avoid this *y #sin" Dynamic SQL. Dynamic SQL allo!s yo# to create a SQL statement dynamically at r#ntime. SQL.

T!o techniV#es are availa*le or Dynamic Native Dynamic SQL D-&SNSQL 'acka"e

USIN: N,TI;+ D.N,&IC SQL


USIN: +/+CUT+ I&&+DI,T+

+34

-+:IN

+3ec#te immediate Lcreate ta*le st#dent$no n#m*er$5%)name varchar$67%%=2 or +3ec#te immediate $Lcreate ta*le st#dent$no n#m*er$5%)name varchar$67%%=%2
+ND2 USIN: +/+CUT+ I&&+DI,T+ 9IT< BLASQL ;,RI,-L+S

+34
D+CL,R+

Copy rights are reserved.

v varchar$677%2
-+:IN

137

+ND2

v 4I ?create ta*le st#dent$no n#m*er$5%)name varchar$67%%?2 e3ec#te immediate v2

USIN: +/+CUT+ I&&+DI,T+ 9IT< -IND ;,RI,-L+S ,ND USIN: CL,US+

+34
D+CL,R+ -+:IN

v varchar$677%2 v 4I ?insert into st#dent val#es$4v6)4v5)4v8%?2 e3ec#te immediate v #sin" E)? ?)E772

+ND2

+/+CUTIN: QU+RI+S 9IT< OB+N COR ,ND USIN: CL,US+

+34
CR+,T+ OR R+BL,C+ BROC+DUR+ B$smarks

-+:IN

in n#m*er% IS s varchar$677% 4I ?select F rom st#dent !here marks 1 4m?2 ty'e t is re c#rsor2 c t2 v st#dentMro!ty'e2 o'en c or s #sin" smarks2 loo' etch c into v2 e3it !hen cMnot o#nd2 d*msNo#t'#t.'#tNline$?St#dent &arks I ? YY v.marks%2 end loo'2 close c2

+ND2

O#t'#t4
SQL1

e3ec '$677% St#dent &arks I 577 St#dent &arks I 877 St#dent &arks I 477

QU+RI+S 9IT< +/+CUT+ I&&+DI,T+

+34

D+CL,R+

-+:IN

dNname de't.dnameMty'e2 lc de't.locMty'e2 v varchar$677%2 v 4I ?select dname rom de't !here de'tno I 67?2 e3ec#te immediate v into dNname2 d*msNo#t'#t.'#tNline$?Dname I ?YY dNname%2 v 4I ?select loc rom de't !here dname I 4dn?2 e3ec#te immediate v into lc #sin" dNname2 d*msNo#t'#t.'#tNline$?Loc I ? YY lc%2

+ND2

O#t'#t4 Dname I ,CCOUNTIN: Loc I N+9 .ORG


;,RI,-L+ N,&+S

+34
D+CL,R+

Copy rights are reserved.

&arks n#m*er$8% 4I 6772


-+:IN

138
(( this !ill delete all the ta*le

Delete st#dent !here marks I marks2 ro!s in the st#dent


+ND2

This can *e avoided *y #sin" the la*eled *locks. 00myN*lock11


D+CL,R+ -+:IN

&arks n#m*er$8% 4I 6772 (( delete ro!s marks o 677

Delete st#dent !here marks I myN*lock.marks2 !hich has a


+ND2 :+TTIN: D,T, INTO BLASQL ;,RI,-L+S

+34
D+CL,R+

-+:IN +ND2

;6 n#m*er2 ;5 varchar$5%2 Select no)name into v6)v5 rom st#dent !here marks I 6772

D&L ,ND R+CORDS

+34
CR+,T+ OR R+BL,C+ BROC+DUR+ B$sro! -+:IN

in st#dentMro!ty'e%

IS

insert into st#dent val#es sro!2


+ND B2 D+CL,R+ -+:IN

s st#dentMro!ty'e2 s.no 4I 662 s.name 4I ?aa?2 s.marks 4I 6772 '$s%2

+ND2 R+CORD -,S+D INS+RTS

+34

D+CL,R+ -+:IN

sro! st#dentMro!ty'e2 sro!.no 4I W2 sro!.name 4I ?cc?2 sro!.marks 4I D772 insert into st#dent val#es sro!2

+ND2 R+CORD -,S+D UBD,T+S

+34

D+CL,R+ -+:IN

sro! st#dentMro!ty'e2 sro!.no 4I E2 sro!.name 4I ?cc?2

Copy rights are reserved.

+ND2

sro!.marks 4I D772 #'date st#dent set ro!Isro! !here no I sro!.no2

139

USIN: R+CORDS 9IT< R+TURNIN: CL,US+

+34
D+CL,R+

-+:IN

sro! st#dentMro!ty'e2 sret#rn st#dentMro!ty'e2

sro!.no 4I X2 sro!.name 4I ?dd?2 sro!.marks 4I D772 insert into st#dent val#es sro! ret#rnin" no)name)marks into sret#rn2 d*msNo#t'#t.'#tNline$?No I ? YY sret#rn.no%2 d*msNo#t'#t.'#tNline$?No I ? YY sret#rn.name%2 d*msNo#t'#t.'#tNline$?No I ? YY sret#rn.marks%2
+ND2

O#t'#t4

No I X No I dd No I D77

COR,LL ST,T+&+NT This can *e #sed to "et the data rom the data*ase at once *y red#ctin" the n#m*er o conte3t s!itches !hich is a trans er o control *et!een BLASQL and SQL en"ine. Synta34 Corall inde3Nvar in O 3ower_bound..upper_bound Y Indices o inde&in _collection Y ;al#es o inde&in _collection P SQL statement2
COR,LL 9IT< NON(S+QU+NTI,L ,RR,.S

+34

D+CL,R+

ty'e t is ta*le o st#dent.noMty'e inde3 *y *inaryNinte"er2 i*t t2


-+:IN

+ND2

i*t$6% 4I 62 i*t$67% 4I 52 orall i in i*t. irst..i*t.last #'date st#dent set marks I T77 !here no I i*t$i%2

The a*ove 'ro"ram !ill "ive error like Lelement at inde3 O5P does not e3ists. .o# can recti y it in one o the t!o ollo!in" !ays.
US:,:+ OC INDIC+S OC TO ,;OID T<+ ,-O;+ -+<,;IOUR

+34

D+CL,R+

ty'e t is ta*le o st#dent.noMty'e inde3 *y *inaryNinte"er2 i*t t2 ty'e t6 is ta*le o *oolean inde3 *y *inaryNinte"er2 i*t6 t62

Copy rights are reserved.

-+:IN

140

i*t$6% 4I 62 i*t$67% 4I 52 i*t$677% 4I 82 i*t6$6% 4I tr#e2 i*t6$67% 4I tr#e2 i*t6$677% 4I tr#e2 orall i in indices o i*t6 #'date st#dent set marks I T77 !here no I i*t$i%2
+ND2 US:,:+ OC INDIC+S OC TO ,;OID T<+ ,-O;+ -+<,;IOUR

+34

D+CL,R+

ty'e t is ta*le o st#dent.noMty'e inde3 *y *inaryNinte"er2 i*t t2 ty'e t6 is ta*le o 'lsNinte"er inde3 *y *inaryNinte"er2 i*t6 t62
-+:IN

+ND2

i*t$6% 4I 62 i*t$67% 4I 52 i*t$677% 4I 82 i*t6$66% 4I 62 i*t6$6D% 4I 672 i*t6$6X% 4I 6772 orall i in val#es o i*t6 #'date st#dent set marks I DEW !here no I i*t$i%2

BOINTS ,-OUT -ULG -INDS

Bassin" the entire BLASQL ta*le to the SQL en"ine in one ste' is kno!n as
*#lk *ind. -#lk *inds are done #sin" the orall statement. I there is an error 'rocessin" one o the ro!s in *#lk D&L o'eration) only that ro! is rolled *ack.

BOINTS ,-OUT R+TURIN: CL,US+

This !ill *e #sed only !ith

D&L statements to ret#rn data into BLASQL varia*les. This !ill *e #se #l in sit#ations like ) !hen 'er ormin" insert or #'date or delete i yo# !ant to kno! the data o the ta*le !hich has *een e ected *y the D&L. 9ith o#t "oin" or another S+L+CT #sin" R+TURNIN: cla#se !e !ill "et the data !hich !ill avoid a call to RD-&S kernel.

COLL+CTIONS
Collections are also com'osite ty'es) in that they allo! yo# to treat several varia*les as a #nit. , collection com*ines varia*les o the same ty'e. T.B+S ;,RR,.S ;arrays Nested ta*les Inde3 ( *y ta*les $,ssociate arrays%

Copy rights are reserved.

141
, varray is dataty'e very similar to an array. , varray has a i3ed limit on its siUe) s'eci ied as 'art o the declaration. +lements are inserted into varray startin" at inde3 6) #' to ma3im#m lenth declared in the varray ty'e. The ma3im#m siUe o the varray is 5 "i"a *ytes. Synta34 Ty'e 0type_name$ 0element_type$2 +364
D+CL,R+

is

varray

varyin"

array

$0 limit$%

-+:IN

ty'e t is varray$67% o varchar$5%2 va t 4I t$?a?)?*?)?c?)?d?%2 la" *oolean2

d*msNo#t'#t.'#tNline$?Limit I ? YY va.limit%2 d*msNo#t'#t.'#tNline$?Co#nt I ? YY va.co#nt%2 d*msNo#t'#t.'#tNline$?Cirst Inde3 I ? YY va. irst%2 d*msNo#t'#t.'#tNline$?Last Inde3 I ? YY va.last%2 d*msNo#t'#t.'#tNline$?Ne3t Inde3 I ? YY va.ne3t$5%%2 d*msNo#t'#t.'#tNline$?Brevio#s Inde3 I ? YY va.'rior$8%%2 d*msNo#t'#t.'#tNline$?;,RR,. +L+&+NTS?%2 or i in va. irst..va.last loo' d*msNo#t'#t.'#tNline$?vaO? YY i YY ?P I ? YY va$i%%2 end loo'2 la" 4I va.e3ists$8%2 i la" I tr#e then d*msNo#t'#t.'#tNline$?Inde3 8 e3ists !ith an element ? YY va$8%%2 else d*msNo#t'#t.'#tNline$?Inde3 8 does not e3ists?%2 end i 2 va.e3tend2 d*msNo#t'#t.'#tNline$?, ter e3tend o one inde3) Co#nt I ? YY va.co#nt%2 la" 4I va.e3ists$D%2 i la" I tr#e then d*msNo#t'#t.'#tNline$?Inde3 D e3ists !ith an element ? YY va$D%%2 else d*msNo#t'#t.'#tNline$?Inde3 D does not e3ists?%2 end i 2 la" 4I va.e3ists$E%2 i la" I tr#e then d*msNo#t'#t.'#tNline$?Inde3 E e3ists !ith an element ? YY va$E%%2 else d*msNo#t'#t.'#tNline$?Inde3 E does not e3ists?%2 end i 2 va.e3tend$5%2 d*msNo#t'#t.'#tNline$?, ter e3tend o t!o inde3es) Co#nt I ? YY va.co#nt%2 d*msNo#t'#t.'#tNline$?;,RR,. +L+&+NTS?%2 or i in va. irst..va.last loo' d*msNo#t'#t.'#tNline$?vaO? YY i YY ?P I ? YY va$i%%2 end loo'2 va$D% 4I ?e?2 va$E% 4I ? ?2 va$W% 4I ?"?2 d*msNo#t'#t.'#tNline$?,CT+R ,SSIN:NIN: ;,LU+S TO +/T+ND+D +L+&+NTS) ;,RR,. +L+&+NTS?%2 or i in va. irst..va.last loo' d*msNo#t'#t.'#tNline$?vaO? YY i YY ?P I ? YY va$i%%2 end loo'2

Copy rights are reserved.

va.e3tend$8)5%2 d*msNo#t'#t.'#tNline$?, ter e3tend o three inde3es) Co#nt I ? va.co#nt%2 d*msNo#t'#t.'#tNline$?;,RR,. +L+&+NTS?%2 or i in va. irst..va.last loo' d*msNo#t'#t.'#tNline$?vaO? YY i YY ?P I ? YY va$i%%2 end loo'2 va.trim2 d*msNo#t'#t.'#tNline$?, ter trim o one inde3) Co#nt I ? va.co#nt%2 va.trim$8%2 d*msNo#t'#t.'#tNline$?, ter trim o three inde3s) Co#nt I ? va.co#nt%2 d*msNo#t'#t.'#tNline$?,CT+R TRI&) ;,RR,. +L+&+NTS?%2 or i in va. irst..va.last loo' d*msNo#t'#t.'#tNline$?vaO? YY i YY ?P I ? YY va$i%%2 end loo'2 va.delete2 d*msNo#t'#t.'#tNline$?, ter delete o entire varray) Co#nt I ? va.co#nt%2
+ND2

142
YY

YY YY

YY

O#t'#t4 Limit I 67 Co#nt I 4 Cirst Inde3 I 6 Last Inde3 I 4 Ne3t Inde3 I 8 Brevio#s Inde3 I 5 ;,RR,. +L+&+NTS vaO6P I a vaO5P I * vaO8P I c vaO4P I d Inde3 8 e3ists !ith an element c , ter e3tend o one inde3) Co#nt I D Inde3 D e3ists !ith an element Inde3 E does not e3ists , ter e3tend o t!o inde3es) Co#nt I W ;,RR,. +L+&+NTS vaO6P I a vaO5P I * vaO8P I c vaO4P I d vaODP I vaOEP I vaOWP I ,CT+R ,SSIN:NIN: ;,LU+S TO +/T+ND+D +L+&+NTS) ;,RR,. +L+&+NTS vaO6P I a vaO5P I * vaO8P I c vaO4P I d vaODP I e vaOEP I vaOWP I " , ter e3tend o three inde3es) Co#nt I 67 ;,RR,. +L+&+NTS vaO6P I a vaO5P I * vaO8P I c

Copy rights are reserved.

vaO4P I d vaODP I e vaOEP I vaOWP I " vaOXP I * vaOTP I * vaO67P I * , ter trim o one inde3) Co#nt I T , ter trim o three inde3s) Co#nt I E ,CT+R TRI&) ;,RR,. +L+&+NTS vaO6P I a vaO5P I * vaO8P I c vaO4P I d vaODP I e vaOEP I , ter delete o entire varray) Co#nt I 7 +354
D+CL,R+

143

ty'e t is varray$4% o st#dentMro!ty'e2 va t 4I t$n#ll)n#ll)n#ll)n#ll%2


-+:IN

or i in 6..va.co#nt loo' select F into va$i% rom st#dent !here sno I i2 d*msNo#t'#t.'#tNline$?Sno I ? YY va$i%.sno YY ? Sname I ? YY va$i%.sname%2 end loo'2
+ND2

O#t'#t4

Sno Sno Sno Sno

I I I I

6 5 8 4

Sname Sname Sname Sname

I I I I

saketh srin# divya mano"ni

+384

D+CL,R+

ty'e t is varray$4% o st#dent.smarksMty'e2 va t 4I t$n#ll)n#ll)n#ll)n#ll%2


-+:IN

+ND2

or i in 6..va.co#nt loo' select smarks into va$i% rom st#dent !here sno I i2 d*msNo#t'#t.'#tNline$?Smarks I ? YY va$i%%2 end loo'2

O#t'#t4 Smarks Smarks Smarks Smarks +344


D+CL,R+

I I I I

677 577 877 477

ty'e r is record$c6 st#dent.snameMty'e)c5 st#dent.smarksMty'e%2 ty'e t is varray$4% o r2 va t 4I t$n#ll)n#ll)n#ll)n#ll%2

-+:IN

or i in 6..va.co#nt loo' select sname)smarks into va$i% rom st#dent !here sno I i2 d*msNo#t'#t.'#tNline$?Sname I ? YY va$i%.c6 YY ? Smarks I ? YY va$i%.c5%2

Copy rights are reserved.

end loo'2
+ND2

144

O#t'#t4

Sname Sname Sname Sname

I I I I

saketh Smarks I 677 srin# Smarks I 577 divya Smarks I 877 mano"ni Smarks I 477

+3D4

D+CL,R+

ty'e t is varray$6% o addr2 va t 4I t$n#ll%2 c#rsor c is select F rom em'loy2 i n#m*er 4I 62


-+:IN

or v in c loo' select address into va$i% rom em'loy !here ename I v.ename2 d*msNo#t'#t.'#tNline$?<no I ? YY va$i%.hno YY ? City I ? YY va$i%.city%2 end loo'2
+ND2

O#t'#t4

<no I 66 City I hyd <no I 55 City I *an" <no I 88 City I kochi

+3E4
D+CL,R+

ty'e t is varray$D% o varchar$5%2 va6 t2 va5 t 4I t$%2 i va6 is n#ll then d*msNo#t'#t.'#tNline$?va6 else d*msNo#t'#t.'#tNline$?va6 end i 2 i va5 is n#ll then d*msNo#t'#t.'#tNline$?va5 else d*msNo#t'#t.'#tNline$?va5 end i 2 is n#ll?%2 is not n#ll?%2 is n#ll?%2 is not n#ll?%2

-+:IN

+ND2

O#t'#t4 va6 is n#ll va5 is not n#ll N+ST+D T,-L+S , nested ta*le is tho#"ht o a data*ase ta*le !hich has no limit on its siUe. +lements are inserted into nested ta*le startin" at inde3 6. The ma3im#m siUe o the varray is 5 "i"a *ytes. Synta34 Ty'e 0type_name$ is ta*le o 0table_type$2 +364
D+CL,R+

Copy rights are reserved.

ty'e t is ta*le o varchar$5%2 nt t 4I t$?a?)?*?)?c?)?d?%2 la" *oolean2


-+:IN

145

i nt.limit is n#ll then d*msNo#t'#t.'#tNline$?No limit to Nested Ta*les?%2 else d*msNo#t'#t.'#tNline$?Limit I ? YY nt.limit%2 end i 2 d*msNo#t'#t.'#tNline$?Co#nt I ? YY nt.co#nt%2 d*msNo#t'#t.'#tNline$?Cirst Inde3 I ? YY nt. irst%2 d*msNo#t'#t.'#tNline$?Last Inde3 I ? YY nt.last%2 d*msNo#t'#t.'#tNline$?Ne3t Inde3 I ? YY nt.ne3t$5%%2 d*msNo#t'#t.'#tNline$?Brevio#s Inde3 I ? YY nt.'rior$8%%2 d*msNo#t'#t.'#tNline$?N+ST+D T,-L+ +L+&+NTS?%2 or i in 6..nt.co#nt loo' d*msNo#t'#t.'#tNline$?ntO? YY i YY ?P I ? YY nt$i%%2 end loo'2 la" 4I nt.e3ists$8%2 i la" I tr#e then d*msNo#t'#t.'#tNline$?Inde3 8 e3ists !ith an element ? YY nt$8%%2 else d*msNo#t'#t.'#tNline$?Inde3 8 does not e3ists?%2 end i 2 nt.e3tend2 d*msNo#t'#t.'#tNline$?, ter e3tend o one inde3) Co#nt I ? YY nt.co#nt%2 la" 4I nt.e3ists$D%2 i la" I tr#e then d*msNo#t'#t.'#tNline$?Inde3 D e3ists !ith an element ? YY nt$D%%2 else d*msNo#t'#t.'#tNline$?Inde3 D does not e3ists?%2 end i 2 la" 4I nt.e3ists$E%2 i la" I tr#e then d*msNo#t'#t.'#tNline$?Inde3 E e3ists !ith an element ? YY nt$E%%2 else d*msNo#t'#t.'#tNline$?Inde3 E does not e3ists?%2 end i 2 nt.e3tend$5%2 d*msNo#t'#t.'#tNline$?, ter e3tend o t!o inde3es) Co#nt I ? YY nt.co#nt%2 d*msNo#t'#t.'#tNline$?N+ST+D T,-L+ +L+&+NTS?%2 or i in 6..nt.co#nt loo' d*msNo#t'#t.'#tNline$?ntO? YY i YY ?P I ? YY nt$i%%2 end loo'2 nt$D% 4I ?e?2 nt$E% 4I ? ?2 nt$W% 4I ?"?2 d*msNo#t'#t.'#tNline$?,CT+R ,SSIN:NIN: ;,LU+S TO +/T+ND+D +L+&+NTS)
N+ST+D T,-L+

or i in 6..nt.co#nt loo' d*msNo#t'#t.'#tNline$?ntO? YY i YY ?P I ? YY nt$i%%2 end loo'2 nt.e3tend$D)5%2 d*msNo#t'#t.'#tNline$?, ter e3tend o ive inde3es) Co#nt I ? YY nt.co#nt%2 d*msNo#t'#t.'#tNline$?N+ST+D T,-L+ +L+&+NTS?%2 or i in 6..nt.co#nt loo' d*msNo#t'#t.'#tNline$?ntO? YY i YY ?P I ? YY nt$i%%2

+L+&+NTS?%2

Copy rights are reserved.

end loo'2 nt.trim2 d*msNo#t'#t.'#tNline$?, ter trim o one inde3) Co#nt I ? YY nt.co#nt%2 nt.trim$8%2 d*msNo#t'#t.'#tNline$?, ter trim o three inde3s) Co#nt I ? YY nt.co#nt%2 d*msNo#t'#t.'#tNline$?,CT+R TRI&) N+ST+D T,-L+ +L+&+NTS?%2 or i in 6..nt.co#nt loo' d*msNo#t'#t.'#tNline$?ntO? YY i YY ?P I ? YY nt$i%%2 end loo'2 nt.delete$6%2 d*msNo#t'#t.'#tNline$?, ter delete o irst inde3) Co#nt I ? YY nt.co#nt%2 d*msNo#t'#t.'#tNline$?N+ST+D T,-L+ +L+&+NTS?%2 or i in 5..nt.co#ntH6 loo' d*msNo#t'#t.'#tNline$?ntO? YY i YY ?P I ? YY nt$i%%2 end loo'2 nt.delete$4%2 d*msNo#t'#t.'#tNline$?, ter delete o o#rth inde3) Co#nt I ? YY nt.co#nt%2 d*msNo#t'#t.'#tNline$?N+ST+D T,-L+ +L+&+NTS?%2 or i in 5..8 loo' d*msNo#t'#t.'#tNline$?ntO? YY i YY ?P I ? YY nt$i%%2 end loo'2 or i in D..nt.co#ntH5 loo' d*msNo#t'#t.'#tNline$?ntO? YY i YY ?P I ? YY nt$i%%2 end loo'2 nt.delete2 d*msNo#t'#t.'#tNline$?, ter delete o entire nested ta*le) Co#nt I ? YY nt.co#nt%2
+ND2

146

O#t'#t4

No limit to Nested Ta*les Co#nt I 4 Cirst Inde3 I 6 Last Inde3 I 4 Ne3t Inde3 I 8 Brevio#s Inde3 I 5 N+ST+D T,-L+ +L+&+NTS ntO6P I a ntO5P I * ntO8P I c ntO4P I d Inde3 8 e3ists !ith an element c , ter e3tend o one inde3) Co#nt I D Inde3 D e3ists !ith an element Inde3 E does not e3ists , ter e3tend o t!o inde3es) Co#nt I W N+ST+D T,-L+ +L+&+NTS ntO6P I a ntO5P I * ntO8P I c ntO4P I d ntODP I ntOEP I ntOWP I ,CT+R ,SSIN:NIN: ;,LU+S TO +/T+ND+D +L+&+NTS) N+ST+D T,-L+ +L+&+NTS ntO6P I a

Copy rights are reserved.

ntO5P I * ntO8P I c ntO4P I d ntODP I e ntOEP I ntOWP I " , ter e3tend o ive inde3es) Co#nt I 65 N+ST+D T,-L+ +L+&+NTS ntO6P I a ntO5P I * ntO8P I c ntO4P I d ntODP I e ntOEP I ntOWP I " ntOXP I * ntOTP I * ntO67P I * ntO66P I * ntO65P I * , ter trim o one inde3) Co#nt I 66 , ter trim o three inde3s) Co#nt I X ,CT+R TRI&) N+ST+D T,-L+ +L+&+NTS ntO6P I a ntO5P I * ntO8P I c ntO4P I d ntODP I e ntOEP I ntOWP I " ntOXP I * , ter delete o irst inde3) Co#nt I W N+ST+D T,-L+ +L+&+NTS ntO5P I * ntO8P I c ntO4P I d ntODP I e ntOEP I ntOWP I " ntOXP I * , ter delete o o#rth inde3) Co#nt I E N+ST+D T,-L+ +L+&+NTS ntO5P I * ntO8P I c ntODP I e ntOEP I ntOWP I " ntOXP I * , ter delete o entire nested ta*le) Co#nt I 7 +354
D+CL,R+

147

-+:IN

ty'e t is ta*le o st#dentMro!ty'e2 nt t 4I t$n#ll)n#ll)n#ll)n#ll%2

or i in 6..nt.co#nt loo' select F into nt$i% rom st#dent !here sno I i2 d*msNo#t'#t.'#tNline$?Sno I ? YY nt$i%.sno YY ? Sname I ? YY nt$i%.sname%2 end loo'2
+ND2

Copy rights are reserved.

148
O#t'#t4 Sno Sno Sno Sno I I I I 6 5 8 4 Sname Sname Sname Sname I I I I saketh srin# divya mano"ni

+384

D+CL,R+

ty'e t is ta*le o st#dent.smarksMty'e2 nt t 4I t$n#ll)n#ll)n#ll)n#ll%2


-+:IN

+ND2

or i in 6..nt.co#nt loo' select smarks into nt$i% rom st#dent !here sno I i2 d*msNo#t'#t.'#tNline$?Smarks I ? YY nt$i%%2 end loo'2

O#t'#t4 Smarks Smarks Smarks Smarks +344


D+CL,R+

I I I I

677 577 877 477

ty'e r is record$c6 st#dent.snameMty'e)c5 st#dent.smarksMty'e%2 ty'e t is ta*le o r2 nt t 4I t$n#ll)n#ll)n#ll)n#ll%2

-+:IN

or i in 6..nt.co#nt loo' select sname)smarks into nt$i% rom st#dent !here sno I i2 d*msNo#t'#t.'#tNline$?Sname I ? YY nt$i%.c6 YY ? Smarks I ? YY nt$i%.c5%2 end loo'2
+ND2

O#t'#t4

Sname Sname Sname Sname

I I I I

saketh Smarks I 677 srin# Smarks I 577 divya Smarks I 877 mano"ni Smarks I 477

+3D4

D+CL,R+

ty'e t is ta*le o addr2 nt t 4I t$n#ll%2 c#rsor c is select F rom em'loy2 i n#m*er 4I 62


-+:IN

or v in c loo' select address into nt$i% rom em'loy !here ename I v.ename2 d*msNo#t'#t.'#tNline$?<no I ? YY nt$i%.hno YY ? City I ? YY nt$i%.city%2 end loo'2
+ND2

O#t'#t4

<no I 66 City I hyd <no I 55 City I *an" <no I 88 City I kochi

Copy rights are reserved.

+3E4
D+CL,R+

149
ty'e t is varray$D% o varchar$5%2 nt6 t2 nt5 t 4I t$%2 i nt6 is n#ll then d*msNo#t'#t.'#tNline$?nt6 else d*msNo#t'#t.'#tNline$?nt6 end i 2 i nt5 is n#ll then d*msNo#t'#t.'#tNline$?nt5 else d*msNo#t'#t.'#tNline$?nt5 end i 2 is n#ll?%2 is not n#ll?%2 is n#ll?%2 is not n#ll?%2

-+:IN

+ND2

O#t'#t4 nt6 is n#ll nt5 is not n#ll IND+/(-. T,-L+S ,n inde3(*y ta*le has no limit on its siUe. +lements are inserted into inde3(*y ta*le !hose inde3 may start non(seV#entially incl#din" ne"ative inte"ers. Synta34 Ty'e 0type_name$ is ta*le o 0table_type$ inde3 *y *inaryNinte"er2 +34
D+CL,R+

ty'e t is ta*le o varchar$5% inde3 *y *inaryNinte"er2 i*t t2 la" *oolean2

-+:IN

i*t$6% 4I ?a?2 i*t$(57% 4I ?*?2 i*t$87% 4I ?c?2 i*t$677% 4I ?d?2 i i*t.limit is n#ll then d*msNo#t'#t.'#tNline$?No limit to Inde3 *y Ta*les?%2 else d*msNo#t'#t.'#tNline$?Limit I ? YY i*t.limit%2 end i 2 d*msNo#t'#t.'#tNline$?Co#nt I ? YY i*t.co#nt%2 d*msNo#t'#t.'#tNline$?Cirst Inde3 I ? YY i*t. irst%2 d*msNo#t'#t.'#tNline$?Last Inde3 I ? YY i*t.last%2 d*msNo#t'#t.'#tNline$?Ne3t Inde3 I ? YY i*t.ne3t$5%%2 d*msNo#t'#t.'#tNline$?Brevio#s Inde3 I ? YY i*t.'rior$8%%2 d*msNo#t'#t.'#tNline$?IND+/ -. T,-L+ +L+&+NTS?%2 d*msNo#t'#t.'#tNline$?i*tO(57P I ? YY i*t$(57%%2 d*msNo#t'#t.'#tNline$?i*tO6P I ? YY i*t$6%%2 d*msNo#t'#t.'#tNline$?i*tO87P I ? YY i*t$87%%2 d*msNo#t'#t.'#tNline$?i*tO677P I ? YY i*t$677%%2 la" 4I i*t.e3ists$87%2 i la" I tr#e then d*msNo#t'#t.'#tNline$?Inde3 87 e3ists !ith an element ? YY i*t$87%%2 else d*msNo#t'#t.'#tNline$?Inde3 87 does not e3ists?%2 end i 2

Copy rights are reserved.

la" 4I i*t.e3ists$D7%2 i la" I tr#e then d*msNo#t'#t.'#tNline$?Inde3 D7 e3ists !ith an element ? YY i*t$87%%2 else d*msNo#t'#t.'#tNline$?Inde3 D7 does not e3ists?%2 end i 2 i*t.delete$6%2 d*msNo#t'#t.'#tNline$?, ter delete o irst inde3) Co#nt I ? YY i*t.co#nt%2 i*t.delete$87%2 d*msNo#t'#t.'#tNline$?, ter delete o inde3 thirty) Co#nt I ? YY i*t.co#nt%2 d*msNo#t'#t.'#tNline$?IND+/ -. T,-L+ +L+&+NTS?%2 d*msNo#t'#t.'#tNline$?i*tO(57P I ? YY i*t$(57%%2 d*msNo#t'#t.'#tNline$?i*tO677P I ? YY i*t$677%%2 i*t.delete2 d*msNo#t'#t.'#tNline$?, ter delete o entire inde3(*y ta*le) Co#nt I ? YY i*t.co#nt%2
+ND2

150

O#t'#t4

No limit to Inde3 *y Ta*les Co#nt I 4 Cirst Inde3 I (57 Last Inde3 I 677 Ne3t Inde3 I 87 Brevio#s Inde3 I 6 IND+/ -. T,-L+ +L+&+NTS i*tO(57P I * i*tO6P I a i*tO87P I c i*tO677P I d Inde3 87 e3ists !ith an element c Inde3 D7 does not e3ists , ter delete o irst inde3) Co#nt I 8 , ter delete o inde3 thirty) Co#nt I 5 IND+/ -. T,-L+ +L+&+NTS i*tO(57P I * i*tO677P I d , ter delete o entire inde3(*y ta*le) Co#nt I 7 DICC+R+NC+S ,&ON: COLL+CTIONS ;arrays has limit) nested ta*les and inde3(*y ta*les has no limit. ;arrays and nested ta*les m#st *e initialiUed *e ore assi"nment o elements) in inde3(*y ta*les !e can directly assi"n elements. ;arrays and nested ta*les stored in data*ase) *#t inde3(*y ta*les can not. Nested ta*les and inde3(*y ta*les are BLASQL ta*les) *#t varrays can not. Geys m#st *e 'ositive in case o nested ta*les and varrays) in case o inde3(*y ta*les keys can *e 'ositive or ne"ative. Re erencin" none3istent elements raises SU-SCRIBTN-+.ONDNCOUNT in *oth nested ta*les and varrays) *#t in case o inde3(*y ta*les NOND,T,NCOUND raises. Geys are seV#ential in *oth nested ta*les and varrays) non(seV#ential in inde3(*y ta*les. Individ#al inde3es can *e deleted in *oth nested ta*les and inde3(*y ta*les) *#t in varrays can not. Individ#al inde3es can *e trimmed in *oth nested ta*les and varrays) *#t in inde3(*y ta*les can not.

Copy rights are reserved.

151
Individ#al inde3es can *e e3tended in *oth nested ta*les and varrays) *#t in inde3(*y ta*les can not.

&ULTIL+;+L COLL+CTIONS Collections o more than one dimension !hich is a collection o kno!n as m#ltilevel collections. collections)

Synta34 Ty'e 0type_name1$ is ta*le o 0table_type$ inde3 *y *inaryNinte"er2 Ty'e 0type_name2$ is varray$0limit1% Y ta*le Y o 0type_name1$ < inde3 *y *inaryNinte"er2 +364
D+CL,R+

-+:IN

ty'e t6 is ta*le o varchar$5% inde3 *y *inaryNinte"er2 ty'e t5 is varray$D% o t62 va t5 4I t5$%2 c n#m*er 4I TW2 la" *oolean2 va.e3tend$4%2 d*msNo#t'#t.'#tNline$?Co#nt I ? YY va.co#nt%2 d*msNo#t'#t.'#tNline$?Limit I ? YY va.limit%2 or i in 6..va.co#nt loo' or K in 6..va.co#nt loo' va$i%$K% 4I chr$c%2 c 4I c H 62 end loo'2 end loo'2 d*msNo#t'#t.'#tNline$?;,RR,. +L+&+NTS?%2 or i in 6..va.co#nt loo' or K in 6..va.co#nt loo' d*msNo#t'#t.'#tNline$?vaO? YY i YY ?PO? YY K YY ?P I ? YY va$i%

$K%%2

end loo'2 end loo'2 d*msNo#t'#t.'#tNline$?Cirst inde3 I ? YY va. irst%2 d*msNo#t'#t.'#tNline$?Last inde3 I ? YY va.last%2 d*msNo#t'#t.'#tNline$?Ne3t inde3 I ? YY va.ne3t$5%%2 d*msNo#t'#t.'#tNline$?Brevio#s inde3 I ? YY va.'rior$8%%2 la" 4I va.e3ists$5%2 i la" I tr#e then d*msNo#t'#t.'#tNline$?Inde3 5 e3ists?%2 else d*msNo#t'#t.'#tNline$?Inde3 5 e3ists?%2 end i 2 va.e3tend2 va$6%$D% 4I ?V?2 va$5%$D% 4I ?r?2 va$8%$D% 4I ?s?2 va$4%$D% 4I ?t?2 va$D%$6% 4I ?#?2 va$D%$5% 4I ?v?2 va$D%$8% 4I ?!?2 va$D%$4% 4I ?3?2 va$D%$D% 4I ?y?2 d*msNo#t'#t.'#tNline$?, ter e3tend o one inde3) Co#nt I ? YY va.co#nt%2 d*msNo#t'#t.'#tNline$?;,RR,. +L+&+NTS?%2

Copy rights are reserved.

or i in 6..va.co#nt loo' or K in 6..va.co#nt loo' d*msNo#t'#t.'#tNline$?vaO? YY i YY ?PO? YY K YY ?P I ? YY va$i% $K%%2

152

end loo'2 end loo'2 va.trim2 d*msNo#t'#t.'#tNline$?, ter trim o one inde3) Co#nt I ? YY va.co#nt%2 va.trim$5%2 d*msNo#t'#t.'#tNline$?, ter trim o t!o inde3es) Co#nt I ? YY va.co#nt%2 d*msNo#t'#t.'#tNline$?;,RR,. +L+&+NTS?%2 or i in 6..va.co#nt loo' or K in 6..va.co#nt loo' d*msNo#t'#t.'#tNline$?vaO? YY i YY ?PO? YY K YY ?P I ? YY va$i% $K%%2 end loo'2 end loo'2 va.delete2 d*msNo#t'#t.'#tNline$?, ter delete o entire varray) Co#nt I ? YY va.co#nt%2
+ND2

O#t'#t4 Co#nt I 4 Limit I D ;,RR,. +L+&+NTS vaO6PO6P I a vaO6PO5P I * vaO6PO8P I c vaO6PO4P I d vaO5PO6P I e vaO5PO5P I vaO5PO8P I " vaO5PO4P I h vaO8PO6P I i vaO8PO5P I K vaO8PO8P I k vaO8PO4P I l vaO4PO6P I m vaO4PO5P I n vaO4PO8P I o vaO4PO4P I ' Cirst inde3 I 6 Last inde3 I 4 Ne3t inde3 I 8 Brevio#s inde3 I 5 Inde3 5 e3ists , ter e3tend o one inde3) Co#nt I D ;,RR,. +L+&+NTS vaO6PO6P I a vaO6PO5P I * vaO6PO8P I c vaO6PO4P I d vaO6PODP I V vaO5PO6P I e vaO5PO5P I vaO5PO8P I " vaO5PO4P I h vaO5PODP I r

Copy rights are reserved.

vaO8PO6P I i vaO8PO5P I K vaO8PO8P I k vaO8PO4P I l vaO8PODP I s vaO4PO6P I m vaO4PO5P I n vaO4PO8P I o vaO4PO4P I ' vaO4PODP I t vaODPO6P I # vaODPO5P I v vaODPO8P I ! vaODPO4P I 3 vaODPODP I y , ter trim o one inde3) Co#nt I 4 , ter trim o t!o inde3es) Co#nt I 5 ;,RR,. +L+&+NTS vaO6PO6P I a vaO6PO5P I * vaO5PO6P I e vaO5PO5P I , ter delete o entire varray) Co#nt I 7 +354
D+CL,R+

153

-+:IN

ty'e t6 is ta*le o varchar$5% inde3 *y *inaryNinte"er2 ty'e t5 is ta*le o t62 nt t5 4I t5$%2 c n#m*er 4I ED2 v n#m*er 4I 62 la" *oolean2 nt.e3tend$4%2 d*msNo#t'#t.'#tNline$?Co#nt I ? YY nt.co#nt%2 i nt.limit is n#ll then d*msNo#t'#t.'#tNline$?No limit to Nested Ta*les?%2 else d*msNo#t'#t.'#tNline$?Limit I ? YY nt.limit%2 end i 2 or i in 6..nt.co#nt loo' or K in 6..nt.co#nt loo' nt$i%$K% 4I chr$c%2 c 4I c H 62 i c I T6 then c 4I TW2 end i 2 end loo'2 end loo'2 d*msNo#t'#t.'#tNline$?N+ST+D T,-L+ +L+&+NTS?%2 or i in 6..nt.co#nt loo' or K in 6..nt.co#nt loo' d*msNo#t'#t.'#tNline$?ntO? YY i YY ?PO? YY K YY ?P I ? YY nt$i%$K%%2 end loo'2 end loo'2 d*msNo#t'#t.'#tNline$?Cirst inde3 I ? YY nt. irst%2 d*msNo#t'#t.'#tNline$?Last inde3 I ? YY nt.last%2 d*msNo#t'#t.'#tNline$?Ne3t inde3 I ? YY nt.ne3t$5%%2 d*msNo#t'#t.'#tNline$?Brevio#s inde3 I ? YY nt.'rior$8%%2 la" 4I nt.e3ists$5%2 i la" I tr#e then

Copy rights are reserved.

d*msNo#t'#t.'#tNline$?Inde3 5 e3ists?%2 else d*msNo#t'#t.'#tNline$?Inde3 5 e3ists?%2 end i 2 nt.e3tend$5%2 nt$6%$D% 4I ?Q?2 nt$6%$E% 4I ?R?2 nt$5%$D% 4I ?S?2 nt$5%$E% 4I ?T?2 nt$8%$D% 4I ?U?2 nt$8%$E% 4I ?;?2 nt$4%$D% 4I ?9?2 nt$4%$E% 4I ?/?2 nt$D%$6% 4I ?.?2 nt$D%$5% 4I ?[?2 nt$D%$8% 4I ?a?2 nt$D%$4% 4I ?*?2 nt$D%$D% 4I ?c?2 nt$D%$E% 4I ?d?2 nt$E%$6% 4I ?e?2 nt$E%$5% 4I ? ?2 nt$E%$8% 4I ?"?2 nt$E%$4% 4I ?h?2 nt$E%$D% 4I ?i?2 nt$E%$E% 4I ?K?2 d*msNo#t'#t.'#tNline$?, ter e3tend o one inde3) Co#nt I ? YY nt.co#nt%2 d*msNo#t'#t.'#tNline$?N+ST+D T,-L+ +L+&+NTS?%2 or i in 6..nt.co#nt loo' or K in 6..nt.co#nt loo' d*msNo#t'#t.'#tNline$?ntO? YY i YY ?PO? YY K YY ?P I ? YY nt$i%$K%%2 end loo'2 end loo'2 nt.trim2 d*msNo#t'#t.'#tNline$?, ter trim o one inde3e) Co#nt I ? YY nt.co#nt%2 nt.trim$5%2 d*msNo#t'#t.'#tNline$?, ter trim o t!o inde3es) Co#nt I ? YY nt.co#nt%2 d*msNo#t'#t.'#tNline$?N+ST+D T,-L+ +L+&+NTS?%2 or i in 6..nt.co#nt loo' or K in 6..nt.co#nt loo' d*msNo#t'#t.'#tNline$?ntO? YY i YY ?PO? YY K YY ?P I ? YY nt$i%$K%%2 end loo'2 end loo'2 nt.delete$5%2 d*msNo#t'#t.'#tNline$?, ter delete o second inde3) Co#nt I ? YY nt.co#nt%2 d*msNo#t'#t.'#tNline$?N+ST+D T,-L+ +L+&+NTS?%2 loo' e3it !hen v I 42 or K in 6..nt.co#ntH6 loo' d*msNo#t'#t.'#tNline$?ntO? YY v YY ?PO? YY K YY ?P I ? YY nt$v% $K%%2 end loo'2 v 4I v H 62 i vI 5 then v 4I 82 end i 2 end loo'2 nt.delete2

154

Copy rights are reserved.

d*msNo#t'#t.'#tNline$?, ter delete o entire nested ta*le) Co#nt I ? YY nt.co#nt%2


+ND2

155

O#t'#t4 Co#nt I 4 No limit to Nested Ta*les N+ST+D T,-L+ +L+&+NTS ntO6PO6P I , ntO6PO5P I ntO6PO8P I C ntO6PO4P I D ntO5PO6P I + ntO5PO5P I C ntO5PO8P I : ntO5PO4P I < ntO8PO6P I I ntO8PO5P I @ ntO8PO8P I G ntO8PO4P I L ntO4PO6P I & ntO4PO5P I N ntO4PO8P I O ntO4PO4P I B Cirst inde3 I 6 Last inde3 I 4 Ne3t inde3 I 8 Brevio#s inde3 I 5 Inde3 5 e3ists , ter e3tend o one inde3) Co#nt I E N+ST+D T,-L+ +L+&+NTS ntO6PO6P I , ntO6PO5P I ntO6PO8P I C ntO6PO4P I D ntO6PODP I Q ntO6POEP I R ntO5PO6P I + ntO5PO5P I C ntO5PO8P I : ntO5PO4P I < ntO5PODP I S ntO5POEP I T ntO8PO6P I I ntO8PO5P I @ ntO8PO8P I G ntO8PO4P I L ntO8PODP I U ntO8POEP I ; ntO4PO6P I & ntO4PO5P I N ntO4PO8P I O ntO4PO4P I B ntO4PODP I 9 ntO4POEP I / ntODPO6P I . ntODPO5P I [ ntODPO8P I a ntODPO4P I * ntODPODP I c ntODPOEP I d

Copy rights are reserved.

ntOEPO6P I e ntOEPO5P I ntOEPO8P I " ntOEPO4P I h ntOEPODP I i ntOEPOEP I K , ter trim o one inde3e) Co#nt I D , ter trim o t!o inde3es) Co#nt I 8 N+ST+D T,-L+ +L+&+NTS ntO6PO6P I , ntO6PO5P I ntO6PO8P I C ntO5PO6P I + ntO5PO5P I C ntO5PO8P I : ntO8PO6P I I ntO8PO5P I @ ntO8PO8P I G , ter delete o second inde3) Co#nt I 5 N+ST+D T,-L+ +L+&+NTS ntO6PO6P I , ntO6PO5P I ntO6PO8P I C ntO8PO6P I I ntO8PO5P I @ ntO8PO8P I G , ter delete o entire nested ta*le) Co#nt I 7 +384
D+CL,R+

156

-+:IN

ty'e t6 is ta*le o varchar$5% inde3 *y *inaryNinte"er2 ty'e t5 is ta*le o t6 inde3 *y *inaryNinte"er2 i*t t52 la" *oolean2 d*msNo#t'#t.'#tNline$?Co#nt I ? YY i*t.co#nt%2 i i*t.limit is n#ll then d*msNo#t'#t.'#tNline$?No limit to Inde3(*y Ta*les?%2 else d*msNo#t'#t.'#tNline$?Limit I ? YY i*t.limit%2 end i 2 i*t$6%$6% 4I ?a?2 i*t$4%$D% 4I ?*?2 i*t$D%$6% 4I ?c?2 i*t$E%$5% 4I ?d?2 i*t$X%$8% 4I ?e?2 i*t$8%$4% 4I ? ?2 d*msNo#t'#t.'#tNline$?IND+/(-. T,-L+ +L+&+NTS?%2 d*msNo#t'#t.'#tNline$?i*t$O6PO6P I ? YY i*t$6%$6%%2 d*msNo#t'#t.'#tNline$?i*t$O4PODP I ? YY i*t$4%$D%%2 d*msNo#t'#t.'#tNline$?i*t$ODPO6P I ? YY i*t$D%$6%%2 d*msNo#t'#t.'#tNline$?i*t$OEPO5P I ? YY i*t$E%$5%%2 d*msNo#t'#t.'#tNline$?i*t$OXPO8P I ? YY i*t$X%$8%%2 d*msNo#t'#t.'#tNline$?i*t$O8PO4P I ? YY i*t$8%$4%%2 d*msNo#t'#t.'#tNline$?Cirst Inde3 I ? YY i*t. irst%2 d*msNo#t'#t.'#tNline$?Last Inde3 I ? YY i*t.last%2 d*msNo#t'#t.'#tNline$?Ne3t Inde3 I ? YY i*t.ne3t$8%%2 d*msNo#t'#t.'#tNline$?Brior Inde3 I ? YY i*t.'rior$X%%2 i*t$6%$5% 4I ?"?2 i*t$6%$8% 4I ?h?2 i*t$6%$4% 4I ?i?2

Copy rights are reserved.

i*t$6%$D% 4I ?k?2 i*t$6%$E% 4I ?l?2 i*t$6%$W% 4I ?m?2 i*t$6%$X% 4I ?n?2 d*msNo#t'#t.'#tNline$?Co#nt I ? YY i*t.co#nt%2 d*msNo#t'#t.'#tNline$?IND+/(-. T,-L+ +L+&+NTS?%2 or i in 6..X loo' d*msNo#t'#t.'#tNline$?i*tO6PO? YY i YY ?P I ? YY i*t$6%$i%%2 end loo'2 d*msNo#t'#t.'#tNline$?i*t$O4PODP I ? YY i*t$4%$D%%2 d*msNo#t'#t.'#tNline$?i*t$ODPO6P I ? YY i*t$D%$6%%2 d*msNo#t'#t.'#tNline$?i*t$OEPO5P I ? YY i*t$E%$5%%2 d*msNo#t'#t.'#tNline$?i*t$OXPO8P I ? YY i*t$X%$8%%2 d*msNo#t'#t.'#tNline$?i*t$O8PO4P I ? YY i*t$8%$4%%2 la" 4I i*t.e3ists$8%2 i la" I tr#e then d*msNo#t'#t.'#tNline$?Inde3 8 e3ists?%2 else d*msNo#t'#t.'#tNline$?Inde3 8 e3ists?%2 end i 2 i*t.delete$6%2 d*msNo#t'#t.'#tNline$?, ter delete o irst inde3) Co#nt I ? YY i*t.co#nt%2 i*t.delete$4%2 d*msNo#t'#t.'#tNline$?, ter delete o o#rth inde3) Co#nt I ? YY i*t.co#nt%2 d*msNo#t'#t.'#tNline$?IND+/(-. T,-L+ +L+&+NTS?%2 d*msNo#t'#t.'#tNline$?i*t$ODPO6P I ? YY i*t$D%$6%%2 d*msNo#t'#t.'#tNline$?i*t$OEPO5P I ? YY i*t$E%$5%%2 d*msNo#t'#t.'#tNline$?i*t$OXPO8P I ? YY i*t$X%$8%%2 d*msNo#t'#t.'#tNline$?i*t$O8PO4P I ? YY i*t$8%$4%%2 i*t.delete2 d*msNo#t'#t.'#tNline$?, ter delete o entire inde3(*y ta*le) Co#nt I ? YY i*t.co#nt%2
+ND2

157

O#t'#t4 Co#nt I 7 No limit to Inde3(*y Ta*les IND+/(-. T,-L+ +L+&+NTS i*t$O6PO6P I a i*t$O4PODP I * i*t$ODPO6P I c i*t$OEPO5P I d i*t$OXPO8P I e i*t$O8PO4P I Cirst Inde3 I 6 Last Inde3 I X Ne3t Inde3 I 4 Brior Inde3 I E Co#nt I E IND+/(-. T,-L+ +L+&+NTS i*tO6PO6P I a i*tO6PO5P I " i*tO6PO8P I h i*tO6PO4P I i i*tO6PODP I k i*tO6POEP I l i*tO6POWP I m i*tO6POXP I n i*t$O4PODP I *

Copy rights are reserved.

i*t$ODPO6P I c i*t$OEPO5P I d i*t$OXPO8P I e i*t$O8PO4P I Inde3 8 e3ists , ter delete o irst inde3) Co#nt I D , ter delete o o#rth inde3) Co#nt I 4 IND+/(-. T,-L+ +L+&+NTS i*t$ODPO6P I c i*t$OEPO5P I d i*t$OXPO8P I e i*t$O8PO4P I , ter delete o entire inde3(*y ta*le) Co#nt I 7 +344
D+CL,R+

158

ty'e t6 is ta*le o varchar$5% inde3 *y *inaryNinte"er2 ty'e t5 is ta*le o t6 inde3 *y *inaryNinte"er2 ty'e t8 is ta*le o t52 nt t8 4I t8$%2 c n#m*er 4I ED2

-+:IN

nt.e3tend$5%2 d*msNo#t'#t.'#tNline$?Co#nt I ? YY nt.co#nt%2 or i in 6..nt.co#nt loo' or K in 6..nt.co#nt loo' or k in 6..nt.co#nt loo' nt$i%$K%$k% 4I chr$c%2 c 4I c H 62 end loo'2 end loo'2 end loo'2 d*msNo#t'#t.'#tNline$?N+ST+D T,-L+ +L+&+NTS?%2 or i in 6..nt.co#nt loo' or K in 6..nt.co#nt loo' or k in 6..nt.co#nt loo' d*msNo#t'#t.'#tNline$?ntO? YY i YY ?PO? YY K YY ?PO? YY k YY ?P I ? YY nt$i%$K%$k%%2 end loo'2 end loo'2 end loo'2
+ND2

O#t'#t4 Co#nt I 5 N+ST+D T,-L+ +L+&+NTS ntO6PO6PO6P I , ntO6PO6PO5P I ntO6PO5PO6P I C ntO6PO5PO5P I D ntO5PO6PO6P I + ntO5PO6PO5P I C ntO5PO5PO6P I : ntO5PO5PO5P I < O-@+CTS US+D IN T<+ +/,&BL+S
SQL1

select F rom st#dent2

Copy rights are reserved.

SNO (((((((((( 6 5 8 4
SQL1

SN,&+ (((((((((((((( saketh srin# divya mano"ni

S&,RGS (((((((((( 677 577 877 477

159

create or re'lace ty'e addr as o*Kect$hno n#m*er$5%)city varchar$67%%2A


SQL1

select F rom em'loy2

+N,&+ @O,DDR+SS$<NO) CIT.% (((((((((( (((((((((( ((((((((((((((((((((((((((((( RanKit clerk ,DDR$66) ?hyd?% Satish mana"er ,DDR$55) ?*an"?% Srin# en"ineer ,DDR$88) ?kochi?%

+RROR <,NDLIN:
BLASQL im'lements error handlin" !ith e3ce'tions and e3ce'tion handlers. +3ce'tions can *e associated !ith oracle errors or !ith yo#r o!n #ser(de ined errors. -y #sin" e3ce'tions and e3ce'tion handlers) yo# can make yo#r BLASQL 'ro"rams ro*#st and a*le to deal !ith *oth #ne3'ected and e3'ected errors d#rin" e3ec#tion. +RROR T.B+S Com'ile(time errors R#ntime errors

+rrors that occ#r d#rin" the com'ilation 'hase are detected *y the BLASQL en"ine and re'orted *ack to the #ser) !e have to correct them. R#ntime errors are detected *y the BLASQL r#ntime en"ine !hich can 'ro"rammatically raise and ca#"ht *y e3ce'tion handlers. +3ce'tions are desi"ned or r#n(time error handlin") rather than com'ile(time error handlin". <,NDLIN: +/C+BTIONS 9hen e3ce'tion is raised) control 'asses to the e3ce'tion section o the *lock. The e3ce'tion section consists o handlers or some or all o the e3ce'tions. ,n e3ce'tion handler contains the code that is e3ec#ted !hen the error associated !ith the e3ce'tion occ#rs) and the e3ce'tion is raised. Synta34
+/C+BTION

9hen e3ce'tionNname then SeV#enceNo Nstatements2 9hen e3ce'tionNname then SeV#enceNo Nstatements2 9hen others then SeV#enceNo Nstatements2 +/C+BTION T.B+S
+ND2

Copy rights are reserved.

160
Brede ined e3ce'tions User(de ined e3ce'tions

BR+D+CIN+D +/C+BTIONS Oracle has 'rede ined several e3ce'tions that corres'onds to the most common oracle errors. Like the 'rede ined ty'es) the identi iers o these e3ce'tions are de ined in the ST,ND,RD 'acka"e. -eca#se o this) they are already availa*le to the 'ro"ram) it is not necessary to declare them in the declarative secion. +364
D+CL,R+

a n#m*er2 * varchar$5%2 vNmarks n#m*er2 c#rsor c is select F rom st#dent2 ty'e t is varray$8% o varchar$5%2 va t 4I t$?a?)?*?%2 va6 t2

-+:IN (( NOND,T,NCOUND -+:IN +/C+BTION

select smarks into vNmarks rom st#dent !here sno I D72 !hen noNdataN o#nd then d*msNo#t'#t.'#tNline$?Invalid st#dent n#m*er?%2

+ND2 (( CURSORN,LR+,D.NOB+N -+:IN

o'en c2 o'en c2
+/C+BTION

+ND2

!hen c#rsorNalreadyNo'en then d*msNo#t'#t.'#tNline$?C#rsor is already o'ened?%2

(( IN;,LIDNCURSOR -+:IN

+/C+BTION

close c2 o'en c2 close c2 close c2

!hen invalidNc#rsor then d*msNo#t'#t.'#tNline$?C#rsor is already closed?%2


+ND2 (( TOON&,N.NRO9S -+:IN +/C+BTION

select smarks into vNmarks rom st#dent !here sno 1 62

!hen tooNmanyNro!s then d*msNo#t'#t.'#tNline$?Too many val#es are comin" to marks varia*le?%2
+ND2 (( [+RONDI;ID+ -+:IN

a 4I DA72

+/C+BTION

o'eration?%2

!hen UeroNdivide then d*msNo#t'#t.'#tNline$?Divided *y Uero ( invalid

+ND2 (( ;,LU+N+RROR -+:IN +/C+BTION

* 4I ?saketh?2

Copy rights are reserved.

+ND2 (( IN;,LIDNNU&-+R -+:IN +/C+BTION

!hen val#eNerror then d*msNo#t'#t.'#tNline$?Invalid strin" len"th?%2

161

insert into st#dent val#es$?a?)?srin#?)677%2 !hen invalidNn#m*er then d*msNo#t'#t.'#tNline$?Invalid n#m*er?%2

+ND2 (( SU-SCRIBTNOUTSID+NLI&IT -+:IN

va$4% 4I ?c?2

+/C+BTION

+ND2 (( SU-SCRIBTN-+.ONDNCOUNT -+:IN +/C+BTION

!hen s#*scri'tNo#tsideNlimit then d*msNo#t'#t.'#tNline$?Inde3 is "reater than the limit?%2

va$8% 4I ?c?2 !hen s#*scri'tN*eyondNco#nt then d*msNo#t'#t.'#tNline$?Inde3 is "reater than the co#nt?%2

+ND2 (( COLL+CTIONNISNNULL -+:IN

va6$6% 4I ?a?2

+/C+BTION

+ND2

!hen collectionNisNn#ll then d*msNo#t'#t.'#tNline$?Collection is em'ty?%2

+ND2

((

O#t'#t4 Invalid st#dent n#m*er C#rsor is already o'ened C#rsor is already closed Too many val#es are comin" to marks varia*le Divided *y Uero ( invalid o'eration Invalid strin" len"th Invalid n#m*er Inde3 is "reater than the limit Inde3 is "reater than the co#nt Collection is em'ty +354
D+CL,R+ -+:IN

c n#m*er2 c 4I DA72

+/C+BTION

!hen UeroNdivide then d*msNo#t'#t.'#tNline$?Invalid O'eration?%2 !hen others then d*msNo#t'#t.'#tNline$?Crom OT<+RS handler4 Invalid O'eration?%2
+ND2

O#t'#t4

Invalid O'eration

US+R(D+CIN+D +/C+BTIONS

Copy rights are reserved.

, #ser(de ined e3ce'tion is an error that is de ined *y the 'ro"rammer. User( de ined e3ce'tions are declared in the declarative secion o a BLASQL *lock. @#st like varia*les) e3e'tions have a ty'e +/C+BTION and sco'e.
R,ISIN: +/C+BTIONS

162

User(de ined e3ce'tions are raised e3'licitly via the R,IS+ statement. +34
D+CL,R+ -+:IN

e e3ce'tion2 raise e2 !hen e then d*msNo#t'#t.'#tNline$?e is raised?%2

+/C+BTION

+ND2

O#t'#t4

e is raised

-ULIT(IN +RROR CUNCTIONS


SQLCOD+ ,ND SQL+RR&

ret#rns the c#rrent error code) and SQL+RR& ret#rns the c#rrent error messa"e te3t2 Cor #ser(de ined e3ce'tion SQLCOD+ ret#rns 6 and SQL+RR& ret#rns Q#ser(dei ned e3ce'tionR. SQL+RR& !iil take only ne"ative val#e e3ce't 677. I any 'ositive val#e other than 677 ret#rns non(oracle e3ce'tion.
SQLCOD+

+364

D+CL,R+

e e3ce'tion2 vNdname varchar$67%2


-+:IN (( US+R(D+CIN+D +/C+BTION -+:IN

raise e2

+/C+BTION

+ND2

!hen e then d*msNo#t'#t.'#tNline$SQLCOD+ YY ? ? YY

SQL+RR&%2

(( BR+D+CIN+D +/C+BTION -+:IN +/C+BTION

select dname into vNdname rom de't !here de'tno I D72 !hen noNdataN o#nd then d*msNo#t'#t.'#tNline$SQLCOD+ YY ? ? YY
SQL+RR&%2

+ND2 +ND2

O#t'#t4

6 User(De ined +3ce'tion 677 OR,(764784 no data o#nd


-+:IN

+354

d*msNo#t'#t.'#tNline$SQL+RR&$677%%2 d*msNo#t'#t.'#tNline$SQL+RR&$7%%2

Copy rights are reserved.

d*msNo#t'#t.'#tNline$SQL+RR&$6%%2 d*msNo#t'#t.'#tNline$SQL+RR&$(677%%2 d*msNo#t'#t.'#tNline$SQL+RR&$(D77%%2 d*msNo#t'#t.'#tNline$SQL+RR&$577%%2 d*msNo#t'#t.'#tNline$SQL+RR&$(T77%%2


+ND2

163

O#t'#t4

OR,(764784 no data o#nd OR,(77774 normal) s#ccess #l com'letion User(De ined +3ce'tion OR,(776774 no data o#nd OR,(77D774 &essa"e D77 not o#nd2 'rod#ctIRD-&S2 acilityIOR, (5774 non(OR,CL+ e3ce'tion OR,(77T774 invalid SQL statement

D-&SNUTILIT..COR&,TN+RRORNST,CG

The *#ilt(in #nction) like SQL+RR&) ret#rns the messa"e associated !ith the c#rrent error. It di ers rom SQL+RR& in t!o !ays4 Its len"th is not restricted2 it !ill ret#rn the #ll error messa"e strin". .o# can not 'ass an error code n#m*er to this #nction2 it cannot *e #sed to ret#rn the messa"e or a random error code. +34
D+CL,R+ -+:IN

v n#m*er 4I ?a*?2 n#ll2

+/C+BTION

!hen others then d*msNo#t'#t.'#tNline$d*msN#tility. ormatNerrorNstack%2


+ND2

O#t'#t4

declare F +RROR at line 64 OR,(7ED754 BLASQL4 n#meric or val#e error4 character to n#m*er conversion error OR,(7ED654 at line 5

D-&SNUTILIT..COR&,TNC,LLNST,CG

This #nction ret#rns a ormatted strin" sho!in" the e3ec#tion call stack inside yo#r BLASQL a''lication. Its #se #lness is not restricted to error mana"ement2 yo# !ill also ind its handy or tracin" the e3ect#tion o yo#r code. .o# may not #se this #nction in e3ce'tion *lock. +34
-+:IN +ND2

d*msNo#t'#t.'#tNline$d*msN#tility. ormatNcallNstack%2

O#t'#t4

((((( BLASQL Call Stack ((((( O*KectNhandle lineNn#m*er o*KectNname ETWE74WX 5 anonymo#s *lock

D-&SNUTILIT..COR&,TN+RRORN-,CGTR,C+

Copy rights are reserved.

164
It dis'lays the e3ec#tion stack at the 'oint !here an e3ce'tion !as raised. Th#s ) yo# can call this #nction !ith an e3ce'tion section at the to' level o yo#r stack and still ind o#t !here the error !as raised dee' !ithin the call stack. +34
CR+,T+ OR R+BL,C+ BROC+DUR+ B6 IS -+:IN

d*msNo#t'#t.'#tNline$? rom 'roced#re 6?%2 raise val#eNerror2


+ND B62

CR+,T+ OR R+BL,C+ BROC+DUR+ B5 IS -+:IN

+ND B52

d*msNo#t'#t.'#tNline$? rom 'roced#re 5?%2 '62

CR+,T+ OR R+BL,C+ BROC+DUR+ B8 IS -+:IN

+/C+BTION

d*msNo#t'#t.'#tNline$? rom 'roced#re 8?%2 '52

!hen others then d*msNo#t'#t.'#tNline$d*msN#tility. ormatNerrorN*acktra ce%2


+ND B82

O#t'#t4
SQL1

e3ec '8

rom 'roced#re 8 rom 'roced#re 5 rom 'roced#re 6 OR,(7ED654 at bS,G+T<.B6b) line 4 OR,(7ED654 at bS,G+T<.B5b) line 4 OR,(7ED654 at bS,G+T<.B8b) line 4 +/C+BTIONNINIT BR,:&, Usin" this yo# can associate a named e3ce'tion !ith a 'artic#lar oracle error. This "ives yo# the a*ility to tra' this error s'eci ically) rather than via an OT<+RS handler. Synta34 +34
BR,:&, +/C+BTIONNINIT$e&ception_name) D+CL,R+

oracle_error_number%2

e e3ce'tion2 'ra"ma e3ce'tionNinit$e)(64WE%2 c n#m*er2 c 4I DA72 !hen e then d*msNo#t'#t.'#tNline$?Invalid O'eration?%2

-+:IN

+/C+BTION

+ND2

O#t'#t4 Invalid O'eration R,IS+N,BBLIC,TIONN+RROR

Copy rights are reserved.

165
.o# can #se this *#ilt(in #nction to create yo#r o!n error messa"es) !hich can *e more descri'tive than named e3ce'tions. Synta34
R,IS+N,BBLIC,TIONN+RROR$error_number) error_messa e)) Okeep_errors_%la P%2 The -oolean 'arameter keep_errors_%la is o'tional. I it is TRU+) the ne! error is added to the list o errors already raised. I it is C,LS+) !hich is de a#lt) the ne! error !ill re'lace the c#rrent list o errors.

+34

D+CL,R+ -+:IN

c n#m*er2 c 4I DA72

+/C+BTION

+ND2

!hen UeroNdivide then raiseNa''licationNerror$(57555)?Invalid O'eration?%2

O#t'#t4
D+CL,R+

at line 64 OR,(575554 Invalid O'eration OR,(7ED654 at line W


+RROR

+/C+BTION BROB,:,TION +3ce'tions can occ#r in the declarative) the e3ec#ta*le) or the e3ce'tion section o a BLASQL *lock.
+/C+BTION R,IS+D IN T<+ +/+CU,T,-L+ S+CTION

+3ce'tions raised in e3ec#ata*le section can *e handled in c#rrent *lock or o#ter *lock. +364
D+CL,R+ -+:IN -+:IN

e e3ce'tion2 raise e2 !hen e then d*msNo#t'#t.'#tNline$?e is raised?%2

+ND2 +/C+BTION

+ND2

O#t'#t4 +354

e is raised
D+CL,R+

e e3ce'tion2 raise e2

-+:IN -+:IN +ND2 +ND2

O#t'#t4

Copy rights are reserved.

at line 64 OR,(7ED674 BLASQL4 #nhandled #ser(de ined e3ce'tion OR,(7ED654 at line D


+RROR +/C+BTION R,IS+D IN T<+ D+CL,R,TI;+ S+CTION

166

+3ce'tions raised in the declarative secion m#st *e handled in the o#ter *lock. +364
D+CL,R+ -+:IN

c n#m*er$8% 4I ?a*cd?2 d*msNo#t'#t.'#tNline$?<ello?%2 !hen others then d*msNo#t'#t.'#tNline$?Invalid strin" len"th?%2

+/C+BTION

+ND2

O#t'#t4 at line 64 OR,(7ED754 BLASQL4 n#meric or val#e error4 character to n#m*er conversion error OR,(7ED654 at line 5
+RROR

+354
-+:IN D+CL,R+ -+:IN

c n#m*er$8% 4I ?a*cd?2 d*msNo#t'#t.'#tNline$?<ello?%2 !hen others then d*msNo#t'#t.'#tNline$?Invalid strin" len"th?%2

+/C+BTION

+ND2 +/C+BTION

!hen others then d*msNo#t'#t.'#tNline$?Crom o#ter *lock4 Invalid strin" len"th?%2


+ND2

O#t'#t4
+/C+BTION R,IS+D IN T<+ +/C+BTION S+CTION

Crom o#ter *lock4 Invalid strin" len"th

+3ce'tions raised in the declarative secion m#st *e handled in the o#ter *lock. +364
D+CL,R+

-+:IN

e6 e3ce'tion2 e5 e3ce'tion2 raise e62 !hen e6 then d*msNo#t'#t.'#tNline$?e6 is raised?%2 raise e52 !hen e5 then d*msNo#t'#t.'#tNline$?e5 is raised?%2

+/C+BTION

+ND2

O#t'#t4 e6 is raised D+CL,R+ F

Copy rights are reserved.

+RROR at line 64 OR,(7ED674 BLASQL4 #nhandled #ser(de ined e3ce'tion OR,(7ED654 at line T OR,(7ED674 BLASQL4 #nhandled #ser(de ined e3ce'tion +354
D+CL,R+

167

e6 e3ce'tion2 e5 e3ce'tion2
-+:IN -+:IN +/C+BTION

raise e62 !hen e6 then d*msNo#t'#t.'#tNline$?e6 is raised?%2 raise e52 !hen e5 then d*msNo#t'#t.'#tNline$?e5 is raised?%2

+ND2 +/C+BTION

+ND2

!hen e5 then d*msNo#t'#t.'#tNline$?Crom o#ter *lock4 e5 is raised?%2

O#t'#t4 e6 is raised Crom o#ter *lock4 e5 is raised +384


D+CL,R+ -+:IN

e e3ce'tion2 raise e2 !hen e then d*msNo#t'#t.'#tNline$?e is raised?%2 raise e2

+/C+BTION

+ND2

O#t'#t4

e is raised D+CL,R+ F +RROR at line 64 OR,(7ED674 BLASQL4 #nhandled #ser(de ined e3ce'tion OR,(7ED654 at line X OR,(7ED674 BLASQL4 #nhandled #ser(de ined e3ce'tion

R+STRICTIONS .o# can not 'ass e3ce'tion as an ar"#ment to a s#*'ro"ram.

D,T,-,S+ TRI::+RS
Tri""ers are similar to 'roced#res or #nctions in that they are named BLASQL *locks !ith declarative) e3ec#ta*le) and e3ce'tion handlin" sections. , tri""er is e3ec#ted im'licitly !henever the tri""erin" event ha''ens. The act o e3ec#tin" a tri""er is kno!n as irin" the tri""er. R+STRICTIONS ON TRI::+R+S

Copy rights are reserved.

168
Like 'acka"es) tri""ers m#st *e stored as stand(alone o*Kects in the data*ase and cannot *e local to a *lock or 'acka"e. , tri""er does not acce't ar"#ments.

US+ OC TRI::+RS &aintainin" com'le3 inte"rity constraints not 'ossi*le thro#"h declarative constraints ena*le at ta*le creation. ,#ditin" in ormation in a ta*le *y recordin" the chan"es made and !ho made them. ,#tomatically si"nalin" other 'ro"rams that action needs to take 'lace !hen cha"es are made to a ta*le. Ber orm validation on chan"es *ein" made to ta*les. ,#tomate maintenance o the data*ase.

T.B+S OC TRI::+RS C,T+:ORI+S Timin" (( -e ore or , ter Level (( Ro! or Statement Ro! level tri""er ires once or each ro! a ected *y the tri""erin" statement. Ro! level tri""er is identi ied *y the COR +,C< RO9 cla#se. Statement level tri""er ires once either *e ore or a ter the statement. D&L TRI::+R S.NT,/ Create or re'lace tri""er 0tri er_name1 -e ore Y a ter on insert or #'date or delete OCor each ro!P -e"in (( tri""er *ody +nd 0tri er_name12 D&L TRI::+RS , D&L tri""er is ired on an INS+RT) UBD,T+) or D+L+T+ o'eration on a data*ase ta*le. It can *e ired either *e ore or a ter the statement e3ec#tes) and can *e ired once 'er a ected ro!) or once 'er statement. The com*ination o these actors determines the ty'es o the tri""ers. These are a total o 65 'ossi*le ty'es $8 statements F 5 timin" F 5 levels%.
ORD+R OC D&L TRI::+R CIRIN:

D&L Tri""ers Instead o Tri""ers DDL Tri""ers System Tri""ers S#s'end Tri""ers

-e ore statement level -e ore ro! level , ter ro! level , ter statement level

+34 S#''ose !e have a oll!in" ta*le.


SQL1

select F rom st#dent2

Copy rights are reserved.

169

NO N,&+ ((((( ((((((( 6 a 5 * 8 c 4 d

&,RGS (((((((((( 677 577 877 477

,lso !e have tri""erin"N irin"Norder ta*le !ith irin"Norder as the ield.


CR+,T+ OR R+BL,C+ TRI::+R TRI::+R6 -+:IN

*e ore insert on st#dent

insert into tri""erN irin"Norder val#es$?-e ore Statement Level?%2


+ND TRI::+R62 CR+,T+ OR R+BL,C+ TRI::+R TRI::+R5

-+:IN

*e ore insert on st#dent or each ro!

+ND TRI::+R52

insert into tri""erN irin"Norder val#es$?-e ore Ro! Level?%2

CR+,T+ OR R+BL,C+ TRI::+R TRI::+R8 -+:IN

a ter insert on st#dent insert into tri""erN irin"Norder val#es$?, ter Statement Level?%2

+ND TRI::+R82

CR+,T+ OR R+BL,C+ TRI::+R TRI::+R4

a ter insert on st#dent or each ro!


-+:IN

insert into tri""erN irin"Norder val#es$?, ter Ro! Level?%2

+ND TRI::+R42

O#t'#t4

select F rom tri""erN irin"Norder2 no ro!s selected


SQL1

insert into st#dent val#es$D)?e?)D77%2 6 ro! created.


SQL1

select F rom tri""erN irin"Norder2 CIRIN:NORD+R (((((((((((((((((((((((((((((((((((((((((((((((((( -e ore Statement Level -e ore Ro! Level , ter Ro! Level , ter Statement Level
SQL1 SQL1

select F rom st#dent2 NO N,&+ &,RGS (((( (((((((( (((((((((( 6 a 677 5 * 577 8 c 877 4 d 477 D e D77

Copy rights are reserved.

CORR+L,TION ID+NTICI+RS IN RO9(L+;+L TRI::+RS

170

Inside the tri""er) yo# can access the data in the ro! that is c#rrently *ein" 'rocessed. This is accom'lished thro#"h t!o correlation identi iers ( 4old and 4ne!. , correlation identi%ier is a s'ecial kind o BLASQL *ind varia*le. The colon in ront o each indicates that they are *ind varia*les) in the sense o host varia*les #sed in em*edded BLASQL) and indicates that they are not re"#lar BLASQL varia*les. The BLASQL com'iler !ill treat them as records o ty'e Tri""erin"Nta*leMRO9T.B+. ,ltho#"h syntactically they are treated as records) in reality they are not. 4old and 4ne! are also kno!n as pseudorecords) or this reason.
TRI::+RIN: ST,T+&+NT 4OLD (((((((((((((((((((((((((((((((((((((( ((((((((((((((((((((((((((((((((((((((((((((((( INS+RT all 4N+9 ((((((((((((((((((((((((((((

ields are

NULL.

val#es that !ill *e 9hen the statement is

inserted com'leted.
UBD,T+

#'dated com'leted.
D+L+T+

ori"inal val#es or the ro! *e ore the #'date. ori"inal val#es *e ore the ro! is deleted.

ne! val#es that !ill *e !hen the statement is all ields are
NULL.

+34

S#''ose !e have a ta*le called marks !ith ne!Nmarks.


CR+,T+ OR R+BL,C+ TRI::+R OLDNN+9

ields no) oldNmarks)

-+:IN

*e ore insert or #'date or delete on st#dent or each ro! insert into marks val#es$4old.no)4old.marks)4ne!.marks%2

+ND OLDNN+92

O#t'#t4
SQL1

select F rom st#dent2

NO N,&+ &,RGS ((((( ((((((( (((((((((( 6 a 677 5 * 577 8 c 877 4 d 477 D e D77
SQL1

select F rom marks2

no ro!s selected
SQL1

insert into st#dent val#es$E)? ?)E77%2

6 ro! created.
SQL1

select F rom st#dent2

Copy rights are reserved.

171
NO N,&+ &,RGS (((( (((((((( (((((((((( 6 a 677 5 * 577 8 c 877 4 d 477 D e D77 E E77
SQL1

select F rom marks2 NO OLDN&,RGS N+9N&,RGS (((( ((((((((((((((( ((((((((((((((( E77

SQL1

#'date st#dent set marksIDDD !here noID2

6 ro! #'dated.
SQL1

select F rom st#dent2

NO N,&+ &,RGS ((((( ((((((( (((((((((( 6 a 677 5 * 577 8 c 877 4 d 477 D e DDD E E77
SQL1

select F rom marks2

NO OLDN&,RGS N+9N&,RGS (((((( (((((((((((((((( ((((((((((((((( E77 D D77 DDD SQL1 delete st#dent !here no I 52 6 ro! deleted.
SQL1

select F rom st#dent2

NO N,&+ &,RGS (((( (((((((( (((((((((( 6 a 677 8 c 877 4 d 477 D e DDD E E77
SQL1

R+C+R+NCIN: CL,US+

select F rom marks2 NO OLDN&,RGS N+9N&,RGS ((((( (((((((((((((( (((((((((((((((( E77 D D77 DDD 5 577

I desired) yo# can #se the R+C+R+NCIN: cla#se to s'eci y a di erent name or 4old ane 4ne!. This cla#se is o#nd a ter the tri""erin" event) *e ore the 9<+N cla#se.

Copy rights are reserved.

Synta34
R+C+R+NCIN:

172
Oold as oldNnameP One! as ne!NnameP

+34

CR+,T+ OR R+BL,C+ TRI::+R R+C+R+NC+NTRI::+R

-+:IN

*e ore insert or #'date or delete on st#dent re erencin" old as oldNst#dent ne! as ne!Nst#dent or each ro! insert into marks

val#es$4oldNst#dent.no)4oldNst#dent.marks)4ne!Nst#dent.marks%2
+ND R+C+R+NC+NTRI::+R2 9<+N CL,US+

cla#se is valid or ro!(level tri""ers only. I 'resent) the tri""er *ody !ill *e e3ec#ted only or those ro!s that meet the condition s'eci ied *y the 9<+N cla#se.
9<+N

Synta34
9<+N

tri

er_condition2

9here tri er_condition is a -oolean e3'ression. It !ill *e eval#ated or each ro!. The =new and =old records can *e re erenced inside tri er_condition as !ell) *#t like R+C+R+NCIN:) the colon is not #sed there. The colon is only valid in the tri""er *ody. +34
CR+,T+ OR R+BL,C+ TRI::+R 9<+NNTRI::+R

-+:IN

*e ore insert or #'date or delete on st#dent re erencin" old as oldNst#dent ne! as ne!Nst#dent or each ro! !hen $ne!Nst#dent.marks 1 D77% insert into marks

val#es$4oldNst#dent.no)4oldNst#dent.marks)4ne!Nst#dent.marks%2
+ND 9<+NNTRI::+R2 TRI::+R BR+DIC,T+S

There are three -oolean o'eration is. The 'redicates are


#nctions that yo# can #se to determine !hat the

INS+RTIN: UBD,TIN: D+L+TIN:

+34
CR+,T+ OR R+BL,C+ TRI::+R BR+DIC,T+NTRI::+R -+:IN

*e ore insert or #'date or delete on st#dent i insertin" then insert into 'redicates val#es$?I?%2 elsi #'datin" then insert into 'redicates val#es$?U?%2 elsi deletin" then insert into 'redicates val#es$?D?%2 end i 2

+ND BR+DIC,T+NTRI::+R2

O#t'#t4
SQL1

delete st#dent !here noI62

Copy rights are reserved.

6 ro! deleted.
SQL1

173

select F rom 'redicates2 &S: ((((((((((((((( D

insert into st#dent val#es$W)?"?)W77%2 6 ro! created.


SQL1

select F rom 'redicates2 &S: ((((((((((((((( D I SQL1 #'date st#dent set marks I WWW !here noIW2 6 ro! #'dated.
SQL1 SQL1

select F rom 'redicates2 &S: ((((((((((((((( D I U

INST+,D(OC TRI::+RS Instead(o tri""ers ire instead o a D&L o'eration. ,lso) instead(o tri""ers can *e de ined only on vie!s. Instead(o tri""ers are #sed in t!o cases4 To allo! a vie! that !o#ld other!ise not *e modi ia*le to *e modi ied. To modi y the col#mns o a nested ta*le col#mn in a vie!.

Copy rights are reserved.

You might also like