Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

PostgreSQL-Array type operation

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

I. create tables for array types

Array type, which requires that the elements in the array belong to the same type, when No function matches the given name and argument types. You might need to add explicit type casts. When reporting an error, the type is very important because the format of list is different from that of inserting or modifying data. You need to ensure that the type is the same before you can operate.

1.1. Create a table to specify the array type

You just need to add'[] 'after the table field type

Postgres=# create table test1 (id serial, arr int [], name varchar (10) [], age char (10) [], score float []); postgres=#\ d + test1 Table "public.test1" Column | Type | Modifiers | Storage | Stats target | Description-+-- -- +-id | integer | not null default nextval ('test1_id_seq'::regclass) | plain | | | arr | integer [] | | extended | | name | character varying (10) [] | | extended | | age | character (10) [] | | | extended | | score | double precision [] | | extended | | 1.2, Data insertion into postgres=# insert into test (id Uid) values (3,'{1,2,3}') Insert array method 1postgres=# insert into test (id, uid) values (3, array [20,30]:: int8 []); insert array method 21.3; modify array: postgres=# update test set uid= uid | |'{0}'; append an array postgres=# update test set uid=' {0Pert 0}'| | uid; inserts an array postgres=# update arr_test set uid=array_append (uid, '1'::int) in front of it Append a number postgres=# update arr_test set uid=array_append (uid, 1) to the specified type, append a number postgres=# update arr_test set uid=array_prepend ('1'::int, uid) to the default int type, and insert a number 1.4to delete the data in the array postgres=# update arr_test set uid=array_remove (uid,' 1'::int). Indicates that the type removes the specified number 1.5.Lookup the data in the array postgres=# select * from test where 20=any (uid); rowpostgres=# select * from test where uid & & array [20,1]:: int8 [] exists in the # uid array; postgres=# select * from arr_test where uid@ >'{1,2} 'where there is an intersection between the uid array and the array [20,1]; the uid array also contains the postgres=# select * from arr_test where uid of [1,2]

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report