MySQL 1366: Incorrect string value for column
The column is three-byte utf8 and the value needs four. Convert to utf8mb4, align the connection charset and keep strict mode on.

Introduction & Quick Summary
An INSERT that worked with plain ASCII text fails the moment a user types an emoji:
ERROR 1366 (HY000): Incorrect string value: '\xF0\x9F\x98\x80' for column 'comment' at row 1
Quick answer: the column cannot store four-byte characters. Convert it to utf8mb4 and make sure the connection speaks the same encoding.
ALTER TABLE comments CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
SET NAMES utf8mb4;The escaped value in the message is the real clue. \xF0\x9F\x98\x80 is the UTF-8 encoding of 😀, four bytes long. A column declared utf8 in MySQL means utf8mb3, which stores at most three bytes per character, so the fourth byte cannot be written and the server rejects the row. Nothing is wrong with the client, the driver or the text; the storage format of that one column is too narrow.
Core Mechanics, Signatures & Reproducible Setup
Reproduce it deterministically:
CREATE DATABASE demo CHARACTER SET utf8mb3;
USE demo;
CREATE TABLE comments (id INT PRIMARY KEY AUTO_INCREMENT, body VARCHAR(200)) CHARACTER SET utf8mb3;
INSERT INTO comments (body) VALUES ('😀');
-- ERROR 1366 (HY000): Incorrect string value: '\xF0\x9F\x98\x80' for column 'body' at row 1Character sets matter per level, and the narrowest one wins:
| Charset | Bytes per character | Stores emoji | Notes |
|---|---|---|---|
ascii | 1 | no | latin letters only |
latin1 | 1 | no | cp1252 in MySQL |
utf8mb3 (alias utf8) | up to 3 | no | the default until MySQL 8.0 |
ucs2 | 2 | no | BMP only, rarely used |
utf8mb4 | up to 4 | yes | the correct choice |
binary | bytes | irrelevant | no character semantics |
Four levels decide what a column can hold: the server default, the database default, the table default and the column definition. A column created without an explicit charset inherits the table's at creation time — changing the table later does not change it. That inheritance rule explains why a database converted to utf8mb4 can still have three-byte columns years later.
Key Comparison Table
Where to change the charset, and what each setting only affects:
| Level | Command | Affects | Safe online |
|---|---|---|---|
| Connection | SET NAMES utf8mb4; | this session's encoding | yes, per session |
| Database default | ALTER DATABASE d CHARACTER SET utf8mb4; | tables created afterwards | yes |
| Table default | ALTER TABLE t DEFAULT CHARACTER SET utf8mb4; | columns added afterwards | yes |
| Existing columns | ALTER TABLE t CONVERT TO CHARACTER SET utf8mb4; | rewrites every text column | needs a lock, plan it |
| Single column | ALTER TABLE t MODIFY body VARCHAR(200) CHARACTER SET utf8mb4; | one column | rebuilds that column |
The error family around the same root cause:
| Error | Meaning | Fix direction |
|---|---|---|
1366 Incorrect string value | value cannot be represented | widen the charset |
1265 Data truncated for column | value fits, precision does not | check length and strict mode |
1300 Invalid utf8 character string | bytes are not valid UTF-8 | fix the client encoding |
1071 Specified key was too long | index grew past 3072 bytes after conversion | prefix index or shorter key |
1366 only on some rows | source data has mixed encodings | inspect with HEX() |
Practical Edge Cases, Error Fixes & Deep Dive Code Examples
1. Convert the table, then check indexes
ALTER TABLE comments CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci;
SHOW CREATE TABLE comments\GCONVERT TO rewrites every character column and rebuilds indexes, so an indexed VARCHAR(255) goes from 765 bytes to 1020 bytes at the maximum length. InnoDB's index limit is 3072 bytes, which accommodates VARCHAR(768) of utf8mb4; a wider indexed column fails with error 1071 during the conversion. On MySQL 8.0 the default collation is utf8mb4_0900_ai_ci; on 5.7 use utf8mb4_unicode_ci. Generate and review the exact DDL before running it on a large table — the SQL schema tool renders the statement structure, and the SQL formatter makes a multi-table migration script readable before it touches production.
2. The connection charset is a separate setting
A utf8mb4 column still rejects emoji if the client announces a narrower charset, because the server re-encodes the literal on arrival:
SHOW VARIABLES LIKE 'character_set_%';
-- character_set_client, character_set_connection, character_set_results
SET NAMES utf8mb4 COLLATE utf8mb4_0900_ai_ci;Driver-side equivalents, because most applications never issue SET NAMES by hand:
| Driver | Setting |
|---|---|
| MySQL Connector/J | characterEncoding=UTF-8&connectionCollation=utf8mb4_0900_ai_ci |
| node mysql2 | charset: "utf8mb4" in the pool options |
| PyMySQL | charset="utf8mb4" |
| PHP PDO | charset=utf8mb4 in the DSN |
| Go go-sql-driver | charset=utf8mb4&collation=utf8mb4_0900_ai_ci |
| Laravel | 'charset' => 'utf8mb4', 'collation' => 'utf8mb4_unicode_ci' |
Note that utf8 in a driver option often means utf8mb3; write utf8mb4 explicitly even where the driver would accept the shorter alias.
3. Strict mode decides between an error and silent damage
SELECT @@sql_mode;
-- STRICT_TRANS_TABLES,ONLY_FULL_GROUP_BY,...With STRICT_TRANS_TABLES the insert fails with 1366. Without it, MySQL truncates at the first unrepresentable character and raises a warning — the row is written, the user loses half their text, and nobody notices until a support ticket. Keep strict mode on and treat 1366 as a deployment bug in the schema. On MariaDB the equivalent variable is also sql_mode, with STRICT_TRANS_TABLES behaving the same way.
4. Finding the rows that will fail
-- four-byte UTF-8 sequences start with a lead byte F0..F4 at a byte boundary
SELECT id, HEX(body) FROM comments WHERE HEX(body) REGEXP '(^|..)F[0-4]';Matching on the byte-aligned lead byte avoids false positives: (^|..) allows a match only at the start of the hex string or after two hex digits, so F4 inside A4F4 is ignored. Scanning is fast and catches data that arrived through a binary column, a CSV import or a latin1 dump before it reaches a utf8mb4 column.
Rows loaded through a latin1 connection contain mojibake (ë°°ê²½ instead of 배경): the UTF-8 bytes were interpreted as Latin-1 on the way in. A charset change cannot repair those, because the bytes were already decoded with the wrong table. Detect them by the Latin-1 pairs that UTF-8 continuation bytes produce:
SELECT id, body FROM comments WHERE body REGEXP '[ÂÃìíîïð]';Those rows need re-importing from the original source with the correct connection charset.
5. Collation, uniqueness and sorting
CREATE TABLE tags (
name VARCHAR(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci,
UNIQUE KEY uk_name (name)
);utf8mb4_0900_ai_ci is accent-insensitive and case-insensitive, so Cafe, café and CAFE collide on the unique key. If users must be able to register Cafe and café separately, choose utf8mb4_0900_as_cs (accent and case sensitive). Changing the collation of an indexed column rebuilds the index and can surface duplicate-key errors that were previously impossible — check for duplicates before altering.
6. The application layer that keeps sending three-byte text
A Python or Node service that encodes with latin-1 produces the same error from the other side. Verify the exact bytes before blaming the server:
value = "😀"
print(value.encode("utf-8").hex()) # f09f9880
print(len(value.encode("utf-8"))) # 4If the driver receives four bytes and the column is utf8mb4, the insert succeeds. Comparing a stored value with what the application sent is easiest with a hash: SELECT SHA2(body, 256) against the same hash computed locally with the hash tool tells you whether the data was mangled in transit or in storage.
Common Compilation/Runtime Mistakes & Version Gotchas
Converting only the database default. Existing columns keep their old charset; ALTER TABLE ... CONVERT TO is the statement that rewrites them.
Assuming utf8 means UTF-8. In MySQL it means utf8mb3 forever; utf8mb4 is the only name that covers the full range. MySQL 8.0 still accepts utf8 as an alias for compatibility.
Widening the column but not the index. A conversion that pushes an index past 3072 bytes fails partway, leaving some tables converted. Run it table by table with a transaction and a backup.
Turning off strict mode to make the error go away. The data is then silently truncated, which is worse than a failed insert and much harder to detect later.
Version notes. MySQL 8.0 changed the server default to utf8mb4 and the default collation to utf8mb4_0900_ai_ci; MariaDB 10.6+ still defaults to utf8mb4_general_ci in many distributions, so behaviour differs between the two when a table is created without an explicit charset. utf8mb4_0900_* collations do not exist in MariaDB, which is why a dump taken from MySQL 8.0 fails to restore into MariaDB with "Unknown collation".
Reference material: Setting character sets and collations and Unicode.
Official references
primary documentationThe SQL documentation that defines the behaviour this guide explains.
Try matching browser tools
All toolsSmall utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.
Related SQL guides
All SQL guidesSQLite: database is locked (SQLITE_BUSY) under concurrent writes
SQLite allows one writer. Set a busy timeout, switch to WAL, start write transactions as IMMEDIATE, and reduce to a single writer connection.
SQL Window Functions: ROW_NUMBER, RANK, DENSE_RANK, and LEAD/LAG Explained
How the four ranking and offset functions differ on ties, why the default RANGE frame makes running totals repeat, and how LAG builds gap-and-island.
Postgres: column does not exist (SQLSTATE 42703)
42703 means the identifier you wrote is not the one that exists. Check case folding, alias scope, search_path and migration drift.
PostgreSQL: canceling statement due to statement timeout
The statement was canceled by statement_timeout. Find whether the role, the pooler or the client set it, then scope a longer budget to that job.
More in SQL
All SQL guidesThe guides filed next to this one in SQL, in library order.