Nested Tables
Nested tables are persistent form of collections which are physically stored in database (disk space consumption based on number of elements they hold). They are unbounded and distributed set of elements in key-value structure.
As per the Oracle internal architecture, a nested table type is stored as a table structure. If a column of nested table type is included in a table, oracle generates a separate relational table to hold the data and actual column referring to it. This system generated table is maintained by the server and it is named in SYS_NCXXXXX$ format. The actual table column is marked as a Virtual Column by Oracle.
During retrieval of a nested table data, the nested table rows are fetched in sequence of the subscript. Note that the subscript is not stored along with the nested table but Oracle implicitly maintains the index to identify each row in a nested table.
Syntax [2a]
In PL/SQL
Sample Code
DECLARE
TYPE type_name IS TABLE OF element_type [NOT NULL];
Copyright exforsys.com
Element type can be a User defined type or primitive data type, but cannot be REF CURSOR.
Syntax [2b]
In SQL
Sample Code
CREATE [OR REPLACE] TYPE type_name IS TABLE OF element_type;
Copyright exforsys.com
Element types cannot be any of BOOLEAN, LONG, LONG RAW, NATURAL, NATURALN, POSITIVE, POSITIVEN, REF CURSOR, SIGNTYPE, or STRING.
Example [3a]
The example below demonstrates the creation of a table with a column of nested table type.
Sample Code
SQL> CREATE TYPE EX_NEST_TAB AS TABLE OF NUMBER;
/
Type created.
Copyright exforsys.com
Note the syntax to create the table with a nested table column. The table definition must specify an arbitrary name of separate storage table, which would be used by the server to hold column data.
Example [3b]
Sample Code
SQL> CREATE TABLE EX_COLL_TABLE
(COL1 NUMBER,
COL2 EX_NEST_TAB)
NESTED TABLE COL2 STORE AS NESTED_COL2;
TABLE created.
Copyright exforsys.com
Amazingly, when the column was queried in USER_TAB_COLS view, it was discovered as a virtual column in the USER_TAB_COLS dictionary view. Oracle internally identifies a nested table collection type column as a virtual column.
Example [3c]
Sample Code
SQL> SELECT TABLE_NAME, COLUMN_NAME, VIRTUAL_COLUMN FROM USER_TAB_COLS WHERE TABLE_NAME=EX_COLL_TABLE;
TABLE_NAME COLUMN_NAME VIR
------------------------------ ------------------------------ ---
EX_COLL_TABLE COL1 NO
EX_COLL_TABLE COL2 YES
EX_COLL_TABLE SYS_NC0000200003$ NO
Copyright exforsys.com
USER_NESTED_TABLES is another dictionary view which maintains the metadata of nested tables which are owned by the current user.
The additional storage table (NESTED_COL2 in this case) is also maintained in USER_TABLES dictionary view, with NESTED column value as YES.
Handling Nested tables: Initialization, DML operations, and Selection
Nested table in SQL as standalone type stored in database: In SQL, nested table is physically stored in the database. Later it can be used in an anonymous PL/SQL block or during table creation. We shall see the DML operations on nested tables and their Selection.
Insertion: INSERT statement can be prepared using the nested table collection type.
Example [4a]
Sample Code
SQL> INSERT INTO EX_COLL_TABLE VALUES (1, EX_NEST_TAB(100,200,300));
1 row inserted.
SQL> INSERT INTO EX_COLL_TABLE VALUES (11, EX_NEST_TAB(123,694,212,341));
1 row inserted.
SQL> INSERT INTO EX_COLL_TABLE VALUES (23, EX_NEST_TAB(42,20));
1 row inserted.
SQL> COMMIT;
Commit complete.
Copyright exforsys.com
Selection: Conventional way of data selection from a table would get the data in complex and instance format.
Example [4b]
Sample Code
SQL> SELECT * FROM EX_COLL_TABLE;
COL1 COL2
---- ----
1 EX_NEST_TAB(100,200,300)
11 EX_NEST_TAB(123,694,212,341)
23 EX_NEST_TAB(42,20)
Copyright exforsys.com
To get the data in the readable format, TABLE function can be used. It brings the nested table column data in relational format, thereby making it easy inferable.
Example [4c]
Sample Code
SQL> SELECT T1.COL1 COL1, T2.*
FROM EX_COLL_TABLE T1, TABLE(T1.COL2) T2;
COL1 COLUMN_VALUE
---- ------------
1 100
1 200
1 300
11 123
11 694
11 212
11 341
23 42
23 20
Copyright exforsys.com
Update the collection attribute value
Example [4d]
The update statement below updates the collection column in the table. Alternatively, it can be done using a PL/SQL block too.
Sample Code
SQL> UPDATE EX_COLL_TABLE
SET COL2 = EX_NEST_TAB (93,81)
WHERE COL1 = 23;
1 row updated.
SQL> SELECT T1.COL1 COL1, T2.*
FROM EX_COLL_TABLE T1, TABLE(T1.COL2) T2;
COL1 COLUMN_VALUE
---- ------------
1 100
1 200
1 300
11 123
11 694
11 212
11 341
23 93
23 81
Copyright exforsys.com
As a subprogram parameter: Nested table column can be passed as a parameter too. Additionally, it can be the return type of a stored function.
Example [4e]
The procedure P_CHECK_ARRAY counts of even and odd numbers in the input array. The PL/SQL block below uses the above procedure to verify the objective.
Sample Code
SQL> CREATE OR REPLACE PROCEDURE P_CHECK_ARRAY (P_NEST EX_NEST_TAB)
IS
L_EVE NUMBER := 0;
L_ODD NUMBER := 0;
BEGIN
IF P_NEST.FIRST IS NULL THEN
DBMS_OUTPUT.PUT_LINE ('Empty Collection');
ELSE
FOR I IN 1..P_NEST.COUNT
LOOP
IF MOD(P_NEST(I),2) = 0 THEN
L_EVE := L_EVE+1;
ELSE
L_ODD := L_ODD+1;
END IF;
END LOOP;
DBMS_OUTPUT.PUT_LINE ('Array contains '|| L_EVE||' even numbers');
DBMS_OUTPUT.PUT_LINE ('Array contains '|| L_ODD||' odd numbers');
END IF;
END;
/
Procedure created.
SQL> DECLARE
L_ARRAY EX_NEST_TAB := EX_NEST_TAB();
BEGIN
P_CHECK_ARRAY (L_ARRAY);
DBMS_OUTPUT.PUT_LINE (----Populating array----);
L_ARRAY := EX_NEST_TAB (24,164,256,17,82,93,52);
P_CHECK_ARRAY (L_ARRAY);
END;
/
Empty collection
----Populating array----
Array contains 5 even numbers
Array contains 2 odd numbers
PL/SQL procedure successfully completed.
Copyright exforsys.com
Nested tables as PL/SQL construct: Initialization and usage: When defined in a PL/SQL block, nested table variable remains uninitialized and NULL collection. It is mandatory to initialize it to make it empty, a non null collection; else it raises ORA-06531 exception.
Example [5a]
A PL/SQL block declares initializes and prints two nested table collection arrays. Collection methods used in the blocks are discussed later in this tutorial.
Sample Code
DECLARE
TYPE EX_NEST_TAB IS TABLE OF NUMBER;
L_INI_ARRAY EX_NEST_TAB := EX_NEST_TAB();
L_VAL_ARRAY EX_NEST_TAB := EX_NEST_TAB (1,2,34);
N NUMBER := 0;
BEGIN
WHILE (N <10) LOOP
L_INI_ARRAY.EXTEND;
L_VAL_ARRAY.EXTEND;
N := N+1;
L_INI_ARRAY (L_INI_ARRAY.LAST) := N;
L_VAL_ARRAY (L_VAL_ARRAY.LAST) := N;
END LOOP;
DBMS_OUTPUT.PUT_LINE('----Display L_INI_ARRAY----');
FOR I IN 1..L_INI_ARRAY.COUNT
LOOP
DBMS_OUTPUT.PUT_LINE('Cell: '||I||' holds element '||L_INI_ARRAY(I));
END LOOP;
DBMS_OUTPUT.PUT_LINE('----Display L_VAL_ARRAY----');
FOR I IN 1..L_VAL_ARRAY.COUNT
LOOP
DBMS_OUTPUT.PUT_LINE('Cell: '||I||' holds element '||L_VAL_ARRAY(I));
END LOOP;
END;
/
Copyright exforsys.com
Nested Tables Usage Notes
- Size of the nested table increases dynamically. It is capable of accommodating arbitrary number of elements
- In case of deletion of any element from a nested table, it is rendered sparse.
- Applications of Nested tables: They fit into almost major scenarios in real time applications. Mostly, when the number of elements is variable or not known, nested tables are the best choice.
This tutorial is part of a Oracle 11g Tutorial tutorial series. Read it from the beginning and learn yourself.
H I D E