MySQL数据类型char和varchar的区别

mysql定义varchar数据类型的长度是0到65535,注意单位是字节。 (65,535 bytes, which is shared among all columns),并且同一个表的
	所有的varchar列长度的总和不能超过65535.实际测试要比这个值还要小,原因是还有别的开销。(行溢出问题)
char数据类型的长度是0到255,单位是字符。注意varchar和char字符和字节的区别。字节还要受编码格式的限制,charset=utf8和gbk、latin1是不同的。

下面通过创建表的方式来验证上面的定义:
varchar类型:
	mysql> create table d (name varchar(65535)) charset=latin1 engine=innodb;		#65535报错,too large。
		ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs
	mysql> create table d (name varchar(65532)) charset=latin1 engine=innodb;		#调整成65532创建成功。注意此时的charset是latin1的编码。
		Query OK, 0 rows affected (0.18 sec)
	mysql> create table d (name varchar(65532)) charset=utf8 engine=innodb;			#改变字符编码是utf8时,创建表报错
		ERROR 1074 (42000): Column length too big for column 'name' (max = 21845); use BLOB or TEXT instead
	mysql> create table d (name varchar(65532)) charset=gbk engine=innodb;			#改变字符编码是gbk时,创建表报错
		ERROR 1074 (42000): Column length too big for column 'name' (max = 32767); use BLOB or TEXT instead
	mysql> select 21845*3;
	+---------+
	| 21845*3 |
	+---------+
	|   65535 |
	+---------+
	1 row in set (0.01 sec)
	
	mysql> select 32767*2;
	+---------+
	| 32767*2 |
	+---------+
	|   65534 |
	+---------+
	1 row in set (0.00 sec)
	
	mysql> create table d (name varchar(32767)) charset=gbk engine=innodb;	#即使创建表的列的长度指定为报错提示的值,还是无法创建,因为还有别的开销
		ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs
	mysql> create table d (name varchar(32762)) charset=gbk engine=innodb;
		Query OK, 0 rows affected (0.20 sec)
	mysql> create table d (name varchar(32766)) charset=gbk engine=innodb;
		Query OK, 0 rows affected (0.16 sec)
	mysql> create table d (name varchar(21845)) charset=utf8 engine=innodb;
		ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs
	mysql> create table d (name varchar(21844)) charset=utf8 engine=innodb;
		Query OK, 0 rows affected (0.14 sec)
	mysql> create table d (name1 varchar(65532),name2 varchar(65532)) charset=latin1 engine=innodb;		#创建两列指定长度,报错
		ERROR 1118 (42000): Row size too large. The maximum row size for the used table type, not counting BLOBs, is 65535. This includes storage overhead, check the manual. You have to change some columns to TEXT or BLOBs
	mysql> create table d (name1 varchar(32765),name2 varchar(32765)) charset=latin1 engine=innodb;		
		Query OK, 0 rows affected (0.12 sec)
char类型:无论编码是utf8还是gbk都可以创建,255指的是字符而不是字节长度。
	mysql> create table d (name char(255)) charset=utf8 engine=innodb;
		Query OK, 0 rows affected (0.16 sec)
	mysql> create table d (name char(255)) charset=gbk engine=innodb;
		Query OK, 0 rows affected (0.15 sec)

你可能感兴趣的:(MySQL)