You are on page 1of 2

Nested Table:

Nested table is a collection of rows, represented as a column within the main


table. For each record within the main table, the nested table may contain multi
ple rows. In one sense, it's a way of storing a one-to-many relationship within
one table, Consider a table that contains information about departments, each of
which may have many projects in progress at any one time. In a strictly relatio
nal model, you would create two separate tables - DEPT and PROJECT.
nested table allow you to store the information about projects within the DEPT
table. The PROJECT table records can be accessed directly via the DEPT table, w
ithout the need to perform a join. The ability to select the data without traver
sing joins may make data easier to access for users. Even if you do not define m
ethods for accessing the nested data, you have clearly associated the department
and project data. In a strictly relational model, the association between the D
EPT and PROJECT tables would be accomplished via foreign key relationships.

SQL>
2
3
4
5

create or replace type emp_ty as object


(desg varchar2(10),
dname varchar2(10),
doj date);
/

Type created.
SQL> create type emp_nt as table of emp_ty;
2 /
Type created.
SQL>
2
3
4

create table empdata


(ename varchar2(10),
details emp_nt)
nested table details store as emp_nt_tab;

Table created.
SQL> desc empdata
Name
Null?
----------------------------------------- -------ENAME
DETAILS

Type
---------------------------VARCHAR2(10)
EMP_NT

SQL> set describe depth 2


SQL> desc empdata
Name
Null?
----------------------------------------- -------ENAME
DETAILS
DESG
DNAME
DOJ

Type
---------------------------VARCHAR2(10)
EMP_NT
VARCHAR2(10)
VARCHAR2(10)
DATE

SQL> ed
Wrote file afiedt.buf
1 insert into empdata values (
2 'Raju',
3 emp_nt(

4
5
6*
SQL>

emp_ty('Clerk','Sales','12-Sep-05'),
emp_ty('Asst','Mrkt','15-Oct-04'),
emp_ty('Mngr','Sales','13-Aug-05')))
/

1 row created.
SQL> select * from empdata;
ENAME
---------DETAILS(DESG, DNAME, DOJ)
-------------------------------------------------------------------------------Raju
EMP_NT(EMP_TY('Clerk', 'Sales', '12-SEP-05'), EMP_TY('Asst', 'Mrkt', '15-OCT-04'
), EMP_TY('Mngr', 'Sales', '13-AUG-05'))
SQL> select ename,n.desg,n.doj from empdata,table(empdata.details) n;
ENAME
---------Raju
Raju
Raju

DESG
---------Clerk
Asst
Mngr

DOJ
--------12-SEP-05
15-OCT-04
13-AUG-05

You might also like