SQL utf8mb4 charset
Why 'Incorrect string value' appears on emoji inserts, why indexes grow, and the four places (column, table, connection, driver) utf8mb4 must be set.
- Category
- Strings
- Returns
charset- In this category
- 47 of 172
Syntax
ALTER TABLE t CONVERT TO CHARACTER SET utf8mb4;
Engine support
| Engine | Support | Notes |
|---|---|---|
| SQL Server | Not built in | No built-in equivalent. |
| PostgreSQL | Not built in | No built-in equivalent. |
| MySQL | Yes | Supported under this name. |
| SQLite | Not built in | No built-in equivalent. |
| Oracle | Not built in | No built-in equivalent. |
Examples
MySQL
INSERT INTO reactions (emoji) VALUES ('👍');
-- fails on utf8mb3: Incorrect string value
MySQL
SELECT CONVERT('👍' USING utf8mb4) AS ok;
Good to know
- MySQL's utf8 alias means utf8mb3 (max 3 bytes), so emoji and some rare CJK fail to insert. utf8mb4 (4 bytes) is the real UTF-8; use it everywhere new.
- Index key lengths grow: a VARCHAR(255) utf8mb4 unique key needs 1020 bytes, which fits the default 3072-byte InnoDB limit but not old 767-byte settings.
- The connection must agree too: SET NAMES utf8mb4 (or the driver's charset option), or 4-byte characters turn into '?' between app and server even with utf8mb4 columns.
- Converting: ALTER TABLE t CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci. Test index lengths and collation changes on real data first.
Questions about utf8mb4
Why can MySQL not store emoji?
The legacy utf8 charset is utf8mb3, capped at 3-byte characters; emoji need 4. Convert the column and connection to utf8mb4.
Is there a downside to utf8mb4?
Slightly larger index keys and memory estimates for multi-byte data. Since 8.0 the defaults are utf8mb4; the cost is mostly in old schemas.
What is the difference between utf8mb4_general_ci and utf8mb4_0900_ai_ci?
The 0900 collation uses the Unicode 9.0 algorithm: more correct ordering, true NO PAD behaviour, and case/accent insensitivity. Prefer it on MySQL 8+.
Browse every function in the SQL function reference, or see what the SQLly desktop app does for your database.
Related on SQLly
Cheat sheets for the engines that have it: MySQL.
Functions your engine actually has
SQLly's completion lists the built-in functions of the engine you're connected to, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle. Hover a function for help, and signature hints show each argument as you type.