如果您对MySQL学习笔记——字符集和mysql中字符集感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解MySQL学习笔记——字符集的各种细节,并对mysql中字符集进行深入的分析,此外还有关于
如果您对MySQL 学习笔记 —— 字符集和mysql中字符集感兴趣,那么这篇文章一定是您不可错过的。我们将详细讲解MySQL 学习笔记 —— 字符集的各种细节,并对mysql中字符集进行深入的分析,此外还有关于2020 重新出发,MySql 基础,MySql 字符集、Ajax查询与dataType一起使用:’text’失败,dataType:’text / xml;字符集= UTF-8′、Azure AD B2C Graph 终结点是否支持用于上传策略的 UTF-8 字符集?、c# – HTTP / 1.1 415无法处理消息,因为内容类型为’application / json; charset = utf-8’不是预期的类型’text / xml;字符集= UTF-8′的实用技巧。
本文目录一览:- MySQL 学习笔记 —— 字符集(mysql中字符集)
- 2020 重新出发,MySql 基础,MySql 字符集
- Ajax查询与dataType一起使用:’text’失败,dataType:’text / xml;字符集= UTF-8′
- Azure AD B2C Graph 终结点是否支持用于上传策略的 UTF-8 字符集?
- c# – HTTP / 1.1 415无法处理消息,因为内容类型为’application / json; charset = utf-8’不是预期的类型’text / xml;字符集= UTF-8′
MySQL 学习笔记 —— 字符集(mysql中字符集)
字符值包含字母、数字和特殊符号。在字符值可以存储之前,字母、数字和字符必须转换为数值代码。所以必须建立一个转换表,其中包含了每个相关字符的数值代码。这样的转换表就称为字符集,有时也称为代码字符集(code character set)和字符编码(character encoding)。
要想让计算机处理字符,不仅需要字符到数值的映射,还要考虑如果存储这些数值,所以便诞生了编码方案的概念。是定长存储呢,还是变长存储?是用一个字节还是用多个字节?仁者见仁,智者见智。依据需要的不同,诞生了很多的编码方案。对于 Unicode,就存在 UTF-8、UTF-16、UTF-32。
而在 MySQL 中,字符集的概念和编码方案的概念被看作是同义词。一个字符集 (character set) 是一个转换表和一个编码方案的组合。校对(collation)的概念是为了解决排序的顺序或字符的分组问题。因为字符的排序和分组需要字符之间的比较,校对就定义了这些比较的大小关系。
显示可用的字符集
SHOW CHARACTER SET 或者 SELECT CHARACTER_SET_NAME,DESCRIPTION,DEFAULT_COLLATE_NAME,MAXLEN FROM INFORMATION_SCHEMA.CHARACTER_SETS
显示字符集 utf8 可用的校对
SHOW COLLATION LIKE ''utf8%'' 或者 SELECT * FROM INFOMATION_SCHEMA.COLLATIONS WHERE COLLATION_NAME LIKE ''utf8%''
很多时候,数据库中或客户端显示乱码是由于字符集没有设置正确,用 latin1 字符集显示 utf8 字符集的数据当然会出现问题。这时需要查看数据库、表和列的字符集是否是你想要的;客户端的字符集是否的当。
如下是字符集和校对的系统变量
系统变量 | 说明 |
CHARACTER_SET_CLIENT | 从客户机发送给服务器的语句的字符集 |
CHARACTER_SET_CONNECTION | 客户机和服务器连接的字符集 |
CHARACTER_SET_DATABASE | 当前数据库的默认字符集。每次使用 USE 语句来 “跳转” 到另一个数据库时,这个变量就会改变。如果没有当前数据库,其值为 CHARACTER_SET_SERVER |
CHARACTER_SET_RESULTS | 从服务器发送到客户机的 SELECT 语句的最终结果的字符集,包括列的值,列的元数据 —— 列名,错误信息 |
CHARACTER_SET_SERVER | 服务器的默认字符集 |
CHARACTER_SET_SYSTEM | 系统字符集。用于数据库中对象(如表和列)的名字,也用于存储在目录表中函数的名字。其值总是等于 utf8 |
CHARACTER_SET_DIR | 注册的所有字符的文件都在这个目录中 |
COLLATION_CONNECTION |
当前连接的校对 |
COLLATION_DATABASE | 当前日期的默认校对。每次使用 USE 语句来 “跳转” 到另一个数据库时,这个变量就会改变。 |
COLLATION_SERVER | 服务器默认校对 |
数据库对象的字符集的指定有如下继承关系:
Server -> Database -> Table -> Column
也就是说,如果后者没有显示指定字符集,那么将采用前者的字符集。
Server Character Set and Collation
MySQL Server has a server character set and a server collation. These can be set at server startup on the command line or in an option file and changed at runtime.
Initially, the server character set and collation depend on the options that you use when you start mysqld. You can use --character-set-server
for the character set. Along with it, you can add --collation-server
for the collation. If you don''t specify a character set, that is the same as saying --character-set-server=latin1
. If you specify only a character set (for example, latin1
) but not a collation, that is the same as saying --character-set-server=latin1
--collation-server=latin1_swedish_ci
because latin1_swedish_ci
is the default collation for latin1
. Therefore, the following three commands all have the same effect:
shell>mysqld
shell>mysqld --character-set-server=latin1
shell>mysqld --character-set-server=latin1 \
--collation-server=latin1_swedish_ci
The server character set and collation are used as default values if the database character set and collation are not specified in CREATE DATABASE
statements. They have no other purpose.
The current server character set and collation can be determined from the values of the character_set_server
and collation_server
system variables. These variables can be changed at runtime.
Database Character Set and Collation
Every database has a database character set and a database collation. The CREATE DATABASE
and ALTER DATABASE
statements have optional clauses for specifying the database character set and collation:
CREATE DATABASEdb_name
[[DEFAULT] CHARACTER SETcharset_name
] [[DEFAULT] COLLATEcollation_name
] ALTER DATABASEdb_name
[[DEFAULT] CHARACTER SETcharset_name
] [[DEFAULT] COLLATEcollation_name
]
The keyword SCHEMA
can be used instead of DATABASE
.
The database character set and collation are used as default values for table definitions if the table character set and collation are not specified in CREATE TABLE
statements. The database character set also is used by LOAD DATA INFILE
. The character set and collation have no other purposes.
The character set and collation for the default database can be determined from the values of thecharacter_set_database
and collation_database
system variables. The server sets these variables whenever the default database changes. If there is no default database, the variables have the same value as the corresponding server-level system variables, character_set_server
and collation_server
.
Table Character Set and Collation
Every table has a table character set and a table collation. The CREATE TABLE
and ALTER TABLE
statements have optional clauses for specifying the table character set and collation:
CREATE TABLEtbl_name
(column_list
) [[DEFAULT] CHARACTER SETcharset_name
] [COLLATEcollation_name
]] ALTER TABLEtbl_name
[[DEFAULT] CHARACTER SETcharset_name
] [COLLATEcollation_name
] The table character set and collation are used as default values for column definitions if the column character set and collation are not specified in individual column definitions. The table character set and collation are MySQL extensions; there are no such things in standard SQL.
Column Character Set and Collation
Every “character” column (that is, a column of type CHAR
, VARCHAR
, or TEXT
) has a column character set and a column collation. Column definition syntax for CREATE TABLE
and ALTER TABLE
has optional clauses for specifying the column character set and collation:
col_name
{CHAR | VARCHAR | TEXT} (col_length
) [CHARACTER SETcharset_name
] [COLLATEcollation_name
]
These clauses can also be used for ENUM
and SET
columns:
col_name
{ENUM | SET} (val_list
) [CHARACTER SETcharset_name
] [COLLATEcollation_name
]
Examples:
CREATE TABLE t1
(
col1 VARCHAR(5)
CHARACTER SET latin1
COLLATE latin1_german1_ci
);
ALTER TABLE t1 MODIFY
col1 VARCHAR(5)
CHARACTER SET latin1
COLLATE latin1_swedish_ci;
If you use ALTER TABLE
to convert a column from one character set to another, MySQL attempts to map the data values, but if the character sets are incompatible, there may be data loss.
转换字符集注意事项:
ALTER [IGNORE] TABLE table
CONVERT TO CHARACTER SET charset [COLLATE collation] | [DEFAULT]CHARACTER SET charset[COLLATE collation]
CONVERT 子句可能带来数据上的问题。因此,在使用该子句前,请确保做过备份并再完成前检查转换的数据。如果你有字符集列,在转换过程中数据有可能丢失,首先应该把该列转换为二进制大对象(BLOB)数据类型,接着转换成想要的数据类型和字符集。通常情况下,这种做法极好,因为 BLOB 数据不能转换字符集。
Character String Literal Character Set and Collation
Every character string literal has a character set and a collation.
A character string literal may have an optional character set introducer and COLLATE
clause:
[_charset_name
]''string
'' [COLLATEcollation_name
]
Examples:
SELECT ''string
''; SELECT _latin1''string
''; SELECT _latin1''string
'' COLLATE latin1_danish_ci;
For the simple statement SELECT ''
, the string has the character set and collation defined by thestring
''character_set_connection
and collation_connection
system variables.
The _
expression is formally called an introducer. It tells the parser, “the string that is about to follow uses character set charset_name
X
.” Because this has confused people in the past, we emphasize that an introducer does not change the string to the introducer character set like CONVERT()
would do. It does not change the string''s value, although padding may occur. The introducer is just a signal. An introducer is also legal before standard hex literal and numeric hex literal notation (x''
and literal
''0x
), or before bit-field literal notation (nnnn
b''
and literal
''0b
). nnnn
National Character Set
标准的 SQL 中使用 NCHAR,NVARCHAR 等表示国际字符集。但是 MySQL 不是,它只有 CHAR 和 VARCHAR。需要通过设置字符集来达到存储存储其他字符的目的。
For example, these data type declarations are equivalent:
CHAR(10) CHARACTER SET utf8
NATIONAL CHARACTER(10)
NCHAR(10)
As are these:
VARCHAR(10) CHARACTER SET utf8
NATIONAL VARCHAR(10)
NCHAR VARCHAR(10)
NATIONAL CHARACTER VARYING(10)
NATIONAL CHAR VARYING(10)
You can use N''
(or literal
''n''
) to create a string in the national character set. These statements are equivalent: literal
''
SELECT N''some text'';
SELECT n''some text'';
SELECT _utf8''some text'';
Connection Character Sets and Collations
Two statements affect the connection-related character set variables as a group:
-
SET NAMES ''
charset_name
'' [COLLATE ''collation_name
'']SET NAMES
indicates what character set the client will use to send SQL statements to the server. Thus,SET NAMES ''cp1251''
tells the server, “future incoming messages from this client are in character setcp1251
.” It also specifies the character set that the server should use for sending results back to the client. (For example, it indicates what character set to use for column values if you use aSELECT
statement.)A
SET NAMES ''
statement is equivalent to these three statements:x
''SET character_set_client =
x
; SET character_set_results =x
; SET character_set_connection =x
;Setting
character_set_connection
tox
also implicitly setscollation_connection
to the default collation forx
. It is unnecessary to set that collation explicitly. To specify a particular collation, use the optionalCOLLATE
clause:SET NAMES ''
charset_name
'' COLLATE ''collation_name
'' -
SET CHARACTER SET
charset_name
SET CHARACTER SET
is similar toSET NAMES
but setscharacter_set_connection
andcollation_connection
tocharacter_set_database
andcollation_database
. ASET CHARACTER SET
statement is equivalent to these three statements:x
SET character_set_client =
x
; SET character_set_results =x
; SET collation_connection = @@collation_database;Setting
collation_connection
also implicitly setscharacter_set_connection
to the character set associated with the collation (equivalent to executingSET character_set_connection = @@character_set_database
). It is unnecessary to setcharacter_set_connection
explicitly.
ucs2
, utf16
, and utf32
cannot be used as a client character set, which means that they do not work for SET NAMES
or SET CHARACTER SET
.
The MySQL client programs mysql
, mysqladmin
, mysqlcheck
, mysqlimport
, and mysqlshow
determine the default character set to use as follows:
-
In the absence of other information, the programs use the compiled-in default character set, usually
latin1
. -
The programs can autodetect which character set to use based on the operating system setting, such as the value of the
LANG
orLC_ALL
locale environment variable on Unix systems or the code page setting on Windows systems. For systems on which the locale is available from the OS, the client uses it to set the default character set rather than using the compiled-in default. For example, settingLANG
toru_RU.KOI8-R
causes thekoi8r
character set to be used. Thus, users can configure the locale in their environment for use by MySQL clients.The OS character set is mapped to the closest MySQL character set if there is no exact match. If the client does not support the matching character set, it uses the compiled-in default. For example,
ucs2
is not supported as a connection character set.C applications that wish to use character set autodetection based on the OS setting can invoke the following
mysql_options()
call before connecting to the server:mysql_options(mysql, MYSQL_SET_CHARSET_NAME, MYSQL_AUTODETECT_CHARSET_NAME);
-
The programs support a
--default-character-set
option, which enables users to specify the character set explicitly to override whatever default the client otherwise determines.
Before MySQL 5.5, in the absence of other information, the MySQL client programs used the compiled-in default character set, usually latin1
. An implication of this difference is that if your environment is configured to use a non-latin1
locale, MySQL client programs will use a different connection character set than previously, as though you had issued an implicit SET NAMES
statement. If the previous behavior is required, start the client with the --default-character-set=latin1
option.
When a client connects to the server, it sends the name of the character set that it wants to use. The server uses the name to set the character_set_client
, character_set_results
, and character_set_connection
system variables. In effect, the server performs a SET NAMES
operation using the character set name.
With the mysql client, if you want to use a character set different from the default, you could explicitly execute SET NAMES
every time you start up. However, to accomplish the same result more easily, you can add the --default-character-set
option setting to your mysql command line or in your option file. For example, the following option file setting changes the three connection-related character set variables set to koi8r
each time you invoke mysql:
[mysql]
default-character-set=koi8r
To see the values of the character set and collation system variables that apply to your connection, use these statements:
SHOW VARIABLES LIKE ''character_set%'';
SHOW VARIABLES LIKE ''collation%''; If you change the default character set or collation for a database, stored routines that use the database defaults must be dropped and recreated so that they use the new defaults. (In a stored routine, variables with character data types use the database defaults if the character set or collation are not specified explicitly. 校对命名规则
Collation Names
MySQL collation names follow these rules:
-
A name ending in
_ci
indicates a case-insensitive collation. -
A name ending in
_cs
indicates a case-sensitive collation. -
A name ending in
_bin
indicates a binary collation. Character comparisons are based on character binary code values.
Nonbinary strings have PADSPACE
behavior for all collations, including_bin
collations. Trailing spaces are insignificant in comparisons:(也就是说,字符串中末尾的空格不起作用)
mysql>SET NAMES utf8 COLLATE utf8_bin;
Query OK, 0 rows affected (0.00 sec) mysql>SELECT ''a '' = ''a'';
+------------+ | ''a '' = ''a'' | +------------+ | 1 | +------------+ 1 row in set (0.00 sec)
For binary strings, all characters are significant in comparisons, including trailing spaces:
mysql>SET NAMES binary;
Query OK, 0 rows affected (0.00 sec) mysql>SELECT ''a '' = ''a'';
+------------+ | ''a '' = ''a'' | +------------+ | 0 | +------------+ 1 row in set (0.00 sec)
The BINARY
Operator
The BINARY
operator casts the string following it to a binary string. This is an easy way to force a comparison to be done byte by byte rather than character by character. BINARY
also causes trailing spaces to be significant.
mysql>SELECT ''a'' = ''A'';
-> 1 mysql>SELECT BINARY ''a'' = ''A'';
-> 0 mysql>SELECT ''a'' = ''a '';
-> 1 mysql>SELECT BINARY ''a'' = ''a '';
-> 0
BINARY
is shorthand for str
CAST(
. str
AS BINARY)
The BINARY
attribute in character column definitions has a different effect. A character column defined with theBINARY
attribute is assigned the binary collation of the column character set. Every character set has a binary collation. For example, the binary collation for the latin1
character set is latin1_bin
, so if the table default character set is latin1
, these two column definitions are equivalent:
CHAR(10) BINARY
CHAR(10) CHARACTER SET latin1 COLLATE latin1_bin
Collation and INFORMATION_SCHEMA
Searches
String columns in INFORMATION_SCHEMA
tables have a collation of utf8_general_ci
, which is case insensitive. However, searches in INFORMATION_SCHEMA
string columns are also affected by file system case sensitivity. For values that correspond to objects that are represented in the file system, such as names of databases and tables, searches may be case sensitive if the file system is case sensitive. This section describes how to work around this issue if necessary; see also Bug #34921.
Suppose that a query searches the SCHEMATA.SCHEMA_NAME
column for the test
database. On Linux, file systems are case sensitive, so comparisons of SCHEMATA.SCHEMA_NAME
with ''test''
match, but comparisons with ''TEST''
do not:
mysql>SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA
->WHERE SCHEMA_NAME = ''test'';
+-------------+ | SCHEMA_NAME | +-------------+ | test | +-------------+ 1 row in set (0.01 sec) mysql>SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA
->WHERE SCHEMA_NAME = ''TEST'';
Empty set (0.00 sec)
On Windows or Mac OS X where file systems are not case sensitive, comparisons match both ''test''
and ''TEST''
:
mysql>SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA
->WHERE SCHEMA_NAME = ''test'';
+-------------+ | SCHEMA_NAME | +-------------+ | test | +-------------+ 1 row in set (0.00 sec) mysql>SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA
->WHERE SCHEMA_NAME = ''TEST'';
+-------------+ | SCHEMA_NAME | +-------------+ | TEST | +-------------+ 1 row in set (0.00 sec)
The value of the lower_case_table_names
system variable makes no difference in this context.
This behavior occurs because the utf8_general_ci
collation is not used for INFORMATION_SCHEMA
queries when searching the file system for database objects. It is a result of optimizations implemented for INFORMATION_SCHEMA
searches in MySQL. For information about these optimizations, see Section 7.2.4, “OptimizingINFORMATION_SCHEMA
Queries”.
Searches in INFORMATION_SCHEMA
string columns for values that refer to INFORMATION_SCHEMA
itself do use theutf8_general_ci
collation because INFORMATION_SCHEMA
is a “virtual” database and is not represented in the file system. For example, comparisons with SCHEMATA.SCHEMA_NAME
match ''information_schema''
or''INFORMATION_SCHEMA''
regardless of platform:
mysql>SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA
->WHERE SCHEMA_NAME = ''information_schema'';
+--------------------+ | SCHEMA_NAME | +--------------------+ | information_schema | +--------------------+ 1 row in set (0.00 sec) mysql>SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA
->WHERE SCHEMA_NAME = ''INFORMATION_SCHEMA'';
+--------------------+ | SCHEMA_NAME | +--------------------+ | information_schema | +--------------------+ 1 row in set (0.00 sec)
If the result of a string operation on an INFORMATION_SCHEMA
column differs from expectations, a workaround is to use an explicit COLLATE
clause to force a suitable collation (Section 9.1.7.2, “Using COLLATE
in SQL Statements”). For example, to perform a case-insensitive search, use COLLATE
with the INFORMATION_SCHEMA
column name:
mysql>SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA
->WHERE SCHEMA_NAME COLLATE utf8_general_ci = ''test'';
+-------------+ | SCHEMA_NAME | +-------------+ | test | +-------------+ 1 row in set (0.00 sec) mysql>SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA
->WHERE SCHEMA_NAME COLLATE utf8_general_ci = ''TEST'';
| SCHEMA_NAME | +-------------+ | test | +-------------+ 1 row in set (0.00 sec)
You can also use the UPPER()
or LOWER()
function:
WHERE UPPER(SCHEMA_NAME) = ''TEST''
WHERE LOWER(SCHEMA_NAME) = ''test''
详细 MySQL 字符集参考帮助手册:http://dev.mysql.com/doc/refman/5.5/en/globalization.html
2020 重新出发,MySql 基础,MySql 字符集

转:
2020 重新出发,MySql 基础,MySql 字符集
- MySQL 字符集和校对规则详解
- MySQL 字符集的转换过程
- MySQL 查看字符集和校对规则
- MySQL 设置默认字符集和校对规则
- 服务器字符集和校对规则
- 数据库字符集和校对规则
- 表字符集和校对规则
- 列字符集和校对规则
- 连接字符集和校对规则
- MySQL 字符集的选择
MySQL 字符集和校对规则详解
先来简单了解一下字符、字符集和字符编码。
字符(Character):是计算机中字母、数字、符号的统称,一个字符可以是一个中文汉字、一个英文字母、一个阿拉伯数字、一个标点符号等。
- 计算机是以二进制的形式来存储数据的。平时我们在显示器上看到的数字、英文、标点符号、汉字等字符都是二进制数转换之后的结果。
字符集(Character set):定义了字符和二进制的对应关系,为字符分配了唯一的编号。常见的字符集有 ASCII、GBK、IOS-8859-1 等。
字符编码(Character encoding):也可以称为字集码,规定了如何将字符的编号存储到计算机中。大部分字符集都只对应一种字符编码。
- 例如:ASCII、IOS-8859-1、GB2312、GBK,都是既表示了字符集又表示了对应的字符编码。所以一般情况下,可以将两者视为同义词。Unicode 字符集除外,Unicode 有三种编码方案,即 UTF-8、UTF-16 和 UTF-32。最为常用的是 UTF-8 编码。
校对规则(Collation):也可以称为排序规则,是指在同一个字符集内字符之间的比较规则。字符集和校对规则是一对多的关系,每个字符集都有一个默认的校对规则。字符集和校对规则相辅相成,相互依赖关联。
简单来说,字符集用来定义 MySQL 存储字符串的方式,校对规则用来定义 MySQL 比较字符串的方式。
有些数据库并没有清晰的区分开字符集和校对规则。
- 例如,在 SQL Server 中创建数据库时,选择字符集就相当于选定了字符集和校对规则。
而在 MySQL 中,字符集和校对规则是区分开的,必须设置字符集和校对规则。一般情况下,没有特殊需求,只设置其一即可。只设置字符集时,MySQL 会将校对规则设置为字符集中对应的默认校对规则。
可以通过 SHOW VARIABLES LIKE ''character%'';
命令查看当前 MySQL 使用的字符集,命令和运行结果如下:
mysql> SHOW VARIABLES LIKE ''character%'';
+--------------------------+---------------------------------------------------------+
| Variable_name | Value |
+--------------------------+---------------------------------------------------------+
| character_set_client | gbk |
| character_set_connection | gbk |
| character_set_database | latin1 |
| character_set_filesystem | binary |
| character_set_results | gbk |
| character_set_server | latin1 |
| character_set_system | utf8 |
| character_sets_dir | C:Program FilesMySQLMySQL Server 5.7sharecharsets |
+--------------------------+---------------------------------------------------------+
8 rows in set, 1 warning (0.01 sec)
上述运行结果说明如下表所示:
- character_set_client :MySQL 客户端使用的字符集
- character_set_connection :连接数据库时使用的字符集
- character_set_database : 创建数据库使用的字符集
- character_set_filesystem :MySQL 服务器文件系统使用的字符集,默认值为 binary,不做任何转换
- character_set_results : 数据库给客户端返回数据时使用的字符集
- character_set_server : MySQL 服务器使用的字符集,建议由系统自己管理,不要人为定义
- character_set_system :数据库系统使用的字符集,默认值为 utf8,不需要设置 character_sets_dir 字符集的安装目录
注意:乱码时,不需要关心 character_set_filesystem、character_set_system 和 character_sets_dir 这 3 个系统变量,它们不会影响乱码 。
可以通过 SHOW VARIABLES LIKE ''collation_%'';
命令查看当前 MySQL 使用的校对规则,命令和运行结果如下:
mysql> SHOW VARIABLES LIKE ''collation_%'';
+----------------------+-------------------+
| Variable_name | Value |
+----------------------+-------------------+
| collation_connection | gbk_chinese_ci |
| collation_database | latin1_swedish_ci |
| collation_server | latin1_swedish_ci |
+----------------------+-------------------+
3 rows in set, 1 warning (0.01 sec)
对上述运行结果说明如下:
- collation_connection:连接数据库时使用的校对规则
- collation_database:创建数据库时使用的校对规则
- collation_server:MySQL 服务器使用的校对规则
校对规则命令约定如下:
- 以校对规则所对应的字符集名开头
- 以国家名居中(或以 general 居中)
- 以 ci、cs 或 bin 结尾,ci 表示大小写不敏感,cs 表示大小写敏感,bin 表示按二进制编码值比较。
MySQL 字符集的转换过程
MySQL 中字符集的转换过程如下:
- 在命令提示符窗口(cmd 命令行)中执行 MySQL 命令或 sql 语句时,这些命令或语句从 “命令提示符窗口字符集” 转换为 “character_set_client” 定义的字符集。
- 使用命令提示符窗口成功连接 MySQL 服务器后,就建立了一条 “数据通信链路”,MySQL 命令或 sql 语句沿着 “数据链路” 传向 MySQL 服务器,由 character_set_client 定义的字符集转换为 character_set_connection 定义的字符集。
- MySQL 服务实例收到数据通信链路中的 MySQL 命令或 sql 语句后,将 MySQL 命令或 sql 语句从 character_set_connection 定义的字符集转换为 character_set_server 定义的字符集。
- 若 MySQL 命令或 sql 语句针对于某个数据库进行操作,此时将 MySQL 命令或 sql 语句从 character_set_server 定义的字符集转换为 character_set_database 定义的字符集。
- MySQL 命令或 sql 语句执行结束后,将执行结果设置为 character_set_results 定义的字符集。
- 执行结果沿着打开的数据通信链路原路返回,将执行结果从 character_set_results 定义的字符集转换为 character_set_client 定义的字符集,最终转换为命令提示符窗口字符集,显示到命令提示符窗口中。
MySQL 查看字符集和校对规则
在 MySQL 中,查看可用字符集的命令和执行过程如下:
SHOW CHARACTER set
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.02 sec)
其中:
- 第一列(Charset)为字符集名称;
- 第二列(Description)为字符集描述;
- 第三列(Default collation)为字符集的默认校对规则;
- 第四列(Maxlen)表示字符集中一个字符占用的最大字节数。
常用的字符集如下:
- latin1 支持西欧字符、希腊字符等。
- gbk 支持中文简体字符。
- big5 支持中文繁体字符。
- utf8 几乎支持所有国家的字符。
也可以通过查询 information_schema.character_set 表中的记录,来查看 MySQL 支持的字符集。SQL 语句和执行过程如下:
mysql> SELECT * FROM information_schema.character_sets;
+--------------------+----------------------+---------------------------------+--------+
| CHARACTER_SET_NAME | DEFAULT_COLLATE_NAME | DESCRIPTION | MAXLEN |
+--------------------+----------------------+---------------------------------+--------+
| big5 | big5_chinese_ci | Big5 Traditional Chinese | 2 |
| dec8 | dec8_swedish_ci | DEC West European | 1 |
| cp850 | cp850_general_ci | DOS West European | 1 |
| hp8 | hp8_english_ci | HP West European | 1 |
......
可以使用 SHOW COLLATION LIKE ''***'';
命令来查看相关字符集的校对规则。
mysql> SHOW COLLATION LIKE ''gbk%'';
+----------------+---------+----+---------+----------+---------+
| Collation | Charset | Id | Default | Compiled | Sortlen |
+----------------+---------+----+---------+----------+---------+
| gbk_chinese_ci | gbk | 28 | Yes | Yes | 1 |
| gbk_bin | gbk | 87 | | Yes | 1 |
+----------------+---------+----+---------+----------+---------+
2 rows in set (0.00 sec)
上面运行结果为 GBK 字符集所对应的校对规则,其中 gbk_chinese_ci 是默认的校对规则,对大小写不敏感。而 gbk_bin 按照二进制编码的值进行比较,对大小写敏感。
也可以通过查询 information_schema.COLLATIONS 表中的记录,来查看 MySQL 中可用的校对规则。SQL 语句和执行过程如下:
mysql> SELECT * FROM information_schema.COLLATIONS;
+--------------------------+--------------------+-----+------------+-------------+---------+
| COLLATION_NAME | CHARACTER_SET_NAME | ID | IS_DEFAULT | IS_COMPILED | SORTLEN |
+--------------------------+--------------------+-----+------------+-------------+---------+
| big5_chinese_ci | big5 | 1 | Yes | Yes | 1 |
| big5_bin | big5 | 84 | | Yes | 1 |
| dec8_swedish_ci | dec8 | 3 | Yes | Yes | 1 |
| dec8_bin | dec8 | 69 | | Yes | 1 |
| cp850_general_ci | cp850 | 4 | Yes | Yes | 1 |
| cp850_bin | cp850 | 80 | | Yes | 1 |
......
在实际应用中,我们应事先确认应用需要按照什么样方式排序,是否区分大小写,然后选择相应的校对规则。
MySQL 设置默认字符集和校对规则
MySQL 服务器可以支持多种字符集,在同一台服务器、同一个数据库甚至同一个表的不同字段中,都可以使用不同的字符集。Oracle 等其它数据库管理系统都只能使用相同的字符集,相比之下,MySQL 明显存在更大的灵活性。
MySQL 的字符集和校对规则有 4 个级别的默认设置,即服务器级、数据库级、表级和字段级。它们分别在不同的地方设置,作用也不相同。
服务器字符集和校对规则
修改服务器默认字符集和校对规则的方法如下。
可以在 my.ini 配置文件中设置服务器字符集和校对规则,添加内容如下:
[mysqld]
character-set-server=字符集名称
连接 MySQL 服务器时指定字符集:
mysql --default-character-set=字符集名称 -h 主机IP地址 -u 用户名 -p 密码
如果没有指定服务器字符集,MySQL 会默认使用 latin1 作为服务器字符集。如果只指定了字符集,没有指定校对规则,MySQL 会使用该字符集对应的默认校对规则。如果要使用字符集的非默认校对规则,需要在指定字符集的同时指定校对规则。
可以用 SHOW VARIABLES LIKE ''character_set_server'' 和 SHOW VARIABLES LIKE ''collation_server'' 命令查询当前服务器的字符集和校对规则。
mysql> SHOW VARIABLES LIKE ''character_set_server'';
+----------------------+--------+
| Variable_name | Value |
+----------------------+--------+
| character_set_server | gbk |
+----------------------+--------+
1 row in set, 1 warning (0.01 sec)
mysql> SHOW VARIABLES LIKE ''collation_server'';
+------------------+-------------------+
| Variable_name | Value |
+------------------+-------------------+
| collation_server | gbk_chinese_ci |
+------------------+-------------------+
1 row in set, 1 warning (0.01 sec)
数据库字符集和校对规则
数据库的字符集和校对规则在创建数据库时指定,也可以在创建完数据库后通过 ALTER DATABASE 命令进行修改
需要注意的是,如果数据库里已经存在数据,修改字符集后,已有的数据不会按照新的字符集重新存放,所以不能通过修改数据库的字符集来修改数据的内容。
设置数据库字符集的规则如下:
- 如果指定了字符集和校对规则,则使用指定的字符集和校对规则;
- 如果指定了字符集没有指定校对规则,则使用指定字符集的默认校对规则;
- 如果指定了校对规则但未指定字符集,则字符集使用与该校对规则关联的字符集;
- 如果没有指定字符集和校对规则,则使用服务器字符集和校对规则作为数据库的字符集和校对规则。
为了避免受到默认值的影响,推荐在创建数据库时指定字符集和校对规则。
可以使用 SHOW VARIABLES LIKE ''character_set_database'' 和 SHOW VARIABLES LIKE ''collation_database'' 命令查看当前数据库的字符集和校对规则。
mysql> SHOW VARIABLES LIKE ''character_set_database'';
+------------------------+--------+
| Variable_name | Value |
+------------------------+--------+
| character_set_database | latin1 |
+------------------------+--------+
1 row in set, 1 warning (0.00 sec)
mysql> SHOW VARIABLES LIKE ''collation_database'';
+--------------------+-------------------+
| Variable_name | Value |
+--------------------+-------------------+
| collation_database | latin1_swedish_ci |
+--------------------+-------------------+
1 row in set, 1 warning (0.00 sec)
表字符集和校对规则
表的字符集和校对规则在创建表的时候指定,也可以在创建完表后通过 ALTER TABLE 命令进行修改。
同样,如果表中已有记录,修改字符集后,原有的记录不会按照新的字符集重新存放。表的字段仍然使用原来的字符集。
设置表的字符集规则和设置数据库字符集的规则基本类似:
- 如果指定了字符集和校对规则,使用指定的字符集和校对规则;
- 如果指定了字符集没有指定校对规则,使用指定字符集的默认校对规则;
- 如果指定了校对规则但未指定字符集,则字符集使用与该校对规则关联的字符集;
- 如果没有指定字符集和校对规则,使用数据库字符集和校对规则作为表的字符集和校对规则。
为了避免受到默认值的影响,推荐在创建表的时候指定字符集和校对规则。
可以使用 SHOW CREATE TABLE 命令查看当前表的字符集和校对规则,SQL 语句和运行结果如下:
mysql> SHOW CREATE TABLE tb_students_info G
*************************** 1. row ***************************
Table: tb_students_info
Create Table: CREATE TABLE `tb_students_info` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(10) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
`sex` char(1) DEFAULT NULL,
`height` float DEFAULT NULL,
`course_id` int(11) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=12 DEFAULT CHARSET=utf8
1 row in set (0.00 sec)
列字符集和校对规则
MySQL 可以定义列级别的字符集和校对规则,主要是针对相同表的不同字段需要使用不同字符集的情况。一般遇到这种情况的几率比较小,这只是 MySQL 提供给我们一个灵活设置的手段。
列字符集和校对规则的定义可以在创建表时指定,或者在修改表时调整。语法格式如下:
ALTER TABLE 表名 MODIFY 列名 数据类型 CHARACTER SET 字符集名;
如果在创建列的时候没有特别指定字符集和校对规则,默认使用表的字符集和校对规则。
连接字符集和校对规则
上面所讲的 4 种设置方式,确定的都是数据保存的字符集和校对规则。实际应用中,还需要设置客户端和服务器之间交互的字符集和校对规则。
对于客户端和服务器的交互操作,MySQL 提供了 3 个不同的参数:character_set_client、character_set_connection 和 character_set_results,分别代表客户端、连接和返回结果的字符集。通常情况下,这 3 个字符集是相同的,这样可以确保正确读出用户写入的数据,尤其是中文字符。字符集不同时,容易导致写入的记录不能正确读出。
设置客户端和服务器连接的字符集和校对规则有以下几种方法:
- 在 my.ini 配置文件中,设置以下语句:
[mysql]
default-character-set=gbk
这样服务器启动后,所有连接默认使用 GBK 字符集进行连接。
- 可以通过以下命令来设置连接的字符集和校对规则,这个命令可以同时修改以上 3 个参数(character_set_client、character_set_connection 和 character_set_results)的值。
SET NAMES gbk;
使用这个方法可以 “临时一次性地” 修改客户端和服务器连接时的字符集为 gbk。
- MySQL 还提供了下列 MySQL 命令 “临时地” 修改 MySQL “当前会话的” 字符集和校对规则。
set character_set_client = gbk;
set character_set_connection = gbk;
set character_set_database = gbk;
set character_set_results = gbk;
set character_set_server = gbk;
set collation_connection = gbk_chinese_ci;
set collation_database = gbk_chinese_ci;
set collation_server = gbk_chinese_ci;
MySQL 字符集的选择
由于数据库中存储的数据大部分都是各种文字,所以字符集对数据库的存储、处理性能,以及日后系统的移植、推广都会有影响。对数据库来说,字符集非常重要。不论是在 MySQL 数据库还是其它数据库,都存在字符集的选择问题。
如果在创建数据库时没有正确选择字符集,在后期就可能需要更换字符集,而更换字符集是代价比较高的操作,也存在一定的风险。所以推荐在应用开始阶段,就按照实际需求,正确的选择合适的字符集,避免后期不必要的调整。
目前 MySQL 5.7 支持几十种字符集,包括 UCS-2、UTF-16、UTF-16LE、UTF-32、 UTF-8 和 utf8mb4 等 Unicode 字符集。
那么面对众多的字符集,我们该如何选择呢?
在选择数据库字符集时,可以根据应用的需求,结合字符集的特点来权衡,主要考虑以下几方面的因素。
- 满足应用支持语言的需求。如果应用要处理各种各样的文字,或者将发布到使用不同语言的国家或地区,就应该选择 Unicode 字符集。对 MySQL 来说,目前就是 UTF-8。
- 如果应用中涉及已有数据的导入,就要充分考虑数据库字符集对已有数据的兼容性。假如已有数据的字符集是 GBK,如果选择 GB 2312-80 为数据库字符集,就很可能出现某些文字无法正确导入。
- 如果数据库只需要支持一般中文,数据量很大,性能要求也很高,那就应该选择双字节定长编码的中文字符集,比如 GBK。
- 因为,相对于 UTF-8 而言,GBK 比较 “小”,每个汉字只占 2 个字节,而 UTF-8 汉字编码需要 3 个字节,这样可以减少磁盘 I/O、数据库 Cache 以及网络传输的时间,从而提高性能。相反,如果应用主要处理英文字符,仅有少量汉字数据,那么选择 UTF-8 更好,因为 GBK、UCS-2、UTF-16 的西文字符编码都是 2 个字节,会造成很多不必要的开销。
- 如果数据库需要做大量的字符运算,如比较、排序等,那么选择定长字符集可能更好,因为定长字符集的处理速度要比变长字符集的处理速度快。
- 如果所有客户端程序都支持相同的字符集,则应该优先选择该字符集作为数据库字符集。这样可以避免因字符集转换带来的性能开销和数据损失。
- 在多种字符集都能够满足应用的前提下,应尽量使用小的字符集。因为更小的字符集意味着能够节省空间、减少网络传输字节数,同时由于存储空间的较小间接的提高了系统的性能。
注意:有很多字符集都可以保存汉字,比如 UTF-8、GB2312、GBK、Latin1 等等。但是常用的是 GB2312 和 GBK。因为 GB2312 字库比 GBK 字库小,有些偏僻字(例如:洺)不能保存,因此在选择字符集的时候一定要权衡这些偏僻字出现的几率,一般情况下,最好选用 GBK。
转:
2020 重新出发,MySql 基础,MySql 字符集
--Posted from Rpc
Ajax查询与dataType一起使用:’text’失败,dataType:’text / xml;字符集= UTF-8′
> xml响应是有效的xml
>响应标题显示Content-Type为’text / xml;
字符集= UTF-8′ .
>这不是跨域请求
这三个问题是其他parsererror问题的答案.
我的ajax看起来像这样:
$('#submitLogin2').click(function (e) { e.preventDefault(); var formData = $('#loginForm2').serialize(); var url = 'http://somewhere.com/Api2.0/Session_Create.aspx'; $.ajax({ url: url,type: "POST",dataType: 'text/xml; charset=utf-8',data: formData,contentType: 'application/x-www-form-urlencoded; charset=UTF-8',success: function (data) { $('#loginResult').html(data.replace(/&/g,'&').replace(/</g,'<').replace(/>/g,'>').replace(/ /g,' ').replace(/\n/g,'<br />')); },error: function (textStatus,errorThrown) { alert(errorThrown); alert(JSON.stringify(textStatus)); } }); });
响应是:
<Response><Error code='0'>Invalid User Name or Password</Error></Response>
“text”请求很有用……但是让Ajax为我解析xml会很好.有关如何使其工作的任何想法?
将查询更改为以下内容应该会给您预期的结果:
url: url,dataType: 'xml',
Azure AD B2C Graph 终结点是否支持用于上传策略的 UTF-8 字符集?
如何解决Azure AD B2C Graph 终结点是否支持用于上传策略的 UTF-8 字符集??
我有一个自定义策略 XML 文档,其中包含需要正确呈现 UTF-8 的字符(库拉索岛的国家/地区名称)
当我手动运行使用 UTF-8 的 CI 脚本并将策略上传到 Azure B2C 的 UI 时,我看到了预期的字符。
但是,尽管使用 UTF-8 编码读取文件,但当我为 PUT 请求调用 graph.microsoft.com/beta/trustframework/policies/
端点时,结果策略似乎未使用 UTF-8 呈现。
我尝试了以下内容标题
-
$headers.Add("Content-Type",''application/xml'')
-- 原来,我第一次看到问题的地方 -
$headers.Add("Content-Type",''application/xml; charset=UTF-8'')
-- 没有变化 -
$headers.Add("Content-Type",''application/xml; charset="UTF-8"'')
-- 来自服务器的 500 错误 -
$headers.Add("Content-Type",''application/xml; charset=utf-8'')
-- 问题仍然存在。
有什么想法吗?我确定它一定是我遗漏的东西。
我正在做的完整 powershell 摘录:
$headers = New-Object "System.Collections.Generic.Dictionary[[String],[String]]"
$headers.Add("Content-Type",''application/xml; charset=utf-8'')
$headers.Add("Authorization",''Bearer '' + $token)
$graphuri = ''https://graph.microsoft.com/beta/trustframework/policies/'' + $PolicyId + ''/$value''
$policycontent = Get-Content $PathToFile -Encoding UTF8
$response = Invoke-RestMethod -Uri $graphuri -Method Put -Body $policycontent -Headers $headers
结果图像:
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)
c# – HTTP / 1.1 415无法处理消息,因为内容类型为’application / json; charset = utf-8’不是预期的类型’text / xml;字符集= UTF-8′
对象类
[DataContract] public class Class1 { [DataMember] public string AccNo; [DataMember] public string CompanyName; [DataMember] public string DocDate; }
IService1.cs
[OperationContract] [WebInvoke(Method = "POST",UriTemplate = "json/PostSalesOrderData",RequestFormat = Webmessageformat.Json,ResponseFormat = Webmessageformat.Json,BodyStyle = WebMessageBodyStyle.Bare)] string PostSalesOrderData(string data);
Service1.svc.cs
public string PostSalesOrderData(string data) { JavaScriptSerializer serializer = new JavaScriptSerializer(); Dictionary<string,Class1> dict = serializer.Deserialize<Dictionary<string,Class1>>(data); return dict["Debtor"].AccNo.ToString(); }
小提琴细节
HTTP / 1.1 415无法处理消息,因为内容类型为’application / json; charset = utf-8’不是预期的类型’text / xml;字符集= UTF-8′ .
服务器:Microsoft-IIS / 7.5
X-Powered-By:ASP.NET
日期:星期四,2012年11月29日01:21:55 GMT
内容长度:0
解决方法
确保正确配置的一种简单方法是使用.svc文件上的Factory属性.类似下面的例子:
<%@ ServiceHost Language="C#" Debug="true" Service="YourNamespace.YourServiceClass" Factory="System.ServiceModel.Activation.WebServiceHostFactory" %>
今天的关于MySQL 学习笔记 —— 字符集和mysql中字符集的分享已经结束,谢谢您的关注,如果想了解更多关于2020 重新出发,MySql 基础,MySql 字符集、Ajax查询与dataType一起使用:’text’失败,dataType:’text / xml;字符集= UTF-8′、Azure AD B2C Graph 终结点是否支持用于上传策略的 UTF-8 字符集?、c# – HTTP / 1.1 415无法处理消息,因为内容类型为’application / json; charset = utf-8’不是预期的类型’text / xml;字符集= UTF-8′的相关知识,请在本站进行查询。
本文标签: