在 MySQL 中,我应该选择哪种排序规则?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1231105/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:47:49  来源:igfitidea点击:

In MySQL, which collation should I choose?

mysqlcollation

提问by bbtang

When I create a new MySQL database through phpMyAdmin, I have the option to choose the collation (e.g.-default, armscii8, ascii, ... and UTF-8). The one I know is UTF-8, since I always see this in HTML source code. But what is the default collation? What are the differences between these choices, and which one should I use?

当我通过 phpMyAdmin 创建一个新的 MySQL 数据库时,我可以选择排序规则(例如-default、armcii8、ascii、...和 ​​UTF-8)。我知道的是 UTF-8,因为我总是在 HTML 源代码中看到这一点。但是默认的排序规则是什么?这些选择之间有什么区别,我应该使用哪一个?

采纳答案by Eric

Collation is not actually the default, it's giving you the default collation as the first choice.

排序规则实际上并不是默认设置,而是将默认排序规则作为首选。

What we're talking about is collation, or the character set that your database will use in its text types. Your default option is usually based on regional settings, so unless you're planning to globalize, that's usually peachy-keen.

我们正在谈论的是collat​​ion,或者您的数据库将在其文本类型中使用的字符集。您的默认选项通常基于区域设置,因此除非您计划全球化,否则通常会非常热衷。

Collations also determine case and accent sensitivity (i.e.-Is 'Big' == 'big'? With a CI, it is). Check out the MySQL listfor all the options.

排序规则还确定大小写和重音敏感度(即,是 'Big' == 'big'?对于 CI,它是)。查看MySQL 列表以获取所有选项。

回答by ZZ Coder

Collation tells database how to perform string matching and sorting. It should match your charset.

排序规则告诉数据库如何执行字符串匹配和排序。它应该与您的字符集匹配。

If you use UTF-8, the collation should be utf8_general_ci. This will sort in Unicode order (case-insensitive) and it works for most languages. It also preserves ASCII and Latin1 order.

如果使用 UTF-8,则排序规则应为 utf8_general_ci。这将按 Unicode 顺序排序(不区分大小写)并且适用于大多数语言。它还保留了 ASCII 和 Latin1 顺序。

The default collation is normally latin1.

默认排序规则通常是 latin1。

回答by Chris Dev

Short answer: always use utf8mb4(specifically utf8mb4_unicode_ci) when dealing with collation in MySql & MariaDB.

简短回答:在处理 MySql 和 MariaDB 中的排序规则时始终使用utf8mb4(特别是utf8mb4_unicode_ci)。

Long answer:

长答案:

MySQL's utf8encoding is awkwardly named, as it's different from proper UTF-8 encoding. It doesn't offer full Unicode support, which can lead to data loss or security vulnerabilities.

Luckily, MySQL 5.5.3 (released in early 2010) introduced a new encoding called utf8mb4which maps to proper UTF-8 and thus fully supports Unicode.

MySQL 的utf8编码命名笨拙,因为它与正确的 UTF-8 编码不同。它不提供完整的 Unicode 支持,这可能会导致数据丢失或安全漏洞。

幸运的是,MySQL 5.5.3(于 2010 年初发布)引入了一种称为utf8mb4的新编码,它映射到正确的 UTF-8,因此完全支持 Unicode。

Read the full text here: https://mathiasbynens.be/notes/mysql-utf8mb4

在此处阅读全文:https: //mathiasbynens.be/notes/mysql-utf8mb4

As to which specific utf8mbto choose, go with utf8mb4_unicode_ciso that sorting is always handled properly with minimal/unnoticeable performance drawbacks. See more details here: What's the difference between utf8_general_ci and utf8_unicode_ci

至于具体utf8mb选择哪个,请使用,utf8mb4_unicode_ci以便始终正确处理排序,并具有最小/不明显的性能缺陷。在此处查看更多详细信息:utf8_general_ci 和 utf8_unicode_ci 有什么区别