您好,登錄后才能下訂單哦!
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. 總結
以需求驅動技術,技術本身沒有優略之分,只有業務之分。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。