欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

MySQL创建数据库并支持中文字符的操作方法

程序员文章站 2022-09-26 08:25:43
我们首先看一下mysql官方文档:create {database | schema} [if not exists] db_name [create_option] ...create_option...

我们首先看一下mysql官方文档:

create {database | schema} [if not exists] db_name
 [create_option] ...

create_option: [default] {
 character set [=] charset_name
 | collate [=] collation_name
}

通过查看mysql服务器的变量我们知道mysql默认创建数据库开启的字符集是latinl

MySQL创建数据库并支持中文字符的操作方法

现在我们需要给创建的数据库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的介绍参考这篇:

到此这篇关于mysql创建数据库并支持中文字符的操作方法的文章就介绍到这了,更多相关mysql支持中文字符内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!