PL/SQL Collection: Nested Table

By Manish Sharma

Nested Table as Database Object

If you plan to reuse the nested table that you want to create then doing so as a database object is the best choice for you. You can store them in your database permanently and use them whenever you want.

Apart from creating Nested Table type PL/SQL Collection inside a PL/SQL block you can also create them as database object and store permanently. Also you can reuse them whenever you want. Nested table created as database object can be based on either Primitive Datatype or User-Define Datatype. In this tutorial we will concentrate on former and leave the latter for the next tutorial.

How to Create Nested table type collection based on primitive datatype

By primitive datatype we mean the datatypes which are predefined by the language and are named by a reserved keyword. You can refer to this Oracle Document to read more about PL/SQL Datatypes.

The following tables have no constraint, index or anything designed on them and are created purely for demonstrating how to create nested table as database object.

Step 1: Set Server output on


Step 2: Create Nested Table type collection

The above statement on successful execution will create a nested table with name ‘my_nested_table’ which will be based on primitive datatype VARCHAR2.

Step 3: How to use nested table?

The collection type which we created above can be used to specify the type of a column of a table.

 1  CREATE TABLE  my_subject (
 2   sub_id       NUMBER,
 3   sub_name     VARCHAR2 (20),
 4   sub_schedule_day  my_nested_table
 5  ) NESTED TABLE  sub_schedule_day  STORE AS  nested_tab_space;
 6  /
The above table is a normal table except that its 3rd column is of nested table type which can hold multiple values. In order to define a column of a table as nested table type you have to tell the compiler the name of the column and a storage table. You can do so by using NESTED ABLE and STORE AS clause, as we did here in line number 5. Using clause NESTED TABLE we specify the name of the column and using STORE AS clause we specify the storage table for the nested table.

You can refer to the video tutorial where I have stepwise explained the above table creation process.

Insert rows into the table

 INSERT INTO my_subject (sub_id, sub_name, sub_schedule_day)
 VALUES (101, 'Maths', my_nested_table('mon', 'Fri'));
You insert rows into the nested table same as you insert into the normal table. However in order to insert data into the column of nested table type you first have to write the name of nested table which in our case is ‘my_nested_table’ (refer step 2) and then write the data according to the datatype of your nested table and enclose it inside the parenthesis.

Retrieve data from the table

A simple SELECT DML statement can be used to retrieve the data from the table.

 SELECT * FROM my_subject;
This simple DML statement will show you all the data stored into the table that we created above. To see the data from a specific row you can use WHERE clause with SELECT DML

 SELECT * FROM my_subject WHERE sub_id = 101;
If you want then you can take help of sub-query to just check the data from the column which you defined as nested table type.

  SELECT sub_schedule_day FROM my_subject WHERE sub_id = 101
The above query will show you the data of subject which has subject-id 101 only from sub_schedule_day column. In this query we used TABLE expression to open the instance and display the data in relational format.

Update data of the table

You can either update all the values of the column which you define as nested table or you can update a single instance of the same.

Update all the values of the nested table type column.

 UPDATE my_subject SET sub_schedule_day = my_nested_table('Tue', 'Sat')
 WHERE sub_id = 101;
The above query will update all the values of sub_schedule_day from ‘Mon’, ‘Fri’ to ‘Tue’ and ‘Sat’. Now suppose you want to update only a single instance of this column by replacing ‘Sat’ with ‘Thu’. How will you do that?

Hope you enjoyed reading and learnt something new. Do make sure to subscribe to our channel as many such interesting tutorials are on their way. Thanks & have a great day!

SQL Script and Presentation used

You can DOWNLOAD SQL script and presentation used in the Video and in this article.

NO SQL Script used in Video and in this article

Do Not Drink & Drive Use My Uber Code "UberRebellionRider"
SignUp using my referral code and get first ride worth $20 FREE

  • Manish Sharma Oracle Rebellion Rider
  • Manish Sharma Oracle certified SQL expert
  • Manish Sharma oracle certified associate
  • Manish Sharma oracle certified professional
  • View Manish Sharma's profile on LinkedIn

         View Manish Sharma's profile on LinkedIn