openGauss每日一练第12天 | 数据类型 from seven
学习目标
学习openGauss定义数据类型
课程学习
连接数据库
#第一次进入等待15秒#数据库启动中...
su - omm
gsql -r
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. 创建一个复合类型,重命名复合类型,为复合类型增加属性、删除属性
omm=# create type comtype1 as (fid int,fname char(20));
CREATE TYPE
omm=# alter type comtype1 rename to comtype11;
ALTER TYPE
omm=# alter type comtype11 add attribute f3 int;
ALTER TYPE
omm=# alter type comtype11 drop attribute f3;
ALTER TYPE
omm=#
2. 创建一个枚举类型,新增标签值,重命名标签值
omm=# create type dbstatus as enum('open','shutdown','started');
CREATE TYPE
omm=# alter type dbstatus add value 'mount';
ALTER TYPE
omm=# alter type dbstatus rename value 'started' to 'nomount'
omm-# ;
ALTER TYPE
omm=# select * from pg_enum;
enumtypid | enumsortorder | enumlabel
-----------+---------------+-----------
16410 | 1 | open
16410 | 2 | shutdown
16410 | 4 | mount
16410 | 3 | nomount
(4 rows)
3. 使用新创建的类型创建表
omm=# create table t1_comtype11 (id int, comp1 comtype11);
CREATE TABLE
4. 删除类型
omm=# drop type dbstatus;
DROP TYPE
omm=# drop type comtype11 cascade;
NOTICE: drop cascades to table t1_comtype11 column comp1
DROP TYPE
omm=# \d comtype11
Did not find any relation named "comtype11".
omm=# select * from pg_enum;
enumtypid | enumsortorder | enumlabel
-----------+---------------+-----------
(0 rows)




