本次课学习的内容是openGauss定义数据类型。
视图与基本表不同,是一个虚拟的表。数据库中仅存放视图的定义,而不存放视图对应的数据,这些数据仍存放在原来的基本表中。
先进入实训环境,输入su - omm命令和密码连接openGauss。
root@modb:~#
root@modb:~# su - omm
omm@modb:~$ gsql -r
gsql ((openGauss 2.0.0 build 78689da9) compiled at 2021-03-31 21:03:52 commit 0 last mr )
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.
作业打卡
1、创建一个复合类型,重命名复合类型,为复合类型增加属性、删除属性
omm=# create type stuff as (st1 int,st2 varchar(20));
CREATE TYPE
omm=# alter type stuff rename to test1;
ALTER TYPE
omm=# \d test1;
Composite type "public.test1"
Column | Type | Modifiers
--------+-----------------------+-----------
st1 | integer |
st2 | character varying(20) |
omm=# alter type test1 add attribute st3 text;
ALTER TYPE
omm=# \d test1;
Composite type "public.test1"
Column | Type | Modifiers
--------+-----------------------+-----------
st1 | integer |
st2 | character varying(20) |
st3 | text |
omm=# alter type test1 drop attribute st2;
ALTER TYPE
omm=# \d test1;
Composite type "public.test1"
Column | Type | Modifiers
--------+---------+-----------
st1 | integer |
st3 | text |
2、创建一个枚举类型,新增标签值,重命名标签值
omm=# create type test2 as enum ('ts_1','ts_2','ts_3');
CREATE TYPE
omm=# select * from pg_enum;
enumtypid | enumsortorder | enumlabel
-----------+---------------+-----------
16393 | 1 | ts_1
16393 | 2 | ts_2
16393 | 3 | ts_3
(3 rows)
omm=# alter type test2 add value if not exists 'ts_20' before 'ts_3';
ALTER TYPE
omm=# select * from pg_enum;
enumtypid | enumsortorder | enumlabel
-----------+---------------+-----------
16393 | 1 | ts_1
16393 | 2 | ts_2
16393 | 3 | ts_3
16393 | 2.5 | ts_20
(4 rows)
omm=# alter type test2 rename value 'ts_20' to 'ts_2.5';
ALTER TYPE
omm=# select * from pg_enum;
enumtypid | enumsortorder | enumlabel
-----------+---------------+-----------
16393 | 1 | ts_1
16393 | 2 | ts_2
16393 | 3 | ts_3
16393 | 2.5 | ts_2.5
(4 rows)3、使用新创建的类型创建表
omm=# create table ts
omm-# (
omm(# id integer,
omm(# stuff test1,
omm(# ts test2
omm(# );
CREATE TABLE
omm=# select * from ts;
id | stuff | ts
----+-------+----
(0 rows)
omm=# insert into ts values (1001,(100,'ac'));
INSERT 0 1
omm=# insert into ts values (1002,(100,'insert2'),'ts_2');
INSERT 0 1
omm=# select * from ts;
id | stuff | ts
------+---------------+------
1001 | (100,ac) |
1002 | (100,insert2) | ts_2
(2 rows)
4、删除类型
删除类型前需要将使用到该类型的表字段删掉,如果不要这个表的话可以把这个表给删了,这样就可以删除类型了
omm=# drop type test1;
ERROR: cannot drop type test1 because other objects depend on it
DETAIL: table ts column stuff depends on type test1
HINT: Use DROP ... CASCADE to drop the dependent objects too.
omm=# alter table ts drop column test1;
ALTER TABLE
omm=# drop type test1;
DROP TYPE
omm=# drop table ts;
DROP TABLE
omm=# drop type test2;
DROP TYPE
打卡完成,开心!
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。




