MySQL創建數據庫並支持中文字符的操作方法

我們首先看一下MySQL官方文檔:5.7

CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name
 [create_option] ...

create_option: [DEFAULT] {
 CHARACTER SET [=] charset_name
 | COLLATE [=] collation_name
}

通過查看mysql服務器的變量我們知道mysql默認創建數據庫開啟的字符集是latinl

在這裡插入圖片描述

現在我們需要給創建的數據庫db2指定支持中文的字符集utf8,下面是一行的內容哦!

CREATE DATABASE db2 IF NOT EXISTS db2 
DEFAULT CHARACTER SET utf8 
COLLATE utf_general_ci;

上面我們就完成支持插入中文數據的數據庫的創建,現在我們來思考一個問題,支持中文的字符集就隻有utf8,還有哪些呢?它們之間的區別是什麼?
我們先來看一下mysql 5.7.x 支持的字符集以及字符規則:

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 這四個字符集都是支持中文。
關於gb2312、gbk、gb18030的介紹參考知乎這篇:三種介紹
關於utf8、utf8mb4的介紹參考這篇:utf8與utf8mb4的區別

到此這篇關於MySQL創建數據庫並支持中文字符的操作方法的文章就介紹到這瞭,更多相關MySQL支持中文字符內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: