资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

MySQL数据类型--------字符串类型实战-创新互联

1. 背景

成都创新互联公司专注于丰满网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供丰满营销型网站建设,丰满网站制作、丰满网页设计、丰满网站官网定制、小程序制作服务,打造丰满网络公司原创品牌,更为您提供丰满网站排名全网营销落地服务。

 * MySQL支持的字符串类型有CHAR、VARCHAR、二进制、var二进制、BLOB、文本等等。

2. 字符串类型所需的存储和值范围

类型
说明N的含义是否有字符集大长度
CHAR(N)定义字符字符255
VARCHAR(N)变长字符字符16384
BINARY(N)定长二进制字节字节255
VARBINARY(N)
变长二进制字节字节16384
TINYBLOB
二进制大对象字节256
BLOB
二进制大对象字节16K
MEDIUMBLOB
二进制大对象字节

16M

LONGBLOB
二进制大对象字节4G
TINYTEXT
大对象字节256
TEXT
大对象字节16K
MEDUIMBLOB
大对象字节16M
LONGTEXT
大对象字节4G

3. 定义与变长区别 (CHAR VS VARCHAR)

CHAR(4)占用空间VARHCAR(4)占用空间
'''   '4 bytes''1 bytes
'ab''ab  '4 bytes'ab'3 bytes
'abcd''abcd'4 bytes'abcd'5 bytes
'abcdefgh'
'abcd'4 bytes'abcd'5 bytes

4. 字符串类型相关注意事项

  * 在BLOB和TEXT列上创建索引时,必须制定索引前缀的长度

  * VARCHAR和VARBINARY必须长度是可选的

  * BLOB和TEXT列不能有默认值

  * BLOB和TEXT列排序时只使用该列的前max_sort_length个字节

mysql> show variables like 'max_sort_length'; +-----------------+-------+ | Variable_name   | Value | +-----------------+-------+ | max_sort_length | 1024  | +-----------------+-------+ 1 row in set (0.01 sec)

5. 字符串类型与排序规则

* 查看字符串相关信息 [ Default collation: 默认排序规则 ]

mysql> show character set like 'utf8mb4'; +---------+---------------+--------------------+--------+ | Charset | Description   | Default collation  | Maxlen | +---------+---------------+--------------------+--------+ | utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci |      4 | +---------+---------------+--------------------+--------+ 1 row in set (0.01 sec)

* 查看utfmb4所有排序规则

mysql> show collation like 'utf8mb4%'; +------------------------+---------+-----+---------+----------+---------+ | Collation              | Charset | Id  | Default | Compiled | Sortlen | +------------------------+---------+-----+---------+----------+---------+ | utf8mb4_general_ci     | utf8mb4 |  45 | Yes     | Yes      |       1 | | utf8mb4_bin            | utf8mb4 |  46 |         | Yes      |       1 | | utf8mb4_unicode_ci     | utf8mb4 | 224 |         | Yes      |       8 | | utf8mb4_icelandic_ci   | utf8mb4 | 225 |         | Yes      |       8 | | utf8mb4_latvian_ci     | utf8mb4 | 226 |         | Yes      |       8 | | utf8mb4_romanian_ci    | utf8mb4 | 227 |         | Yes      |       8 | | utf8mb4_slovenian_ci   | utf8mb4 | 228 |         | Yes      |       8 | | utf8mb4_polish_ci      | utf8mb4 | 229 |         | Yes      |       8 | | utf8mb4_estonian_ci    | utf8mb4 | 230 |         | Yes      |       8 | | utf8mb4_spanish_ci     | utf8mb4 | 231 |         | Yes      |       8 | | utf8mb4_swedish_ci     | utf8mb4 | 232 |         | Yes      |       8 | | utf8mb4_turkish_ci     | utf8mb4 | 233 |         | Yes      |       8 | | utf8mb4_czech_ci       | utf8mb4 | 234 |         | Yes      |       8 | | utf8mb4_danish_ci      | utf8mb4 | 235 |         | Yes      |       8 | | utf8mb4_lithuanian_ci  | utf8mb4 | 236 |         | Yes      |       8 | | utf8mb4_slovak_ci      | utf8mb4 | 237 |         | Yes      |       8 | | utf8mb4_spanish3_ci    | utf8mb4 | 238 |         | Yes      |       8 | | utf8mb4_roman_ci       | utf8mb4 | 239 |         | Yes      |       8 | | utf8mb4_persian_ci     | utf8mb4 | 240 |         | Yes      |       8 | | utf8mb4_esperanto_ci   | utf8mb4 | 241 |         | Yes      |       8 | | utf8mb4_hungarian_ci   | utf8mb4 | 242 |         | Yes      |       8 | | utf8mb4_sinhala_ci     | utf8mb4 | 243 |         | Yes      |       8 | | utf8mb4_german2_ci     | utf8mb4 | 244 |         | Yes      |       8 | | utf8mb4_croatian_ci    | utf8mb4 | 245 |         | Yes      |       8 | | utf8mb4_unicode_520_ci | utf8mb4 | 246 |         | Yes      |       8 | | utf8mb4_vietnamese_ci  | utf8mb4 | 247 |         | Yes      |       8 | +------------------------+---------+-----+---------+----------+---------+ 26 rows in set (0.00 sec)

* 修改默认排序规则

mysql> set names utf8mb4 collate utf8mb4_bin; Query OK, 0 rows affected (0.00 sec) mysql> show character set like 'utf8mb4'; +---------+---------------+--------------------+--------+ | Charset | Description   | Default collation  | Maxlen | +---------+---------------+--------------------+--------+ | utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci |      4 | +---------+---------------+--------------------+--------+ 1 row in set (0.00 sec)

6. 总结

以需求驱动技术,技术本身没有优略之分,只有业务之分。

另外有需要云服务器可以了解下创新互联cdcxhl.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


当前名称:MySQL数据类型--------字符串类型实战-创新互联
网站路径:http://cdkjz.cn/article/dsdejg.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220