字符类型
表 1 字符类型
在openGauss里另外还有两种定长字符类型。在表2里显示。name类型只用在内部系统表中,作为存储标识符,不建议普通用户使用。该类型长度当前定为64字节(63可用字符加结束符)。类型”char”只用了一个字节的存储空间。他在系统内部主要用于系统表,主要作为简单化的枚举类型使用。
表 2 特殊字符类型
--创建表。
postgres=# CREATE TABLE char_type_t2
(
CT_COL1 VARCHAR(5)
);
--插入数据。
postgres=# INSERT INTO char_type_t2 VALUES ('ok');
--插入的数据长度超过类型规定的长度报错。
postgres=# INSERT INTO char_type_t2 VALUES ('too long');
ERROR: value too long for type character varying(4)
CONTEXT: referenced column: ct_col1
--明确类型的长度,超过数据类型长度后会自动截断。
postgres=# INSERT INTO char_type_t2 VALUES ('too long'::varchar(5));
postgres=# SELECT ct_col1, char_length(ct_col1) FROM char_type_t2;
ct_col1 | char_length
---------+-------------
ok | 2
good | 4
too l | 5
(3 rows)