Presentation is loading. Please wait.

Presentation is loading. Please wait.

Collection in PL/SQL.

Similar presentations


Presentation on theme: "Collection in PL/SQL."— Presentation transcript:

1 Collection in PL/SQL

2 Introduction A composite data type stores values that have internal components. You can pass entire composite variables to subprograms as parameters, and you can access internal components of composite variables individually. Internal components can be either scalar or composite. You can use scalar components wherever you can use scalar variables. PL/SQL lets you define two kinds of composite data types, collection and record. You can use composite components wherever you can use composite variables of the same type. In a collection, the internal components always have the same data type, and are called elements. You can access each element of a collection variable by its unique subscript, with this syntax: variable_name(subscript). To create a collection variable, you either define a collection type and then create a variable of that type or use %TYPE. In a record, the internal components can have different data types, and are called fields. You can access each field of a record variable by its name, with this syntax: variable_name.field_name. To create a record variable, you either define a RECORD type and then create a variable of that type or use %ROWTYPE or %TYPE.

3 Understanding Associative Arrays (Index-By Tables)
An associative array (also called an index-by table) 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. Using a key-value pair for the first time adds that pair to the associative array. Using the same key with a different value changes the value. Like a database table, an associative array holds a data set of arbitrary size, and you can access its elements without knowing their positions in the array. An associative array does not need the disk space or network operations of a database table, but an associative array cannot be manipulated by SQL statements (such as INSERT and DELETE). An associative array is intended for temporary data storage.

4 Understanding Associative Arrays (Index-By Tables)
-- Change value associated with key 'Smallville': city_population('Smallville') := 2001; -- Print associative array: i := city_population.FIRST; WHILE i IS NOT NULL LOOP DBMS_Output.PUT_LINE ('Population of ' || i || ' is ' || TO_CHAR(city_population(i))); i := city_population.NEXT(i); END LOOP; END; / Population of Megalopolis is Population of Midland is Population of Smallville is 2001 DECLARE -- Associative array indexed by string: TYPE population IS TABLE OF NUMBER -- Associative array type INDEX BY VARCHAR2(64); city_population population; Associative array variable i VARCHAR2(64); BEGIN -- Add new elements to associative array: city_population('Smallville') := 2000; city_population('Midland') := ; city_population('Megalopolis') := ;

5 Understanding Nested Tables
Conceptually, a nested table is like a one-dimensional array with an arbitrary number of elements. Within the database, a nested table is a column type that holds a set of values. The database stores the rows of a nested table in no particular order. When you retrieve a nested table from the database into a PL/SQL variable, the rows are given consecutive subscripts starting at 1. These subscripts give you array-like access to individual rows. A nested table differs from an array in these important ways: An array has a declared number of elements, but a nested table does not. The size of a nested table can increase dynamically (however, a maximum limit is imposed—see Referencing Collection Elements). An array is always dense (that is, it always has consecutive subcripts). A nested array is dense initially, but it can become sparse, because you can delete elements from it.

6 Understanding Nested Tables
Figure Array and Nested Table

7 Understanding Variable-Size Arrays (Varrays)
A variable-size array (varray) is an item of the data type VARRAY. A varray has a maximum size, which you specify in its type definition. A varray can contain a varying number of elements, from zero (when empty) to the maximum size. A varray index has a fixed lower bound of 1 and an extensible upper bound. To access an element of a varray, you use standard subscripting syntax. Figure shows a varray named Grades, which has maximum size 10 and contains seven elements. The current upper bound for Grades is 7, but you can increase it to the maximum of 10. Grades(n) references the nth element of Grades. Varray of Size 10

8 Example Declaring Nested Tables, Varrays, and Associative Arrays
BEGIN -- an arbitrary number of strings can be inserted v1 v1 := nested_type('Shipping','Sales','Finance','Payroll'); v2 := varray_type(1, 2, 3, 4, 5); -- Up to 5 integers v3(99) := 10; -- Just start assigning to elements v3(7) := 100; -- Subscripts can be any integer values v4(42) := 'Smith'; -- Just start assigning to elements v4(54) := 'Jones'; -- Subscripts can be any integer values v5('Canada') := 'North America'; -- Just start assigning to elements v5('Greece') := 'Europe'; -- Subscripts can be string values END; / DECLARE TYPE nested_type IS TABLE OF VARCHAR2(30); TYPE varray_type IS VARRAY(5) OF INTEGER; TYPE assoc_array_num_type IS TABLE OF NUMBER INDEX BY PLS_INTEGER; TYPE assoc_array_str_type IS TABLE OF VARCHAR2(32) INDEX BY PLS_INTEGER; TYPE assoc_array_str_type2 IS TABLE OF VARCHAR2(32) INDEX BY VARCHAR2(64); v1 nested_type; v2 varray_type; v3 assoc_array_num_type; v4 assoc_array_str_type; v5 assoc_array_str_type2;

9 Summary Introduction Associative array (or index-by table) Nested table Variable-size array (varray)


Download ppt "Collection in PL/SQL."

Similar presentations


Ads by Google