云数据库 GAUSSDB-字符类型:示例

时间:2024-04-26 16:16:05

示例

--创建表。
gaussdb=# CREATE TABLE char_type_t1 
(
    CT_COL1 CHARACTER(4)
)DISTRIBUTE BY HASH (CT_COL1);

--插入数据。
gaussdb=# INSERT INTO char_type_t1 VALUES ('ok');

--查询表中的数据。
gaussdb=# SELECT ct_col1, char_length(ct_col1) FROM char_type_t1;
 ct_col1 | char_length 
---------+-------------
 ok      |           4
(1 row)

--删除表。
gaussdb=# DROP TABLE char_type_t1;
 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
--创建表。
gaussdb=# CREATE TABLE char_type_t2  
(
    CT_COL1 VARCHAR(5)
)DISTRIBUTE BY HASH (CT_COL1);

--插入数据。
gaussdb=# INSERT INTO char_type_t2 VALUES ('ok');

gaussdb=# INSERT INTO char_type_t2 VALUES ('good');

--插入的数据长度超过类型规定的长度报错。
gaussdb=# INSERT INTO char_type_t2 VALUES ('too long');
ERROR:  value too long for type character varying(5)
CONTEXT:  referenced column: ct_col1

--明确类型的长度,超过数据类型长度后会自动截断。
gaussdb=# INSERT INTO char_type_t2 VALUES ('too long'::varchar(5));

--查询数据。
gaussdb=# SELECT ct_col1, char_length(ct_col1) FROM char_type_t2;
 ct_col1 | char_length 
---------+-------------
 ok      |           2
 good    |           4
 too l   |           5
(3 rows)

--删除数据。
gaussdb=# DROP TABLE char_type_t2;
support.huaweicloud.com/distributed-devg-v3-gaussdb/gaussdb-12-0324.html