varchar(n)
n就是指你希望存放的最大字符數(shù)(一個漢字橄登、一個英文字母、一個數(shù)字都是一個字符)
The CHAR and VARCHAR types are declared with a length
that indicates the maximum number of characters you want to store.
For example, CHAR(30) can hold up to 30 characters.
n最多可以是多少讥此?
The length can be specified as a value from 0 to 65,535.
The effective maximum length of a VARCHAR is subject to
the maximum row size (65,535 bytes, which is shared
among all columns) and the character set used.
這里的限制是字節(jié)的限制,不僅要考慮列字段的長度限制赖临,還要考慮整個行數(shù)據(jù)的長度限制星立。在gbk編碼下爽茴,一個漢字最多占用2個字節(jié);在utf-8編碼下绰垂,一個漢字最多占用3個字節(jié)(有待考證室奏,不確定是3還是4)。
測試表
CREATE TABLE `test` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8
mysql> insert into test(name) values('abcdeabcdeabcdeabcde');
Query OK, 1 rows affected (0.04 sec)
mysql> insert into test(name) values('abcdeabcdeabcdeabcdea');
Data too long for column 'name' at row 1
mysql> insert into test(name) values('我是中國人我是中國人我是中國人我是中國人');
Query OK, 1 rows affected (0.08 sec)
mysql> insert into test(name) values('我是中國人我是中國人我是中國人我是中國人我');
Data too long for column 'name' at row 1
mysql> insert into test(name) values('12345678901234567890');
Query OK, 1 rows affected (0.05 sec)
mysql> insert into test(name) values('123456789012345678901');
Data too long for column 'name' at row 1
int(n)
int is always 4 bytes wide. The n is the "display width"
顯示寬度并不限制可以在列內(nèi)保存的值的范圍劲装,也不限制超過列的指定寬度的值的顯示胧沫。
在創(chuàng)建表的時候,如果確定不會有負值酱畅,最好指定為ungsighed琳袄,即無符號:0~4294967295;