开发的语言有数组的概念,对应于postgresql也有相关的数据字段类型,数组是英文array的翻译,可以定义一维,二维甚至更多维度,数学上跟矩阵很类似。在postgres里面可以直接存储使用,某些场景下使用很方便,也很强大。
环境:
OS:CentOS 6.2
DB: PostgreSQL 9.2.4
1.数组的定义
不一样的维度元素长度定义在数据库中的实际存储都是一样的,数组元素的长度和类型必须要保持一致,并且以中括号来表示。
合理的:
array[1,2] --一维数组
array[[1,2],[3,5]] --二维数组
'{99,889}'
不合理的:
array[[1,2],[3]] --元素长度不一致
array[[1,2],['Kenyon','good']] --类型不匹配
附数组操作符:
数组函数:
参考:http://www.postgresql.org/docs/9.2/static/functions-array.html
环境:
OS:CentOS 6.2
DB: PostgreSQL 9.2.4
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的。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[1] = 4; DELETE 0 postgres=# delete from t_kenyon where items[1] = 3; DELETE 1c.数据更新
往后追加 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[1] = 55; id | items ----+--------------------- 1 | {55,1,2,7,55,99,66} (1 row) postgres=# select * from t_kenyon where items[3] = 5; id | items ----+--------- 4 | {3,4,5} (1 row) postgres=# select items[1],items[3],items[4] 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[2]; 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)
附数组操作符:
= | equal | ARRAY[1.1,2.1,3.1]::int[] = ARRAY[1,2,3] | t |
<> | not equal | ARRAY[1,2,3] <> ARRAY[1,2,4] | t |
< | less than | ARRAY[1,2,3] < ARRAY[1,2,4] | t |
> | greater than | ARRAY[1,4,3] > ARRAY[1,2,4] | t |
<= | less than or equal | ARRAY[1,2,3] <= ARRAY[1,2,3] | t |
>= | greater than or equal | ARRAY[1,4,3] >= ARRAY[1,4,3] | t |
@> | contains | ARRAY[1,4,3] @> ARRAY[3,1] | t |
<@ | is contained by | ARRAY[2,7] <@ ARRAY[1,7,4,2,6] | t |
&& | overlap (have elements in common) | ARRAY[1,4,3] && ARRAY[2,1] | t |
|| | array-to-array concatenation | ARRAY[1,2,3] || ARRAY[4,5,6] | {1,2,3,4,5,6} |
|| | array-to-array concatenation | ARRAY[1,2,3] || ARRAY[[4,5,6],[7,8,9]] | {{1,2,3},{4,5,6},{7,8,9}} |
|| | element-to-array concatenation | 3 || ARRAY[4,5,6] | {3,4,5,6} |
|| | array-to-element concatenation | ARRAY[4,5,6] || 7 | {4,5,6,7} |
数组函数:
array_append(anyarray, anyelement) | anyarray | append an element to the end of an array | array_append(ARRAY[1,2], 3) | {1,2,3} |
array_cat(anyarray, anyarray) | anyarray | concatenate two arrays | array_cat(ARRAY[1,2,3], ARRAY[4,5]) | {1,2,3,4,5} |
array_ndims(anyarray) | int | returns the number of dimensions of the array | array_ndims(ARRAY[[1,2,3], [4,5,6]]) | 2 |
array_dims(anyarray) | text | returns a text representation of array's dimensions | array_dims(ARRAY[[1,2,3], [4,5,6]]) | [1:2][1:3] |
array_fill(anyelement, int[], [, int[]]) | anyarray | returns an array initialized with supplied value and dimensions, optionally with lower bounds other than 1 | array_fill(7, ARRAY[3], ARRAY[2]) | [2:4]={7,7,7} |
array_length(anyarray, int) | int | returns the length of the requested array dimension | array_length(array[1,2,3], 1) | 3 |
array_lower(anyarray, int) | int | returns lower bound of the requested array dimension | array_lower('[0:2]={1,2,3}'::int[], 1) | 0 |
array_prepend(anyelement, anyarray) | anyarray | append an element to the beginning of an array | array_prepend(1, ARRAY[2,3]) | {1,2,3} |
array_to_string(anyarray, text [, text]) | text | concatenates array elements using supplied delimiter and optional null string | array_to_string(ARRAY[1, 2, 3, NULL, 5], ',', '*') | 1,2,3,*,5 |
array_upper(anyarray, int) | int | returns upper bound of the requested array dimension | array_upper(ARRAY[1,8,3,7], 1) | 4 |
string_to_array(text, text [, text]) | text[] | splits string into array elements using supplied delimiter and optional null string | string_to_array('xx~^~yy~^~zz', '~^~', 'yy') | {xx,NULL,zz} |
unnest(anyarray) | setof anyelement | expand an array to a set of rows | unnest(ARRAY[1,2]) |
1 2(2 rows) |
参考:http://www.postgresql.org/docs/9.2/static/functions-array.html
原文地址:http://my.oschina.net/Kenyon/blog/133974
相关推荐
将数组和分隔字符串转换为 PostgreSQL 数组表示。 $ npm install pg-array 例子 从逗号分隔的字符串 var pgarray = require('pg-array'); pgarray('node, ruby, rust'); //=> '{node,ruby,rust}' 来自任意分隔的...
该插件使用PostgreSQL数组的功能实现了在Rails中进行has_and_belongs_to_many关联的替代方法。 在很多情况下,你只需要或的功能使用许多一对多与连接表是不必要的传统方法。 我们可以只存储id的整数数组。 它是如何...
PostgreSQL的数组
日常中使用数组类型的机会不多,但还是可以了解一下。不像C或JAVA高级语言的数组下标从0开始,postgresql数组下标从1开始,既可以指定长度,也可以不指定长度。且postgresql既支持一维数组,也支持多维数组,但是...
我非常高兴在宣布Rails 4.0 现在支持 PostgreSQL 数组类型. 你可以方便的在migration通过 :array => true里创建数组类型的字段. 创建数组类型的字段的时候还可以添加其它的选项(length,default,等等) create_...
当前仅支持PostgreSQL数组。 安装 将此行添加到您的应用程序的Gemfile中: gem "acts-as-taggable-array-on" 然后执行: bundle 设置 要使用它,您需要具有一个数组列以充当taggable- tags 。 class CreateUser...
主要介绍了mybatis调用postgresql自定义函数传递数组参数的解决方案,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
个人完整的源码安装步骤,希望对你有用。亲测可用。Centos6.2下,以postgresql_fdw驱动连接其他pg数据库,并批量导入表
主要介绍了在PostgreSQL中使用数组时值得注意的一些地方,包括如何提高输入性能,需要的朋友可以参考下
六、数组: 六、数组: .22 PostgreSQL PostgreSQL PostgreSQL学习手册 学习手册 学习手册 (函数和操作符 函数和操作符 函数和操作符 <一>) 25 一、逻辑操作符: 一、逻辑操作符: 一、逻辑操作符: 一、逻辑操作符...
PHP 读取Postgresql中的数组,需要的朋友可以参考一下
PostgreSQL 12.2安装与使用,非常适合初学PostgreSQL的朋友下载学习,内容非常详细的
使用Druid数据连接池连接PostgreSQL简单例子,搭建一个简单测试环境
PostgerSQL的jar包版本:postgresql-9.3-1102-jdbc4.jar mybatis-generator版本:mybatis-generator-core-1.3.7.jar 关于逆向工程自动生成dao层代码的功能,网上很多介绍的都是mysql版本的,而且介绍的都是在开发...
主要介绍了举例简单介绍PostgreSQL中的数组,PostgreSQL是一个高性能关系型数据库,学习PostgreSQL将成为趋势,需要的朋友可以参考下
全面且实践性强:本书从SQL基础、安装配置、数据类型、数据库的逻辑结构等基础知识一直讲到PostgreSQL的架构、技术内幕、特色功能、Standby、数据库优化以及一些配套的开源软件的使用,覆盖了PostgreSQL领域...
postgresql PGCA 课程PPT01_postgresql_PGCA_PGCP_PGCM_课程全免费 postgresql PGCA 课程PPT01_postgresql_PGCA_PGCP_PGCM_课程全免费 postgresql PGCA 课程PPT01_postgresql_PGCA_PGCP_PGCM_课程全免费 postgresql ...