Open In App

PL/SQL Arrays

Last Updated : 18 Jan, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

PL/SQL, an extension of SQL developed through Oracle, empowers builders with robust programming skills for powerful database management. Among its many capabilities, arrays stand out as an essential tool for organizing and manipulating data correctly. In this article, we’ll dive deep into the sector of PL/SQL arrays, overlaying their definition, introduction, and realistic examples.

In this article, we are going to discuss the different types through which arrays can be declared in PL/SQL. we will delve into the concept of indexes in SQL, exploring their types, and best practices for creating and managing them.

Arrays in PL/SQL

Arrays, mainly called Varrays (Variable Arrays), are dynamic information systems that permit the storage of multiple values of the same information type underneath a single variable name. Unlike simple variables, arrays offer an effective approach to dealing with statistics collections, facilitating streamlined facts control and manipulation of data.

Creating a Varray Type:

To harness the power of Varrays, it is essential to outline a kind that specifies the detail type and the range of elements the array can preserve. For instance:

CREATE TYPE num_array AS VARRAY(10) OF INT;

Examples of PL/SQL Array

Example of Varray using INT Data Type

Let’s delve into a practical example using a Varray of integers. In this situation, we declare and use a Varray named `numbers`:

DECLARE
numbers num_array;
BEGIN
numbers := num_array(1, 2, 3, 4, 5);
-- Perform operations on 'numbers'
FOR i IN 1..numbers.COUNT LOOP
DBMS_OUTPUT.PUT_LINE('Element ' || i || ': ' || numbers(i));
END LOOP;

-- Update an element
numbers(3) := 30;

-- Display the updated elements
FOR i IN 1..numbers.COUNT LOOP
DBMS_OUTPUT.PUT_LINE('Updated Element ' || i || ': ' || numbers(i));
END LOOP;
END;

In this case, the Varray ‘numbers’ is initialized with integers from 1 to 5 and operations are performed, showcasing the simplicity of dealing with numerical collections.

Output:

Example-of-Varray-using-INT-Data-Type

Example of Varray using INT Data Type

Explanation:

This PL/SQL code publicizes a variable number of the VARRAY type num_array, initializes it with values, plays operations on it (displaying elements), updates one among its factors, after which shows the updated factors.

  • Declare a VARRAY variable `numbers` and initialize it with values 1, 2, three, 4, and 5.
  • Use a loop to display the initial factors.

Output:

Element 1: 1
Element 2: 2
Element 3:3
Element 4: 4
Element 5: 5
  • Update the detail at index three (value three) to 30.
  • Use a loop to display the updated elements.

Output:

 Updated Element 1: 1
Updated Element 2: 2
Updated Element 3: 30
Updated Element 4: 4
Updated Element 5: 5

Example of Varray using CHAR Data Type

Varrays are versatile and no longer restrained to numerical statistics sorts. Consider a scenario where you create and use a Varray of characters:

CREATE TYPE char_array AS VARRAY(5) OF CHAR(10);

This example introduces a `char_array` Varray able to protect as much as 5 strings, each with a most period of 10 characters.

DECLARE
-- Declare a variable of the VARRAY type
my_array char_array := char_array('Apple', 'Banana', 'Cherry');

BEGIN
-- Display elements
FOR i IN 1..my_array.COUNT LOOP
DBMS_OUTPUT.PUT_LINE('Element ' || i || ': ' || my_array(i));
END LOOP;

-- Insert a new element
my_array.EXTEND;
my_array(4) := 'Date';

-- Display elements after insertion
FOR i IN 1..my_array.COUNT LOOP
DBMS_OUTPUT.PUT_LINE('Element ' || i || ': ' || my_array(i));
END LOOP;

-- Update an element
my_array(2) := 'Blueberry';

-- Display elements after update
FOR i IN 1..my_array.COUNT LOOP
DBMS_OUTPUT.PUT_LINE('Updated Element ' || i || ': ' || my_array(i));
END LOOP;
END;

Output:

Varray-using-CHAR-Data-Type

Varray using CHAR Data Type

Explanation

This PL/SQL code broadcasts a variable my_array of the VARRAY (Variable Size Array) kind char_array and plays various operations on it, which includes showing factors, inserting a new detail, updating an existing element, and displaying the elements again after each operation.

  • Declare a VARRAY variable `my_array` of kind `char_array`.
  • Initialize it with 3 factors: ‘Apple’, ‘Banana’, ‘Cherry’.
  • Display factors the usage of a loop.

Output:

Element 1: Apple
Element 2: Banana
Element 3: Cherry
  • Use `EXTEND` to growth array size.
  • Insert ‘Date’ at index four.
  • Display updated elements.

Output:

 Element 1: Apple
Element 2: Banana
Element 3: Cherry
Element 4: Date
  • Update element at index 2 to ‘Blueberry’.
  • Display final elements after the update.
 Updated Element 1: Apple
Updated Element 2: Blueberry
Updated Element 3: Cherry
Updated Element 4: Date

Data Type Example of Varray using %ROWTYPE or %TYPE

PL/SQL offers the flexibility to define a Varray based at the records sort of a table column using `%ROWTYPE` or `%TYPE`:

CREATE TYPE empp.CROWTYPE AS TABLE OF VARCHAR2(50);
-- Assuming empp.CROWTYPE is a nested table type
CREATE TYPE emp_details AS VARRAY(50) OF empp.CROWTYPE;

In this situation, `emp_details` is a Varray designed to save up to 50 rows from the `emp` table, demonstrating the adaptability of PL/SQL arrays to numerous statistics sorts.

Conclusion

PL/SQL arrays, particularly Varrays, function a cornerstone for powerful records organization and manipulation in Oracle databases. This article has explored their advent, utility, and versatility, supplying builders with a complete know-how of how to harness the whole ability of PL/SQL arrays. By studying those ideas, developers can elevate their ability to construct green and scalable database applications, making PL/SQL arrays an critical tool of their toolkit.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads