云数据库 GAUSSDB-CREATE TYPE:示例

时间:2024-11-02 18:51:23

示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
--创建一种复合类型,建表并插入数据以及查询:
openGauss=# CREATE TYPE compfoo AS (f1 int, f2 text);
openGauss=# CREATE TABLE t1_compfoo(a int, b compfoo);
openGauss=# CREATE TABLE t2_compfoo(a int, b compfoo);
openGauss=# INSERT INTO t1_compfoo values(1,(1,'demo'));
openGauss=# INSERT INTO t2_compfoo select * from t1_typ5;
openGauss=# SELECT (b).f1 FROM t1_compfoo;
openGauss=# SELECT * FROM t1_compfoo t1 join t2_compfoo t2 on (t1.b).f1=(t1.b).f1;

--重命名数据类型:
openGauss=# ALTER TYPE compfoo RENAME TO compfoo1;

--要改变一个用户定义类型compfoo1的所有者为usr1:
CREATE USER usr1 PASSWORD '********';
openGauss=# ALTER TYPE compfoo1 OWNER TO usr1;

--把用户定义类型compfoo1的模式改变为usr1:
openGauss=# ALTER TYPE compfoo1 SET SCHEMA usr1;

给一个数据类型增加一个新的属性:
openGauss=# ALTER TYPE usr1.compfoo1 ADD ATTRIBUTE f3 int;

删除compfoo1类型
openGauss=# DROP TYPE usr1.compfoo1 cascade;

删除相关表和用户:
openGauss=# DROP TABLE t1_compfoo;
openGauss=# DROP TABLE t2_compfoo;
openGauss=# DROP SCHEMA usr1;
openGauss=# DROP USER usr1;

--创建一个枚举类型
openGauss=# CREATE TYPE bugstatus AS ENUM ('create', 'modify', 'closed');

--添加一个标签值
openGauss=# ALTER TYPE bugstatus ADD VALUE IF NOT EXISTS 'regress' BEFORE 'closed';

--重命名一个标签值
openGauss=# ALTER TYPE bugstatus RENAME VALUE 'create' TO 'new';

--创建一个集合类型
openGauss=# CREATE TYPE compfoo_table AS TABLE OF compfoo;
support.huaweicloud.com/distributed-devg-v2-gaussdb/gaussdb-12-0383.html