What is the main purpose of using collections in oracle ?
Index by tables
Nested tables
Variable size ARRAY
A collection is an ordered group of elements, all of the same type. Each element is identified by a unique subscript that represents its position in the collection
PL/SQL provides three collection types −
Index-by tables or Associative array Nested table Variable-size array or Varray
Let's start with Nested Tables, they are the most common form of collection and so represent a useful basis of comparison.
A nested table is a variable which can hold more than one instance of something, often a record from a database table. They might be declared like this:
type emp_nt is table of emp%rowtype;
emp_rec_nt emp_nt;
They are useful whenever we want to store multiple instances of data against which we want to do the same thing. The classic example is using BULK COLLECT to store multiple records:
select *
bulk collect into emp_rec_nt
from employees;
This gives us a source of data we can loop round; crucially we can navigate backwards as well as forwards, even skip to the end or the beginning, which are things we cannot do with a cursor. Nested tables can be collections of any data type, including composites such as PL/SQL records or user-defined types.
An Index By table is better called (as the docs do) an Associative Array . These are simple collections of single attributes with an index. Nested tables also have indexes but their indexes are just row counts. With an associative array the index can be meaningful, i.e. sourced from a data value. So they are useful for caching data values for later use. The index can be a number, or (since 9iR2) a string which can be very useful. For instance, here is an associative array of salaries which is indexed by the employee identifier.
type emp_sal_aa is table of emp.sql%type
index by emp.empno%type;
l_emp_sales emp_sal_aa;
Note that I could have declared that array using INDEX BY BINARY_INTEGER but it is clearer to use the %TYPE syntax instead (self-documenting code). Elements of that array can identified by an index value, in this case EMPNO:
l_emp_sals(l_emp_no) := l_emp_sal;
Other than caching reference tables or similar look-up values there aren't many use cases for associative arrays.
Variable arrays are just nested tables with a pre-defined limit on the number of elements. So perhaps the name is misleading: they are actually fixed arrays. There's little we can do with VArrays which we can't do with nested tables (except constrain the number of elements and it's pretty rare that we would want to do that). They are declared like this:
type emp_va is varray(14) of emp%rowtype;
emp_rec_va emp_va;
We can use bulk collect to populate a VArray ...
select *
bulk collect into emp_rec_va
from employees;
However we must be certain the query will return at most the number of elements specified in the VArray's declaration. Otherwise the SELECT will hurl ORA-22165.
There are no known use cases for variable arrays. Okay that's a bit harsh, but almost all of the time you will use nested tables instead. 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 PL/SQL documentation devotes an entire chapter to collections. Find out more.
U can use link
What is the difference between nested array and associative array?
or just read it.
A nested table is just an array of n elements.
declare
type nested_table_of_integer is table of integer;
v_my_nested_table nested_table_of_integer;
begin
v_my_nested_table := nested_table_of_integer(); -- initialize
v_my_nested_table.extend(10); -- add 10 elements
v_my_nested_table(1) := 100;
v_my_nested_table(11) := 1000; -- ORA-06533: Subscript beyond count
end;
A nested table must be initialized as shown. It has zero elements at first. To add elements we use EXTEND. This nested table has 10 elements. They are indexed 1 to 10. Element 1 has the value 100. The others have value null. An access to a non-existent element, say the 11th element, raises an error.
An associative array on the other hand is an array of name/value pairs. Let's use numbers (pls_integer typically) for the naming:
declare
type associative_array_of_integer is table of integer index by pls_integer;
v_my_associative_array associative_array_of_integer;
begin
v_my_associative_array(1) := 100;
v_my_associative_array(11) := 1000;
v_my_associative_array(12) := v_my_associative_array(2); -- ORA-01403: no data found
end;
An associative array needs no initialization. It is empty and gets populated. Here we associate the element called 1 with the value 100 and the element with the name 11 with the value 1000. So there are two elements in the array. We get a no data found exception when we try to access a name that is not in the array.
We can also use strings for the names:
declare
type associative_array_of_integer is table of integer index by varchar2(100);
v_my_associative_array associative_array_of_integer;
begin
v_my_associative_array('age father') := 39;
v_my_associative_array('age mother') := 32;
v_my_associative_array('age daughter') := 11;
end;
You can use both collections to get table data, but you use them differently. The nested table has a count and you can just loop from 1 to count to access its elements:
declare
type nested_table_of_integer is table of integer;
v_my_nested_table nested_table_of_integer;
begin
v_my_nested_table := nested_table_of_integer(); -- initialize
select table_name bulk collect into v_my_nested_table from user_tables;
for i in 1 .. v_my_nested_table.count loop
dbms_output.put_line(v_my_nested_table(i));
end loop;
end;
The associative array however must be read from whatever happens to be the first index to the next and next and next using FIRST and NEXT.
declare
type associative_array_of_integer is table of integer index by pls_integer;
v_my_associative_array associative_array_of_integer;
i integer;
begin
select table_name bulk collect into v_my_associative_array from user_tables;
i := v_my_associative_array.first;
while i is not null loop
dbms_output.put_line(v_my_associative_array(i));
i := v_my_associative_array.next(i);
end loop;
end;
The "names" happen to be 1, 2, 3, etc. here (given thus by the bulk collection) and you could access v_my_associative_array(1) for instance. Later in your program, however, after some possible delete operations in the array, there may be gaps, so you don't know whether an element named 1 exists and whether the element before element 4 happens to be element 3. As with bulk collect the "names" for the elements have no meaning you would not really use them, but go instead through the chain as shown.
Associative arrays, also known as index-by tables, let you look up elements using arbitrary numbers and strings for subscript values. These are similar to hash tables in other programming languages.
Nested tables hold an arbitrary number of elements. They use sequential numbers as subscripts. You can define equivalent SQL types, allowing nested tables to be stored in database tables and manipulated through SQL.
Varrays (short for variable-size arrays) hold a fixed number of elements (although you can change the number of elements at runtime). They use sequential numbers as subscripts. You can define equivalent SQL types, allowing varrays to be stored in database tables. They can be stored and retrieved through SQL, but with less flexibility than nested tables.
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).
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.
Index by tables are user defined data types which is used to store multiple data items. Basically these tables are unconstrained tables. Index by table having two parts, these are value field and key field. In value field, oracle server stores actual data, where as in key field oracle server stores indexes, that’s why index by table having ‘key value’ pairs and also indexes are by default integers, and these indexes are in between negative to positive numbers. This index field behaves like a primary key, it doesn’t accept duplicate values. Generally index by tables are used to improve the performance or application, because these tables are stored in memory area, that’s why these tables are also called a memory tables. Generally to improve the performance of the application, these table indexes are using ‘binary-integer’ data type, so we are creating in two step process. First we are creating type, then only we are creating a variable of that type.
Syntax:-
Step1:- Type typename is table of data type (size) index by binary_integer;
Step2:- Variablename typename;
For Morinformation >>>PL/SQL Index by tables (or) PL/SQL tables (or) associative arrays
The main purpose of using collection is to improve application performance. By using collections, we can 'cache' static data that needs to be accessed frequently and need to be modified. This results in reduced calls to a database.
Also, if you need to process a number of items of a similar type, storing these items in a collection will allow you to loop through each element with ease, referencing each one by an index.
Refer this article for more detail information about collection