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

opengauss学习的第12天

原创 hehe 2021-12-12
255

#opengauss的第12天

1.创建一个复合类型,重命名复合类型,为复合类型增加属性、删除属性
omm=# create type compfoo as(col1 int,colour char,status int);
CREATE TYPE

omm=# alter type compfoo rename to compfoo_rename;
ALTER TYPE

omm=# alter type compfoo_rename add ATTRIBUTE f1 text;
ALTER TYPE
omm=# \d compfoo_rename
Composite type “public.compfoo_rename”
Column | Type | Modifiers
--------±-------------±----------
col1 | integer |
colour | character(1) |
status | integer |
f1 | text |

omm=# alter type compfoo_rename drop ATTRIBUTE col1;
ALTER TYPE

omm=# \d compfoo_rename
Composite type “public.compfoo_rename”
Column | Type | Modifiers
--------±-------------±----------
colour | character(1) |
status | integer |
f1 | text |

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

omm=# create type testtype as enum(‘red’,‘blue’,‘yellow’);
CREATE TYPE
omm=# select * from pg_enum;
enumtypid | enumsortorder | enumlabel
-----------±--------------±----------
16423 | 1 | red
16423 | 2 | blue
16423 | 3 | yellow

omm=# alter type testtype add value ‘green’;
ALTER TYPE

omm=# select * from pg_enum;
enumtypid | enumsortorder | enumlabel
-----------±--------------±----------
16423 | 1 | red
16423 | 2 | blue
16423 | 3 | yellow
16423 | 4 | green

omm=# alter type testtype rename value ‘red’ to ‘black’;
ALTER TYPE

omm=# select * from pg_enum;
enumtypid | enumsortorder | enumlabel
-----------±--------------±----------
16423 | 2 | blue
16423 | 3 | yellow
16423 | 4 | green
16423 | 1 | black

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

omm=# create table t1(id bigint,describe_test testtype);
CREATE TABLE
omm=#
omm=# \d t1
Table “public.t1”
Column | Type | Modifiers
---------------±---------±----------
id | bigint |
describe_test | testtype |

4.删除类型

omm=# drop type testtype;
ERROR: cannot drop type testtype because other objects depend on it
DETAIL: table t1 column describe_test depends on type testtype
HINT: Use DROP … CASCADE to drop the dependent objects too.
omm=# drop type testtype cascade;
NOTICE: drop cascades to table t1 column describe_test
DROP TYPE
omm=#
omm=# drop type compfoo_rename ;
ERROR: cannot drop type compfoo_rename because other objects depend on it
DETAIL: table tab column describe depends on type compfoo_rename
HINT: Use DROP … CASCADE to drop the dependent objects too.
omm=# drop type compfoo_rename cascade;
NOTICE: drop cascades to table tab column describe
DROP TYPE

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

评论