Skip to main content

VillageSQL is a drop-in replacement for MySQL with extensions.

All examples in this guide work on VillageSQL. Install Now →
MySQL’s character set controls how strings are stored (which bytes represent which characters). The collation controls how strings are compared and sorted. Getting these right at the start avoids corrupted data and broken sorts that are painful to fix later.

utf8mb4 — The Right Default

Always use utf8mb4. MySQL’s utf8 (without the mb4) is a 3-byte encoding that cannot store 4-byte Unicode characters — which includes emoji, many Chinese characters, and various symbols. utf8mb4 is the complete UTF-8 encoding.
If you’re on a legacy database using utf8, migrating to utf8mb4 requires converting the database, tables, and columns.

Character set quick-reference

The cost column is about the work required to fix a wrong choice later, not the performance cost of the charset itself. latin1 is the trap: it accepts any byte value without error, so bad data gets in quietly and surfaces only when you try to display or convert it.

Collations

The collation determines sort order and comparison behavior. Common choices: utf8mb4_0900_ai_ci is the default collation and the best general-purpose choice. It handles Unicode correctly, is case-insensitive (ci), and accent-insensitive (ai). Use utf8mb4_bin only when you need exact byte matching. ci = case-insensitive, cs = case-sensitive, ai = accent-insensitive, as = accent-sensitive.

Setting Character Sets

At database creation:
At table creation:
At column level (overrides table default):
Column-level character sets are useful when one column needs case-sensitive matching while the rest of the table uses case-insensitive. Change a column’s character set:
Convert an entire table:
CONVERT TO changes every column in one statement. It rebuilds the table, so it takes time proportional to table size.

Checking Character Sets

Columns in the output of SHOW CREATE TABLE that don’t show a charset are inheriting from the table default.

Connection Character Set

The connection character set controls the encoding MySQL expects from the client and uses for string literals. Always set it explicitly:
Most drivers do this automatically when you set the charset in the connection string. Connections default to utf8mb4, so explicit overrides are rarely needed.

Collation Effects on Queries

Collation affects WHERE, ORDER BY, and uniqueness:
To sort with a different collation than the column’s default:

Converting from utf8 to utf8mb4

If your legacy database uses utf8 (3-byte), convert it:
Also update the server config to make utf8mb4 the default for new connections:
Converting large tables takes time and rebuilds the table. Schedule it during a maintenance window or use an online schema change tool.

Frequently Asked Questions

Why does utf8 in MySQL not support emoji?

MySQL’s utf8 is a non-standard 3-byte subset of UTF-8 that predates the 4-byte Unicode extension. Emoji (and many other modern Unicode characters) require 4 bytes. utf8mb4 is the correct full implementation of UTF-8. The old utf8 alias exists for backward compatibility only — never use it for new schemas.

Does changing collation affect existing data?

Changing collation doesn’t change the stored bytes. It changes how those bytes are compared and sorted. However, unique indexes are enforced using the collation, so changing from _bin to _ci may reveal collisions (Alice and alice become equal) that must be resolved first.

Which collation should I use?

utf8mb4_0900_ai_ci for most use cases — it’s the default collation and handles Unicode well. Use utf8mb4_bin only when you need exact byte matching. Avoid utf8mb4_general_ci in new projects — it predates _0900_ai_ci and handles some Unicode edge cases incorrectly.

Troubleshooting

See also