欢迎来到代码驿站!

Mysql

当前位置:首页 > 数据库 > Mysql

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

时间:2021-07-15 09:32:19|栏目: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的区别

上一篇:解决Win7 x64安装解压版mysql 5.7.18 winx64出现服务无法启动问题

栏    目:Mysql

下一篇:MySQL中对于not in和minus使用的优化

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

本文地址:http://www.codeinn.net/misctech/157735.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有