Article Directory
foreword
PostgreSQL supports fixed-length or variable-length one-dimensional or multi-dimensional arrays for table fields, and the type of the array can be any database built-in type, user-defined type, enumeration type, and composite type.
The definition of the array type is realized by adding square brackets [] behind its basic type, and a length number can be given or not in the square brackets.
1. Define the array type
First of all, we first created the t_test table, which contains the array field name of type int and the array field address of type text, as shown in the following code snippet.
create table t_test (
id bigint,
name integer[],
address text[]
);
2. Array type insert data
Method 1: Use {} to convert data into an array
insert into t_test(id,name,address) values(1,'{1,2}','{"beijing","shanghai"}');
Method 2: Use array to convert data into an array
insert into t_test(id,name,address) values(2,array[1,2],array['beijing','shanghai']);
The result after insertion is shown in the figure below.
3. Array type modify data
Append array elements at the end: You can use the array_append function or use ||.
--使用||修改
update t_test set name = name||'{3}' , address = address||'{"shenzhen"}' where id=1;
--使用array_append函数修改
update t_test set name = array_append(name,3) , address = array_append(address,'shenzhen') where id=1;
Append array elements to the head: use the array_prepend function
update t_test set name = array_prepend(3,name) , address = array_prepend('shenzhen',address) where id=2;
4. Array type delete data
Delete the entire row of data by array type.
--如果name数组的第二个元素为1,则删除整行数据
delete from t_test where name[2] = 1;
If you want to delete an element in the array, you need to use the array_remove function with the update statement to achieve, the code is as follows.
update t_test set name= array_remove(name,3) where id=2;
5. Array type query data
Query an element in the array, such as querying the address array and the second element of the address array
select address,address[2] from t_test;
Query the data row containing 3 elements in the name array
select * from t_test where array_position(name,3)>0;
6. Array type operators
The following operators are used for processing or judging between arrays.
operator | describe | example | result |
---|---|---|---|
= | equal | ARRAY[1,2,3] = ARRAY[1,2,3] | true |
<> | not equal to | ARRAY[1,2,3] <> ARRAY[1,2,4] | true |
< | less than | ARRAY[1,2,3] < ARRAY[1,2,4] | true |
> | more than the | ARRAY[1,4,3] > ARRAY[1,2,4] | true |
<= | less than or equal to | ARRAY[1,2,3] <= ARRAY[1,2,3] | true |
>= | greater than or equal to | ARRAY[1,4,3] >= ARRAY[1,4,3] | true |
@> | Include | ARRAY[1,4,3] @> ARRAY[3,1] | true |
<@ | contained in | ARRAY[2,7] <@ ARRAY[1,7,4,2,6] | true |
&& | overlap (have common elements) | ARRAY[1,4,3] && ARRAY[2,1] | true |
|| | Array to Array Concatenation | ARRAY[1,2,3] || ARRAY[4,5,6] | {1,2,3,4,5,6} |
7. Commonly used functions of array type
function | illustrate | sentence | result |
---|---|---|---|
array_append | Add an element to the end of the array | array_append(ARRAY[1,2], 3) | {1,2,3} |
array_prepend | Add a function to the beginning of an array | array_prepend(1, ARRAY[2,3]) | {1,2,3} |
array_cat | concatenates two arrays | array_cat(ARRAY[1,2,3], ARRAY[4,5]) | {1,2,3,4,5} |
array_replace | Replaces each array element equal to a given value with a new value | array_replace(ARRAY[1,2,5,4], 5, 3) | {1,2,3,4} |
array_remove | Deletes an array element with a given value | array_remove(ARRAY[1,2,3,4],4) | {1,2,3} |
[start:end] | Range array elements by subscript | select (ARRAY[‘a’,‘b’,‘c’,‘d’])[1:2] | {a,b} |
array_position | The position at which the specified element occurs in the array | array_position(ARRAY[1,8,3,7], 8) | 2 |
array_dims | Returns a textual representation of the array's dimensions | array_dims(ARRAY[[1,2,3], [4,5,6]]) | [1:2][1:3] |
array_lower | Returns the lower bound of the array dimension | array_lower(ARRAY[1,2,3,4], 1) | 1 |
array_upper | Returns the upper bound of the array dimension | array_upper(ARRAY[1,2,3,4], 1) | 4 |
array_ndims | Returns the dimension of the array | array_ndims(ARRAY[[1,2,3], [4,5,6]]) | 2 |
array_length | Returns the length of an array dimension | array_length(array[1,2,3], 1) | 3 |
cardinality | Returns the total number of elements in the array, or 0 if the array is empty | cardinality(ARRAY[1,2,3,5]) | 4 |
array_to_string | Convert array to string, concatenate array elements with delimiter | select array_to_string(ARRAY[1, 2, 3], ‘,’) | 1,2,3 |
string_to_array | Splits a string into array elements using the specified delimiter | select string_to_array(‘a,b,c’,‘,’) | {a,b,c} |
array_agg | Combine multiple values into an array | SELECT id, array_agg(label) FROM t_label group by id | {label1,label2} |
please | decompose an array into a set of rows | select unnest(ARRAY[1,2,3]) | 1 2 3 |