原文: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']]  --类型不匹配

  1. [postgres@localhost ~]$ psql
  2. psql (9.2.4)
  3. Type "help" for help.
  4. postgres=# create table t_kenyon(id serial primary key,items int[]);
  5. NOTICE: CREATE TABLE will create implicit sequence "t_kenyon_id_seq" for serial column "t_kenyon.id"
  6. NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t_kenyon_pkey" for table "t_kenyon"
  7. CREATE TABLE
  8. postgres=# \d+ t_kenyon
  9. Table "public.t_kenyon"
  10. Column | Type | Modifiers | Storage | Stats target | Description
  11. --------+-----------+-------------------------------------------------------+----------+--------------+-------------
  12. id | integer | not null default nextval('t_kenyon_id_seq'::regclass) | plain | |
  13. items | integer[] | | extended | |
  14. Indexes:
  15. "t_kenyon_pkey" PRIMARY KEY, btree (id)
  16. Has OIDs: no
  17.  
  18. postgres=# create table t_ken(id serial primary key,items int[]);
  19. NOTICE: CREATE TABLE will create implicit sequence "t_ken_id_seq" for serial column "t_ken.id"
  20. NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t_ken_pkey" for table "t_ken"
  21. CREATE TABLE
  22.  
  23. postgres=# \d+ t_ken
  24. Table "public.t_ken"
  25. Column | Type | Modifiers | Storage | Stats target | Description
  26. --------+-----------+----------------------------------------------------+----------+--------------+-------------
  27. id | integer | not null default nextval('t_ken_id_seq'::regclass) | plain | |
  28. items | integer[] | | extended | |
  29. Indexes:
  30. "t_ken_pkey" PRIMARY KEY, btree (id)
  31. Has OIDs: no
  32.  
  33. 数组的存储方式是extended的。

2.数组操作

a.数据插入(两种方式)

  1. postgres=# insert into t_kenyon(items) values('{1,2}');
  2. INSERT 0 1
  3. postgres=# insert into t_kenyon(items) values('{3,4,5}');
  4. INSERT 0 1
  5. postgres=# insert into t_kenyon(items) values(array[6,7,8,9]);
  6. INSERT 0 1
  7. postgres=# select * from t_kenyon;
  8. id | items
  9. ----+-----------
  10. 1 | {1,2}
  11. 2 | {3,4,5}
  12. 3 | {6,7,8,9}
  13. (3 rows)

b.数据删除

  1. postgres=# delete from t_kenyon where id = 3;
  2. DELETE 1
  3. postgres=# delete from t_kenyon where items[] = 4;
  4. DELETE 0
  5. postgres=# delete from t_kenyon where items[] = 3;
  6. DELETE 1

c.数据更新

  1. 往后追加
  2. postgres=# update t_kenyon set items = items||7;
  3. UPDATE 1
  4. postgres=# select * from t_kenyon;
  5. id | items
  6. ----+---------
  7. 1 | {1,2,7}
  8. (1 row)
  9.  
  10. postgres=# update t_kenyon set items = items||'{99,66}';
  11. UPDATE 1
  12. postgres=# select * from t_kenyon;
  13. id | items
  14. ----+------------------
  15. 1 | {1,2,7,55,99,66}
  16. (1 row)
  17.  
  18. 往前插
  19. postgres=# update t_kenyon set items = array_prepend(55,items) ;
  20. UPDATE 1
  21. postgres=# select * from t_kenyon;
  22. id | items
  23. ----+---------------------
  24. 1 | {55,1,2,7,55,99,66}
  25. (1 row)

d.数据查询

  1. postgres=# insert into t_kenyon(items) values('{3,4,5}');
  2. INSERT 0 1
  3.  
  4. postgres=# select * from t_kenyon where id = 1;
  5. id | items
  6. ----+---------------------
  7. 1 | {55,1,2,7,55,99,66}
  8. (1 row)
  9.  
  10. postgres=# select * from t_kenyon where items[] = 55;
  11. id | items
  12. ----+---------------------
  13. 1 | {55,1,2,7,55,99,66}
  14. (1 row)
  15.  
  16. postgres=# select * from t_kenyon where items[] = 5;
  17. id | items
  18. ----+---------
  19. 4 | {3,4,5}
  20. (1 row)
  21.  
  22. postgres=# select items[],items[],items[] from t_kenyon;
  23. items | items | items
  24. -------+-------+-------
  25. 55 | 2 | 7
  26. 3 | 5 |
  27. (2 rows)
  28.  
  29. postgres=# select unnest(items) from t_kenyon where id = 4;
  30. unnest
  31. --------
  32. 3
  33. 4
  34. 5
  35. (3 rows)

e.数组比较

  1. postgres=# select ARRAY[1,2,3] <= ARRAY[1,2,3];
  2. ?column?
  3. ----------
  4. t
  5. (1 row)

f.数组字段类型转换

  1. postgres=# select array[['11','12'],['23','34']]::int[];
  2. array
  3. -------------------
  4. {{11,12},{23,34}}
  5. (1 row)
  6.  
  7. postgres=# select array[[11,12],[23,34]]::text[];
  8. array
  9. -------------------
  10. {{11,12},{23,34}}
  11. (1 row)

3.数组索引

  1. postgres=# create table t_kenyon(id int,items int[]);
  2. CREATE TABLE
  3. postgres=# insert into t_kenyon values(1,'{1,2,3}');
  4. INSERT 0 1
  5. postgres=# insert into t_kenyon values(1,'{2,4}');
  6. INSERT 0 1
  7. postgres=# insert into t_kenyon values(1,'{34,7,8}');
  8. INSERT 0 1
  9. postgres=# insert into t_kenyon values(1,'{99,12}');
  10. INSERT 0 1
  11. postgres=# create index idx_t_kenyon on t_kenyon using gin(items);
  12. CREATE INDEX
  13. postgres=# set enable_seqscan = off;
  14. postgres=# explain select * from t_kenyon where items@>array[];
  15. QUERY PLAN
  16. ---------------------------------------------------------------------------
  17. Bitmap Heap Scan on t_kenyon (cost=8.00..12.01 rows=1 width=36)
  18. Recheck Cond: (items @> '{2}'::integer[])
  19. -> Bitmap Index Scan on idx_t_kenyon (cost=0.00..8.00 rows=1 width=0)
  20. Index Cond: (items @> '{2}'::integer[])
  21. (4 rows)

1.数组的定义 
 不一样的维度元素长度定义在数据库中的实际存储都是一样的,数组元素的长度和类型必须要保持一致,并且以中括号来表示。 
合理的: 
array[1,2]            --一维数组 
array[[1,2],[3,5]]  --二维数组 
'{99,889}'

不合理的: 
array[[1,2],[3]]                     --元素长度不一致 
 array[[1,2],['Kenyon','good']]  --类型不匹配

  1. [postgres@localhost ~]$ psql
  2. psql (9.2.4)
  3. Type "help" for help.
  4. postgres=# create table t_kenyon(id serial primary key,items int[]);
  5. NOTICE: CREATE TABLE will create implicit sequence "t_kenyon_id_seq" for serial column "t_kenyon.id"
  6. NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t_kenyon_pkey" for table "t_kenyon"
  7. CREATE TABLE
  8. postgres=# \d+ t_kenyon
  9. Table "public.t_kenyon"
  10. Column | Type | Modifiers | Storage | Stats target | Description
  11. --------+-----------+-------------------------------------------------------+----------+--------------+-------------
  12. id | integer | not null default nextval('t_kenyon_id_seq'::regclass) | plain | |
  13. items | integer[] | | extended | |
  14. Indexes:
  15. "t_kenyon_pkey" PRIMARY KEY, btree (id)
  16. Has OIDs: no
  17. postgres=# create table t_ken(id serial primary key,items int[4]);
  18. NOTICE: CREATE TABLE will create implicit sequence "t_ken_id_seq" for serial column "t_ken.id"
  19. NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index "t_ken_pkey" for table "t_ken"
  20. CREATE TABLE
  21. postgres=# \d+ t_ken
  22.                                               Table "public.t_ken"
  23.  Column |   Type    |                     Modifiers                      | Storage  | Stats target | Description 
  24. --------+-----------+----------------------------------------------------+----------+--------------+-------------
  25.  id     | integer   | not null default nextval('t_ken_id_seq'::regclass) | plain    |              | 
  26.  items  | integer[] |                                                    | extended |              | 
  27. Indexes:
  28.     "t_ken_pkey" PRIMARY KEY, btree (id)
  29. Has OIDs: no
  30. 数组的存储方式是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. Fiddler导出Jmeter脚本

    版本:V4.4 用途:将fiddler抓取的请求,导出为jmx格式,方便jmeter直接调用 新增功能: 1.在测试计划下,新增[HTTP请求默认值],内容为空,后续需将站点的IP和端口填下在这个下面 ...

  2. 微信小程序 Video默认横屏

    wxml文件 <video id='myvideo' src='你的视频文件路径'> </video> js文件 onLoad: function (options) { va ...

  3. ReactNative——页面跳转

    效果图: 进入工作目录,运行 react-native init NavigatorProject 创建项目NavigatorProject import React, { Component } f ...

  4. python内置的魔术命令(builtin magic commands)

    在ipython或者jupyter notebook中,会出现"%"开头并且一个很短的命令,例如交互式的matlablib绘图: %matplotlib inline 之前一直不知 ...

  5. Nancy 寄宿IIS

    一:简介 Nancy是一个轻量级的独立的框架,下面是官网的一些介绍: Nancy 是一个轻量级用于构建基于 HTTP 的 Web 服务,基于 .NET 和 Mono 平台,框架的目标是保持尽可能多的方 ...

  6. 修改Tomcat默认连接数

    <Connector port=" protocol="HTTP/1.1" connectionTimeout=" redirectPort=" ...

  7. 简单有效:解决 Excel 打开 UTF-8 编码 CSV 文件乱码的 BUG

    https://blog.csdn.net/Sukie_csdn/article/details/78752969

  8. Top 查看某些或者某个进程(top -p pid)

    https://blog.csdn.net/zhangfn2011/article/details/7488746?utm_source=blogxgwz5

  9. C#学习-方法

    方法是由方法签名和一系列语句的代码块组成. 其中方法签名包括方法的访问级别(比如public或private).可修饰符(例如abstract关键字).方法名称和参数. C#也支持方法重载.方法重载指 ...

  10. ExceptionLess本地环境部署

    1.先去看看github上面本地流程说明 https://github.com/exceptionless/Exceptionless/wiki/Self-Hosting 比较总要的环境有 NET 4 ...