Associative Arrays

 

1. What is an Associative Array in Oracle?

An Associative Array (also called Index-by Table) is a PL/SQL collection type that stores elements as key-value pairs.

  • Each element has a unique index (key).
  • Keys can be INTEGER or VARCHAR2.
  • The collection can grow dynamically; no fixed size is required.

 

2. How is it different from VARRAYs or Nested Tables?

 

Feature

Associative Array

VARRAY

Nested Table

Size

Unlimited

Fixed max size

Can grow dynamically

Index type

INTEGER or VARCHAR2

Integer (1..N)

Integer (1..N)

Storage

PL/SQL only

Inline in table or stored separately

Stored out-of-line

Order

Not guaranteed

Maintains order

Order maintained

SQL Usage

PL/SQL only

Can be stored in table

Can be stored in table

Use Associative Arrays for fast lookups and dynamic data in PL/SQL.

 

3. How do you declare an Associative Array?

Example with INTEGER keys:

DECLARE

    TYPE emp_tab_type IS TABLE OF VARCHAR2(50) INDEX BY PLS_INTEGER;

    employees emp_tab_type;

BEGIN

    employees(1) := 'Alice';

    employees(2) := 'Bob';

    DBMS_OUTPUT.PUT_LINE(employees(1)); -- Output: Alice

END;

/

Example with VARCHAR2 keys:

DECLARE

    TYPE emp_tab_type IS TABLE OF NUMBER INDEX BY VARCHAR2(10);

    salaries emp_tab_type;

BEGIN

    salaries('Alice') := 5000;

    salaries('Bob') := 4500;

    DBMS_OUTPUT.PUT_LINE(salaries('Bob')); -- Output: 4500

END;

/

 

4. How do you loop through an Associative Array?

For INTEGER keys:

FOR i IN employees.FIRST .. employees.LAST LOOP

    DBMS_OUTPUT.PUT_LINE('Employee: ' || employees(i));

END LOOP;

For non-sequential keys:

DECLARE

    idx VARCHAR2(10);

BEGIN

    idx := salaries.FIRST;

    WHILE idx IS NOT NULL LOOP

        DBMS_OUTPUT.PUT_LINE(idx || ': ' || salaries(idx));

        idx := salaries.NEXT(idx);

    END LOOP;

END;

/

Use FIRST, LAST, NEXT, PRIOR to navigate associative arrays.

 

5. Can Associative Arrays store different data types?

No, all elements in a single associative array must have the same data type.

  • You can use OBJECT types if you need multiple fields per element.

Example with objects:

CREATE TYPE emp_obj AS OBJECT (

    name VARCHAR2(50),

    salary NUMBER

);

DECLARE

    TYPE emp_tab_type IS TABLE OF emp_obj INDEX BY PLS_INTEGER;

    employees emp_tab_type;

BEGIN

    employees(1) := emp_obj('Alice', 5000);

    DBMS_OUTPUT.PUT_LINE(employees(1).name || ': ' || employees(1).salary);

END;

/

 

6. Can Associative Arrays be stored in database tables?

No, Associative Arrays exist only in PL/SQL.

  • Use VARRAYs or Nested Tables if you need to store collections in database tables. 

 

7. How do you delete elements from an Associative Array?

  • Delete a single element: collection.DELETE(key);
  • Delete all elements: collection.DELETE;

Example:

employees.DELETE(1); -- Deletes element with key 1

employees.DELETE;    -- Deletes all elements

 

8. Advantages of Associative Arrays

  • Fast lookups using keys.
  • Dynamic size; grows as needed.
  • Supports non-sequential keys (like a dictionary).
  • Useful for temporary data processing in PL/SQL.

 

9. Limitations of Associative Arrays

  • Cannot be used directly in SQL.
  • Not stored in tables (PL/SQL only).
  • Only one data type per array; use OBJECT types for multiple fields.

 

10. When should I use Associative Arrays?

  • Temporary storage of key-value pairs in PL/SQL.
  • Fast in-memory lookups or caching.
  • Storing dynamic data whose size is unknown.

RECORD Type

1. What is a RECORD in Oracle?

A RECORD is a PL/SQL composite data type that can store multiple fields of different data types in a single variable.

  • Think of it as a row in a table stored in PL/SQL.
  • Each field is called a component or attribute.

2. How do I declare a RECORD type?

You can declare a RECORD using the TYPE keyword, then create a variable of that type.

Example:

DECLARE

    TYPE student_rec IS RECORD (

        id     NUMBER,

        name   VARCHAR2(50),

        marks  NUMBER

    );

    s student_rec;

BEGIN

    s.id := 101;

    s.name := 'Alice';

    s.marks := 95;

    DBMS_OUTPUT.PUT_LINE('Student: ' || s.name || ' - Marks: ' || s.marks);

END;

/

3. Can a RECORD be based on a table structure?

Yes, you can define a RECORD using %ROWTYPE to automatically match a table’s row structure.

Example:

DECLARE

    s employees%ROWTYPE;

BEGIN

    s.employee_id := 1001;

    s.first_name := 'John';

    DBMS_OUTPUT.PUT_LINE('Employee: ' || s.first_name);

END;

/

  • %ROWTYPE ensures your RECORD matches the table structure.
  • Useful for working with table rows dynamically.

4. How do I access fields in a RECORD?

Use dot notation: record_name.field_name.

Example:

DBMS_OUTPUT.PUT_LINE('ID: ' || s.id);

DBMS_OUTPUT.PUT_LINE('Name: ' || s.name);

5. Can RECORDs store different data types?

Yes, each field in a RECORD can have a different data type, e.g., NUMBER, VARCHAR2, DATE, etc.

Example:

TYPE order_rec IS RECORD (

    order_id NUMBER,

    customer_name VARCHAR2(50),

    order_date DATE

);

6. Can RECORDs be used in loops?

You cannot loop directly over fields, but you can loop through collections of RECORDs.

Example:

DECLARE

    TYPE student_tab IS TABLE OF student_rec;

    students student_tab := student_tab();

BEGIN

    students.EXTEND(2);

    students(1) := student_rec(101, 'Alice', 95);

    students(2) := student_rec(102, 'Bob', 88);

 

    FOR i IN 1..students.COUNT LOOP

        DBMS_OUTPUT.PUT_LINE('Name: ' || students(i).name || ', Marks: ' || students(i).marks);

    END LOOP;

END;

/

7. Can RECORDs contain other RECORDs?

Yes, RECORDs can be nested inside other RECORDs for complex data structures.

Example:

TYPE address_rec IS RECORD (

    street VARCHAR2(50),

    city   VARCHAR2(50)

);

TYPE student_rec IS RECORD (

    id NUMBER,

    name VARCHAR2(50),

    addr address_rec

);

8. Can RECORDs be stored in database tables?

No, RECORDs exist only in PL/SQL. They cannot be stored directly in a table.

  • Use OBJECT types or collections if you want to store composite structures in tables.

9. Advantages of RECORDs

  • Combine multiple related fields in a single variable.
  • Simplifies PL/SQL programming.
  • Works well with %ROWTYPE for table rows.
  • Supports modular and readable code.

10. Limitations of RECORDs

  • PL/SQL-only; cannot be used directly in SQL.
  • Cannot loop over fields individually.
  • Nested RECORDs can become complex to manage.