暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

openGauss每日一练第12天

原创 华军 2021-12-20
196

今天学习openGauss定义数据类型
1.创建类型
–创建一种复合类型

omm=# CREATE TYPE compfoo AS (f1 int, f2 text);
CREATE TYPE
omm=# CREATE TABLE t1_compfoo(a int, b compfoo);
CREATE TABLE
omm=# INSERT INTO t1_compfoo values(1,(1,'demo'));
INSERT 0 1
omm=# 
omm=# SELECT (b).f1 FROM t1_compfoo;
 f1 
----
  1
(1 row)

omm=# select (b).f2 from t1_compfoo;
  f2  
------
 demo
(1 row)

omm=# select * from t1_compfoo;
 1 | (1,demo)
(1 row)

omm=#  a |    b     
---+----------



复制

–创建一个枚举类型

CREATE TYPE bugstatus AS ENUM (‘create’, ‘modify’, ‘closed’);


复制

–查看类型

omm=# select * from pg_enum;
 enumtypid | enumsortorder | enumlabel 
-----------+---------------+-----------
     16399 |             1 | create
     16399 |             2 | modify
     16399 |             3 | closed
(3 rows)

复制

2.修改类型定义
–重命名数据类型

omm=# ALTER TYPE compfoo RENAME TO compfoo1;
ALTER TYPE

复制

–增加一个新的属性

omm=# ALTER TYPE compfoo1 ADD ATTRIBUTE f3 int;
ALTER TYPE
omm=# \d compfoo1
Composite type "public.compfoo1"
 Column |  Type   | Modifiers 
--------+---------+-----------
 f1     | integer | 
 f2     | text    | 
 f3     | integer | 

omm=# select * from t1_compfoo;
 a |     b     
---+-----------
 1 | (1,demo,)
(1 row)

复制

–删除一个属性

omm=# ALTER TYPE compfoo1 drop ATTRIBUTE f1;
ALTER TYPE
omm=# \d compfoo1
Composite type "public.compfoo1"
 Column |  Type   | Modifiers 
--------+---------+-----------
 f2     | text    | 
 f3     | integer | 

omm=# select * from t1_compfoo;
 a |    b    
---+---------
 1 | (demo,)
(1 row)

复制

–为枚举类型添加一个标签值

omm=# ALTER TYPE bugstatus ADD VALUE IF NOT EXISTS 'regress' BEFORE 'closed';
ALTER TYPE
omm=# select * from pg_enum;
 enumtypid | enumsortorder | enumlabel 
-----------+---------------+-----------
     16399 |             1 | create
     16399 |             2 | modify
     16399 |             3 | closed
     16399 |           2.5 | regress
(4 rows)

复制

–重命名一个标签值

omm=# ALTER TYPE bugstatus RENAME VALUE 'create' TO 'new';
ALTER TYPE
omm=# select * from pg_enum;
 enumtypid | enumsortorder | enumlabel 
-----------+---------------+-----------
     16399 |             2 | modify
     16399 |             3 | closed
     16399 |           2.5 | regress
     16399 |             1 | new
(4 rows)


复制

3.删除类型

DROP TYPE compfoo1;
DROP TYPE compfoo1 cascade;
drop type bugstatus;

复制

课程作业
1.创建一个复合类型,重命名复合类型,为复合类型增加属性、删除属性

omm=# create type type1 as (id int,name char(10));
CREATE TYPE
omm=# create table table1(id int,name type1);
CREATE TABLE
omm=# insert into table1 values(1,(1,'aaa'));
INSERT 0 1
omm=# select * from table1;
 id |       name       
----+------------------
  1 | (1,"aaa       ")
(1 row)

复制

2.创建一个枚举类型,新增标签值,重命名标签值

omm=# create type type3 as enum('a','b','c');
CREATE TYPE
omm=# select * from pg_enum;
 enumtypid | enumsortorder | enumlabel 
-----------+---------------+-----------
     16416 |             1 | a
     16416 |             2 | b
     16416 |             3 | c
(3 rows)


omm=# alter type type3 add value 'd';
ALTER TYPE
omm=# select * from pg_enum;
 enumtypid | enumsortorder | enumlabel 
-----------+---------------+-----------
     16416 |             1 | a
     16416 |             2 | bomm=# 
     16416 |             3 | c
     16416 |             4 | d
(4 rows)


omm=# select * from pg_enum;
     16416 |             3 | c
     16416 |             4 | d
(4 rows)

omm=#  enumtypid | enumsortorder | enumlabel 
-----------+---------------+-----------
     16416 |             1 | a
     16416 |             2 | b

omm=# 
omm=# 
omm=# select * from pg_enum;
 enumtypid | enumsortorder | enumlabel 
-----------+---------------+-----------
     16416 |             1 | a
     16416 |             2 | b
     16416 |             3 | c
     16416 |             4 | d
(4 rows)

omm=# alter type type3 rename to type4;
ALTER TYPE
omm=# select * from pg_enum;

     16416 |             4 | d
(4 rows)

omm=#  enumtypid | enumsortorder | enumlabel 
-----------+---------------+-----------
     16416 |             1 | a
     16416 |             2 | b
     16416 |             3 | c

复制

3.使用新创建的类型创建表

omm=# create table table1(id int,name type1);
CREATE TABLE
omm=# insert into table1 values(1,(1,'aaa'));
INSERT 0 1
omm=# select * from table1;
 id |       name       
----+------------------
  1 | (1,"aaa       ")
(1 row)

复制

4.删除类型


omm=# drop table table1;
DROP TABLE
omm=# drop type type2;
DROP TYPE
omm=# drop type type4;
DROP TYPE

复制
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

文章被以下合辑收录

评论