mysql中int和varchar的长度详解
2019/10/10/17:36:26 阅读:2225 来源:谷歌SEO算法 标签:
人工智能卓医
在mysql中一般人可能不会去问int与varchar区别了,这个估计一个数字类型,一个字符类型没有可比性了,同时他们长度也不一样,下面我来介绍一下mysql 中int和varchar的长度一些细节吧.
int:从 -2^31 (-2,147,483,648) 到 2^31 – 1 (2,147,483,647) 的整型数据(所有数字)。存储大小为 4 个字节。int 的 SQL-92 同义字为 integer
varcharL长度是0-255个字符.
mysql 字段中int后面所跟数字有何意义?varchar后的数字又有何意义?代码如下:
- mysql>createtablet(aint(1));
- QueryOK,0rowsaffected(0.10sec)
- mysql>insertintotvalues(123);
- QueryOK,1rowaffected(0.02sec)
- mysql>insertintotvalues(12345678);
- QueryOK,1rowaffected(0.03sec)
- mysql>select*fromt;
- +----------+
- |a|
- +----------+
- |123|
- |12345678|
- +----------+
可见,int(1)并不表示一个字节,如果更长的数字会不会报错?代码如下:
- mysql>insertintotvalues(1234567812345678);
- ERROR1264(22003):Outofrangevalueforcolumn'a'atrow1
- mysql>insertintotvalues(2147483648);
- ERROR1264(22003):Outofrangevalueforcolumn'a'atrow1
- mysql>insertintotvalues(2147483647);
- QueryOK,1rowaffected(0.03sec)
int 型长度最大值是2^31 -1,加上有符号数,应该是四个字节的长度,代码如下:
- mysql>altertabletaddcolumnbint;
- QueryOK,5rowsaffected(0.25sec)
- mysql>insertintotvalues(2147483647,2147483648);
- ERROR1264(22003):Outofrangevalueforcolumn'b'atrow1
- mysql>insertintotvalues(2147483647,2147483647);
- QueryOK,1rowaffected(0.03sec)
- mysql>select*fromt;
- +------------+------------+
- |a|b|
- +------------+------------+
- |123|NULL|
- |12345678|NULL|
- |65536|NULL|
- |1073741824|NULL|
- |2147483647|NULL|
- |2147483647|2147483647|
- +------------+------------+
- 6rowsinset(0.00sec)
可见,int后是否跟数字与最大值没有关系,再看char 和varchar后的数字,代码如下:
- mysql>altertabletaddcolumncchar(2);
- QueryOK,6rowsaffected(0.17sec)
- Records:6Duplicates:0Warnings:0
- mysql>altertabletaddcolumndvarchar(2);
- QueryOK,6rowsaffected(0.17sec)
- Records:6Duplicates:0Warnings:0
- mysql>desct;
- +-------+------------+------+-----+---------+-------+
- |Field|Type|Null|Key|Default|Extra|
- +-------+------------+------+-----+---------+-------+
- |a|int(1)|YES||NULL||
- |b|int(11)|YES||NULL||
- |c|char(2)|YES||NULL||
- |d|varchar(2)|YES||NULL||
- +-------+------------+------+-----+---------+-------+
- 4rowsinset(0.01sec)
- mysql>insertintotvalues(2147483647,2147483647,'abc','abc');
- ERROR1406(22001):Datatoolongforcolumn'c'atrow1
- mysql>insertintotvalues(2147483647,2147483647,'ab','abc');
- ERROR1406(22001):Datatoolongforcolumn'd'atrow1
- mysql>insertintotvalues(2147483647,2147483647,'ab','ab');
- QueryOK,1rowaffected(0.02sec)
可见char和varchar后的数字是存储长度,那么对于中文字,会不会有字节数大于字数而溢出的问题呢?代码如下:
- mysql>insertintotvalues(2147483647,2147483647,'ab','中化');
- QueryOK,1rowaffected(0.02sec)
- mysql>select*fromt;
- +------------+------------+------+------+
- |a|b|c|d|
- +------------+------------+------+------+
- |123|NULL|NULL|NULL|
- |12345678|NULL|NULL|NULL|
- |65536|NULL|NULL|NULL|
- |1073741824|NULL|NULL|NULL|
- |2147483647|NULL|NULL|NULL|
- |2147483647|2147483647|NULL|NULL|
- |2147483647|2147483647|ab|ab|
- |2147483647|2147483647|ab|中化|
- +------------+------------+------+------+
- 8rowsinset(0.00sec)
中文字也是包含在2个字内的,代码如下:
- mysql>insertintotvalues(2147483647,2147483647,'ab','中化a');
- ERROR1406(22001):Datatoolongforcolumn'd'atrow1
- mysql>insertintotvalues(2147483647,2147483647,'ab','?糸F');
- QueryOK,1rowaffected(0.05sec)
- mysql>select*fromt;
- +------------+------------+------+------+
- |a|b|c|d|
- +------------+------------+------+------+
- |123|NULL|NULL|NULL|
- |12345678|NULL|NULL|NULL|
- |65536|NULL|NULL|NULL|
- |1073741824|NULL|NULL|NULL|
- |2147483647|NULL|NULL|NULL|
- |2147483647|2147483647|NULL|NULL|
- |2147483647|2147483647|ab|ab|
- |2147483647|2147483647|ab|中化|
- |2147483647|2147483647|ab|?糸F|
- +------------+------------+------+------+
- 9rowsinset(0.01sec)
多一个字节都不行,看一下字符编码,mysql字符编码由服务器,数据库,表,字段四级组成,在windows下的mysql设置,代码如下:
- mysql>showvariableslike"character%";
- +--------------------------+--------------------------+
- |Variable_name|Value|
- +--------------------------+--------------------------+
- |character_set_client|gbk|
- |character_set_connection|gbk|
- |character_set_database|utf8|
- |character_set_filesystem|binary|
- |character_set_results|gbk|
- |character_set_server|utf8|
- |character_set_system|utf8|
- |character_sets_dir|D:mysqlsharecharsets|
- +--------------------------+--------------------------+
- 8rowsinset(0.00sec)//phpfensi.com
- mysql>showcreatetablet;
- t|CREATETABLE`t`(
- `a`int(1)DEFAULTNULL,
- `b`int(11)DEFAULTNULL,
- `c`char(2)DEFAULTNULL,
- `d`varchar(2)DEFAULTNULL
- ENGINE=InnoDBDEFAULTCHARSET=utf8
linux下centos设置,代码如下:
- mysql>showvariableslike"character%";
- +--------------------------+----------------------------+
- |Variable_name|Value|
- +--------------------------+----------------------------+
- |character_set_client|utf8|
- |character_set_connection|utf8|
- |character_set_database|utf8|
- |character_set_filesystem|binary|
- |character_set_results|utf8|
- |character_set_server|utf8|
- |character_set_system|utf8|
- |character_sets_dir|/opt/mysql/share/charsets/|
- +--------------------------+----------------------------+
- 8rowsinset(0.00sec)
- mysql>showfullfieldsfromt;
- mysql>showcreatedatabasetest;
- CREATEDATABASE`test`/*!40100DEFAULTCHARACTERSETutf8*/
字符编码是utf8,因此,对于utf8中文字符,varchar后面的数字,无论是中文还是英文,都表示相应的字数,不用担心截断.
热门评论