MySQLデータ型charとvarcharの数字はバイト数ですか、文字数ですか.

12590 ワード

例は一番いい説明なので、くだらないことは言わないで、表を見て例を見てみましょう.
mysql> show create table test_varchar_utf8\G
*************************** 1. row ***************************
       Table: test_varchar_utf8
Create Table: CREATE TABLE `test_varchar_utf8` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(12) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
#  name       varchar     12,  12            ?

#            
mysql> insert into test_varchar_utf8(name)values('12345678901');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_varchar_utf8(name)values('123456789012');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_varchar_utf8(name)values('1234567890123');
ERROR 1406 (22001): Data too long for column 'name' at row 1
#  ,        ,      12       12       ,   12      。
#                    ,  ,       varchar(n)  n            

#                   
mysql> insert into test_varchar_utf8(name)values(' ');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_varchar_utf8(name)values('  ');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_varchar_utf8(name)values('   ');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_varchar_utf8(name)values('    ');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_varchar_utf8(name)values('      ');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_varchar_utf8(name)values('            ');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_varchar_utf8(name)values('             ');
ERROR 1406 (22001): Data too long for column 'name' at row 1
#  ,       ,       12       12       ,   12      。
#                   ,  ,  varchar(n)  n          。

#  char  ,        ,    
mysql> show create table test_char_utf8\G
*************************** 1. row ***************************
       Table: test_char_utf8
Create Table: CREATE TABLE `test_char_utf8` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` char(12) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.00 sec)

mysql> insert into test_char_utf8(name)values('12345678901');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_char_utf8(name)values('123456789012');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_char_utf8(name)values('1234567890123');
ERROR 1406 (22001): Data too long for column 'name' at row 1

mysql> insert into test_char_utf8(name)values('  ');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_char_utf8(name)values('   ');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_char_utf8(name)values('    ');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_char_utf8(name)values('      ');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_char_utf8(name)values('            ');
Query OK, 1 row affected (0.00 sec)

mysql> insert into test_char_utf8(name)values('             ');
ERROR 1406 (22001): Data too long for column 'name' at row 1

#        utf8                       
mysql> select id,name,length(name),char_length(name)  from test_varchar_utf8;
+----+--------------------------------------+--------------+-------------------+
| id | name                                 | length(name) | char_length(name) |
+----+--------------------------------------+--------------+-------------------+
|  1 | 12345678901                          |           11 |                11 |
|  2 | 123456789012                         |           12 |                12 |
|  3 |                                     |            3 |                 1 |
|  4 |                                    |            6 |                 2 |
|  5 |                                   |            9 |                 3 |
|  6 |                                  |           12 |                 4 |
|  7 |                                |           18 |                 6 |
|  8 |                          |           36 |                12 |
+----+--------------------------------------+--------------+-------------------+
8 rows in set (0.00 sec)

mysql> select id,name,length(name),char_length(name)  from test_char_utf8;
+----+--------------------------------------+--------------+-------------------+
| id | name                                 | length(name) | char_length(name) |
+----+--------------------------------------+--------------+-------------------+
|  1 | 12345678901                          |           11 |                11 |
|  2 | 123456789012                         |           12 |                12 |
|  3 |                                    |            6 |                 2 |
|  4 |                                   |            9 |                 3 |
|  5 |                                  |           12 |                 4 |
|  6 |                                |           18 |                 6 |
|  7 |                          |           36 |                12 |
+----+--------------------------------------+--------------+-------------------+
7 rows in set (0.00 sec)

もしあなたが例を最初から最後まで見たら、あなたはすでに深く理解していて、私が何を総括している必要はありません.はい、そうです.(n)は文字数を表します.