原文: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数组使用的更多相关文章

  1. PostgreSQL 数组类型

    PostgreSQL 支持表的字段使用定长或可变长度的一维或多维数组,数组的类型可以是任何数据库内建的类型.用户自定义的类型.枚举类型, 以及组合类型.但目前还不支持 domain 类型. 数组类型的 ...

  2. PostgreSQL数组类型应用

    在使用 awk 脚本:数组是一大利器:在很多场景是用数组能处理. 在 python 中,数据类型list:相当于array类型. 在 Oracle 中,对 array 不够友好,感觉像是鸡肋.但是在 ...

  3. postgresql —— 数组类型

    创建数组 CREATE TABLE sal_emp ( name text, pay_by_quarter integer[] --还可以定义为integer[4]或integer ARRAY[4] ...

  4. PostgreSQL JSON 处理

    1.JSON类型    PostgreSQL支持JSON和JSONB.这两种类型在使用上几乎完全一致,主要区别是: (1)JSON类型把输入的数据原封不动的存放到数据库中.JSONB类型在存放时把JS ...

  5. PostgreSQL中的The Oversized-Attribute Storage Technique(TOAST:超大属性存储技术)

    PostgreSQL使用固定的页面大小(通常为8kB),并且不允许元组跨越多个页面.因此,不可能直接存储非常大的字段值.为了克服这种限制,将大字段值压缩和/或分解成多个物理行.这对用户来说是透明的,对 ...

  6. PostgreSQL 输出 JSON 结果

    PostGreSQL 从 9.2 开始增加对 JSON 的支持.9.5 已经支持多个 JSON 函数,见 http://www.postgres.cn/docs/9.5/functions-json. ...

  7. postgresql----数组类型和函数

    postgresql支持数组类型,可以是基本类型,也可以是用户自定义的类型.日常中使用数组类型的机会不多,但还是可以了解一下.不像C或JAVA高级语言的数组下标从0开始,postgresql数组下标从 ...

  8. Sequelize-nodejs-3-model definition

    Model definition模型定义 To define mappings between a model and a table, use the define method.定义模型和表之间的 ...

  9. postgres —— 窗口函数入门

    注:测试数据在 postgres —— 分组集与部分聚集 中 聚集将多行转变成较少.聚集的行.而窗口则不同,它把当前行与分组中的所有行对比,并且返回的行数没有变化. 组合当前行与 production ...

随机推荐

  1. jQuery 常用的方法

    <!DOCTYPE html><html lang="en"><head> <meta charset="utf-8" ...

  2. MySQL修改数据表存储引擎的3种方法介绍

    这篇文章主要介绍了MySQL修改数据表存储引擎的3种方法介绍,分别是直接修改.导出导入.创建插入3种方法, 可以参考下   MySQL作为最常用的数据库,经常遇到各种各样的问题.今天要说的就是表存储引 ...

  3. Python(字符串操作实例1)一个字符串用空格隔开

    # 将字符中单词用空格隔开# 已知传入的字符串中只有字母,每个单词的首字母大写,# 请将每个单词用空格隔开,只保留第一个单词的首字母大写传入:“HelloMyWorld”# 返回“Hello my w ...

  4. Pymysql-总结

    背景:工作需要大量链接数据库进行一些操作查询,但是也会有出现异常情况 1.添加字段 1 ALTER TABLE app01_student ADD COLUMN Relation VARCHAR(25 ...

  5. WCF 非http寄宿IIS

    摘要 从IIS 7 开始, IIS增加了对非HTTP协议的支持. 因此, 自IIS 7之后, 可以将NetTcpBinding等非HTTP协议的Bindings直接寄宿在IIS上面. 本文将介绍如何在 ...

  6. SVG 图像入门教程

    http://www.ruanyifeng.com/blog/2018/08/svg.html 一.概述 SVG 是一种基于 XML 语法的图像格式,全称是可缩放矢量图(Scalable Vector ...

  7. jQuery数字滚动(模拟网站人气、访问量递增)原创

    插件描述:实现数字上下滚动,模拟网站人气.访问量递增的动画效果,兼容性如下: 使用方法 $(el).runNum(val,params);   参数详解 val:数值型(默认70225800): pa ...

  8. tornado中form表单验证详解

    #!/usr/bin/env python# _*_ coding:utf-8 _*_import tornado.webimport tornado.ioloopimport re class Ba ...

  9. pandas处理finance.yahoo股票数据 WTI CL USO OIL

    1.参考 用Python做科学计算-基础篇 »matplotlib-绘制精美的图表 »快速绘图 使用pyplot模块绘图 2.数据来源 CL USO OIL 3.代码 #encoding='utf-8 ...

  10. jQuery选择器总结 jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法

    新年第一编文章 jQuery 的选择器可谓之强大无比,这里简单地总结一下常用的元素查找方法   $("#myELement")    选择id值等于myElement的元素,id值 ...