如何将 MySQL 配置为区分大小写
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4879846/
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
How to Configure MySQL to be Case Sensitive
提问by Cerin
What are the commands to enable and disable case sensitivity by default for an entire database on a MySQL server? I'm aware of the COLLATEstatement, but it seems it's necessary to place this in every SQL statement that's run. Is there an option to set this globally?
默认情况下,为 MySQL 服务器上的整个数据库启用和禁用区分大小写的命令是什么?我知道COLLATE语句,但似乎有必要将它放在每个运行的 SQL 语句中。有没有可以全局设置的选项?
回答by John Parker
You can set collation at both the database creation and table creation level as a part of the CREATE TABLEstatement.
作为CREATE TABLE语句的一部分,您可以在数据库创建和表创建级别设置排序规则。
To set the collation for the entire database, you can use:
要为整个数据库设置排序规则,您可以使用:
CREATE DATABASE test_database CHARACTER SET utf8 COLLATE utf8_general_cs;
You can also change the collation on an existing database via ALTER DATABASE. (For more information see the MySQL Database Character Set and Collationmanual entry.)
您还可以通过 ALTER DATABASE 更改现有数据库的排序规则。(有关更多信息,请参阅 MySQL数据库字符集和排序规则手册条目。)
If however, only a single table needs to be treated as case sensitive, you could simply use:
但是,如果只需要将单个表视为区分大小写,则可以简单地使用:
DROP TABLE IF EXISTS test_table;
CREATE TABLE test_table (
test_id bigint unsigned NOT NULL auto_increment,
...
PRIMARY KEY test_id (test_id),
...
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE utf8_general_cs;
(Case insensitive being "utf8_general_ci
".)
(不区分大小写的“ utf8_general_ci
”。)
Finally, the main MySQL Character Set Supportmanual section is probably worth a quick peruse. (It lists the character sets and collations supported by MySQL, tells you how to set the character set/collation at the server level, etc.)
最后,主要的 MySQL字符集支持手册部分可能值得快速阅读。(它列出了MySQL 支持的字符集和排序规则,告诉您如何在服务器级别设置字符集/排序规则等)
回答by NullReference
I've come here looking to modify the collation only for a specific column for it to be case-sensitive and not for the entire table or the database. Hope it helps someone looking just for this.
我来这里只是想修改特定列的排序规则,使其区分大小写,而不是整个表或数据库。希望它可以帮助寻找这个的人。
This query could be tried:
可以尝试此查询:
ALTER TABLE table_name MODIFY column_name column_datatype COLLATE utf8_bin;
回答by Juan Jimenez
The most common way to create a MySQL database with case-sensitive collation is:
使用区分大小写的排序规则创建 MySQL 数据库的最常见方法是:
CREATE DATABASE your_db_name CHARACTER SET utf8 COLLATE utf8-bin;