Home >Database >Mysql Tutorial >MySQL 的 char 与 varchar_MySQL
今天发现,create table 时,MySQL 4.1有时会把 char 自动转换成 varchar
测试举例:
CREATE TABLE `varcharLessThan4` (`lastName` varchar(3)) ;mysql> desc varcharLessThan4;+----------+---------+------+-----+---------+-------+| Field| Type| Null | Key | Default | Extra |+----------+---------+------+-----+---------+-------+| lastName | char(3) | YES| | NULL| |+----------+---------+------+-----+---------+-------+1 row in set (0.01 sec)CREATE TABLE `charGreaterThan4` (`firstName` char(4),`lastName` varchar(4)) ;mysql> desc charGreaterThan4;+-----------+------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-----------+------------+------+-----+---------+-------+| firstName | varchar(4) | YES| | NULL| || lastName| varchar(4) | YES| | NULL| |+-----------+------------+------+-----+---------+-------+2 rows in set (0.00 sec)CREATE TABLE `charLessThan4` (`firstName` char(3),`lastName` varchar(4)) ;mysql> desc charLessThan4;+-----------+------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-----------+------------+------+-----+---------+-------+| firstName | char(3)| YES| | NULL| || lastName| varchar(4) | YES| | NULL| |+-----------+------------+------+-----+---------+-------+2 rows in set (0.00 sec)事实上, MySQL 5 之前有这样的规则:
Note that using CHAR will only speed up your access if the whole record is fixed size. That is, if you use any variable size object, you might as well make all of them variable size. You gain no speed by using a CHAR in a table that also contains a VARCHAR.因此,实际中可以统一使用 varchar 而不必考虑过多