Character Sets and Collations: utf8mb4, Emoji and Case Rules

MySQL 50 min min read Updated: Nov 27, 2026 Intermediate
Character Sets and Collations: utf8mb4, Emoji and Case Rules
Intermediate Topic 3 of 3

Two Settings Control Text

SettingMeaningExample
Character set (charset)Which characters can be stored and how many bytes each takesutf8mb4
CollationThe rules for comparing and sorting text in that charsetutf8mb4_0900_ai_ci

The collation name decodes like this: ai accent insensitive (e and é equal), ci case insensitive (a and A equal). Variants: as accent sensitive, cs case sensitive, bin binary.

What Our Server Uses

sql SELECT @@character_set_server AS charset, @@collation_server AS collation;

Result:

charsetcollation
utf8mb4utf8mb4_0900_ai_ci

utf8 vs utf8mb4

In MySQL, the old charset called utf8 (also utf8mb3) stores only 3 bytes per character, so it cannot hold emoji or some rare characters. utf8mb4 is the real UTF-8 with up to 4 bytes. Always use utf8mb4. It is the default in MySQL 8.

sql CREATE TABLE messages_3 (id INT PRIMARY KEY, body VARCHAR(50)) CHARACTER SET utf8mb3; CREATE TABLE messages_4 (id INT PRIMARY KEY, body VARCHAR(50)) CHARACTER SET utf8mb4; INSERT INTO messages_4 VALUES (1, 'Hello ? வணக்கம்'); SELECT id, body, CHAR_LENGTH(body) AS characters, LENGTH(body) AS bytes FROM messages_4;

Result:

idbodycharactersbytes
1Hello ? வணக்கம்1532

The emoji uses 4 bytes. The same emoji in an utf8mb3 table is rejected:

sql CREATE TABLE messages_3 (id INT PRIMARY KEY, body VARCHAR(50)) CHARACTER SET utf8mb3; INSERT INTO messages_3 VALUES (1, 'Hello ?');
Error

ERROR 1366: Incorrect string value: '\xF0\x9F\x98\x80' for column 'body' at row 1

What was run These statements ran on MySQL 8.0.46. MySQL warns that utf8mb3 is deprecated, but it still works, which is why we can show the error.

Case and Accent Rules: Comparing Text

With the default ci (case insensitive) collation, these comparisons are all true:

sql SELECT 'apple' = 'APPLE' AS case_equal, 'resume' = 'résumé' AS accent_equal, 'apple' = 'apple ' AS trailing_spaces_equal;

Result:

case_equalaccent_equaltrailing_spaces_equal
110

Trailing spaces are a separate rule called the pad attribute. Older collations such as utf8mb4_unicode_ci are PAD SPACE, meaning 'apple' equals 'apple '. The newer utf8mb4_0900_* collations of MySQL 8 are NO PAD, so they are different. Both results from this server:

sql SELECT ('apple' COLLATE utf8mb4_0900_ai_ci = 'apple ') AS no_pad_0900, ('apple' COLLATE utf8mb4_unicode_ci = 'apple ') AS pad_space_unicode;

Result:

no_pad_0900pad_space_unicode
01

To compare exactly, use a binary or case-sensitive collation for that comparison:

sql SELECT 'apple' = 'APPLE' COLLATE utf8mb4_0900_as_cs AS case_sensitive_equal, 'resume' = 'résumé' COLLATE utf8mb4_0900_as_cs AS accent_sensitive_equal, 'a' = 'a' COLLATE utf8mb4_bin AS binary_equal;

Result:

case_sensitive_equalaccent_sensitive_equalbinary_equal
001

Column Level Collation

Set a collation for a column to change comparing and sorting for that column only. Case sensitive usernames, for example:

sql CREATE TABLE users (id INT PRIMARY KEY, username VARCHAR(30) COLLATE utf8mb4_0900_as_cs NOT NULL UNIQUE); INSERT INTO users VALUES (1, 'Asha'), (2, 'asha'); SELECT id, username FROM users WHERE username = 'asha';

Result:

idusername
2asha

Both Asha and asha can exist because this column is case sensitive.

Sorting Differences

Collations decide the order of text. Compare how accents and case sort:

sql CREATE TABLE words (w VARCHAR(10)); INSERT INTO words VALUES ('zebra'), ('Apple'), ('apple'), ('éclair'), ('Eagle'); SELECT w FROM words ORDER BY w COLLATE utf8mb4_0900_ai_ci, w COLLATE utf8mb4_bin;

Result:

w
Apple
apple
Eagle
éclair
zebra
sql CREATE TABLE words (w VARCHAR(10)); INSERT INTO words VALUES ('zebra'), ('Apple'), ('apple'), ('éclair'), ('Eagle'); SELECT w FROM words ORDER BY w COLLATE utf8mb4_bin;

Result:

w
Apple
Eagle
apple
zebra
éclair

The binary order puts all capitals first and éclair after zebra, because it sorts by raw byte values. Human friendly sorting needs a language-aware collation. MySQL has language specific ones, for example utf8mb4_sv_0900_ai_ci for Swedish.

The Illegal Mix of Collations Error

Comparing columns with different collations fails. A common cause is tables created with different defaults:

sql CREATE TABLE a (name VARCHAR(10) COLLATE utf8mb4_0900_ai_ci); CREATE TABLE b (name VARCHAR(10) COLLATE utf8mb4_unicode_ci); INSERT INTO a VALUES ('x'); INSERT INTO b VALUES ('x'); SELECT * FROM a JOIN b ON a.name = b.name;
Error

ERROR 1267: Illegal mix of collations (utf8mb4_0900_ai_ci,IMPLICIT) and (utf8mb4_unicode_ci,IMPLICIT) for operation '='

Fix it by making the collations the same (best), or by adding COLLATE to one side of the comparison (quick fix, but it can stop an index being used).

Setting Defaults

sql CREATE DATABASE shop CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci; CREATE TABLE notes ( id INT PRIMARY KEY, body TEXT ) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci; -- connection setting (most drivers do this for you) SET NAMES utf8mb4;

The connection must also use utf8mb4. The SQL files you import into this site start with SET NAMES utf8mb4; for exactly this reason.

MariaDB note MariaDB does not have the utf8mb4_0900_* collations. It uses utf8mb4_general_ci or utf8mb4_unicode_ci. A dump from MySQL 8 may need the collation names changed to load into MariaDB.

Common Beginner Mistakes

MistakeWhat happensFix
Using utf8 (utf8mb3)Emoji and some characters failUse utf8mb4
Connection charset different from table charsetGarbled text (mojibake)SET NAMES utf8mb4 or configure the driver
Assuming = is case sensitiveSurprising matchesKnow your collation, or use COLLATE ..._cs
Mixing collations in joinsIllegal mix errorStandardize collations
Using LENGTH to count charactersCounts bytesUse CHAR_LENGTH

Practice

  1. Create a table with utf8mb4 and insert text in two languages and an emoji.
  2. Compare 'a' = 'A' with a case sensitive collation.
  3. Create a unique case sensitive column and insert two names that differ only by case.
  4. Cause the illegal mix of collations error, then fix it.

Conclusion

Use utf8mb4 everywhere, know that default collations ignore case and accents, use a _cs, _as or _bin collation when you need exact comparison, and keep collations consistent across tables.

Quick Summary: utf8mb4, collation names, case and accent sensitivity, sorting, collation mix error.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators