数据仓库服务 GAUSSDB(DWS)-数组类型:数组值输入

时间:2024-09-05 10:32:05

数组值输入

输入数组值时要把一个数组值写成一个文字常数,将元素值用花括号包围并用逗号分隔。一个数组常量的一般格式如下:

1
'{ val1 delim val2 delim ... }'

其中,delim是类型的定界符,每个val可以是数组元素类型的一个常量或子数组。

一个数组常量的例子如下:

1
'{{1,2,3},{4,5,6},{7,8,9}}'

该常量是一个二维的,3乘3数组,它由3个整数子数组构成。

向表books插入数据并查询表books:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
INSERT INTO books 
  VALUES (1, 'One Hundred years of Solitude','{25,25,25,25}','{{"fiction"}, {"adventure"}}'),
         (2, 'Robinson Crusoe', '{30,32,32,32}', '{{"adventure"}, {"fiction"}}'),
         (3, 'Gone with the Wind', '{27,27,29,28}', '{{"romance"}, {"fantasy"}}');  
 
SELECT * FROM books;
 id |             title             | price_by_quarter |          tags
----+-------------------------------+------------------+-------------------------
  1 | One Hundred years of Solitude | {25,25,25,25}    | {{fiction},{adventure}}
  2 | Robinson Crusoe               | {30,32,32,32}    | {{adventure},{fiction}}
  3 | Gone with the Wind            | {27,27,29,28}    | {{romance},{fantasy}}
(3 rows)

插入多维数组数据时,多维数组的每一维都必须有相匹配的长度。

使用ARRAY关键字插入数据:

INSERT INTO books 
  VALUES (1, 'One Hundred years of Solitude',ARRAY[25,25,25,25],ARRAY['fiction', 'adventure']),
         (2, 'Robinson Crusoe', ARRAY[30,32,32,32], ARRAY['adventure', 'fiction']),
         (3, 'Gone with the Wind', ARRAY[27,27,29,28], ARRAY['romance', 'fantasy']);
support.huaweicloud.com/sqlreference-910-dws/dws_06_0368.html