天天看点

嵌套表

一 嵌套表概述

       嵌套表是集合类型的一种。嵌套表是表中之表。一个嵌套表是某些行的集合,它在主表中表示为其中的一列。对主表中的每一条记录,嵌套表可以包含多个行。在某种意义上,它是在一个表中存储一对多关系的一种方法。默认情况下,每个嵌套表列都产生一个额外的raw(16)隐藏列,并在其上创建了唯一约束,用以指向嵌套表。而嵌套表中有两个隐藏列:sys_nc_rowinfo$是作为一个对象返回所有标量元素的一个伪列;另一个nested_table_id的外键回指向父表。

       存储在一个数据库中的嵌套表并不与表中的其他数据存放在同一个数据块中,它们实际上被存放在第二个表中。正如没有order by字句select语句不能保证返回任何有顺序的数据,从数据库中取回的嵌套表也不能保证元素的顺序。由于集合数据是离线存储的,对于大型集合嵌套表是 一个不错的选择。

二  嵌套表的语法:

       嵌套表的定义语法:type type_name is table of element_type [not null];

三 嵌套表的特性:

       嵌套表集合中的元素没有数量限制

       嵌套表的存储不与表中其他数据存放在同一个数据块中,存放在第二个表中。

       嵌套表集合离线存储

四 嵌套表实例

1)创建嵌套表类型

       create type scott.t_tab2_emp as table of varchar2(50);

       创建嵌套表类型在oracle日志表现为:50  create type t_tab2_emp as table of

varchar2(50);;

2)创建嵌套表

       create table scott.test_coll_nested(departement number,

employees   t_tab2_emp);

---创建嵌套表的时候要为嵌套表类型另外创建一个表来保存数据, nested 以下的部分就是在干这事。那个表的名称为:       

next_table_name,当前实例的那个表的名称为test_table_emp。

       创建嵌套表在oracle日志表现为:没有日志

3)获取表信息

       desc scott.test_coll_nested;

        name                       null?    type

        ----------------------------------------- --------

----------------------------

        departement                        number

        employees                        scott.t_tab2_emp

       也可以直接通过数据字典来获得嵌套表的信息

       select * from all_nested_tables where  owner='scott' and

table_name='test_table_emp' and parent_table_name='test_coll_nested';

4)填充嵌套表数据

       insert into scott.test_coll_nested values(1,scott.t_tab2_emp

('hello','world'));

       填充嵌套表数据在oracle日志表现为:

              71  insert into "scott"."test_table_emp"("column_value")

values ('hello');

values ('world');

              68  insert into "scott"."test_coll_nested"("departement")

values ('1');

       insert into scott.test_coll_nested

values(2,scott.t_tab2_emp('123','456','789','101','120','114','45'));

              69  insert into "scott"."test_table_emp"("column_value")

values ('123');

values ('456');

values ('789');

values ('101');

values ('120');

values ('114');

              68  insert into "scott"."test_table_emp"("column_value")

values ('45');

values ('2');

5)更改嵌套表数据

        update  scott.test_coll_nested set

employees=scott.t_tab2_emp('welcome','to','china') where departement=1;

       更改嵌套表数据在oracle日志表现为:

              99  update "scott"."test_coll_nested" set  where

"departement" = '1' and rowid = 'aabrobaajaaalsiaaa';

              73  insert into "scott"."test_table_emp"("column_value")

values ('welcome');

values ('to');

values ('china');

              102 delete from "scott"."test_table_emp" where

"column_value" = 'hello' and rowid = 'aabrocaajaaalsaaaa';

"column_value" = 'world' and rowid = 'aabrocaajaaalsaaab';

6)删除嵌套表数据

       delete from scott.test_coll_nested where departement=1;

       删除嵌套表数据在oracle日志表现为:

              99  delete from "scott"."test_coll_nested" where

              104 delete from "scott"."test_table_emp" where

"column_value" = 'welcome' and rowid = 'aabrocaajaaalsaaaj';

              99  delete from "scott"."test_table_emp" where

"column_value" = 'to' and rowid = 'aabrocaajaaalsaaak';

"column_value" = 'china' and rowid = 'aabrocaajaaalsaaal';

7)检索嵌套表数据

        select * from  scott.test_coll_nested;

departement----------employees-----------------------------------

             2                                t_tab2_emp('123', '456',

'789', '101', '120', '114', '45')