openGauss每日一练之定义数据类型
openGauss 每日一练之定义数据类型
学习目标
学习 openGauss 定义数据类型
课后作业
1.创建一个复合类型,重命名复合类型,为复合类型增加属性、删除属性
--SQL文本
create type comtype as (id integer,name char(10));
alter type comtype rename to comtype_new;
alter type comtype_new add attribute age integer;
alter type comtype_new drop attribute name;
omm=# create type comtype as (id integer,name char(10));
CREATE TYPE
omm=# alter type comtype rename to comtype_new;
ALTER TYPE
omm=# alter type comtype_new add attribute age integer;
ALTER TYPE
omm=# alter type comtype_new drop attribute name;
ALTER TYPE
omm=#
2.创建一个枚举类型,新增标签值,重命名标签值
--SQL文本 create type enumtype as enum ('Monday','Tuesday','Friday'); alter type enumtype add value if not exists 'Wednesday' before 'Friday'; alter type enumtype rename value 'Friday' to 'Thursday'; select * from pg_enum;
omm=# create type comtype as (id integer,name char(10));
CREATE TYPE
omm=# alter type comtype rename to comtype_new;
ALTER TYPE
omm=# alter type comtype_new add attribute age integer;
ALTER TYPE
omm=# alter type comtype_new drop attribute name;
ALTER TYPE
omm=# create type enumtype as enum ('Monday','Tuesday','Friday');
CREATE TYPE
omm=# alter type enumtype add value if not exists 'Wednesday' before 'Friday';
ALTER TYPE
omm=# alter type enumtype rename value 'Friday' to 'Thursday';
ALTER TYPE
omm=# select * from pg_enum;
enumtypid | enumsortorder | enumlabel
-----------+---------------+-----------
33462 | 1 | Monday
33462 | 2 | Tuesday
33462 | 2.5 | Wednesday
33462 | 3 | Thursday
(4 rows)
omm=#
3.使用新创建的类型创建表
--SQL文本
create table t_enumtype (a integer,b comtype_new,c enumtype);
omm=# create table t_enumtype (a integer,b comtype_new,c enumtype); CREATE TABLE omm=#
4.删除类型
--SQL文本 drop table t_enumtype; drop type comtype_new; drop type enumtype;
omm=# drop table t_enumtype;
DROP TABLE
omm=# drop type comtype_new;
DROP TYPE
omm=# drop type enumtype;
DROP TYPE
omm=#
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 记一次.NET内存居高不下排查解决与启示
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了