数据库环境
openGauss:2.0.0 - 数据库实训平台
学习目标
学习openGauss定义数据类型
学习笔记
- 创建复合类型和枚举类型
omm=# CREATE TYPE compfoo AS (f1 int, f2 text);
CREATE TYPE
omm=# \d compfoo
Composite type "public.compfoo"
Column | Type | Modifiers
--------+---------+-----------
f1 | integer |
f2 | text |
omm=# CREATE TYPE bugstatus AS ENUM ('create', 'modify', 'closed');
CREATE TYPE
omm=# select * from pg_enum;
enumtypid | enumsortorder | enumlabel
-----------+---------------+-----------
16410 | 1 | create
16410 | 2 | modify
16410 | 3 | closed
(3 rows)
复制
课后作业
1.创建一个复合类型,重命名复合类型,为复合类型增加属性、删除属性
omm=# create type id_content as (id integer,content text);
CREATE TYPE
omm=# \d id_content;
Composite type "public.id_content"
Column | Type | Modifiers
---------+---------+-----------
id | integer |
content | text |
omm=# alter type id_content rename to id_content_new;
ALTER TYPE
omm=# \d id_content_new;
Composite type "public.id_content_new"
Column | Type | Modifiers
---------+---------+-----------
id | integer |
content | text |
omm=# alter type id_content_new add attribute empty_flag boolean;
ALTER TYPE
omm=# \d id_content_new;
Composite type "public.id_content_new"
Column | Type | Modifiers
------------+---------+-----------
id | integer |
content | text |
empty_flag | boolean |
omm=# alter type id_content_new drop attribute empty_flag;
ALTER TYPE
omm=# \d id_content_new;
omm=# Composite type "public.id_content_new"
Column | Type | Modifiers
---------+---------+-----------
id | integer |
content | text |
复制
2.创建一个枚举类型,新增标签值,重命名标签值
omm=# create type file_permission as enum('read','write');
CREATE TYPE
omm=# select * from pg_enum;
enumtypid | enumsortorder | enumlabel
-----------+---------------+-----------
16421 | 1 | read
16421 | 2 | write
(2 rows)
omm=# alter type file_permission add value if not exists 'eXecute' after 'write';
ALTER TYPE
omm=# select * from pg_enum;
enumtypid | enumsortorder | enumlabel
-----------+---------------+-----------
16421 | 1 | read
16421 | 2 | write
16421 | 3 | eXecute
(3 rows)
omm=# alter type file_permission rename value 'eXecute' to 'execute';
ALTER TYPE
omm=# select * from pg_enum;
enumtypid | enumsortorder | enumlabel
-----------+---------------+-----------
16421 | 1 | read
16421 | 2 | write
16421 | 3 | execute
(3 rows)
复制
3.使用新创建的类型创建表
omm=# create table t1 (a id_content_new,b file_permission);
CREATE TABLE
omm=# \d t1
Table "public.t1"
Column | Type | Modifiers
--------+-----------------+-----------
a | id_content_new |
b | file_permission |
omm=# insert into t1 values((1,'hello',false),'read');
INSERT 0 1
omm=# select * from t1;
a | b
-------------+------
(1,hello,f) | read
(1 row)
omm=# select (a).content from t1;
content
---------
hello
(1 row)
复制
4.删除类型
omm=# drop table t1;
DROP TABLE
omm=# drop type id_content_new;
DROP TYPE
omm=# drop type file_permission;
DROP TYPE
omm=#
复制
学习资源
- openGauss SQL学习参考资料
- 每日一练:openGauss数据库在线实训课程
- openGauss每日一练 | 21期养成好习惯,提升技术能力!
- 墨天轮Markdown编辑器使用介绍
- 墨天轮数据库在线实训平台V1.0操作手册
- 墨天轮数据社区
欢迎各位同学一起来交流学习心得!
最后修改时间:2021-12-16 18:37:54
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
2025年3月国产数据库大事记
墨天轮编辑部
870次阅读
2025-04-03 15:21:16
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
MogDB
286次阅读
2025-04-17 10:41:41
openGauss 7.0.0-RC1 版本正式发布!
Gauss松鼠会
199次阅读
2025-04-01 12:27:03
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
云和恩墨
183次阅读
2025-04-16 09:52:02
openGauss 7.0.0-RC1 版本体验:一主一备快速安装指南
孙莹
175次阅读
2025-04-01 10:30:07
鲲鹏RAG一体机解决方案正式发布 openGauss DataVec向量数据库助力DeepSeek行业应用
Gauss松鼠会
122次阅读
2025-03-31 10:00:29
荣誉时刻!openGauss认证证书快递已发,快来看看谁榜上有名!
墨天轮小教习
105次阅读
2025-04-23 17:39:13
openGauss6.0.0适配操作系统自带的软件,不依赖三方库
来杯拿铁
75次阅读
2025-04-18 10:49:53
opengauss使用gs_probackup进行增量备份恢复
进击的CJR
70次阅读
2025-04-09 16:11:58
Postgresql数据库单个Page最多存储多少行数据
maozicb
54次阅读
2025-04-23 16:02:19