Anda di halaman 1dari 4

7/15/2015

It'sAllAboutORACLE:AssociativeArray(IndexbyTable)VsNestedTableVsVARRAY

Associative Array(Index by Table) Vs Nested Table Vs


VARRAY
Postedbyhimanshukarkiat9:27AM

Acollectionisalistofelementsofthesametype.Eachelementinthelisthasauniquepositionnumberorlabel,calledthe
"subscript".
Togivealogicalanalogy,consideralistofcolors={red,blue,green,yellow}.Thislisthasfourelements,allnamesofcolors.
Thereisauniquepositionofeachelementinthelist.Ifwenumberthepositionssequentiallystartingfromone,wecouldsay:
color[1]=red,color[2]=blue,andsoon.
Here,coloristhenameofthecollection,andthenumberswithin[]arethesubscripts.
PL/SQLhasthreecollectiontypes.Inthisarticle,welllookatachartforcomparingthethree,theiradvantagesandlimitations,
andwhichonetouseforyourneeds.
Tointroducethethreecollectiontypes:
Indexbytables:Alsocalledassociativearrays.
Nestedtables
Varrays:Alsocalledvariablearrays
Thechartbelowliststhepropertiesofthethreecollectiontypesonasetofparameterssuchassize,easeofmodification,
persistence,etc.

IndexBy
Tables

Nested
Tables

Varrays

Unbounded
i.e.the
numberof
elementsit
canholdis
notpre
defined

Boundedi.e.
holdsa
declared
numberof
elements,
thoughthis
numbercan
bechangedat
runtime

Subscript Canbe
Characteristics arbitrary
numbersor
strings.Need
notbe
sequential.

Sequential
numbers,
startingfrom
one

Sequential
numbers,
startingfrom
one

Database Indexby

Canbestored

Canbestored

Size Unbounded
i.e.the
numberof
elementsit
canholdisnot
predefined

data:text/htmlcharset=utf8,%3Ch3%20class%3D%22posttitle%20entrytitle%22%20style%3D%22margin%3A%200px%3B%20padding%3A%200px%200px

1/4

7/15/2015

It'sAllAboutORACLE:AssociativeArray(IndexbyTable)VsNestedTableVsVARRAY

Storage tablescanbe
usedin
PL/SQL
programs
only,cannot
bestoredin
thedatabase.

inthe
database
using
equivalent
SQLtypes,
and
manipulated
throughSQL.

inthe
database
using
equivalent
SQLtypes,
and
manipulated
throughSQL
(butwithless
easethan
nestedtables)

Referencing Worksaskey
andlookups valuepairs.
e.g.Salariesof
employeescan
bestoredwith
unique
employee
numbersused
assubscripts
sal(102):=
2000

Similarto
onecolumn
database
tables.
Oraclestores
thenested
tabledatain
noparticular
order.But
whenyou
retrievethe
nestedtable
intoaPL/SQL
variable,the
rowsaregiven
consecutive
subscripts
startingat1.

Standard
subscripting
syntaxe.g.
color(3)isthe
3rdcolorin
varraycolor

Flexibilityto Mostflexible.
changes Sizecan
increase/
decrease
dynamically.
Elementscan
beaddedto
anypositionin
thelistand
deletedfrom
anyposition.

Almostlike
indexby
tables,except
thatsubscript
valuesarenot
asflexible.
Deletionsare
possiblefrom
non
contiguous
positions.

Notvery
flexible.You
mustretrieve
andupdateall
theelements
ofthevarray
atthesame
time.

Setsandbags

Arrays

Mappingwith Hashtables
other
programming
languages
WhichCollectionTypeToUse?

data:text/htmlcharset=utf8,%3Ch3%20class%3D%22posttitle%20entrytitle%22%20style%3D%22margin%3A%200px%3B%20padding%3A%200px%200px

2/4

7/15/2015

It'sAllAboutORACLE:AssociativeArray(IndexbyTable)VsNestedTableVsVARRAY

Youhaveallthedetailsaboutindexbytables,nestedtablesandvarraysnow.Givenasituation,willoneshouldyouuseforyour
listdata?
Herearesomeguidelines.
Useindexbytableswhen:
Yourprogramneedssmalllookups
Thecollectioncanbemadeatruntimeinthememorywhenthepackage/procedureisinitialized
Thedatavolumeisunknownbeforehand
Thesubscriptvaluesareflexible(e.g.strings,negativenumbers,nonsequential)
Youdonotneedtostorethecollectioninthedatabase
Usenestedtableswhen:
Thedataneedstobestoredinthedatabase
Thenumberofelementsinthecollectionisnotknowninadvance
Theelementsofthecollectionmayneedtoberetrievedoutofsequence
Updatesanddeletionsaffectonlysomeelements,atarbitrarylocations
Yourprogramdoesnotexpecttorelyonthesubscriptremainingstable,astheirordermaychangewhennestedtables
arestoredinthedatabase.
Usevarrayswhen:
Thedataneedstobestoredinthedatabase
Thenumberofelementsofthevarrayisknowninadvance
Thedatafromthevarrayisaccessedinsequence
Updatesanddeletionshappenonthevarrayasawholeandnotonarbitrarilylocatedelementsinthevarray

ExampleDeclaringNestedTables,Varrays,andAssociativeArrays
DECLARE
TYPEnested_typeISTABLEOFVARCHAR2(30);
TYPEvarray_typeISVARRAY(5)OFINTEGER;
TYPEassoc_array_num_typeISTABLEOFNUMBERINDEXBYPLS_INTEGER;
TYPEassoc_array_str_typeISTABLEOFVARCHAR2(32)INDEXBYPLS_INTEGER;
TYPEassoc_array_str_type2ISTABLEOFVARCHAR2(32)INDEXBYVARCHAR2(64);
v1nested_type;
v2varray_type;
v3assoc_array_num_type;
v4assoc_array_str_type;
v5assoc_array_str_type2;
BEGIN
anarbitrarynumberofstringscanbeinsertedv1
v1:=nested_type('Shipping','Sales','Finance','Payroll');
v2:=varray_type(1,2,3,4,5);Upto5integers
v3(99):=10;Juststartassigningtoelements
v3(7):=100;Subscriptscanbeanyintegervalues
v4(42):='Smith';Juststartassigningtoelements
v4(54):='Jones';Subscriptscanbeanyintegervalues
data:text/htmlcharset=utf8,%3Ch3%20class%3D%22posttitle%20entrytitle%22%20style%3D%22margin%3A%200px%3B%20padding%3A%200px%200px

3/4

7/15/2015

It'sAllAboutORACLE:AssociativeArray(IndexbyTable)VsNestedTableVsVARRAY

v5('Canada'):='NorthAmerica';Juststartassigningtoelements
v5('Greece'):='Europe';Subscriptscanbestringvalues
END;
/

data:text/htmlcharset=utf8,%3Ch3%20class%3D%22posttitle%20entrytitle%22%20style%3D%22margin%3A%200px%3B%20padding%3A%200px%200px

4/4

Anda mungkin juga menyukai