skip to content

In MySQL, why should new tables use utf8mb4 rather than utf8mb3, and what does the default collation utf8mb4_0900_ai_ci do to comparisons?

level: middleimportance: should knowfreq 55%

answer

  1. three bytes versus four
  2. emoji and supplementary characters
  3. utf8 is an old alias
  4. ai and ci in the name
  5. unique keys follow the collation

basics

~20 s

MySQL's utf8mb3 stores at most three bytes per character, so emoji and other supplementary characters fail; utf8mb4 stores all of Unicode and is the default. Its default collation, utf8mb4_0900_ai_ci, compares case- and accent-insensitively, so 'Jose' equals 'josé'.

solid answer

~40 s

`utf8mb3` encodes each character in one to three bytes, which covers only the Basic Multilingual Plane, so an emoji or a supplementary CJK character fails with error 1366, Incorrect string value, in strict mode, and is stored damaged with a warning otherwise. `utf8mb3` is deprecated, and the name `utf8` is still a deprecated alias for it, so `CHARSET=utf8` gives you the three-byte set. `utf8mb4` uses one to four bytes and is MySQL 8.4's server default, paired with `utf8mb4_0900_ai_ci`: Unicode Collation Algorithm 9.0.0 weights, accent-insensitive, case-insensitive and `NO PAD`. That collation drives `WHERE`, `ORDER BY`, `GROUP BY` and `UNIQUE` checks, so 'Jose' and 'josé' collide in a unique username index with error 1062. When a column needs exact matching I declare `utf8mb4_0900_as_cs` or `utf8mb4_bin` on that column.

code

sql · 11 lines
sql
CREATE TABLE customer (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
  username VARCHAR(64) NOT NULL,
  api_key VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL,
  UNIQUE KEY uq_username (username),
  UNIQUE KEY uq_api_key (api_key)
) DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

INSERT INTO customer (username, api_key) VALUES ('jose', 'k1');
INSERT INTO customer (username, api_key) VALUES ('José', 'K1');
-- ERROR 1062 (23000): Duplicate entry 'José' for key 'customer.uq_username'

go deeper

for a junior

Recall that utf8 in MySQL means the three-byte utf8mb3, that utf8mb4 is the default and that it stores emoji.

for a middle

Explain what each part of utf8mb4_0900_ai_ci means and show how it makes 'jose' and 'José' collide in a unique index, and when to pick a binary or accent-sensitive collation.

for a senior

Plan a utf8mb3 to utf8mb4 migration: connection charset, index widths, collation-driven duplicates and why CONVERT TO CHARACTER SET on a large table needs an online tool.

for a principal

Decide the default character set and collations a team enforces, including which column classes get binary collations and how mixed-collation joins are prevented.

## Character set versus collation Two separate settings decide how MySQL handles text: - A **character set** decides which characters a column can hold and how they are encoded as bytes. - A **collation** decides how two strings in that character set **compare and sort**: whether case matters, whether accents matter, and whether trailing spaces count. Every string column has both, inherited from the table, database or server unless declared on the column itself. ## utf8mb3, utf8 and utf8mb4 | Name | Bytes per character | Status in MySQL 8.4 | |---|---|---| | `utf8mb4` | 1 to 4 | recommended; the server default | | `utf8mb3` | 1 to 3 | deprecated, expected to be removed in a future major release | | `utf8` | 1 to 3 | a deprecated alias for `utf8mb3`, expected one day to mean `utf8mb4` | The three-byte limit matters because many characters in everyday use need four bytes in UTF-8: emoji, many historic and rare CJK ideographs, and some mathematical symbols. Writing one of them into a `utf8mb3` column produces **error 1366, Incorrect string value**, under strict SQL mode, which MySQL 8.4 enables by default. Without strict mode, MySQL stores what it can and raises a warning, so the text is damaged with only a warning to show for it. A classic trap is reading an old schema that says `CHARSET=utf8` and assuming it is full UTF-8. It is the three-byte set; MySQL 8.4 even shows `utf8mb3` in `SHOW CREATE TABLE` and the Information Schema to make that visible. The connection matters too: the client must send and expect `utf8mb4` (for example through `SET NAMES utf8mb4` or the driver's charset option), or four-byte characters are lost before they reach the column. ## What utf8mb4_0900_ai_ci means MySQL's default `utf8mb4` collation is **`utf8mb4_0900_ai_ci`**. Its name decodes as: - `0900`: weights from **Unicode Collation Algorithm 9.0.0**, newer and more accurate than the older `utf8mb4_unicode_ci` (UCA 4.0.0) and `utf8mb4_general_ci`. - `ai`: **accent-insensitive**, so `e`, `é` and `è` compare equal. - `ci`: **case-insensitive**, so `A` and `a` compare equal. The `0900` collations also have the **`NO PAD`** attribute: trailing spaces are significant, so `'abc'` and `'abc '` are different values. The older `PAD SPACE` collations, such as `utf8mb4_bin` and `utf8mb4_general_ci`, ignore trailing spaces in comparisons. ## Where the collation changes behaviour The collation applies everywhere strings are compared, which is why it is a schema-design decision rather than a display setting: 1. **Equality filters.** `WHERE username = 'jose'` matches `José`, `JOSE` and `jose`. 2. **Unique indexes.** A `UNIQUE` index on `username` treats those values as duplicates, so inserting `'José'` when `'jose'` exists fails with **error 1062, Duplicate entry**. That is often what a product wants for logins and exactly what it does not want for case-sensitive tokens. 3. **Sorting and grouping.** `ORDER BY` and `GROUP BY` follow the collation's weights, so accented and unaccented forms group together. 4. **Joins across collations.** Comparing columns with different, incompatible collations can fail with **error 1267, Illegal mix of collations**, which is common after a partial migration from an older default. When a column needs exact comparison, declare it per column: `utf8mb4_0900_as_cs` for accent- and case-sensitive linguistic comparison, or `utf8mb4_0900_bin` / `utf8mb4_bin` for code-point comparison of identifiers, hashes and API keys. ## Migrating an old utf8mb3 schema - Changing a single **unindexed** `CHAR`, `VARCHAR`, `TEXT` or `ENUM` column from `utf8mb3` to `utf8mb4` is a **metadata-only** change in MySQL 8.4, because utf8mb3 data is already valid utf8mb4. - `ALTER TABLE ... CONVERT TO CHARACTER SET utf8mb4` rebuilds the table when the encoding changes, and the online DDL support table lists it as **not permitting concurrent DML**, so on a large, busy table it becomes a job for an online schema-change tool. - Check index widths: four bytes per character means a fully indexed `VARCHAR` must fit InnoDB's 3,072-byte key limit under the default `DYNAMIC` row format, or 767 bytes under the older `COMPACT` and `REDUNDANT` formats, which is where the old `VARCHAR(191)` habit came from. - Changing a column's collation can change which existing rows count as duplicates, so test unique indexes against real data before switching.

  • Why does an old MySQL schema declared with CHARSET=utf8 reject an emoji in strict mode?
    In MySQL, utf8 is a deprecated alias for utf8mb3, which stores at most three bytes per character. Emoji need four bytes in UTF-8, so the insert fails with error 1366, Incorrect string value. Converting the column or table to utf8mb4, and making the connection use utf8mb4, fixes it.
  • How does MySQL's utf8mb4_0900_ai_ci treat 'abc' and 'abc ' compared with utf8mb4_bin?
    utf8mb4_0900_ai_ci is a NO PAD collation, so the trailing space is significant and the two values differ. utf8mb4_bin is a PAD SPACE collation, so comparisons ignore trailing spaces and the two compare equal. That difference can change which rows a unique index treats as duplicates when a column switches collation.

  • In MySQL, CHARSET=utf8 means full four-byte UTF-8
  • utf8mb3 can store emoji if the column is wide enough
  • The collation only affects ORDER BY, not equality or unique keys
  • utf8mb4_0900_ai_ci is case-sensitive because it is a Unicode collation
  • Converting a big table to utf8mb4 is always a metadata-only change

Assumes MySQL 8.4: utf8mb4 with utf8mb4_0900_ai_ci is the server default, and utf8mb3 (and its alias utf8) is deprecated but still supported through the 8.4 series.