PostgreSQL数组使用
原文:https://my.oschina.net/Kenyon/blog/133974
1.数组的定义
不一样的维度元素长度定义在数据库中的实际存储都是一样的,数组元素的长度和类型必须要保持一致,并且以中括号来表示。
合理的:
array[1,2] --一维数组
array[[1,2],[3,5]] --二维数组
'{99,889}'
不合理的:
array[[1,2],[3]] --元素长度不一致
array[[1,2],['Kenyon','good']] --类型不匹配
- [postgres@localhost ~]$ psql
- psql (9.2.4)
- Type "help" for help.
- postgres=# create table t_kenyon(id serial primary key,items int[]);
- NOTICE: CREATE TABLE will create implicit sequence "t_kenyon_id_seq" for serial column "t_kenyon.id"
- NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t_kenyon_pkey" for table "t_kenyon"
- CREATE TABLE
- postgres=# \d+ t_kenyon
- Table "public.t_kenyon"
- Column | Type | Modifiers | Storage | Stats target | Description
- --------+-----------+-------------------------------------------------------+----------+--------------+-------------
- id | integer | not null default nextval('t_kenyon_id_seq'::regclass) | plain | |
- items | integer[] | | extended | |
- Indexes:
- "t_kenyon_pkey" PRIMARY KEY, btree (id)
- Has OIDs: no
- postgres=# create table t_ken(id serial primary key,items int[]);
- NOTICE: CREATE TABLE will create implicit sequence "t_ken_id_seq" for serial column "t_ken.id"
- NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t_ken_pkey" for table "t_ken"
- CREATE TABLE
- postgres=# \d+ t_ken
- Table "public.t_ken"
- Column | Type | Modifiers | Storage | Stats target | Description
- --------+-----------+----------------------------------------------------+----------+--------------+-------------
- id | integer | not null default nextval('t_ken_id_seq'::regclass) | plain | |
- items | integer[] | | extended | |
- Indexes:
- "t_ken_pkey" PRIMARY KEY, btree (id)
- Has OIDs: no
- 数组的存储方式是extended的。
2.数组操作
a.数据插入(两种方式)
- postgres=# insert into t_kenyon(items) values('{1,2}');
- INSERT 0 1
- postgres=# insert into t_kenyon(items) values('{3,4,5}');
- INSERT 0 1
- postgres=# insert into t_kenyon(items) values(array[6,7,8,9]);
- INSERT 0 1
- postgres=# select * from t_kenyon;
- id | items
- ----+-----------
- 1 | {1,2}
- 2 | {3,4,5}
- 3 | {6,7,8,9}
- (3 rows)
b.数据删除
- postgres=# delete from t_kenyon where id = 3;
- DELETE 1
- postgres=# delete from t_kenyon where items[] = 4;
- DELETE 0
- postgres=# delete from t_kenyon where items[] = 3;
- DELETE 1
c.数据更新
- 往后追加
- postgres=# update t_kenyon set items = items||7;
- UPDATE 1
- postgres=# select * from t_kenyon;
- id | items
- ----+---------
- 1 | {1,2,7}
- (1 row)
- postgres=# update t_kenyon set items = items||'{99,66}';
- UPDATE 1
- postgres=# select * from t_kenyon;
- id | items
- ----+------------------
- 1 | {1,2,7,55,99,66}
- (1 row)
- 往前插
- postgres=# update t_kenyon set items = array_prepend(55,items) ;
- UPDATE 1
- postgres=# select * from t_kenyon;
- id | items
- ----+---------------------
- 1 | {55,1,2,7,55,99,66}
- (1 row)
d.数据查询
- postgres=# insert into t_kenyon(items) values('{3,4,5}');
- INSERT 0 1
- postgres=# select * from t_kenyon where id = 1;
- id | items
- ----+---------------------
- 1 | {55,1,2,7,55,99,66}
- (1 row)
- postgres=# select * from t_kenyon where items[] = 55;
- id | items
- ----+---------------------
- 1 | {55,1,2,7,55,99,66}
- (1 row)
- postgres=# select * from t_kenyon where items[] = 5;
- id | items
- ----+---------
- 4 | {3,4,5}
- (1 row)
- postgres=# select items[],items[],items[] from t_kenyon;
- items | items | items
- -------+-------+-------
- 55 | 2 | 7
- 3 | 5 |
- (2 rows)
- postgres=# select unnest(items) from t_kenyon where id = 4;
- unnest
- --------
- 3
- 4
- 5
- (3 rows)
e.数组比较
- postgres=# select ARRAY[1,2,3] <= ARRAY[1,2,3];
- ?column?
- ----------
- t
- (1 row)
f.数组字段类型转换
- postgres=# select array[['11','12'],['23','34']]::int[];
- array
- -------------------
- {{11,12},{23,34}}
- (1 row)
- postgres=# select array[[11,12],[23,34]]::text[];
- array
- -------------------
- {{11,12},{23,34}}
- (1 row)
3.数组索引
- postgres=# create table t_kenyon(id int,items int[]);
- CREATE TABLE
- postgres=# insert into t_kenyon values(1,'{1,2,3}');
- INSERT 0 1
- postgres=# insert into t_kenyon values(1,'{2,4}');
- INSERT 0 1
- postgres=# insert into t_kenyon values(1,'{34,7,8}');
- INSERT 0 1
- postgres=# insert into t_kenyon values(1,'{99,12}');
- INSERT 0 1
- postgres=# create index idx_t_kenyon on t_kenyon using gin(items);
- CREATE INDEX
- postgres=# set enable_seqscan = off;
- postgres=# explain select * from t_kenyon where items@>array[];
- QUERY PLAN
- ---------------------------------------------------------------------------
- Bitmap Heap Scan on t_kenyon (cost=8.00..12.01 rows=1 width=36)
- Recheck Cond: (items @> '{2}'::integer[])
- -> Bitmap Index Scan on idx_t_kenyon (cost=0.00..8.00 rows=1 width=0)
- Index Cond: (items @> '{2}'::integer[])
- (4 rows)
1.数组的定义
不一样的维度元素长度定义在数据库中的实际存储都是一样的,数组元素的长度和类型必须要保持一致,并且以中括号来表示。
合理的:
array[1,2] --一维数组
array[[1,2],[3,5]] --二维数组
'{99,889}'
不合理的:
array[[1,2],[3]] --元素长度不一致
array[[1,2],['Kenyon','good']] --类型不匹配
[postgres@localhost ~]$ psql
psql (9.2.4)
Type "help" for help.
postgres=# create table t_kenyon(id serial primary key,items int[]);
NOTICE: CREATE TABLE will create implicit sequence "t_kenyon_id_seq" for serial column "t_kenyon.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t_kenyon_pkey" for table "t_kenyon"
CREATE TABLE
postgres=# \d+ t_kenyon
Table "public.t_kenyon"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-----------+-------------------------------------------------------+----------+--------------+-------------
id | integer | not null default nextval('t_kenyon_id_seq'::regclass) | plain | |
items | integer[] | | extended | |
Indexes:
"t_kenyon_pkey" PRIMARY KEY, btree (id)
Has OIDs: no
postgres=# create table t_ken(id serial primary key,items int[4]);
NOTICE: CREATE TABLE will create implicit sequence "t_ken_id_seq" for serial column "t_ken.id"
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t_ken_pkey" for table "t_ken"
CREATE TABLE
postgres=# \d+ t_ken
Table "public.t_ken"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-----------+----------------------------------------------------+----------+--------------+-------------
id | integer | not null default nextval('t_ken_id_seq'::regclass) | plain | |
items | integer[] | | extended | |
Indexes:
"t_ken_pkey" PRIMARY KEY, btree (id)
Has OIDs: no
数组的存储方式是extended的。
PostgreSQL数组使用的更多相关文章
- PostgreSQL 数组类型
PostgreSQL 支持表的字段使用定长或可变长度的一维或多维数组,数组的类型可以是任何数据库内建的类型.用户自定义的类型.枚举类型, 以及组合类型.但目前还不支持 domain 类型. 数组类型的 ...
- PostgreSQL数组类型应用
在使用 awk 脚本:数组是一大利器:在很多场景是用数组能处理. 在 python 中,数据类型list:相当于array类型. 在 Oracle 中,对 array 不够友好,感觉像是鸡肋.但是在 ...
- postgresql —— 数组类型
创建数组 CREATE TABLE sal_emp ( name text, pay_by_quarter integer[] --还可以定义为integer[4]或integer ARRAY[4] ...
- PostgreSQL JSON 处理
1.JSON类型 PostgreSQL支持JSON和JSONB.这两种类型在使用上几乎完全一致,主要区别是: (1)JSON类型把输入的数据原封不动的存放到数据库中.JSONB类型在存放时把JS ...
- PostgreSQL中的The Oversized-Attribute Storage Technique(TOAST:超大属性存储技术)
PostgreSQL使用固定的页面大小(通常为8kB),并且不允许元组跨越多个页面.因此,不可能直接存储非常大的字段值.为了克服这种限制,将大字段值压缩和/或分解成多个物理行.这对用户来说是透明的,对 ...
- PostgreSQL 输出 JSON 结果
PostGreSQL 从 9.2 开始增加对 JSON 的支持.9.5 已经支持多个 JSON 函数,见 http://www.postgres.cn/docs/9.5/functions-json. ...
- postgresql----数组类型和函数
postgresql支持数组类型,可以是基本类型,也可以是用户自定义的类型.日常中使用数组类型的机会不多,但还是可以了解一下.不像C或JAVA高级语言的数组下标从0开始,postgresql数组下标从 ...
- Sequelize-nodejs-3-model definition
Model definition模型定义 To define mappings between a model and a table, use the define method.定义模型和表之间的 ...
- postgres —— 窗口函数入门
注:测试数据在 postgres —— 分组集与部分聚集 中 聚集将多行转变成较少.聚集的行.而窗口则不同,它把当前行与分组中的所有行对比,并且返回的行数没有变化. 组合当前行与 production ...
随机推荐
- Fiddler导出Jmeter脚本
版本:V4.4 用途:将fiddler抓取的请求,导出为jmx格式,方便jmeter直接调用 新增功能: 1.在测试计划下,新增[HTTP请求默认值],内容为空,后续需将站点的IP和端口填下在这个下面 ...
- 微信小程序 Video默认横屏
wxml文件 <video id='myvideo' src='你的视频文件路径'> </video> js文件 onLoad: function (options) { va ...
- ReactNative——页面跳转
效果图: 进入工作目录,运行 react-native init NavigatorProject 创建项目NavigatorProject import React, { Component } f ...
- python内置的魔术命令(builtin magic commands)
在ipython或者jupyter notebook中,会出现"%"开头并且一个很短的命令,例如交互式的matlablib绘图: %matplotlib inline 之前一直不知 ...
- Nancy 寄宿IIS
一:简介 Nancy是一个轻量级的独立的框架,下面是官网的一些介绍: Nancy 是一个轻量级用于构建基于 HTTP 的 Web 服务,基于 .NET 和 Mono 平台,框架的目标是保持尽可能多的方 ...
- 修改Tomcat默认连接数
<Connector port=" protocol="HTTP/1.1" connectionTimeout=" redirectPort=" ...
- 简单有效:解决 Excel 打开 UTF-8 编码 CSV 文件乱码的 BUG
https://blog.csdn.net/Sukie_csdn/article/details/78752969
- Top 查看某些或者某个进程(top -p pid)
https://blog.csdn.net/zhangfn2011/article/details/7488746?utm_source=blogxgwz5
- C#学习-方法
方法是由方法签名和一系列语句的代码块组成. 其中方法签名包括方法的访问级别(比如public或private).可修饰符(例如abstract关键字).方法名称和参数. C#也支持方法重载.方法重载指 ...
- ExceptionLess本地环境部署
1.先去看看github上面本地流程说明 https://github.com/exceptionless/Exceptionless/wiki/Self-Hosting 比较总要的环境有 NET 4 ...