今天学习openGuass的复合类型,有点跟oralce的嵌套表类型相似,不过语法上面相较于oracle,觉得openGauss更顺手些。
一些学习点总结:
- 查询复合类型值:select (表字段).自定义字段 from 表名;
- 自定义枚举类型: as enum 关键字,如表字段引用该类型,插入数据时,必须是该枚举定义的值;
- 枚举类型视图:pg_enum;
- 新增枚举值文中用的语法:
ALTER TYPE bugstatus ADD VALUE IF NOT EXISTS 'regress' BEFORE 'closed';
复制
如果确定没有该新增的枚举值也可以:
ALTER TYPE bugstatus ADD VALUE 'XXX';
复制
如果类型被引用需要删除时需加上cascade,会把相应引用该类型的表字段也删除掉;
对于枚举类型如何删除其中某个值,目前找不到相关语法;
扩展:https://opengauss.org/zh/docs/2.1.0/docs/Developerguide/CREATE-TYPE.html
有四种形式的CREATE TYPE,分别为:复合类型、基本类型、shell类型和枚举类型,具体说明可查阅官方说明。
语法格式:
CREATE TYPE name AS
( [ attribute_name data_type [ COLLATE collation ] [, ... ] ] )
CREATE TYPE name (
INPUT = input_function,
OUTPUT = output_function
[ , RECEIVE = receive_function ]
[ , SEND = send_function ]
[ , TYPMOD_IN =
type_modifier_input_function ]
[ , TYPMOD_OUT =
type_modifier_output_function ]
[ , ANALYZE = analyze_function ]
[ , INTERNALLENGTH = { internallength |
VARIABLE } ]
[ , PASSEDBYVALUE ]
[ , ALIGNMENT = alignment ]
[ , STORAGE = storage ]
[ , LIKE = like_type ]
[ , CATEGORY = category ]
[ , PREFERRED = preferred ]
[ , DEFAULT = default ]
[ , ELEMENT = element ]
[ , DELIMITER = delimiter ]
[ , COLLATABLE = collatable ]
)
CREATE TYPE name
CREATE TYPE name AS ENUM
( [ 'label' [, ... ] ] )
复制
课程练习:
1.创建一个复合类型,重命名复合类型,为复合类型增加属性、删除属性
openGauss=# create type t_comp as(a int,b char(30));
CREATE TYPE
openGauss=# alter type t_comp rename to t_comp1;
ALTER TYPE
openGauss=# alter type t_comp1 add attribute c int;
ALTER TYPE
openGauss=# alter type t_comp1 rename attribute a to aa;
ALTER TYPE
openGauss=# \d t_comp1;
Composite type "public.t_comp1"
Column | Type | Modifiers
--------+---------------+-----------
aa | integer |
b | character(30) |
c | integer |
openGauss=# alter type t_comp1 drop attribute b;
ALTER TYPE
openGauss=# \d t_comp1;
Composite type "public.t_comp1"
Column | Type | Modifiers
--------+---------+-----------
aa | integer |
c | integer |
openGauss=#
复制
2.创建一个枚举类型,新增标签值,重命名标签值
openGauss=# create type t_enum as enum('aa','bb','cc');
CREATE TYPE
openGauss=# alter type t_enum add value 'dd';
ALTER TYPE
openGauss=# select * from pg_enum;
enumtypid | enumsortorder | enumlabel
-----------+---------------+-----------
24731 | 1 | aa
24731 | 2 | bb
24731 | 3 | cc
24731 | 4 | dd
(4 rows)
openGauss=# alter type t_enum rename value 'aa' to 'abc';
ALTER TYPE
openGauss=# select * from pg_enum;
enumtypid | enumsortorder | enumlabel
-----------+---------------+-----------
24731 | 2 | bb
24731 | 3 | cc
24731 | 4 | dd
24731 | 1 | abc
(4 rows)
复制
3.使用新创建的类型创建表
openGauss=# create table tt (id int,aa t_comp1,bb t_enum);
CREATE TABLE
openGauss=# insert into tt values(1,(1,1),'abc'),(2,(2,2),'bb');
INSERT 0 2
openGauss=# select * from tt;
id | aa | bb
----+-------+-----
1 | (1,1) | abc
2 | (2,2) | bb
(2 rows)
复制
4.删除类型
openGauss=# drop type t_comp1;
ERROR: cannot drop type t_comp1 because other objects depend on it
DETAIL: table tt column aa depends on type t_comp1
HINT: Use DROP ... CASCADE to drop the dependent objects too.
openGauss=# drop type t_comp1 cascade;
NOTICE: drop cascades to table tt column aa
DROP TYPE
openGauss=# drop type t_enum cascade;
NOTICE: drop cascades to table tt column bb
DROP TYPE
openGauss=# \d tt;
Table "public.tt"
Column | Type | Modifiers
--------+---------+-----------
id | integer |
openGauss=# select * from tt;
id
----
1
2
(2 rows)
openGauss=# drop table tt;
DROP TABLE
复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
openGauss荣获中国软件行业协会多奖项,技术升级再创行业新高度
openGauss
559次阅读
2025-04-30 14:30:58
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
MogDB
308次阅读
2025-04-17 10:41:41
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
云和恩墨
201次阅读
2025-04-16 09:52:02
GitCode 成 openGauss 新归宿,国产开源数据库里程碑事件
严少安
175次阅读
2025-04-27 11:37:53
荣誉时刻!openGauss认证证书快递已发,快来看看谁榜上有名!
墨天轮小教习
162次阅读
2025-04-23 17:39:13
单个执行机并行执行MySQL到openGauss数据迁移子任务
Clipnosis
151次阅读
2025-04-30 16:39:58
Postgresql数据库单个Page最多存储多少行数据
maozicb
96次阅读
2025-04-23 16:02:19
openGauss6.0.0适配操作系统自带的软件,不依赖三方库
来杯拿铁
96次阅读
2025-04-18 10:49:53
openGauss新特性 | openGauss-DataVec向量数据库特性介绍
openGauss
67次阅读
2025-04-17 10:41:47
RISC-V 首迎 openGauss 7.0.0-RC1 全量版适配!数据库核心功能完整落地开源架构
openGauss
50次阅读
2025-04-16 10:33:59