SQL reference (MySQL and MariaDB dialects)

Collation and case sensitivity

Raft DB compares text without regard to case, like the default collation of both servers; the collations and character sets it lists, and what COLLATE does and does not do.

How text is compared#

Every text in Raft DB is utf8mb4. The default collation of the MySQL dialect is utf8mb4_0900_ai_ci and of the MariaDB dialect utf8mb4_uca1400_ai_ci (the connection collation utf8mb4_general_ci). Both are case-insensitive, and Raft DB's evaluator has one comparison of text: without regard to case.

This holds everywhere a text is compared:

PlaceCase-insensitive
=, <>, <, >, <=, >=, <=>, BETWEEN, INyes
ORDER BY, MIN, MAX, GREATEST, LEAST, STRCMPyes
GROUP BY, DISTINCT, unique keys and primary keysyes: 'Apple' and 'APPLE' are the same key (a second insert fails with error 1062)
LIKE, REGEXP, FIELD, FIND_IN_SET, INSTR, LOCATEyes
REPLACEno: it is case-sensitive, as in MySQL
Column namesyes
Database and table namesno: they are looked up as written (see Identifiers)

For example SELECT w FROM words WHERE w = 'APPLE' finds Apple, 'a' = 'A' is 1, and inserting 'APPLE' after 'Apple' into a primary key column fails.

The order is that of the code points of the lower-cased text, not the order of the collation's tables: letters with accents sort after z, and the language rules of utf8mb4_0900_ai_ci are not applied.

The comparison is by case, not by accent: the ai in the collation name (accent-insensitive) is not implemented: 'e' = 'é' is 0.

COLLATE#

SQL
expr COLLATE collation_name
col VARCHAR(10) COLLATE utf8mb4_general_ci
  • A case-insensitive collation after an expression, a column type or CREATE TABLE ... COLLATE is read and changes nothing.
  • A binary or case-sensitive collation (a name that ends in _bin or _cs, or binary) in an expression is refused as not supported, rather than silently ignored.
  • CHARACTER SET x, CHARSET x and COLLATE x after a text column type are accepted and ignored.
  • BINARY expr is accepted and ignored; the comparison stays case-insensitive. (Real MySQL gives 0 for BINARY 'a' = 'A'.)
  • SHOW CREATE TABLE shows the default collation of the dialect.

Character sets and collations#

SQL
SHOW CHARACTER SET [LIKE 'pattern' | WHERE expr];
SHOW COLLATION [LIKE 'pattern' | WHERE expr];
SELECT ... FROM information_schema.CHARACTER_SETS;
SELECT ... FROM information_schema.COLLATIONS;

They answer with the real lists of each server: 41 character sets and 286 collations for MySQL 9.7, 40 and 550 for MariaDB 11.4 (made from MySQL and MariaDB servers). The lists show what the servers have; Raft DB stores and compares text as utf8mb4 whatever the list says. The output is identical to both servers.

SQL
SHOW CHARACTER SET WHERE Charset = 'latin1';
SHOW COLLATION LIKE 'utf8mb4_unicode%';
SELECT CHARACTER_SET_NAME, DEFAULT_COLLATE_NAME, MAXLEN FROM information_schema.CHARACTER_SETS WHERE CHARACTER_SET_NAME IN ('utf8mb4', 'latin1', 'binary');

Remarks#

  • CHARSET(x) and COLLATION(x) report utf8mb4 and the dialect's connection collation for text and binary for other values.
  • Indexes on text. An index of a CHAR/VARCHAR column up to 255 characters uses a shadow field that holds the value in lower case, so an index lookup agrees with the case-insensitive comparison.
  • Not supported yet: case-sensitive and accent-sensitive comparison, per-column collations that change the comparison. expr COLLATE x_bin (or _cs, binary) compares, orders (ORDER BY), groups (GROUP BY) and de-duplicates (DISTINCT) by bytes. WEIGHT_STRING(x [AS CHAR(n) | AS BINARY(n)]) works for printable ASCII in MySQL and for any text in MariaDB (LEVEL ... is MariaDB's).

See also#