1. 程式人生 > >MySQL資料型別char與varchar中數字代表的究竟是位元組數還是字元數?

MySQL資料型別char與varchar中數字代表的究竟是位元組數還是字元數?

例項是最好的說明,所以,廢話少說,看錶看例子~

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)表示字元數!