学习内容
学习如何定义新的数据类型,包括普通的数据类型和枚举数据类型。
课程学习
1.创建类型
–创建一种复合类型
CREATE TYPE compfoo AS (f1 int, f2 text); CREATE TABLE t1_compfoo(a int, b compfoo); INSERT INTO t1_compfoo values(1,(1,'demo')); SELECT (b).f1 FROM t1_compfoo; \d compfoo
复制
–创建一个枚举类型
CREATE TYPE bugstatus AS ENUM ('create', 'modify', 'closed');
复制
–查看类型
select * from pg_enum;
复制
2.修改类型定义
–重命名数据类型
ALTER TYPE compfoo RENAME TO compfoo1;
复制
–增加一个新的属性
ALTER TYPE compfoo1 ADD ATTRIBUTE f3 int; \d compfoo1 select * from t1_compfoo;
复制
–删除一个属性
ALTER TYPE compfoo1 drop ATTRIBUTE f1; \d compfoo1 select * from t1_compfoo;
复制
–为枚举类型添加一个标签值
ALTER TYPE bugstatus ADD VALUE IF NOT EXISTS 'regress' BEFORE 'closed';
复制
–重命名一个标签值
ALTER TYPE bugstatus RENAME VALUE 'create' TO 'new'; select * from pg_enum;
复制
3.删除类型
DROP TYPE compfoo1; DROP TYPE compfoo1 cascade; drop type bugstatus;
复制
课程作业
1.创建一个复合类型,重命名复合类型,为复合类型增加属性、删除属性
.创建一个复合类型
create type test_type as (id int , name char(32));
复制
复制
create type test_type as (id int , name char(32));
复制
复制
重命名复合类型
alter type test_type rename to test_type_new;
复制
alter type test_type rename to test_type_new;
复制
增加属性
复制
复制
alter type test_type_new add attribute address varchar(64);复制
复制
复制
复制
删除属性
复制
alter type test_type_new drop attribute address;
复制
复制
alter type test_type_new drop attribute address;
复制
2.创建一个枚举类型,新增标签值,重命名标签值
create type test_enum as enum ('qian','shen');
复制
select * from pg_enum;
复制
alter type test_enum add value if not exists 'NB' after 'shen';
复制
alter type test_enum rename value 'NB' to 'nb';
复制
create type test_enum as enum ('qian','shen');
复制
select * from pg_enum;
复制
alter type test_enum add value if not exists 'NB' after 'shen';
复制
alter type test_enum rename value 'NB' to 'nb';
复制
3.使用新创建的类型创建表
create table test_table(address varchar(64),info test_type_new);
复制
insert into test_table(address,info) values('uestc',(111,'qian_shen'));
复制
select * from test_table;
复制
create table test_table(address varchar(64),info test_type_new);
复制
insert into test_table(address,info) values('uestc',(111,'qian_shen'));
复制
select * from test_table;
复制
4.删除类型
drop table test_type;
drop type test_enum cascade;
总结
今天学习的内容比较简单,是对数据类型的补充,是一个自己定义的数据结构。
最后修改时间:2021-12-13 17:29:22
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
评论
相关阅读
openGauss荣获中国软件行业协会多奖项,技术升级再创行业新高度
openGauss
512次阅读
2025-04-30 14:30:58
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
MogDB
305次阅读
2025-04-17 10:41:41
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
云和恩墨
201次阅读
2025-04-16 09:52:02
GitCode 成 openGauss 新归宿,国产开源数据库里程碑事件
严少安
164次阅读
2025-04-27 11:37:53
荣誉时刻!openGauss认证证书快递已发,快来看看谁榜上有名!
墨天轮小教习
156次阅读
2025-04-23 17:39:13
单个执行机并行执行MySQL到openGauss数据迁移子任务
Clipnosis
140次阅读
2025-04-30 16:39:58
openGauss6.0.0适配操作系统自带的软件,不依赖三方库
来杯拿铁
94次阅读
2025-04-18 10:49:53
Postgresql数据库单个Page最多存储多少行数据
maozicb
85次阅读
2025-04-23 16:02:19
openGauss新特性 | openGauss-DataVec向量数据库特性介绍
openGauss
60次阅读
2025-04-17 10:41:47
RISC-V 首迎 openGauss 7.0.0-RC1 全量版适配!数据库核心功能完整落地开源架构
openGauss
48次阅读
2025-04-16 10:33:59