1

以下是我拥有的对象类型。基本上我有一个人表和一个子表作为人表的嵌套表。

我有一个与子表(嵌套)有 M:N 关系的 School 表。所以我正在创建一个中间表来插入 child_school 数据。

在此处输入图像描述

如何创建该中间表并插入数据?

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
/

——有问题。下面不起作用。

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
)
/

--这就是我想做的

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

cid 引用应该是嵌套表中的 cid。问题在于它。

4

2 回答 2

4

我看到了你的编辑,我正要告诉你不可能在外部引用嵌套表。

嵌套表在物理上创建为一个不同的表,它与父表分开保存数据:

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

在这里您可以看到 Oracle 已经创建了一个 CHILD_TABLE 表,但是它对我们隐藏并且只能由 Oracle 在内部工作:

SQL> select * from child_table;

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

在这种情况下,我很确定您不能以任何方式引用子表,但令我惊讶的是,这似乎有效(我们不能从 CHILD_TABLE 中选择,但我们可以引用它):

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

你可以像这样构建你的插入(我真的不喜欢将数据存储为对象,但是你去):

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
于 2011-04-05T12:57:31.657 回答
2

我稍微改变了你的数据模型:

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>

让我们填充嵌套表:

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> 

这是一个嵌套表:

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>

我们像这样填充交集表:

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>

查询返回结果

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>

当然,这提出了一个问题:如果要使用对象的 REF,是否需要 ID 列?当然,我认为对于 CHILD_T 类型有一个称为 NUMBER 类型的 CID 的属性和对于 SCHOOL_CHILD_T 类型具有相同名称但数据类型为 REF 的属性是一种误导。

于 2011-04-05T14:21:42.080 回答