MySQL 整理:latin1_swedish_ci 与 utf8_general_ci

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4769517/
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 18:26:33  来源:igfitidea点击:

MySQL Collation: latin1_swedish_ci Vs utf8_general_ci

mysqldatabase-designcollation

提问by laukok

What should I set for Collation when creating tables in MySQL:

在 MySQL 中创建表时,我应该为 Collat​​ion 设置什么:

  • latin1_swedish_cior utf8_general_ci
  • latin1_swedish_ci或者 utf8_general_ci

What is Collation anyway?

到底什么是整理?

I have been using latin1_swedish_ci, would it cause any problems?

我一直在用latin1_swedish_ci,会不会有什么问题?

回答by hobs

Whatever you do, don't try to use the default swedish_ci collation with utf8 (instead of latin) in mysql, or you'll get an error. Collations must be paired with the right charset to work. This SQL will fail because of the mismatch in charset and collation:

无论您做什么,都不要尝试在 mysql 中使用带有 utf8(而不是拉丁语)的默认 swedish_ci 排序规则,否则您将收到错误消息。排序规则必须与正确的字符集配对才能工作。由于字符集和排序规则不匹配,此 SQL 将失败:

CREATE  TABLE IF NOT EXISTS `db`.`events_user_preference` (
  `user_id` INT(10) UNSIGNED NOT NULL ,
  `email` VARCHAR(40) NULL DEFAULT NULL ,
  PRIMARY KEY (`user_id`) )
ENGINE = InnoDB
DEFAULT CHARACTER SET = utf8
COLLATE = latin1_swedish_ci

And @Blaisorblade pointed out that the way to fix this is to use the character set that goes with the swedish collation:

并且@Blaisorblade 指出解决这个问题的方法是使用与瑞典语排序规则一致的字符集:

DEFAULT CHARACTER SET = utf8_swedish_ci

The SQL for the cal (calendar) module for the Yii php framework had something similar to the above erroneous code. Hopefully they've fixed it by now.

Yii php 框架的cal(日历)模块的SQL 有类似上面的错误代码的东西。希望他们现在已经修复了它。

回答by aalanna

You can read about character sets and collations as of MySQL 5.5 here:
Character Sets and Collations in General
Character Sets and Collations in MySQL

您可以在此处阅读 MySQL 5.5 中的
字符集和排序规则:MySQL中的通用
字符集和排序规则中的字符集和排序规则

The collations support is necessary to support all the many written languages of the world. For instance in my language (Danish) we have a special character '?'. It sounds like Swedish, German, Hungarian (and more) '?' . That character also appears in Danish with words imported form one of those languages. Due to collations' support we can have both printed correctly and and the same sorted (ORDER BY ...) as being identical. Without collations support that was not possible.

排序规则支持对于支持世界上所有的许多书面语言是必要的。例如,在我的语言(丹麦语)中,我们有一个特殊字符“?”。听起来像是瑞典语、德语、匈牙利语(以及更多)'?' . 该字符也出现在丹麦语中,并从其中一种语言中导入单词。由于排序规则的支持,我们可以正确打印和相同的排序(ORDER BY ...)作为相同。没有校对支持,这是不可能的。

Swedish collations is the MySQL default for latin charsets. It works fine with English. English is so easy - it works with everything, because it has no special characters, accents etc. But if you have another language that you use often (for instance Spanish) you could change collation to a Spanish one, so sorting of Spanish Strings would be correct according to Spanish language rules.

瑞典语排序规则是拉丁字符集的 MySQL 默认设置。它适用于英语。英语是如此简单 - 它适用于所有事物,因为它没有特殊字符、重音等。但是如果您有另一种经常使用的语言(例如西班牙语),您可以将排序规则更改为西班牙语,因此西班牙语字符串的排序将根据西班牙语语言规则是正确的。

A very special example of a collation is one of the German ones. It was created to allowed for sorting like in German phone books. German phone books don't follow general rules of german language!

归类的一个非常特殊的例子是德国的一个。它的创建是为了允许像德国电话簿一样进行排序。德国电话簿不遵循德语的一般规则!

You can create your own collation if you like. Collations can be compiled or text-format.

如果您愿意,您可以创建自己的排序规则。排序规则可以编译或文本格式。

回答by Cburns

In Wamp Server 2.5 you can change the collation by going into PHPAdmin, selecting the database you need to change. This will give you another set of tabs. Select the Tab called Operations. In that tab will be section called collation, pick the one you want in the drop-down and select go.

在 Wamp Server 2.5 中,您可以通过进入 PHPAdmin,选择需要更改的数据库来更改排序规则。这将为您提供另一组选项卡。选择名为“操作”的选项卡。在该选项卡中将称为排序规则的部分,在下拉列表中选择您想要的选项并选择执行。

回答by user3914680

Try these:

试试这些:

<?php
echo htmlspecialchars($string);
echo htmlentities($string);
?>

You can see more info from http://php.net/manual/en/function.htmlspecialchars.php. :D

您可以从http://php.net/manual/en/function.htmlspecialchars.php查看更多信息。:D

Worked for me! No more diamonds :)

为我工作!没有更多的钻石:)