|Re: collection and records(basic documents) [message #429366 is a reply to message #429305]
||Tue, 03 November 2009 07:09
Registered: November 2009
PL/SQL Collections |
The composite data types are known as collections and are available in 3 different forms.
1. Nested tables
3. PL/SQL Associative arrays
1. Nested Tables
Single dimensional arrays which can be declared in the database as a column in another table and in PL/SQL as stand alone.
No upper or lower limits.
Constrained by the memory available.
Nested tables are densely populated when created. Each row has a value. But rows can be logically deleted, so nested tables can be sparsely populated.
Each row of the nested table must be of same type.
Declaration is in two types
Declare a type
TYPE <type_name> IS TABLE OF <existing_type>;
Then declare the variable of that type
For example, suppose we want to create a table of numbers, the declarations would be
TYPE number_table_type IS TABLE OF NUMBER;
(Example is written in Varray section)
Is a variable lenght, one dimentional, densely populated arrays (dense in the sense that each cell must exit, although it may be empty)
Each element must be of the same type like nested tables they can be defined in the database or in PL/SQL.
The declaration defines the size of the array which is fixed.
We cannot add new cells to or delete calls from the Varray once we have declared it.
These are designed for storing the small value of attributes.
TYPE <type_name> IS VARRAY <size> OF <existing_type>;
Considering an example, the declarations would be:-
TYPE phone_no_tab_type IS VARRAY(3) OF NUMBER;
TYPE address_tab_type IS VARRAY(3) OF VARCHAR2(500);
TYPE email_addr_tab_type IS VARRAY(3) OF VARCHAR2(100);
TYPE name_tab_type IS TABLE OF VARCHAR2(100);
Note that at the moment, each of the types of data is stored separately and therefore has to be worked with separately.To put this all together we need to create another type as a record then create a nested table to hold the records as follows:-
TYPE contact_rec IS RECORD (
TYPE contacts_tab_type IS TABLE OF contact_rec;
3.PL/SQL Associative arrays:
Associated (ie.,indexed by)with pl/sql integers(BINARY_INTEGER)or character strings VARCHAR2.
Like nested tables associative array are unbounded, single dimensional and each cell must be of the same type (homogeneous).
Unlike nested tables, associative arrays are sparse (each cell does not have to exit).
TYPE <type_name> IS TABLE OF <existing_type> ;
For example, suppose we want to create a table of numbers, the declarations would then be
TYPE number_table_type IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
Considering an example:
First we declare the table type:
TYPE phone_no_tab IS TABLE OF VARCHAR2 (20) INDEX BY BINARY_INTEGER;
Next we declare a variable of that type:
This creates an array(table) that is available within the PL/SQL session.Inserting and retrieving the datas is same as in Nested Tables.But there is no need to initialize the array or extend it.
2 TYPE phone_no_tab IS TABLE OF VARCHAR2(20) INDEX BY BINARY_INTEGER;
3 phone_nos phone_no_tab;
5 phone_nos(1) := '+44 (0) 117 942 2508';
6 dbms_output.put_line('phone_no(1) is '||phone_nos(1));
phone_no(1) is +44 (0) 117 942 2508
Considering an example Associative array indexed by varchar2:
2 TYPE phone_no_tab IS TABLE OF VARCHAR2(20) INDEX BY VARCHAR2(30);
3 phone_nos phone_no_tab;
5 phone_nos('office') := '+44 (0) 117 942 2508';
6 DBMS_output.put_line('phone_no(office) is '||phone_nos('office'));
phone_no(office) is +44 (0) 117 942 2508
The functions used in collections:
COUNT: This function returns the number of elements (cells) in the collection
DELETE: This procedure with no parameters deletes all the elements in the collection,
as with nested tables, though, we can specify a start and end point to say which element(s)
is (are) to be deleted. After deleting an element or the whole collection, any subsequent attempt
to read that element in the collection generates "ORA-01403: no data found". However you can write
to any element without any errors as this re-creates the element
EXISTS(n) : used to determine if the specified element has been created and not deleted,
returns TRUE if the element exists, FALSE if not. The index variable can be either a number of
type BINARY_INTEGER or a character string of type VARCHAR2
FIRST : returns the subscript of the first element in the PLSQL associative array
LAST : returns the subscript of the last element in the PL/SQL associative array
PRIOR(n): returns the subscript of the previous element in the PL/SQL associative array
or NULL if no more elements exist
NEXT(n) : returns the subscript of the next element in the PLSQL associative array or
NULL if no more elements exist.
TRIM : Not appropriate for associative arrays - generates a compilation error
EXTEND : Not appropriate for associative arrays - generates a compilation error