PostgreSQL -- 数组类型操作
发表于:2025-02-03 作者:千家信息网编辑
千家信息网最后更新 2025年02月03日,一、数组类型创建表数组类型,要求数组内的元素属于同一种类型,当出现No function matches the given name and argument types. You might ne
千家信息网最后更新 2025年02月03日PostgreSQL -- 数组类型操作
一、数组类型创建表
数组类型,要求数组内的元素属于同一种类型,当出现No function matches the given name and argument types. You might need to add explicit type casts.报错的时候,说明 list 的格式和插入数据或者修改数据的格式不同导致的, 类型很重要,需要保证类型相同才可以操作
1.1、建表指定数组类型
只需要在表字段类型后面加'[]'
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、数据插入
postgres=# insert into test(id, uid) values(3, '{1, 2, 3}'); 插入数组方式1postgres=# insert into test(id, uid) values(3, array[20, 30]::int8[]); 插入数组方式二
1.3、修改数组:
postgres=# update test set uid = uid || '{0}'; 后面追加一个数组postgres=# update test set uid='{0,0}' || uid; 在前面插入一个数组postgres=# update arr_test set uid=array_append(uid, '1'::int); 指明类型追加一个数postgres=# update arr_test set uid=array_append(uid, 1); 按默认int类型追加一个数postgres=# update arr_test set uid=array_prepend('1'::int, uid); 在前面插入一个数
1.4、删除数组中的数据
postgres=# update arr_test set uid=array_remove(uid, '1'::int); 指明类型移除指定的数
1.5、查找数组中的数据
postgres=# select * from test where 20=any(uid); #uid数组中存在20的rowpostgres=# select * from test where uid && array[20, 1]::int8[]; uid数组中和array[20, 1]存在交集的postgres=# select * from arr_test where uid@>'{1, 2}'; uid 数组中同时包含[1, 2]的postgres=# select * from arr_test where uid<@'{1, 2}'; uid 数组被[1, 2]包含的
postgres=# select * from arr_test where 2=uid[1]; 使用uid 数组下标查询,下标是从1开始的
postgres=# select id, uid[2] from arr_test; 使用下标显示
数组
类型
数据
下标
个数
方式
格式
加一
不同
相同
重要
交集
元素
同时
字段
时候
保证
查询
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
文件锁数据库
数据库设计表工具
江苏工程网络技术代理品牌
魅族怎么回复数据库
360网络安全与国家
关于网络安全方面的调查问卷
外卖网络安全问题的措施
杨紫为网络安全唱的主题曲
初中生网络安全画画图片大全
数据库查询效率最低的
将英文网站上传到服务器
思科服务器安全
天津文件服务器安全狗
数据库中捕获异常的程序
1256号数据库应用技术计算机
xfs 数据库
软件开发软件测试软件安全
用命令创建数据库
学校服务器如何查看学生上网
北京态势推演系统软件开发
网络安全影响因素与防范对策
惠普服务器电源1200瓦
《网络安全法》法律适用情况
上海专业软件开发管理
为什么网络安全法施行不打击骚扰
数据库每天备份到其他电脑
数据库更换列位置
软件开发学数学吗
周村crm软件开发咨询
服务器虚拟盘