简体   繁体   中英

Incorrect string value for column in MySQL with utf8

Running below query on MySQL workbench fails with Incorrect string value error.

insert into mytable (key) values (0x8080808080) gives me below error:
Error Code: 1366. Incorrect string value: '\x80\x80\x80\x80\x80' for column      'key' at row 1

Column data type is defined as char(5) and it uses table's default charset/collation ie, "utf8 - default collation". This query fails to insert any character value above 0x7F.

I want to understand why it fails to insert values above 0x7F. If i change charset/collation type to latin1_ _ , it works fine till characters 0xFF.

This query fails to insert any character value above 0x7F.

It's failing to insert a byte value above 0x7F. If you wanted to insert character U+0080 you would have to encode it as UTF-8 sequence 0xC280 . These bytes are above 0x7F but will insert OK because it's a valid UTF-8 sequence.

This is true for any encoding; 0x8080 is an invalid sequence of bytes in Shift-JIS too, so if you created a character-string column stored in sjis that value would fail top. latin1 , on the other hand, has no invalid byte sequences, so all bytes would happen to work there.

But if you want to store arbitrary bytes and don't care about characters and encodings, you should use a binary collation (eg VARBINARY column type) instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM