MySQL UTF8 varchar column size

The MySQL documentation says that starting with 5.0, varchar lengths refer to units of characters, not bytes. However, I recently ran into a problem when I received truncated data warnings when inserting values ​​that should fit into the varchar columns that it designated.

I reproduced this problem with a simple table in version v5.1

mysql> show create table test\G
*************************** 1. row ***************************
Table: test
Create Table: CREATE TABLE `test` (
  `string` varchar(10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

Then I inserted a few 10 characters with different numbers of UTF8 characters

mysql> insert into test (string) values 
    -> ('abcdefghij'),
    -> ('ãáéíçãáéíç'),
    -> ('ãáéíç67890'),
    -> ('éíç4567890'),
    -> ('íç34567890');
Query OK, 5 rows affected, 4 warnings (0.06 sec)
Records: 5  Duplicates: 0  Warnings: 4

mysql> show warnings;
+---------+------+---------------------------------------------+
| Level   | Code | Message                                     |
+---------+------+---------------------------------------------+
| Warning | 1265 | Data truncated for column 'string' at row 2 |
| Warning | 1265 | Data truncated for column 'string' at row 3 |
| Warning | 1265 | Data truncated for column 'string' at row 4 |
| Warning | 1265 | Data truncated for column 'string' at row 5 |
+---------+------+---------------------------------------------+

mysql> select * from test;
+------------+
| string     |
+------------+
| abcdefghij |
| ãáéíç |
| ãáéíç |
| éíç4567 |
| íç345678 |
+------------+
5 rows in set (0.00 sec)

I think this shows that the varchar size is still specified in bytes or at least not exact in units of characters.

The question is, do I understand the documentation correctly and is this a mistake? Or am I misinterpreting the documentation?

+5
source share
1 answer

, VARCHAR CHAR , .

, latin1 ( ).

, UTF8 :

SET NAMES utf8

, UTF8 .

, .

+7

All Articles