0% found this document useful (0 votes)
72 views

Associative Array

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
72 views

Associative Array

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
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

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

You might also like