PLSQL:集合类型作为表列


创建集合类型(嵌套表, 变长数组作为表的列)


1)嵌套表 Nested Table

下标从1开始,元素个数有限制(*使用时必须先初始化,用extend属性可以扩展元素个数)
可以作为表定义数据类型,但是前提是要先create 创造嵌套表类型,这就可以实现一对多的定义
语法:
create  [ or   replace type  <type_name>  is   table   of  <element_type>;

例: 嵌套表作为表的列
嵌套表类型的列是单独一个表存储
需要先创建一个这样的类型存在DB中才能使用
1
create or replace type type_nest_table is table of varchar2(30);
2
create table test_nest_tab (
3
  id        int,
4
  vals      type_nest_table --vals列的类型是上面声明的嵌套表
5
) nested table vals store as aaaaa;
6
--vals字段用嵌套表存储,表名 coll_nest_table
7
8
/*
9
  上面语句:
10
  1.创建了一个嵌套表集合 type_nest_table
11
  2.创建表 test_nest_tab, 其中一列的类型是嵌套表 type_nest_table
12
  3.建表的同时, 创建了一个 type_nest_table 类型的集合变量 coll_nest_table
13
*/
14
15
insert into test_nest_tab values(1000,type_nest_table('1000A','1000B','1000C','1000D'));
16
declare
17
  v_id  test_nest_tab.id%type;
18
  v_tab test_nest_tab.vals%type;
19
begin
20
  select * into v_id, v_tab from test_nest_tab where id = 1000;
21
  for i in 1 .. v_tab.count loop
22
    dbms_output.put_line(v_tab(i));
23
  end loop;
24
end;



2)Varry 可变数组
语法:
create [or replacetype <type_name> is varray(size_limit) of <element_type> [not null];

和Java中的数组差不多了,下标从1开始 ,定义时先指定最大元素个数. 使用时也必须先用构造方法初始化 ,可以作为表列类型

例:变长数组作为表的列
可变数组是存储在表内部的,不同于嵌套表
1
create or replace type varr_type is varray(10) of varchar2(30);--先创建类型
2
create table test_varray(
3
    id      int,
4
    name    varchar2(30),
5
    params  varr_type --param是使用可变数组类型
6
);
7
8
insert into test_varray values(1,'mingzi',varr_type('a','b','c'));
9
declare
10
    v_varr varr_type;
11
    v_name test_varray.name%type;
12
begin
13
    select name,params into v_name,v_varr from test_varray where id=1;
14
    for i in 1..v_varr.count loop
15
    dbms_output.put_line(v_varr(i));
16
    end loop;
17
end;
18



注意:

想要删除/重建type的时候, 要先删除引用了这个type类型的所有表, 之后才能成功删除/重建!






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值