VastbaseG100

基于openGauss内核开发的企业级关系型数据库。

Menu

字符串大小写不敏感

功能描述

Vastbase支持字符串大小写不敏感,该功能依赖于下列字符集实现:

字符集 说明 默认字符序
utf8 针对Unicode的可变长度字符编码,字符编码长度1~4字节。 utf8_general_ci
utf8mb4 同utf8 utf8mb4_general_ci
  • utf8_general_ci和utf8mb4_general_ci均是兼容MySQL数据库的字符排序规则,此排序规则在比较字符时,不考虑大小写,只比较字符的二进制表示。因此,它对大小写是不敏感的,例如”A”和”a”是相等的。
  • Vastbase不敏感的字符序支持的范围为schena、表、列、常量级别,不能指定数据库级别。
  • 常量比较为纯字符串的比较,不能包含任何列或者其他函数或运算。

该功能相关的参数:

b_format_behavior_compat_options

参数说明:数据库B模式兼容性行为配置项,该参数的值由若干个配置项用逗号隔开构成。详细介绍请参考b_format_behavior_compat_options

该参数属于USERSET类型参数,请参考重设参数表1中对应设置方法进行设置。

取值范围:字符串

默认值: ""

设置此参数b_format_behavior_compat_options='enable_multi_charset'只是为了可以设置collation_connection。

collation_connection

参数说明:该参数用于指定常量字符串的排序规则。只有当参数b_format_behavior_compat_options包含选项enable_multi_charset的场景下才允许设置该参数。详细介绍请参考collation_connection

该参数属于USERSET类型参数,请参考重设参数表1中对应设置方法进行设置。

取值范围:字符串,合法的字符序的字符串,例如 utf8_general_ci

默认值: 当前数据库字符集的默认字符序

注意事项

  • 该功能仅在数据库兼容模式为MySQL时支持(即数据库实例初始化时指定DBCOMPATIBILITY='B')。
  • 不敏感的字符序支持的范围为schena、表、列、常量级别,不能指定数据库级别。所以创建出来的public schema 即使打开了参数也要再次进行alter才能修改默认的排序规则。

语法格式

无。

示例

示例1:模式级别设置,同时设置字符集和字符序。

1、设置参数b_format_behavior_compat_options。

set b_format_behavior_compat_options = 'enable_multi_charset';
show b_format_behavior_compat_options;

返回结果为:

 b_format_behavior_compat_options
----------------------------------
 enable_multi_charset
(1 row)

2、设置参数collation_connection。

set collation_connection='utf8mb4_general_ci';
show collation_connection;

返回结果为:

 collation_connection
----------------------
 utf8mb4_general_ci
(1 row)

3、创建模式,设置字符集和字符序,字符集和字符序需对应。

create schema sch_1174738 charset utf8mb4 collate utf8mb4_general_ci;

4、创建测试表。

create table sch_1174738.table_1174738(id int,name nvarchar(100));
insert into sch_1174738.table_1174738 values(1,'RA123');
insert into sch_1174738.table_1174738 values(2,'ra123');
insert into sch_1174738.table_1174738 values(3,'Ra123');
insert into sch_1174738.table_1174738 values(4,'rA123');

5、查询结果。

select * from sch_1174738.table_1174738 where name='Ra123';

返回结果如下:

 id | name
----+-------
  1 | RA123
  2 | ra123
  3 | Ra123
  4 | rA123
(4 rows)

示例2:常量级别设置(设置enable_multi_charset后,同时设置字符集和字符序)。

1、设置参数b_format_behavior_compat_options。

set b_format_behavior_compat_options = 'enable_multi_charset';
show b_format_behavior_compat_options;

返回结果为:

 b_format_behavior_compat_options
----------------------------------
 enable_multi_charset
(1 row)

2、设置参数collation_connection。

set collation_connection='utf8mb4_general_ci';
show collation_connection;

返回结果为:

 collation_connection
----------------------
 utf8mb4_general_ci
(1 row)

3、允许数据库中含有不同于数据库字符集的数据,合并不同字符集的表达式。

CREATE TABLE table_1174859(c1 varchar(16) character set utf8mb4 collate utf8mb4_bin);
INSERT INTO table_1174859 VALUES('STRING');

4、比较时使用utf8mb4_bin字符序,结果为false。

SELECT c1 = _utf8mb4'string' AS result FROM table_1174859;

返回结果为:

result
--------
 f
(1 row)

5、比较时使用utf8mb4_general_ci字符序,结果为true。

SELECT c1 = _utf8mb4'string' COLLATE utf8mb4_general_ci AS result FROM table_1174859;

返回结果为:

 result
--------
 t
(1 row)

示例3:同时设置模式、表、表列字符序。

1、设置参数b_format_behavior_compat_options和collation_connection。

set b_format_behavior_compat_options to default;
set collation_connection to default;
show b_format_behavior_compat_options;
show collation_connection;

返回结果为:

b_format_behavior_compat_options
----------------------------------

(1 row)

 collation_connection
----------------------

(1 row)

2、创建模式,设置模式字符序utf8_bin。

create schema sch_1175225 collate utf8_bin;

3、创建测试表table_1175225,设置表字符序utf8_general_ci、表列字符序utf8_bin,并插入数据。

UTF8是一种可变长度的编码方式,用于表示Unicode字符集中的字符。UTF-8编码中,每个字符可以由1个到4个字节组成。utf8_general_ci和utf8_bin是MySQL数据库中两种不同的字符排序规则。

  • utf8_general_ci:此排序规则在比较字符时,不考虑大小写,只比较字符的二进制表示。因此,它对大小写是不敏感的。在utf8_general_ci排序规则下,”A”和”a”是相等的。

  • utf8_bin:此排序规则在比较字符时,首先比较字母的大小写,然后比较字母的顺序。也就是说,它对大小写是敏感的。例如,在utf8_general_ci排序规则下,”A”和”a”是不相等的。

create table sch_1175225.table_1175225(id int, col1 mediumtext , col2 text collate utf8_bin) collate utf8_general_ci;
insert into sch_1175225.table_1175225 values(1,'RA123','AB');
insert into sch_1175225.table_1175225 values(2,'ra123','ab');
insert into sch_1175225.table_1175225 values(3,'Ra123','aB');
insert into sch_1175225.table_1175225 values(4,'rA123','Ab');

4、创建测试表table_1175225_2并插入数据。

create table sch_1175225.table_1175225_2(id int, col1 mediumtext , col2 text);
insert into sch_1175225.table_1175225_2 values(1,'RA123','AB');
insert into sch_1175225.table_1175225_2 values(2,'ra123','ab');
insert into sch_1175225.table_1175225_2 values(3,'Ra123','aB');
insert into sch_1175225.table_1175225_2 values(4,'rA123','Ab');

5、查询table_1175225数据,表列col2敏感。

select * from sch_1175225.table_1175225 where col2='ab';

返回结果如下:

 id | col1  | col2
----+-------+------
  2 | ra123 | ab
(1 row)

6、查询table_1175225数据,表列col1不敏感。

select * from sch_1175225.table_1175225 where col1='ra123';

返回结果如下:

 id | col1  | col2
----+-------+------
  1 | RA123 | AB
  2 | ra123 | ab
  3 | Ra123 | aB
  4 | rA123 | Ab
(4 rows)

7、查询其他表数据,大小写均敏感。

select * from sch_1175225.table_1175225_2 where col1='ra123';
select * from sch_1175225.table_1175225_2 where col2='AB';
select * from sch_1175225.table_1175225_2 where col1='ra123' and col2='AB';  

返回结果如下:

 id | col1  | col2
----+-------+------
  2 | ra123 | ab
(1 row)

 id | col1  | col2
----+-------+------
  1 | RA123 | AB
(1 row)

 id | col1 | col2
----+------+------
(0 rows)