0% found this document useful (0 votes)
27 views5 pages

PL/SQL Associative Arrays Explained

An associative array in PL/SQL is a collection type that associates unique keys with values. It has the following characteristics: 1) An associative array type must be defined before array variables can be declared using the TYPE IS TABLE OF statement to specify the key and value data types. 2) Values are assigned and accessed using the key, and the array grows dynamically as elements are added. 3) Keys must be unique but values can be duplicated, and attempting to access a non-existent key will result in an error.

Uploaded by

Sai Krishna
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views5 pages

PL/SQL Associative Arrays Explained

An associative array in PL/SQL is a collection type that associates unique keys with values. It has the following characteristics: 1) An associative array type must be defined before array variables can be declared using the TYPE IS TABLE OF statement to specify the key and value data types. 2) Values are assigned and accessed using the key, and the array grows dynamically as elements are added. 3) Keys must be unique but values can be duplicated, and attempting to access a non-existent key will result in an error.

Uploaded by

Sai Krishna
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

Associative Array:

A PL/SQL associative array is a collection type that associates a unique key with
a value. An associative array has the following characteristics: An associative array
type must be defined before array variables of that array type can be declared. Data
manipulation occurs in the array variable.

A PL/SQL associative array is a collection type that associates a unique key with a
value.

An associative array has the following characteristics:

 An associative array type must be defined before array variables of that array type can be
declared. Data manipulation occurs in the array variable.
 The array does not need to be initialized; simply assign values to array elements.
 There is no defined limit on the number of elements in the array; it grows dynamically as
elements are added.
 The array can be sparse; there can be gaps in the assignment of values to keys.
 An attempt to reference an array element that has not been assigned a value results in an
exception.

Use the TYPE IS TABLE OF statement to define an associative array type.

Syntax:

TYPE__assoctype___IS TABLE
OF___datatype___INDEX
BY___BINARY_INTEGERPLS___INTEGERVARCHAR
2___(___n___BYTE___CHAR)
Description:
TYPE assoctype
Specifies an identifer for the array type.
datatype
Specifies a supported data type, such as VARCHAR2, NUMBER, RECORD, VARRAY, or
associative array type. The %TYPE attribute and the %ROWTYPE attribute are also
supported.
INDEX BY
Specifies that the associative array is to be indexed by one of the data types introduced by
this clause.
BINARY INTEGER
Integer numeric data.
PLS_INTEGER
Integer numeric data.
VARCHAR2 (n[BYTE|CHAR])
A variable-length character string of maximum length n code units, which may range from
1 to 32 672 BYTE or from 1 to 8 168 CHAR. The %TYPE attribute is also supported if the
object to which the %TYPE attribute is being applied is of the BINARY_INTEGER,
PLS_INTEGER, or VARCHAR2 data type.

We have already discussed varray in the chapter 'PL/SQL arrays'. In this chapter,


we will discuss the PL/SQL tables.
Both types of PL/SQL tables, i.e., the index-by tables and the nested tables have the
same structure and their rows are accessed using the subscript notation. However,
these two types of tables differ in one aspect; the nested tables can be stored in a
database column and the index-by tables cannot.

Index-By Table
An index-by table (also called an associative array) is a set of key-value pairs.
Each key is unique and is used to locate the corresponding value. The key can be
either an integer or a string.
An index-by table is created using the following syntax. Here, we are creating
an index-by table named table_name, the keys of which will be of the
subscript_type and associated values will be of the element_type
TYPE type_name IS TABLE OF element_type [NOT NULL] INDEX BY subscript_type;

table_name type_name;

Declaring an associative array type


The following shows the syntax for declaring an associative array type:

TYPE associative_array_type
IS TABLE OF datatype [NOT NULL]
INDEX BY index_type;

In this syntax:

 The associative_array_type is the name of the associative array type.


 The datatype is the data type of the elements in the array.
 The index_type is the data type of the index used to organize the elements in
the array.
 Optionally, you can specify NOT NULL to force every element in the array must
have a value.
 The following example declares an associative array of characters indexed by characters:
TYPE t_capital_type
 IS TABLE OF VARCHAR2(100)
 INDEX BY VARCHAR2(50);

 Code language: SQL (Structured Query Language) (sql)

 Declaring an associative array variable


 After having the associative array type, you need to declare an associative
array variable of that type by using this syntax:

 associative_array associative_array_type
 Code language: SQL (Structured Query Language) (sql)

 For example, this statement declares an associative array t_capital with the


type t_capital_type:

 t_capital t_capital_type;
 Code language: SQL (Structured Query Language) (sql)

 Accessing associative array elements


 To access an array element, you use this syntax:

 array_name(index)
 Code language: SQL (Structured Query Language) (sql)

 Note that index can be a number or a character string.

 Assigning associative array elements


 To assign a value to an associative array element, you use the assignment
operation (:=):

 array_name(index) := value;

Example:
Here array() function is used to create associative array.

<?php   
/* First method to create an associate array. */

$student_one = array("Maths"=>95, "Physics"=>90,  

                  "Chemistry"=>96, "English"=>93,  

                  "Computer"=>98); 

    

/* Second method to create an associate array. */

$student_two["Maths"] = 95; 

$student_two["Physics"] = 90; 

$student_two["Chemistry"] = 96; 

$student_two["English"] = 93; 

$student_two["Computer"] = 98; 

    

/* Accessing the elements directly */

echo "Marks for student one is:\n"; 

echo "Maths:" . $student_two["Maths"], "\n"; 

echo "Physics:" . $student_two["Physics"], "\n"; 

echo "Chemistry:" . $student_two["Chemistry"], "\n"; 

echo "English:" . $student_one["English"], "\n"; 

echo "Computer:" . $student_one["Computer"], "\n"; 

?> 

Output:

Marks for student one is:


Maths:95
Physics:90
Chemistry:96
English:93
Computer:98

Common questions

Powered by AI

Associative arrays are crucial in PL/SQL procedures for managing dynamic data structures due to their ability to handle data with irregular, non-sequential indices. This allows them to model complex relationships or sparse datasets efficiently, adapting to varying sizes and forms as needed without pre-definition of storage capacity. Their use in procedures facilitates operations like non-linear querying, flexible data manipulation, and temporary storage during complex calculations, making them indispensable for advanced data handling strategies in PL/SQL contexts .

Developers might prefer associative arrays for their dynamic nature, allowing them to handle varying amounts of data without prior initialization or a fixed size, which offers flexibility. The use of unique keys (strings or integers) for indexing provides intuitive, sparse data access, resembling a dictionary structure that is beneficial for complex data manipulation tasks where direct access to elements by named keys is desirable. Additionally, associative arrays are uncomplicated in scenarios where temporary, in-memory data representation is required .

To declare an associative array with integer indices, you would use: TYPE int_array_type IS TABLE OF datatype INDEX BY BINARY_INTEGER;. For character-string indices, use: TYPE char_array_type IS TABLE OF datatype INDEX BY VARCHAR2(size);. First, declare the type, then the array variable like int_array int_array_type;. Access and modification of elements remain straightforward: array_name(integer_index) for integers and array_name('char_index') for strings. This approach is versatile, catering to various data indexing needs in PL/SQL-based applications, offering an intuitive syntax akin to dictionary or map structures in other programming languages .

Sparse associative arrays are beneficial in applications where data is unevenly distributed or selectively required. For instance, they are ideal in caching mechanisms where queried data may not cover all possible keys, or in inventory applications where stock levels per item vary widely and only a subset needs tracking at any given moment. Also, they facilitate applications that perform simulations or computations where only selected parameters are of interest, allowing for reduced memory overhead and streamlined processing .

Referencing an element in a PL/SQL associative array that has not been explicitly assigned a value can result in a 'no data found' exception. This occurs because associative arrays can be sparse, and querying an unset element will trigger an error. To mitigate this, developers can implement checks to determine whether an element exists prior to access, using methods such as checking the existence of a key in the array or initializing all possible keys with default values before processing .

The primary difference between associative arrays and nested tables in PL/SQL is their storage capabilities. Nested tables can be stored in database columns, allowing for persistent state, while associative arrays cannot and are limited to in-memory processing. Furthermore, associative arrays use unique key-value pairs for indexing, which can be either integers or strings, whereas nested tables use significantly simpler, sequential indexing .

Using %TYPE and %ROWTYPE attributes in associative array definitions can enhance flexibility and error prevention in PL/SQL. %TYPE allows the associative array elements to dynamically adapt to the data type of a database column or record field, ensuring consistency across code and database schema changes. %ROWTYPE creates associative arrays that mirror entire rows of a table, supporting complex operations involving complete row data manipulation and storage. This leads to improved code maintainability and potentially lessened risk of runtime data type conflicts .

A PL/SQL associative array is a collection type that associates a unique key with a value. It does not require initialization before use; values can be directly assigned to elements, allowing it to grow dynamically without a defined limit. These arrays can be sparse, meaning there can be gaps in the assigned values. Attempting to reference an unassigned element results in an exception. Each key in the associative array is unique and can be either an integer or a string, facilitating dynamic data management .

To access an element in a PL/SQL associative array, use the syntax array_name(index); the index can be either a number or a string, specifying the key of the element. To modify an element, use the assignment operation (:=) with the syntax array_name(index) := value; where value is the new data you want to assign to the indexed element. This approach allows direct manipulation of the array's elements in memory .

The syntax for declaring an associative array type in PL/SQL is: TYPE associative_array_type IS TABLE OF datatype [NOT NULL] INDEX BY index_type;. The components involved include associative_array_type for the array type name, datatype for the type of data the array holds, [NOT NULL] optionally enforcing non-null values for elements, and index_type denoting the data type used for indexing elements (which can be a number or character string).

You might also like