设为首页 加入收藏

TOP

Postgres数组使用(一)
2014-11-24 07:55:46 来源: 作者: 【 】 浏览:1
Tags:Postgres 使用
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']] --类型不匹配
[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 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=
首页 上一页 1 2 3 下一页 尾页 1/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇undo损坏案列 下一篇PostgreSQL查询表中是否存在值的..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·C++模板 (template) (2025-12-26 15:49:49)
·C 语言中模板的几种 (2025-12-26 15:49:47)
·模板(泛型) - C语 (2025-12-26 15:49:44)
·C语言中,“指针”用 (2025-12-26 15:20:18)
·在c语言的指针运算中 (2025-12-26 15:20:15)