一 嵌套表概述
嵌套表是集合類型的一種。嵌套表是表中之表。一個嵌套表是某些行的集合,它在主表中表示為其中的一列。對主表中的每一條記錄,嵌套表可以包含多個行。在某種意義上,它是在一個表中存儲一對多關系的一種方法。預設情況下,每個嵌套表列都産生一個額外的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')