Two Settings Control Text
| Setting | Meaning | Example |
|---|---|---|
| Character set (charset) | Which characters can be stored and how many bytes each takes | utf8mb4 |
| Collation | The rules for comparing and sorting text in that charset | utf8mb4_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
Result:
| charset | collation |
|---|---|
| utf8mb4 | utf8mb4_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.
Result:
| id | body | characters | bytes |
|---|---|---|---|
| 1 | Hello ? வணக்கம் | 15 | 32 |
The emoji uses 4 bytes. The same emoji in an utf8mb3 table is rejected:
ERROR 1366: Incorrect string value: '\xF0\x9F\x98\x80' for column 'body' at row 1
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:
Result:
| case_equal | accent_equal | trailing_spaces_equal |
|---|---|---|
| 1 | 1 | 0 |
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:
Result:
| no_pad_0900 | pad_space_unicode |
|---|---|
| 0 | 1 |
To compare exactly, use a binary or case-sensitive collation for that comparison:
Result:
| case_sensitive_equal | accent_sensitive_equal | binary_equal |
|---|---|---|
| 0 | 0 | 1 |
Column Level Collation
Set a collation for a column to change comparing and sorting for that column only. Case sensitive usernames, for example:
Result:
| id | username |
|---|---|
| 2 | asha |
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:
Result:
| w |
|---|
| Apple |
| apple |
| Eagle |
| éclair |
| zebra |
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:
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
The connection must also use utf8mb4. The SQL files you import into this site start with SET NAMES utf8mb4; for exactly this reason.
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
| Mistake | What happens | Fix |
|---|---|---|
Using utf8 (utf8mb3) | Emoji and some characters fail | Use utf8mb4 |
| Connection charset different from table charset | Garbled text (mojibake) | SET NAMES utf8mb4 or configure the driver |
Assuming = is case sensitive | Surprising matches | Know your collation, or use COLLATE ..._cs |
| Mixing collations in joins | Illegal mix error | Standardize collations |
Using LENGTH to count characters | Counts bytes | Use CHAR_LENGTH |
Practice
- Create a table with
utf8mb4and insert text in two languages and an emoji. - Compare
'a' = 'A'with a case sensitive collation. - Create a unique case sensitive column and insert two names that differ only by case.
- 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.

