DEV Community

Cong Li
Cong Li

Posted on

Configuring Case Sensitivity in GBase 8c Compatibility Mode

MySQL and SQL Server support case sensitivity. How does GBase 8c handle this? Let's explore GBase 8c's performance in terms of case sensitivity for object names and case-insensitive data queries.

1. Column Names Support Case Sensitivity

To ensure compatibility with MySQL and SQL Server, start by creating a GBase 8c database in compatibility mode.

Creating a Database and Table

In the GBase 8c Database Management System, execute the following commands to create a database named test and a table named t1:

CREATE DATABASE test DBCOMPATIBILITY 'B' encoding 'UTF-8' LC_COLLATE 'zh_CN.utf8' LC_CTYPE 'zh_CN.utf8';
CREATE TABLE t1(Name varchar(10), iD int);
Enter fullscreen mode Exit fullscreen mode

Check the table structure:

\d+ t1
Enter fullscreen mode Exit fullscreen mode

Output:

Table "public.t1"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-----------------------+-----------+----------+--------------+-------------
Name   | character varying(10) |           | extended |              |
iD     | integer               |           | plain    |              |
Has OIDs: no
Options: orientation=row, compression=no
Enter fullscreen mode Exit fullscreen mode

Verify the column names:

select column_name from information_schema.columns where table_name='t1';
Enter fullscreen mode Exit fullscreen mode

Output:

column_name
-------------
iD
Name
(2 rows)
Enter fullscreen mode Exit fullscreen mode

Insert data and perform update operations:

insert into t1(name, ID) values ('Test', 1);
update t1 set name='new_test' where Id=1;
select * from t1;
Enter fullscreen mode Exit fullscreen mode

Output:

Name   | iD
-------+----
new_test |  1
(1 row)
Enter fullscreen mode Exit fullscreen mode

As demonstrated, GBase 8c allows case-sensitive column names while ignoring case during DML operations, ensuring compatibility with MySQL and SQL Server.

2. Table Names Support Case Sensitivity

By default, GBase 8c is case-insensitive. To enforce case sensitivity, two methods can be used.

Method 1: Using Double Quotes

To create a table with a case-sensitive name, use double quotes:

CREATE TABLE "T2" (id int, Name varchar(10));
Enter fullscreen mode Exit fullscreen mode

Check the tables:

\d+
Enter fullscreen mode Exit fullscreen mode

Output:

List of relations
Schema | Name | Type | Owner | Size | Storage | Description
--------+------+-------+-------+------------+----------------------------------+-------------
public | T2   | table | gbase | 0 bytes    | {orientation=row, compression=no} |
public | t1   | table | gbase | 8192 bytes | {orientation=row, compression=no} |
Enter fullscreen mode Exit fullscreen mode

Verify table structure:

\d+ t2
Enter fullscreen mode Exit fullscreen mode

Output:

Did not find any relation named "t2".
Enter fullscreen mode Exit fullscreen mode

Check with double quotes:

\d+ "T2"
Enter fullscreen mode Exit fullscreen mode

Output:

Table "public.T2"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-----------------------+-----------+----------+--------------+-------------
id     | integer               |           | plain    |              |
Name   | character varying(10) |           | extended |              |
Has OIDs: no
Options: orientation=row, compression=no
Enter fullscreen mode Exit fullscreen mode

This method requires using double quotes for all operations involving case-sensitive names.

Method 2: Using dolphin.lower_case_table_names Parameter

To enforce case sensitivity without using double quotes, adjust the dolphin.lower_case_table_names parameter:

ALTER DATABASE test SET dolphin.lower_case_table_names TO 0;
Enter fullscreen mode Exit fullscreen mode

Reconnect to the database for the changes to take effect:

gsql -r test -p 15400
Enter fullscreen mode Exit fullscreen mode

Verify the parameter value:

SHOW dolphin.lower_case_table_names;
Enter fullscreen mode Exit fullscreen mode

Output:

dolphin.lower_case_table_names
--------------------------------
0
Enter fullscreen mode Exit fullscreen mode

Create and check a new table:

CREATE TABLE T3(id int, NAme varchar(10));
\d+ T3
Enter fullscreen mode Exit fullscreen mode

Output:

Table "public.T3"
Column | Type | Modifiers | Storage | Stats target | Description
--------+-----------------------+-----------+----------+--------------+-------------
id     | integer               |           | plain    |              |
NAme   | character varying(10) |           | extended |              |
Has OIDs: no
Options: orientation=row, compression=no
Enter fullscreen mode Exit fullscreen mode

Query the table:

SELECT * FROM T3;
Enter fullscreen mode Exit fullscreen mode

Output:

id | NAme
----+------
(0 rows)
Enter fullscreen mode Exit fullscreen mode

Attempt querying with a different case:

SELECT * FROM t3;
Enter fullscreen mode Exit fullscreen mode

Output:

ERROR: relation "t3" does not exist
Enter fullscreen mode Exit fullscreen mode

This ensures case-sensitive table names without needing double quotes.

3. Data Case-Insensitive Queries

MySQL and SQL Server support case-insensitive data queries. GBase 8c also supports this with the utf8_general_ci collation.

Example in MySQL

CREATE TABLE t4(id int, name varchar(100)) COLLATE utf8_general_ci;
INSERT INTO t4 VALUES (1, 'ABC'), (2, 'ABc'), (3, 'abc');
SELECT * FROM t4 WHERE name='abc';
Enter fullscreen mode Exit fullscreen mode

Output:

id | name
----+------
1  | ABC
2  | ABc
3  | abc
Enter fullscreen mode Exit fullscreen mode

Example in GBase 8c

First, ensure the utf8_general_ci collation is supported:

SELECT * FROM pg_collation WHERE collcollate='utf8_general_ci';
Enter fullscreen mode Exit fullscreen mode

Output:

collname | collcollate | collctype
---------+--------------+-----------
utf8_general_ci | utf8_general_ci | utf8_general_ci
Enter fullscreen mode Exit fullscreen mode

Create a table with the utf8_general_ci collation:

CREATE TABLE t4(id int, name varchar(100)) COLLATE utf8_general_ci;
INSERT INTO t4 VALUES (1, 'ABC'), (2, 'ABc'), (3, 'abc');
SELECT * FROM t4 WHERE name='abc';
SELECT * FROM t4 WHERE name='ABC';
Enter fullscreen mode Exit fullscreen mode

Output for both queries:

id | name
----+------
1  | ABC
2  | ABc
3  | abc
Enter fullscreen mode Exit fullscreen mode

To use this feature, ensure the database encoding is UTF8 and exclude_reserved_words is not set.

This configuration guide ensures that GBase 8c handles case sensitivity for both object names and data queries effectively, maintaining compatibility with MySQL and SQL Server.

Top comments (0)