我們首先看一下MySQL官方文檔:5.7
CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
[create_option] ...
create_option: [DEFAULT] {
CHARACTER SET [=] charset_name
| COLLATE [=] collation_name
}
通過(guò)查看mysql服務(wù)器的變量我們知道m(xù)ysql默認(rèn)創(chuàng)建數(shù)據(jù)庫(kù)開(kāi)啟的字符集是latinl:
現(xiàn)在我們需要給創(chuàng)建的數(shù)據(jù)庫(kù)db2指定支持中文的字符集utf8,下面是一行的內(nèi)容哦!
CREATE DATABASE db2 IF NOT EXISTS db2
DEFAULT CHARACTER SET utf8
COLLATE utf_general_ci;
上面我們就完成支持插入中文數(shù)據(jù)的數(shù)據(jù)庫(kù)的創(chuàng)建,現(xiàn)在我們來(lái)思考一個(gè)問(wèn)題,支持中文的字符集就只有utf8,還有哪些呢?它們之間的區(qū)別是什么?
我們先來(lái)看一下mysql 5.7.x 支持的字符集以及字符規(guī)則:
mysql> SELECT version();
+-----------+
| version() |
+-----------+
| 5.7.32 |
+-----------+
1 row in set (0.00 sec)
mysql> SHOW CHARACTER SET;
+----------+---------------------------------+---------------------+--------+
| Charset | Description | Default collation | Maxlen |
+----------+---------------------------------+---------------------+--------+
| big5 | Big5 Traditional Chinese | big5_chinese_ci | 2 |
| dec8 | DEC West European | dec8_swedish_ci | 1 |
| cp850 | DOS West European | cp850_general_ci | 1 |
| hp8 | HP West European | hp8_english_ci | 1 |
| koi8r | KOI8-R Relcom Russian | koi8r_general_ci | 1 |
| latin1 | cp1252 West European | latin1_swedish_ci | 1 |
| latin2 | ISO 8859-2 Central European | latin2_general_ci | 1 |
| swe7 | 7bit Swedish | swe7_swedish_ci | 1 |
| ascii | US ASCII | ascii_general_ci | 1 |
| ujis | EUC-JP Japanese | ujis_japanese_ci | 3 |
| sjis | Shift-JIS Japanese | sjis_japanese_ci | 2 |
| hebrew | ISO 8859-8 Hebrew | hebrew_general_ci | 1 |
| tis620 | TIS620 Thai | tis620_thai_ci | 1 |
| euckr | EUC-KR Korean | euckr_korean_ci | 2 |
| koi8u | KOI8-U Ukrainian | koi8u_general_ci | 1 |
| gb2312 | GB2312 Simplified Chinese | gb2312_chinese_ci | 2 |
| greek | ISO 8859-7 Greek | greek_general_ci | 1 |
| cp1250 | Windows Central European | cp1250_general_ci | 1 |
| gbk | GBK Simplified Chinese | gbk_chinese_ci | 2 |
| latin5 | ISO 8859-9 Turkish | latin5_turkish_ci | 1 |
| armscii8 | ARMSCII-8 Armenian | armscii8_general_ci | 1 |
| utf8 | UTF-8 Unicode | utf8_general_ci | 3 |
| ucs2 | UCS-2 Unicode | ucs2_general_ci | 2 |
| cp866 | DOS Russian | cp866_general_ci | 1 |
| keybcs2 | DOS Kamenicky Czech-Slovak | keybcs2_general_ci | 1 |
| macce | Mac Central European | macce_general_ci | 1 |
| macroman | Mac West European | macroman_general_ci | 1 |
| cp852 | DOS Central European | cp852_general_ci | 1 |
| latin7 | ISO 8859-13 Baltic | latin7_general_ci | 1 |
| utf8mb4 | UTF-8 Unicode | utf8mb4_general_ci | 4 |
| cp1251 | Windows Cyrillic | cp1251_general_ci | 1 |
| utf16 | UTF-16 Unicode | utf16_general_ci | 4 |
| utf16le | UTF-16LE Unicode | utf16le_general_ci | 4 |
| cp1256 | Windows Arabic | cp1256_general_ci | 1 |
| cp1257 | Windows Baltic | cp1257_general_ci | 1 |
| utf32 | UTF-32 Unicode | utf32_general_ci | 4 |
| binary | Binary pseudo charset | binary | 1 |
| geostd8 | GEOSTD8 Georgian | geostd8_general_ci | 1 |
| cp932 | SJIS for Windows Japanese | cp932_japanese_ci | 2 |
| eucjpms | UJIS for Windows Japanese | eucjpms_japanese_ci | 3 |
| gb18030 | China National Standard GB18030 | gb18030_chinese_ci | 4 |
+----------+---------------------------------+---------------------+--------+
41 rows in set (0.00 sec)
gb2312、gbk、gb18030、utf8、utf8mb4 這四個(gè)字符集都是支持中文。
關(guān)于gb2312、gbk、gb18030的介紹參考知乎這篇:三種介紹
關(guān)于utf8、utf8mb4的介紹參考這篇:utf8與utf8mb4的區(qū)別
到此這篇關(guān)于MySQL創(chuàng)建數(shù)據(jù)庫(kù)并支持中文字符的操作方法的文章就介紹到這了,更多相關(guān)MySQL支持中文字符內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- 解決django 向mysql中寫(xiě)入中文字符出錯(cuò)的問(wèn)題
- centOS7下mysql插入中文字符報(bào)錯(cuò)問(wèn)題解決方法
- 在C#和MySQL中存取中文字符時(shí)避免亂碼的方法
- 解決mysql創(chuàng)建數(shù)據(jù)庫(kù)后出現(xiàn):Access denied for user ''root''@''%'' to database ''xxx''的問(wèn)題
- mysql創(chuàng)建本地用戶及賦予數(shù)據(jù)庫(kù)權(quán)限的方法示例