MySQL 字符集学习笔记

本文涉及的产品
RDS MySQL DuckDB 分析主实例,集群系列 4核8GB
简介:
   MySQL includes character set support that enables you to store data using a variety of 
character sets and perform comparisons according to a variety of collations. You can 
specify character . MySQL supports 
the use of character

MySQL支持server,databases,table,column级别的字符集和排序方式。这些设置在information_schema库里边有表记录,COLLATION

SHOW VARIABLES LIKE 'character_set%';
SHOW VARIABLES LIKE 'collation%';

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mysql>  use  information_schema
mysql> show tables like  '%COLL%' ;
+---------------------------------------+
| Tables_in_information_schema (%COLL%) |
+---------------------------------------+
| COLLATIONS                            |
| COLLATION_CHARACTER_SET_APPLICABILITY |
+---------------------------------------+
mysql> show tables like  '%SETS%' ;
+---------------------------------------+
| Tables_in_information_schema (%SETS%) |
+---------------------------------------+
| CHARACTER_SETS                        |
+---------------------------------------+
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
The MySQL server can support multiple character sets. To list the available character sets,
use the SHOW CHARACTER SET statement. A partial listing follows. For more complete 
information, see Character Sets and Collations That MySQL Supports.
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 |
...
Any given character set always has at least one collation. It may have several collations.
To list the collations for a character set, use the SHOW COLLATION statement. For example,
to see the collations for the latin1 (cp1252 West European) character set, use this 
statement to find those collation names that begin with latin1:
mysql> SHOW COLLATION LIKE 'latin1%';
+---------------------+---------+----+---------+----------+---------+
| Collation | Charset | Id | Default | Compiled | Sortlen |
+---------------------+---------+----+---------+----------+---------+
| latin1_german1_ci | latin1 | 5 | | | 0 |
| latin1_swedish_ci | latin1 | 8 | Yes | Yes | 1 |

查看字符集和排序方式通过:SHOW CHARACTER SET;SHOW COLLATION LIKE 'character name'

每一个字符集都有一个默认的排序方式。

SERVER级别设置 可以再服务器启动,或者编译,或者配置文件中配置

1
2
3
[mysqld]
character- set -server=utf8
collation-server=utf8_general_ci
1
2
3
4
5
6
7
8
9
10
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 [ 396 for  the character  set . Along 
with  it,you can add --collation-server [ 396 for  the collation. If you don't specify a 
character  set ,that  is  the same  as  saying --character- set -server=latin1 [ 396 ]. If you 
specify only a character  set  ( for  example, latin1) but not a collation, that  is  the same 
as  saying --characterset-server=latin1 [ 396 ] --collation-server=latin1_swedish_ci [ 396
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

1
2
3
4
One way to change the settings  is  by recompiling. To change the  default  server character
set  and collation when building from sources,  use  the DEFAULT_CHARSET [ 125 ] and 
DEFAULT_COLLATION [ 126 ] options  for  CMake. For example: Specifying Character Sets and 
Collations

shell> cmake . -DDEFAULT_CHARSET=latin1
Or:
shell> cmake . -DDEFAULT_CHARSET=latin1 \
-DDEFAULT_COLLATION=latin1_german1_ci

1
2
3
4
5
6
7
Both mysqld and CMake verify that the character  set /collation combination  is  valid. If not,
each  program displays an error message and terminates.
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 [ 445 ] and collation_server [ 447 ] system  var iables. These  var iables
can be changed at runtime.

DATABASES级别

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:可以通过创建DB时或者后期通过ALTER DATABASE修改

1
2
3
4
5
6
CREATE DATABASE db_name
[[DEFAULT] CHARACTER SET charset_name]
[[DEFAULT] COLLATE collation_name]
ALTER DATABASE db_name
[[DEFAULT] CHARACTER SET charset_name]
[[DEFAULT] COLLATE collation_name]

The keyword SCHEMA can be used instead of DATABASE.
All database options are stored in a text file named db.opt that can be found in the database
directory.每个库下都有一个db.opt保存字符集和排序方式
The CHARACTER SET and COLLATE clauses make it possible to create databases with different
character sets and collations on the same MySQL server.
Example:

1
CREATE DATABASE db_name CHARACTER SET latin1 COLLATE latin1_swedish_ci;

以下是字符集和排序方式设置之后,选择哪个去生效的问题,基本上db,table,colum都是这个规则
If both CHARACTER SET X and COLLATE Y are specified, character set X and collation Y are used.
If CHARACTER SET X is specified without COLLATE, character set X and its default collation are
used. To see the default collation for each character set, use the SHOW COLLATION statement.
If COLLATE Y is specified without CHARACTER SET, the character set associated with Y and
collation Y are used.
Otherwise, the server character set and server collation are used.
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.

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:

1
2
3
4
5
6
7
8
9
CREATE TABLE tbl_name (column_list)
[[DEFAULT] CHARACTER SET charset_name]
[COLLATE collation_name]]
ALTER TABLE tbl_name
[[DEFAULT] CHARACTER SET charset_name]
[COLLATE collation_name]
Example:
CREATE TABLE t1 ( ... )
CHARACTER SET latin1 COLLATE latin1_danish_ci;

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
col_name {CHAR | VARCHAR | TEXT} (col_length)
[CHARACTER SET charset_name]
[COLLATE collation_name]
These clauses can also be used  for  ENUM and SET columns:
col_name {ENUM | SET} (val_list)
[CHARACTER SET charset_name]
[COLLATE collation_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
Specifying Character Sets and Collations
901
COLLATE latin1_swedish_ci;
1
2
3
4
CREATE TABLE t1
(
col1 CHAR( 10 ) CHARACTER SET utf8 COLLATE utf8_unicode_ci
) CHARACTER SET latin1 COLLATE latin1_bin;
1
2
3
4
CREATE TABLE t1
(
col1 CHAR( 10 ) CHARACTER SET utf8
) CHARACTER SET latin1 COLLATE latin1_bin;
1
2
3
4
CREATE TABLE t1
(
col1 CHAR( 10 ) COLLATE utf8_polish_ci
) CHARACTER SET latin1 COLLATE latin1_bin;
1
2
3
4
CREATE TABLE t1
(
col1 CHAR( 10 )
) CHARACTER SET latin1 COLLATE latin1_bin;

Neither the character set nor collation are specified for the column, 

so the table defaults are used.
The column has character set latin1 and collation latin1_bin.
The MySQL client programs mysql, mysqladmin, mysqlcheck, mysqlimport, and mysqlshow
determine the default character set to use as follows: 这些程序使用默认

举例说明  character character name 使用及生效Example: Suppose that column1 is defined as CHAR(5) CHARACTER SET latin2. If you do not say
SET NAMES or SET CHARACTER SET, then for SELECT column1 FROM t, the server sends back
all the values for column1 using the character set that the client specified when it connected. On the other hand, if you say SET NAMES 'latin1' or SET CHARACTER SET latin1 before issuing the SELECT statement, the server converts the latin2 values to latin1 just before sending results back. Conversion may be lossy if there are characters that are not in both character sets.



本文转自 aklaus 51CTO博客,原文链接:http://blog.51cto.com/aklaus/1715805

相关实践学习
每个IT人都想学的“Web应用上云经典架构”实战
本实验从Web应用上云这个最基本的、最普遍的需求出发,帮助IT从业者们通过“阿里云Web应用上云解决方案”,了解一个企业级Web应用上云的常见架构,了解如何构建一个高可用、可扩展的企业级应用架构。
MySQL数据库入门学习
本课程通过最流行的开源数据库MySQL带你了解数据库的世界。   相关的阿里云产品:云数据库RDS MySQL 版 阿里云关系型数据库RDS(Relational Database Service)是一种稳定可靠、可弹性伸缩的在线数据库服务,提供容灾、备份、恢复、迁移等方面的全套解决方案,彻底解决数据库运维的烦恼。 了解产品详情: https://www.aliyun.com/product/rds/mysql 
相关文章
|
11月前
|
存储 关系型数据库 MySQL
MySQL索引学习笔记
本文深入探讨了MySQL数据库中慢查询分析的关键概念和技术手段。
751 81
|
存储 SQL 关系型数据库
Mysql学习笔记(二):数据库命令行代码总结
这篇文章是关于MySQL数据库命令行操作的总结,包括登录、退出、查看时间与版本、数据库和数据表的基本操作(如创建、删除、查看)、数据的增删改查等。它还涉及了如何通过SQL语句进行条件查询、模糊查询、范围查询和限制查询,以及如何进行表结构的修改。这些内容对于初学者来说非常实用,是学习MySQL数据库管理的基础。
359 6
|
8月前
|
关系型数据库 MySQL Linux
CentOS 7系统下详细安装MySQL 5.7的步骤:包括密码配置、字符集配置、远程连接配置
以上就是在CentOS 7系统下安装MySQL 5.7的详细步骤。希望这个指南能帮助你顺利完成安装。
2132 26
|
11月前
|
存储 人工智能 搜索推荐
详解MySQL字符集和Collation
MySQL支持了很多Charset与Collation,并且允许用户在连接、Server、库、表、列、字面量多个层次上进行精细化配置,这有时会让用户眼花缭乱。本文对相关概念、语法、系统变量、影响范围都进行了详细介绍,并且列举了有可能让字符串发生字符集转换的情况,以及来自不同字符集的字符串进行比较等操作时遵循的规则。对于最常用的基于Unicode的字符集,本文介绍了Unicode标准与MySQL中各个字符集的关系,尤其详细介绍了当前版本(8.0.34)默认字符集utf8mb4。
2656 82
|
9月前
|
SQL 关系型数据库 MySQL
【YashanDB知识库】字符集latin1的MySQL中文数据如何迁移到YashanDB
本文探讨了在使用YMP 23.2.1.3迁移MySQL Server字符集为latin1的中文数据至YashanDB时出现乱码的问题。问题根源在于MySQL latin1字符集存放的是实际utf8编码的数据,而YMP尚未支持此类场景。文章提供了两种解决方法:一是通过DBeaver直接迁移表数据;二是将MySQL表数据转换为Insert语句后手动插入YashanDB。同时指出,这两种方法适合单张表迁移,多表迁移可能存在兼容性问题,建议对问题表单独处理。
【YashanDB知识库】字符集latin1的MySQL中文数据如何迁移到YashanDB
|
SQL 关系型数据库 MySQL
Mysql学习笔记(三):fetchone(), fetchmany(), fetchall()详细总结
MySQL中用于数据检索的`fetchone()`, `fetchmany()`, `fetchall()`函数的功能、SQL语句示例和应用场景。
420 3
Mysql学习笔记(三):fetchone(), fetchmany(), fetchall()详细总结
|
SQL Ubuntu 关系型数据库
Mysql学习笔记(一):数据库详细介绍以及Navicat简单使用
本文为MySQL学习笔记,介绍了数据库的基本概念,包括行、列、主键等,并解释了C/S和B/S架构以及SQL语言的分类。接着,指导如何在Windows和Ubuntu系统上安装MySQL,并提供了启动、停止和重启服务的命令。文章还涵盖了Navicat的使用,包括安装、登录和新建表格等步骤。最后,介绍了MySQL中的数据类型和字段约束,如主键、外键、非空和唯一等。
246 3
Mysql学习笔记(一):数据库详细介绍以及Navicat简单使用
|
关系型数据库 MySQL 数据库
Mysql学习笔记(四):Python与Mysql交互--实现增删改查
如何使用Python与MySQL数据库进行交互,实现增删改查等基本操作的教程。
208 1
|
SQL druid Java
Java数据库部分(MySQL+JDBC)(二、JDBC超详细学习笔记)(下)
Java数据库部分(MySQL+JDBC)(二、JDBC超详细学习笔记)
205 3
Java数据库部分(MySQL+JDBC)(二、JDBC超详细学习笔记)(下)
|
SQL Java 关系型数据库
Java数据库部分(MySQL+JDBC)(二、JDBC超详细学习笔记)(上)
Java数据库部分(MySQL+JDBC)(二、JDBC超详细学习笔记)
608 3
Java数据库部分(MySQL+JDBC)(二、JDBC超详细学习笔记)(上)

推荐镜像

更多