Pages

Topics

Tools

↑↓navigate↵openescclose
All guides

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.

~5 min readSQL
#mysql#utf8mb4#charset#errors
Bold white headline reading INCORRECT STRING with a MySQL column crate rejecting a four byte amber character at a three byte gate.

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 1

Character sets matter per level, and the narrowest one wins:

CharsetBytes per characterStores emojiNotes
ascii1nolatin letters only
latin11nocp1252 in MySQL
utf8mb3 (alias utf8)up to 3nothe default until MySQL 8.0
ucs22noBMP only, rarely used
utf8mb4up to 4yesthe correct choice
binarybytesirrelevantno 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:

LevelCommandAffectsSafe online
ConnectionSET NAMES utf8mb4;this session's encodingyes, per session
Database defaultALTER DATABASE d CHARACTER SET utf8mb4;tables created afterwardsyes
Table defaultALTER TABLE t DEFAULT CHARACTER SET utf8mb4;columns added afterwardsyes
Existing columnsALTER TABLE t CONVERT TO CHARACTER SET utf8mb4;rewrites every text columnneeds a lock, plan it
Single columnALTER TABLE t MODIFY body VARCHAR(200) CHARACTER SET utf8mb4;one columnrebuilds that column

The error family around the same root cause:

ErrorMeaningFix direction
1366 Incorrect string valuevalue cannot be representedwiden the charset
1265 Data truncated for columnvalue fits, precision does notcheck length and strict mode
1300 Invalid utf8 character stringbytes are not valid UTF-8fix the client encoding
1071 Specified key was too longindex grew past 3072 bytes after conversionprefix index or shorter key
1366 only on some rowssource data has mixed encodingsinspect 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\G

CONVERT 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:

sql
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:

DriverSetting
MySQL Connector/JcharacterEncoding=UTF-8&connectionCollation=utf8mb4_0900_ai_ci
node mysql2charset: "utf8mb4" in the pool options
PyMySQLcharset="utf8mb4"
PHP PDOcharset=utf8mb4 in the DSN
Go go-sql-drivercharset=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:

python
value = "😀" print(value.encode("utf-8").hex()) # f09f9880 print(len(value.encode("utf-8"))) # 4

If 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 documentation

The SQL documentation that defines the behaviour this guide explains.

Try matching browser tools

All tools

Small utilities that fit SQL work — they run entirely in this browser tab, so nothing you paste leaves your device.

All SQL guides

More in SQL

All SQL guides

The guides filed next to this one in SQL, in library order.