前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >Postgresql数组、复合数组简单实例

Postgresql数组、复合数组简单实例

作者头像
mingjie
发布2022-11-06 09:29:35
发布2022-11-06 09:29:35
49300
代码可运行
举报
运行总次数:0
代码可运行

数组、复合数组使用的简单实例。

数组类型构造方法

代码语言:javascript
代码运行次数:0
运行
复制
DROP TABLE sal_emp;
CREATE TABLE sal_emp (
    name            text,
    pay_by_quarter  integer[],
    schedule        text[][]
);

INSERT INTO sal_emp
    VALUES ('Bill',
    ARRAY[10000, 10000, 10000, 10000],
    ARRAY[['meeting', 'lunch'], ['training', 'presentation']]);

INSERT INTO sal_emp
    VALUES ('Carol',
    ARRAY[20000, 25000, 25000, 25000],
    ARRAY[ARRAY['breakfast', 'consulting'], ARRAY['meeting', 'lunch']]);

复合数组类型构造方法

数组字段只能接受ARRAY类型,而数组元素为复合类型,必须由row()函数将数据拼接为record行类型,然后强制转换为数组的复合类型才能插入。

代码语言:javascript
代码运行次数:0
运行
复制
DROP TABLE sal_emps;
CREATE TABLE sal_emps (emps sal_emp[]);
INSERT INTO sal_emps
    VALUES (
        ARRAY[
        row('Bill',ARRAY[1,2,3,4],ARRAY[['a', 'b'], ['c', 'd']])::sal_emp,
        row('Caro',ARRAY[3,4,5,6],ARRAY[['e', 'g'], ['f', 'h']])::sal_emp
        ]
    );

数组和复合数组类型

无论建表还是创建类型,都会创建两个对应的数据类型,例如

代码语言:javascript
代码运行次数:0
运行
复制
create table ty123(a int, b int);
create table ty123123(c ty123, d int);

会在pg_type中增加四条元组:

pg_type

ty123建表

ty123建表

ty123123建表

ty123123建表

oid

16722

16721

16725

16724

typname

ty123

_ty123

ty123123

_ty123123

typnamespace

2200

2200

2200

2200

typowner

10

10

10

10

typlen

-1

-1

-1

-1

-1表示变长类型

typbyval

f

f

f

f

传引用

typtype

c

b

c

b

c复合类型(表);b基本类型

typcategory

C

A

C

A

优化器使用:如何隐式转换

typispreferred

f

f

f

f

typisdefined

t

t

t

t

类型已经定义了

typdelim

,

,

,

,

输入时的数据分隔符

typrelid

16720

0

16723

0

复合类型都会关联一个pg_class的元组

typsubscript

-

array_subscript_handler

-

array_subscript_handler

下标处理函数

typelem

0

16722

0

16725

typarray

16721

0

16724

0

typinput

record_in

array_in

record_in

array_in

typoutput

record_out

array_out

record_out

array_out

typreceive

record_recv

array_recv

record_recv

array_recv

typsend

record_send

array_send

record_send

array_send

typmodin

-

-

-

-

typmodout

-

-

-

-

typanalyze

-

array_typanalyze

-

array_typanalyze

typalign

d

d

d

d

typstorage

x

x

x

x

typnotnull

f

f

f

f

typbasetype

0

0

0

0

typtypmod

-1

-1

-1

-1

typndims

0

0

0

0

typcollation

0

0

0

0

typdefaultbin

typdefault

typacl

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022-11-01,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 数组类型构造方法
  • 复合数组类型构造方法
  • 数组和复合数组类型
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档