Type
varchar vs vabinary
- varchar(N) 字符串类型,用于存储变长字符串,使用表默认或者指定的校验集合,其中N代表存储字符的个数(一个数字/英文字母/汉字占用一个字符)
- varbinary(N)二进制字符串类型,以 二进制字节串存储字符,无字符集校验区别,均以二进制实际数值作比较,其中N代表字节个数(一个数字/英文字母占用1个字节,一个汉字占用3个字节)
- varchar 可以存储字符串前191个字符的索引,
- varbinary 字段的索引则最多可以存储767字节。如果是英文字母则可以存储更长的字符串。
- varchar类型存储的列在比较的时候是通过字符集的方式进行的,varchar 中’ABAA’和’abaa’是一致的.
- varchar如果对应的数据库排序规则是utf8_general_ci,那么查询的时候将不区分大小写。如果排序规则是utf8_bin,则会区分大小写
- varbinary在排序规则utf8_general_ci下,是可以区分大小写的。
存储类型
数字类型
| Data Type | Storage Required | | —- | —- | |[TINYINT](https://dev.mysql.com/doc/refman/5.7/en/integer-types.html)
| 1 byte | |[SMALLINT](https://dev.mysql.com/doc/refman/5.7/en/integer-types.html)
| 2 bytes | |[MEDIUMINT](https://dev.mysql.com/doc/refman/5.7/en/integer-types.html)
| 3 bytes | |[INT](https://dev.mysql.com/doc/refman/5.7/en/integer-types.html)
,[INTEGER](https://dev.mysql.com/doc/refman/5.7/en/integer-types.html)
| 4 bytes | |[BIGINT](https://dev.mysql.com/doc/refman/5.7/en/integer-types.html)
| 8 bytes | |FLOAT()``_p_
| 4 bytes if 0 <=_p_
<= 24, 8 bytes if 25 <=_p_
<= 53 | |[FLOAT](https://dev.mysql.com/doc/refman/5.7/en/floating-point-types.html)
| 4 bytes | |DOUBLE [PRECISION]
,[REAL](https://dev.mysql.com/doc/refman/5.7/en/floating-point-types.html)
| 8 bytes | |DECIMAL(,)``_M_``_D_
,NUMERIC(,)``_M_``_D_
| Varies; see following discussion | |BIT()``_M_
| approximately (_M_
+7)/8 bytes |
数值区间
Type | Storage (Bytes) | Minimum Value Signed | Minimum Value Unsigned | Maximum Value Signed | Maximum Value Unsigned |
---|---|---|---|---|---|
TINYINT |
1 | -128 |
0 |
127 |
255 |
SMALLINT |
2 | -32768 |
0 |
32767 |
65535 |
MEDIUMINT |
3 | -8388608 |
0 |
8388607 |
16777215 |
INT |
4 | -2147483648 |
0 |
2147483647 |
4294967295 |
BIGINT |
8 | -263 |
0 |
263-1 |
264-1 |
Java应用
- Integer 最大值为21-4748-3647 与int Signed一致
日期时间类型
| Data Type | Storage Required Before MySQL 5.6.4 | Storage Required as of MySQL 5.6.4 | | —- | —- | —- | |[YEAR](https://dev.mysql.com/doc/refman/5.7/en/year.html)
| 1 byte | 1 byte | |[DATE](https://dev.mysql.com/doc/refman/5.7/en/datetime.html)
| 3 bytes | 3 bytes | |[TIME](https://dev.mysql.com/doc/refman/5.7/en/time.html)
| 3 bytes | 3 bytes + fractional seconds storage | |[DATETIME](https://dev.mysql.com/doc/refman/5.7/en/datetime.html)
| 8 bytes | 5 bytes + fractional seconds storage | |[TIMESTAMP](https://dev.mysql.com/doc/refman/5.7/en/datetime.html)
| 4 bytes | 4 bytes + fractional seconds storage |
字符串类型
类型 | 大小 | 用途 |
---|---|---|
CHAR | 0-255 bytes | 定长字符串 |
VARCHAR | 0-65535 bytes | 变长字符串 |
TINYBLOB | 0-255 bytes | 不超过 255 个字符的二进制字符串 |
TINYTEXT | 0-255 bytes | 短文本字符串 |
BLOB | 0-65 535 bytes | 二进制形式的长文本数据 |
TEXT | 0-65 535 bytes | 长文本数据 |
MEDIUMBLOB | 0-16 777 215 bytes | 二进制形式的中等长度文本数据 |
MEDIUMTEXT | 0-16 777 215 bytes | 中等长度文本数据 |
LONGBLOB | 0-4 294 967 295 bytes | 二进制形式的极大文本数据 |
LONGTEXT | 0-4 294 967 295 bytes | 极大文本数据 |