1

Below are the object types I have. Basically I have a person table and a child table as a nested table of person table.

I have a School table with a M:N Relationship with child table (nested). So I'm creating a intermediate table to insert child_school data.

enter image description here

How can I create that intermediate table and insert data?

create type school_t as object(
    sid number(5,2),
    name varchar(20))
/

create type child_t as object(
    cid number(5,2),
    name varchar(20))
/

create type childtable_t as table of child_t
/

create type person_t as object(
    pid number(5,2),
    name varchar(20),
    child childtable_t)
/

create table person_tab of person_t(
    pid primary key
)nested table child store as child_table
/

create table school_tab of school_t
/

--there's some problem. Below does not work.

create type school_child_t as object(
    cid ref person_t,
    sid ref school_t)
/

create table school_child_tab of school_child_t(
    cid references person_tab,
    sid references school_tab
)
/

--Here's what I want to do

create table school_child_tab(
    cid number(5,2) references childtable_t,
    sid number(5,2) references school_tab
)
/

cid reference should be the cid in nested table. The problem is referring it.

Nipuna
  • 6,846
  • 9
  • 64
  • 87
  • 2
    Why does PERSON have a nested table of CHILDTABLE_T? In fact your whoel data model seems a bit confused. Perhaps you should explain what you are trying to model, rather than asking us to interpret your code? – APC Apr 05 '11 at 13:56
  • @APC: This is my try to understand weak entity as entity table and referencing it from outer. I added an image so you can understand. – Nipuna Apr 05 '11 at 14:07
  • But *why* can a person *have* many children? If CHILD is a subtype of PERSON then that is 1:1 relationship, not !:M. If PERSON means something like TEACHER or PARENt then your model is wrong in other ways. – APC Apr 05 '11 at 14:25
  • Yes I think You're correct. It should be 1:1. Thanks! – Nipuna Apr 05 '11 at 14:28

2 Answers2

4

I saw your edit, and I was about to tell you it is impossible to reference a nested table externally.

The nested table is physically created as a distinct table that holds data separately from the parent table:

SQL> SELECT object_name, object_type
  2    FROM all_objects
  3   WHERE created > trunc(sysdate)
  4     AND object_type = 'TABLE';

OBJECT_NAME                    OBJECT_TYPE
------------------------------ -------------------
SCHOOL_TAB                     TABLE
CHILD_TABLE                    TABLE
PERSON_TAB                     TABLE

Here you can see that Oracle has created a CHILD_TABLE table, however it is hidden from us and can only be worked internally by Oracle:

SQL> select * from child_table;

ORA-22812: cannot reference nested table column's storage table

In this case I was pretty sure that you couldn't reference the child table in any way, however to my surprise this seems to work (we can't select from CHILD_TABLE, however we can reference to it):

SQL> alter table child_table add constraint pk_child_table primary key (cid);

Table altered

SQL> CREATE TABLE school_child_tab (
  2     cid REFERENCES child_table,
  3     sid REFERENCES school_tab
  4  );

Table created

You could build your inserts like this (I don't really like to store to store data as objects, but here you go):

SQL> insert into school_tab values (school_t(1, 'school A'));

1 row inserted

SQL> insert into person_tab values (
  2      person_t(1, 'person A', childtable_t(child_t(1, 'child A'))));

1 row inserted

SQL> insert into school_child_tab values (1, 1);

1 row inserted
Vincent Malgrat
  • 66,725
  • 9
  • 119
  • 171
  • 5
    +1 Does anyone EVER use nested tables in database tables, outside of college assignments? – Tony Andrews Apr 05 '11 at 13:23
  • @Vincent: I want to refer to the cid in child_t object which is in nested table. Not the pid in Person_t – Nipuna Apr 05 '11 at 13:36
  • @TonyAndrews - I have come across genuine if highly specialized uses for them. Oracle Spatial uses nested tables, which points to the sort of scenario where the implementation may be valid . But I agree that most examples - like this - are spurious and would be better implemented as normal tables. – APC Apr 05 '11 at 13:51
  • 1
    @Nipuna: you can reference the nested table, see my updated answer – Vincent Malgrat Apr 05 '11 at 14:02
2

I have slightly altered your data model:

SQL> create type school_t as object(
  2      sid number(5,2),
  3      name varchar(20))
  4  /

Type created.

SQL> create type child_t as object(
  2      cid number(5,2),
  3      name varchar(20))
  4  /

Type created.

SQL> create table school_tab of school_t
  2  /

Table created.

SQL> create table child_tab  of child_t
  2  /

Table created.

SQL>

Let's populate the nested tables:

SQL> insert into child_tab
  2      values (111, 'Fred')
  3  /

1 row created.

SQL> insert into child_tab
  2      values (112, 'Ayesha')
  3  /

1 row created.

SQL> insert into child_tab
  2      values (113, 'Aadil')
  3  /

1 row created.

SQL> insert into school_tab
  2      values (222, 'Bash Street')
  3  /

1 row created.

SQL> insert into school_tab
  2      values (223, 'Greyfriars')
  3  /

1 row created.

SQL> 

Here is a nested table:

SQL> create type school_child_t as object(
  2      cid ref child_t,
  3      sid ref school_t)
  4  /

Type created.

SQL> create table school_child_tab of school_child_t
  2  /

Table created.

SQL>

We populate the intersection table like this:

SQL> insert into school_child_tab
  2        select cid, sid
  3        from
  4          ( select ref(c) as cid from child_tab c where c.cid = 111 )
  5          , ( select ref(s) as sid from school_tab s where s.sid = 222 )
  6  /

1 row created.

SQL> insert into school_child_tab
  2        select cid, sid
  3        from
  4          ( select ref(c) as cid from child_tab c where c.cid = 112 )
  5          , ( select ref(s) as sid from school_tab s where s.sid = 222 )
  6  /

1 row created.

SQL> insert into school_child_tab
  2        select cid, sid
  3        from
  4          ( select ref(c) as cid from child_tab c where c.cid = 113 )
  5          , ( select ref(s) as sid from school_tab s where s.sid = 222 )
  6  /

1 row created.

SQL> insert into school_child_tab
  2        select cid, sid
  3        from
  4          ( select ref(c) as cid from child_tab c where c.cid = 113 )
  5          , ( select ref(s) as sid from school_tab s where s.sid = 223 )
  6  /

1 row created.

SQL>

Query back the results

SQL> select c.name as child_name
  2         , s.name as school_name
  3  from     school_child_tab sc
  4              join child_tab c
  5                  on ( ref(c) = sc.cid )
  6              join school_tab s
  7                  on ( ref(s) = sc.sid )
  8  /

CHILD_NAME           SCHOOL_NAME
-------------------- --------------------
Fred                 Bash Street
Ayesha               Bash Street
Aadil                Greyfriars
Aadil                Bash Street

SQL>

Of course, that raises a question: if you're going to use the object's REF do you need the ID column? Certainly I think it is misleading to have a attribute called CID of type NUMBER for the CHILD_T type and an attribute with the same name but a datatype of REF for the SCHOOL_CHILD_T type.

APC
  • 144,005
  • 19
  • 170
  • 281
  • Thanks but i wanted to refer the nested table from a outer query. I got what I wanted from Vincents last edit. – Nipuna Apr 05 '11 at 14:40