Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Nested tables:

Nested tables are single-dimensional, unbounded collections of homogeneous


elements. First, a nested table is single-dimensional, meaning that each row has a
single column of data like a one-dimension array. Second, a nested table is
unbounded. It means that the number of elements of a nested table is
predetermined.

PL/SQL nested tables:


Nested tables are single-dimensional, unbounded collections of homogeneous
elements.

 First, a nested table is single-dimensional, meaning that each row has a single
column of data like a one-dimension array.
 Second, a nested table is unbounded. It means that the number of elements
of a nested table is predetermined.
 Third, homogeneous elements mean that all elements of a nested table have
the same data type.

Noted that a nested table is initially dense. However, it can become sparse through
the removal of elements.

In this diagram, the first table, which is the parent table, contains information about
customers, and associates a unique identifier for each customer. The second table,
the child table, contains the purchases for each customer. The purchases in the child
table are related to the parent table by the unique identifier,
the CustomerKey column. The third table in the diagram shows the two tables
combined.
A nested table is represented in the case table as a special column that has a data
type of TABLE. For any particular case row, this kind of column contains selected
rows from the child table that pertain to the parent table.

The data in a nested table can be used for prediction or for input, or for both. For
example, you might have two nested table columns in a model: one nested table
column might contain a list of the products that a customer has purchased, while the
other nested table column contains information about the customer's hobbies and
interests, possibly obtained from a survey. In this scenario, you could use the
customer's hobbies and interests as an input for analyzing purchasing behavior, and
predicting likely purchases.

The syntax for creating a nested table is similar to that for an index-by table:
TYPE type name IS TABLE OF element datatype [NOT NULL];

For example, here’s the command to create a nested table type called
a_simple_udt_t; its elements consist of numeric data, each with no more than 10
digits:
CREATE TYPE a_simple_udt_t IS TABLE OF NUMBER(10);

The statements are:


1. True. Nested table types can indeed be used as datatypes for Oracle tables
as in this DDL:
2. SQL> CREATE TABLE a_table (

3. 2 col1 number,

4. 3 col2 a_simple_udt_t )

5. 4 NESTED TABLE col2 STORE AS a_table_col2;

6.

Table created.

The NESTED TABLE clause specifies the name of the table used to store the data in
the nested column.
7. True. Whether a nested table type is used as a column in a table or in PL/SQL
itself, there is no limit to the number of rows it can store.
8. False. All rows in a nested table must contain the same type of data.
9. True. Nested table types are stored in the database. To view information
about the nested table types in a database including their structure, use the data
dictionary views ALL_TYPES and ALL_TYPE_ATTRS.
10. False. Nested table types can be based on user-defined data types as well as
Oracle’s native data types. For example, you can create an object as:
11. SQL> CREATE TYPE an_object_t AS OBJECT (

12. 2 column_one VARCHAR2(30),

13. 3 column_two NUMBER(10,2) );

14.

Type created.

And then create a nested table type based on the object as:
SQL> CREATE TYPE a_nested_table_t AS TABLE OF an_object_t; ...

Nested Tables:
A nested table is like a one-dimensional array with an arbitrary number of elements.
However, a nested table differs from an array in the following aspects −
 An array has a declared number of elements, but a nested table does not. The
size of a nested table can increase dynamically.
 An array is always dense, i.e., it always has consecutive subscripts. A nested
array is dense initially, but it can become sparse when elements are deleted
from it.
A nested table is created using the following syntax −
TYPE type_name IS TABLE OF element_type [NOT NULL];

table_name type_name;
This declaration is similar to the declaration of an index-by table, but there is
no INDEX BY clause.
A nested table can be stored in a database column. It can further be used for
simplifying SQL operations where you join a single-column table with a larger table.
An associative array cannot be stored in the database.
Example:
The following examples illustrate the use of nested table –
DECLARE
TYPE names_table IS TABLE OF VARCHAR2(10);
TYPE grades IS TABLE OF INTEGER;
names names_table;
marks grades;
total integer;
BEGIN
names := names_table('Kavita', 'Pritam', 'Ayan', 'Rishav', 'Aziz');
marks:= grades(98, 97, 78, 87, 92);
total := names.count;
dbms_output.put_line('Total '|| total || ' Students');
FOR i IN 1 .. total LOOP
dbms_output.put_line('Student:'||names(i)||', Marks:' || marks(i));
end loop;
END;
/
When the above code is executed at the SQL prompt, it produces the following result

Total 5 Students
Student:Kavita, Marks:98
Student:Pritam, Marks:97
Student:Ayan, Marks:78
Student:Rishav, Marks:87
Student:Aziz, Marks:92

PL/SQL procedure successfully completed.


Create a table with NESTED TABLE column:

CREATE OR REPLACE TYPE my_tab_t AS TABLE OF VARCHAR2(30);


/
CREATE TABLE nested_table (id NUMBER, col1 my_tab_t)
NESTED TABLE col1 STORE AS col1_tab;

Insert data into table:

INSERT INTO nested_table VALUES (1, my_tab_t('A'));


INSERT INTO nested_table VALUES (2, my_tab_t('B', 'C'));
INSERT INTO nested_table VALUES (3, my_tab_t('D', 'E', 'F'));
COMMIT;

Select from nested table:

SQL> SELECT * FROM nested_table;


ID COL1
---------- ------------------------
1 MY_TAB_T('A')
2 MY_TAB_T('B', 'C')
3 MY_TAB_T('D', 'E', 'F')

Unnesting the subtable:

SQL> SELECT id, COLUMN_VALUE FROM nested_table t1, TABLE(t1.col1) t2;


ID COLUMN_VALUE
---------- ------------------------
1 A
2 B
2 C
3 D
3 E
3 F
6 rows selected.

You might also like