Comparatie Tipuri de Colectii

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 4

Varrays

Varray is a collection method in which the size of the array is fixed. The array size
cannot be exceeded than its fixed value. The subscript of the Varray is of a numeric
value. Following are the attributes of Varrays.
· Upper limit size is fixed
· Populated sequentially starting with the subscript '1'
· This collection type is always dense, i.e. we cannot delete any array
elements. Varray can be deleted as a whole, or it can be trimmed from the
end.
· Since it always is dense in nature, it has very less flexibility.
· It is more appropriate to use when the array size is known and to perform
similar activities on all the array elements.
· The subscript and sequence always remain stable, i.e. the subscript and
count of the collection is always same.
· They need to be initialized before using them in programs. Any operation
(except EXISTS operation) on an uninitialized collection will throw an
error.
· It can be created as a database object, which is visible throughout the
database or inside the subprogram, which can be used only in that
subprogram.
· The one big advantage of VArrays over nested tables is that they guarantee the
order of the elements. So if you must get elements out in the same order as you
inserted them use a VArray.
·
The below figure will explain the memory allocation of Varray (dense)
diagrammatically.
Subscri 1 2 3 4 5 6 7
pt
Value Xyz Dfv Sde Cxs Vbc Nhu Qwe
Syntax for VARRAY:
TYPE <type_name> IS VARRAY (<SIZE>) OF <DATA_TYPE>;

· In the above syntax, type_name is declared as VARRAY of the type


'DATA_TYPE' for the given size limit. The data type can be either simple or
complex type.

Nested Tables
A Nested table is a collection in which the size of the array is not fixed. It has the
numeric subscript type. Below are more descriptions about nested table type.
· The Nested table has no upper size limit.
· Since the upper size limit is not fixed, the collection, memory needs to be
extended each time before we use it. We can extend the collection using
'EXTEND' keyword.
· Populated sequentially starting with the subscript '1'.
· This collection type can be of both dense and sparse, i.e. we can create the
collection as a dense, and we can also delete the individual array element
randomly, which make it as sparse.
· It gives more flexibility regarding deleting the array element.
· It is stored in the system generated database table and can be used in the
select query to fetch the values.
· The subscript and sequence are not stable, i.e. the subscript and the count of
the array element can vary.
· They need to be initialized before using them in programs. Any operation
(except EXISTS operation) on the uninitialized collection will throw an
error.
· It can be created as a database object, which is visible throughout the
database or inside the subprogram, which can be used only in that
subprogram.
The below figure will explain the memory allocation of Nested Table (dense and
sparse) diagrammatically. The black colored element space denotes the empty
element in a collection i.e. sparse.
Subscri 1 2 3 4 5 6 7
pt
Value Xyz Dfv Sde Cxs Vbc Nhu Qwe
(dense)
Value(s Qwe Asd Afg Asd Wer
parse)
Syntax for Nested Table:
TYPE <tvpe name> IS TABLE OF <DATA TYPE>;

· In the above syntax, type_name is declared as Nested table collection of the


type 'DATA_TYPE'. The data type can be either simple or complex type.

Index-by-table
Index-by-table is a collection in which the array size is not fixed. Unlike the other
collection types, in the index-by-table collection the subscript can consist be
defined by the user. Following are the attributes of index-by-table.
· The subscript can of integer or strings. At the time of creating the collection,
the subscript type should be mentioned.
· These collections are not stored sequentially.
· They are always sparse in nature.
· The array size is not fixed.
· They cannot be stored in the database column. They shall be created and
used in any program in that particular session.
· They give more flexibility in terms of maintaining subscript.
· The subscripts can be of negative subscript sequence also.
· They are more appropriate to use for relatively smaller collective values in
which the collection can be initialized and used within the same
subprograms.
· They need not be initialized before start using them.
· It cannot be created as a database object. It can only be created inside the
subprogram, which can be used only in that subprogram.
· BULK COLLECT cannot be used in this collection type as the subscript
should be given explicitly for each record in the collection.
The below figure will explain the memory allocation of Nested Table (sparse)
diagrammatically. The black colored element space denotes the empty element in a
collection i.e. sparse.
Subscri FIRST SECON THIRD FOURT FIFTH SIXTH SEVEN
pt D H TH
(varchar
)
Value(s Qwe Asd Afg Asd Wer
parse)
Syntax for Index-by-Table
TYPE <type_name> IS TABLE OF <DATA_TYPE> INDEX BY VARCHAR2 (10);

· In the above syntax, type_name is declared as an index-by-table collection


of the type 'DATA_TYPE'. The data type can be either simple or complex
type. The subsciprt/index variable is given as VARCHAR2 type with
maximum size as 10.

Choosing Between Nested Tables and Associative Arrays:


Both nested tables and associative arrays (formerly known as index-by tables) use similar
subscript notation, but they have different characteristics when it comes to persistence and
ease of parameter passing.
Nested tables can be stored in a database column, but associative arrays cannot. Nested
tables can simplify SQL operations where you would normally join a single-column table with
a larger table.
Associative arrays are appropriate for relatively small lookup tables where the collection can
be constructed in memory each time a procedure is called or a package is initialized. They
are good for collecting information whose volume is unknown beforehand, because there is
no fixed limit on their size. Their index values are more flexible, because associative array
subscripts can be negative, can be nonsequential, and can use string values instead of
numbers.
PL/SQL automatically converts between host arrays and associative arrays that use numeric
key values. The most efficient way to pass collections to and from the database server is to
set up data values in associative arrays, then use those associative arrays with bulk
constructs (the FORALL statement or BULK COLLECT clause).

Choosing Between Nested Tables and Varrays:


Varrays are a good choice when:
The number of elements is known in advance.
The elements are usually all accessed in sequence.
When stored in the database, varrays keep their ordering and subscripts.
Each varray is stored as a single object, either inside the table of which it is a column (if the
varray is less than 4KB) or outside the table but still in the same tablespace (if the varray is
greater than 4KB). You must update or retrieve all elements of the varray at the same time,
which is most appropriate when performing some operation on all the elements at once. But
you might find it impractical to store and retrieve large numbers of elements this way.
Nested tables are a good choice when:
The index values are not consecutive.
There is no set number of index values. However, a maximum limit is imposed.
You need to delete or update some elements, but not all the elements at once.
You would usually create a separate lookup table, with multiple entries for each row of the
main table, and access it through join queries.
Nested tables can be sparse: you can delete arbitrary elements, rather than just removing an
item from the end.
Nested table data is stored in a separate store table, a system-generated database table
associated with the nested table. The database joins the tables for you when you access the
nested table. This makes nested tables suitable for queries and updates that only affect
some elements of the collection.
You cannot rely on the order and subscripts of a nested table remaining stable as the nested
table is stored in and retrieved from the database, because the order and subscripts are not
preserved in the database.

You might also like