ITPub博客

首页 > 数据库 > 国内数据库 > GBase 8s collection使用

GBase 8s collection使用

原创 国内数据库 作者:DMD_202007 时间:2021-03-24 10:08:31 0 删除 编辑

GBase8s collection 使用

一、简介

gbase8s collection属于复杂数据类型的一种,它能够在单个行内存储和处理数据集合,包含三种类型set、multiset和list。

collection中的元素几乎可以是任何数据类型(元素的类型必须相同),除了以下几种:text、byte、serial、serial8、bigserial,collection可以嵌套。

不能使用create index对collection创建索引,也不能创建collection列的函数索引。

set是元素的无序集合,每个元素唯一,不能重复。

multiset是元素的无序集合,元素可以重复。

list是元素的有序集合,元素可以重复。

二、collection使用

用于表中的列

-- 创建表,content是multiset类型的collection,nested_content是嵌套的collection,list里嵌套了set

create table t1

(

  id integer primary key,

  content multiset(varchar(20) not null),

  nested_content list(set(varchar(20) not null) not null)

);

-- 插入三行数据,set中重复的数据插入后会去重

insert into t1 values(1, multiset{'hello', 'world', '!'}, list{set{'ni', 'si', 'bu', 'si', 'sa'}, set{'ni', 'chou', 'sha'}, set{'chou', 'ni', 'za', 'di'}});

insert into t1 values(2, multiset{'hello', 'world', '1', '!'}, list{set{'ni', 'sa'}, set{'ni', 'chou', 'sa'}, set{'chou', 'ni', 'za', 'di'}});

insert into t1 values(3, multiset{'hello', 'world', '2', '!'}, list{set{'ni', 'sa', 'bu', 'sa'}, set{'ni', 'chou', 'sa'}, set{'chou', 'ni', 'za', 'di'}});

-- 查看插入的数据

select * from t1;

用于spl中

create procedure p1()

  -- 定义collection变量

  define coll1 multiset(varchar(20) not null);

  define coll2 list(set(varchar(20) not null) not null);

  define coll3 set(varchar(20) not null);

  define coll4 list(varchar(20) not null);

 

  -- 定义存放元素的变量

  define element varchar(20);

 

  define i int;

  let i = 0;

 

  -- 从表t1中获取collection,并把值赋给collection变量

  select content into coll1 from t1 where id = 2;

  select nested_content into coll2 from t1 where id = 3;

 

  -- 向set中写值

  insert into table(coll3) values('ni');

  insert into table(coll3) values('hao');

  insert into table(coll3) values('a');

 

  -- 给list赋值

  let coll4 = list{'ha', 'ha', 'wu', 'he'};

 

  -- 向list中插值

  insert at 3 into table(coll4) values('dudu');

 

  -- 遍历collection举例

  create temp table tmp0 (value varchar(20)) with no log;

  foreach cur for

    -- 把元素赋值给element变量,并插入临时表用于验证

    select * into element from table(coll1)

    insert into tmp0 values(element);

    let i = i+1;

  end foreach

  insert into tmp0 values(i);

 

  -- 把collection变量存入临时表,用于后续验证

  select * from table(coll1) into temp tmp1;

  select * from table(coll2) into temp tmp2;

  select * from table(coll3) into temp tmp3;

  select * from table(coll4) into temp tmp4;

end procedure;

 

-- 执行procedure,并查看结果

call p1();

select * from tmp0;

select * from tmp1;

select * from tmp2;

select * from tmp3;

select * from tmp4;

 


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/69980677/viewspace-2764578/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2020-07-31

  • 博文量
    36
  • 访问量
    31944