集合是一种类似列表或一维数组的数据结构
1,关联数组. 2,嵌套表. 3,VARRAY
集合变量都要基于一个预先定义好的集合进行声明
同质元素:集合中的每一个元素的数据类型都是相同的。
一个集合对集合中的行数进行了预先的设置,这个集合就是有界的,
VARRAY或长度可变的数组是有界的
嵌套表和关联数组是无界的
紧凑和稀疏:一个集合从第一行到最后一行的所有行都被定义并且赋值,就是紧凑的
关联数组
也叫索引表(定义集合的时候需要显示的说明它们使用行号"索引的")
2.嵌套表
一维、无界、同质、开始是紧凑型随着后面的删除逐渐变的稀松、嵌套表的元素没有固定的顺序
VARRAY
有边界,紧凑、提取数据有顺序
--声明关联数组类型
declare
type list_of_names_t is table of ljc_study_plsql.first_name%type index by pls_integer;
--声明一个关联数组类型,用最具特色的index by 子句。
--基于这个类型的集合变量包含了一个字符串的列表,列表中的每一个元素具有和ljc_study_plsql 表的 first_name 列相同的长度
--index by 可以通过 varchar类型或pls_integer类型来关联或索引集合中的内容
--type typ_dict is table of varchar2(100) index by varchar2(10);
happyfamily list_of_names_t; --使用list_of_names_t 类型声明了一个叫 happyfamily的集合
l_row pls_integer;
begin
happyfamily(1000) := 'QU';
happyfamily(-100) := 'lijiachen';
happyfamily(-1000) := 'cma';
happyfamily(100) := 'SY';
l_row := happyfamily.first; --调用first方法 获取集合的第一行的行号或是有定义的最小行号。
while(l_row is not null)
loop
dbms_output.put_line(happyfamily(l_row));
l_row := happyfamily.next(l_row);
end loop;
end;
运行结果:
cma
lijiachen
SY
QU
--使用嵌套表
declare
type list_of_names_t is table of varchar2(100); --创建一个嵌套表类型
happyfamily list_of_names_t := list_of_names_t(); --声明三种嵌套表 使用构造函数对嵌套表进行初始化,使用嵌套表必须对其初始化
children list_of_names_t := list_of_names_t();
parents list_of_names_t := list_of_names_t();
begin
happyfamily.extend(4); --使用extend方法对表进行扩展控件,必须显示的请求一行
happyfamily(1) := '李靖';
happyfamily(2) := '哪吒';
happyfamily(3) := '金吒';
happyfamily(4) := '四大天王';
children.extend;
children(1) := '哪吒';
children.extend;
children(2) := '金吒';
parents := happyfamily multiset except children;
for l_row in parents.first .. parents.last
loop
dbms_output.put_line(parents(l_row));
end lo0p;
end;
--varray
declare
type first_names_t is varray(2) of varchar2(100);
type child_names_t is varray2(1) of varchar2(100);
parents first_names_t := first_names_t();
children children_names_t := child_names_t();
begin
parents.extend(2);
parents(1) := '李靖';
parents(2) := '东海龙王';
children.extend;
children(1):= '哪吒';
end ;
汇总:
declare
type typ_array is table of integer;
type typ_dict is table of varchar2(100) index by varchar2(10);
type typ_varray is varray(3) of varchar2(10);
v_array typ_array := typ_array();
v_dict typ_dict;
v_varray typ_varray := typ_varray(null, null, null);
begin
v_array.extend(2);
dbms_output.put_line('The v_array''s count is ' || v_array.count);
v_array(1) := 1;
for i in v_array.first .. v_array.last loop
dbms_output.put_line('The v_array(' || i || ') is ' || v_array(i));
end loop;
v_dict('one') := 'day';
v_dict('two') := 'week';
dbms_output.put_line('The v_dict(''one'') is ' ||
nvl(v_dict('one'), 'null'));
dbms_output.put_line('The v_dict(''two'') is ' ||
nvl(v_dict('two'), 'null'));
v_varray(1) := 'a';
v_varray(2) := 'b';
for i in v_varray.first .. v_varray.last loop
dbms_output.put_line('The v_varray(' || i || ') is ' ||
nvl(v_varray(i), 'null'));
end loop;
v_varray.trim(1);
dbms_output.put_line('The v_varray trim(1)');
for i in v_varray.first .. v_varray.last loop
dbms_output.put_line('The v_varray(' || i || ') is ' ||
nvl(v_varray(i), 'null'));
end loop;
v_varray.extend(1);
dbms_output.put_line('The v_varray extend(1)');
for i in v_varray.first .. v_varray.last loop
dbms_output.put_line('The v_varray(' || i || ') is ' ||
nvl(v_varray(i), 'null'));
end loop;
if (v_varray.EXISTS(4)) then
dbms_output.put_line('The v_varray(4) is exists.');
else
dbms_output.put_line('The v_varray(4) is not exists.');
end if;
end;
运行结果: