mysql关于字符集character set的总结

总结

1、创建表和创建数据库时都可以指定字符集

2、创建表没有指定字符集则默认使用数据库的字符集

3、数据库的默认字符集存放在db.opt文件中,create database时会自动生成此文件,show create database时显示db.opt文件中的字符集,每个数据库目录下都有一个db.opt文件

4、db.opt这个文件丢失不影响数据库运行,该文件丢失之后新建表时,找不到数据库的默认字符集,就把character_set_server当成数据库的默认字符集,show create database时显示character_set_server字符集

5、创建数据库没有指定字符集则默认使用character_set_server字符集,如果character_set_server没有设置,则默认是latin1

6、字符序的命名,以其对应的字符集作为前缀,比如字符序utf8_general_ci,标明它是字符集utf8的字符序,所以如果character_set_server变了,则collation_server也会变

7、如果要修改默认字符集,一般在my.cnf文件里设置character_set_server参数

8、如果改变默认字符集,原来存在的数据库和表的字符集不变,db.opt存在的情况下新建的表不指定字符集还是使用原来数据库的字符集即db.opt中的字符集,新建的数据库如果不指定字符集则使用修改后的字符集。比如character_set_server为utf8,原来建库testdb1不加字符集,在testdb1建立表tab1也不指定字符集,则testdb1和testdb1.tab1字符集都是utf8。修改character_set_server为utf16后,在testdb1上建里表tab2不指定字符集,则testdb1.tab2字符集还是testdb1的字符集utf8,新建库testdb2不指定字符集,则testdb2字符集是utf16

9、windows机器查看字符集方法在cmd窗口输入chcp即可,linux机器查看字符集方法echo $LANG或locale

10、使用default-character-set值作为客户端和连接的默认字符集,比如在A机器上字符集utf8和B机器上字符集latin1执行同一条SQL, A机器中文正常, B机器中文出现乱码,这个跟客户端的字符集有关系,此时可以在客户端/etc/my.cnf的client中增加default-character-set=utf8或在服务端执行set global init_connect="SET NAMES utf8",使客户端都使用utf8来连接数据库服务端



https://dev.mysql.com/doc/refman/5.7/en/charset-general.html

A character set is a set of symbols and encodings. A collation is a set of rules for comparing characters in a character set. 

字符集是一组符号和编码。 排序规则是用于比较字符集中的字符的一组规则。

字符集(Character set )

字符序(Collation)是指在同一字符集内字符之间的比较规则

1、一个字符集对应至少一种字符序(一般是1对多)

2、两个不同的字符集不能有相同的字符序

3、每个字符集都有默认的字符序



查看数据库支持的字符集的方法

mysql> SHOW CHARACTER SET;

mysql> select * from information_schema.character_sets


查看数据库支持的字符序的方法

mysql> SHOW COLLATION;

mysql> select * from information_schema.COLLATIONS


mysql> show variables like '%charac%';

+--------------------------+----------------------------+

| Variable_name            | Value                      |

+--------------------------+----------------------------+

| character_set_client     | utf8                       |

| character_set_connection | utf8                       |

| character_set_database   | latin1                     |

| character_set_filesystem | binary                     |

| character_set_results    | utf8                       |

| character_set_server     | latin1                     |

| character_set_system     | utf8                       |

| character_sets_dir       | /usr/share/mysql/charsets/ |

+--------------------------+----------------------------+


mysql> show variables like '%colla%';

+----------------------+-------------------+

| Variable_name        | Value             |

+----------------------+-------------------+

| collation_connection | utf8_general_ci   |

| collation_database   | latin1_swedish_ci |

| collation_server     | latin1_swedish_ci |

+----------------------+-------------------+


character_set_database

The character set used by the default database. The server sets this variable whenever the default database changes. If there is no default database, the variable has the same value as character_set_server

The global character_set_database and collation_database system variables are deprecated in MySQL 5.7 and will be removed in a future version of MySQL.

默认数据库使用的字符集。 每当默认数据库更改时,服务器都会设置此变量。 如果没有缺省数据库,则该变量与character_set_server具有相同的值

MySQL 5.7中不推荐使用全局character_set_database和collation_database系统变量,并将在未来的MySQL版本中删除它们


character_set_server

The server's default character set.

服务器的默认字符集。


character_set_system

The character set used by the server for storing identifiers. The value is always utf8.

服务器用于存储标识符的字符集。 值总是utf8。


character_set_client

The character set for statements that arrive from the client. The session value of this variable is set using the character set requested by the client when the client connects to the server. (Many clients support a --default-character-set option to enable this character set to be specified explicitly. See also Section 10.4, “Connection Character Sets and Collations”.) The global value of the variable is used to set the session value in cases when the client-requested value is unknown or not available, or the server is configured to ignore client requests:

The client requests a character set not known to the server. For example, a Japanese-enabled client requests sjis when connecting to a server not configured with sjis support.

The client is from a version of MySQL older than MySQL 4.1, and thus does not request a character set.

mysqld was started with the --skip-character-set-client-handshake option, which causes it to ignore client character set configuration. This reproduces MySQL 4.0 behavior and is useful should you wish to upgrade the server without upgrading all the clients.

从客户端到达的语句的字符集。 当客户端连接到服务器时, 使用客户端请求的字符集设置此变量的会话值(客户端请求的字符集意思指的就是客户端OS上的字符集) 。 (许多客户端支持--default-character-set选项以显式指定此字符集。另请参见第10.4节“连接字符集和排序”。)变量的全局值用于在客户端请求的值未知或不可用的情况下设置会话值,或者服务器配置为忽略客户端请求:

客户端请求服务器不知道的字符集。 例如,启用日语的客户端在连接到未配置sjis支持的服务器时请求sjis。

客户端来自早于MySQL 4.1的MySQL版本,因此不会请求字符集。

mysqld是使用--skip-character-set-client-handshake选项启动的,这会导致它忽略客户端字符集配置。 这会重现MySQL 4.0的行为,如果您希望在不升级所有客户端的情况下升级服务器,这将非常有用。


character_set_connection

The character set used for literals specified without a character set introducer and for number-to-string conversion

用于指定文字的字符集,这些文字不包括没有字符集导入和数字到字符串的转换


character_set_results

The character set used for returning query results to the client. This includes result data such as column values, result metadata such as column names, and error messages.

用于将查询结果返回给客户端的字符集。 这包括结果数据,如列值,结果元数据(如列名称)和错误消息。


init_connect
A string to be executed by the server for each client that connects. The string consists of one or more SQL statements, separated by semicolon characters.
服务器为每个连接的客户端执行的字符串。 该字符串由一个或多个SQL语句组成,以分号字符分隔。


--default-character-set=charset_name

The --default-character-set=charset_name command option used by MySQL client programs such as mysql and mysqladmin.
Each client supports a --default-character-set option, which enables users to specify the character set explicitly to override whatever default the client otherwise determines.

Use charset_name as the default character set for the client and connection.

This option can be useful if the operating system uses one character set and the mysql client by default uses another. In this case, output may be formatted incorrectly. You can usually fix such issues by using this option to force the client to use the system character set instead.

MySQL客户端程序 (如mysql和mysqladmin) 使用的--default-character-set = charset_name命令选项
每个客户端都支持--default-character-set选项,该选项允许用户显式指定字符集以覆盖客户端否的任何默认值。

使用default-character-set值作为客户端和连接的默认字符集。

如果操作系统使用一个字符集并且默认情况下mysql客户端使用另一个字符集,则此选项很有用。 在这种情况下,输出可能格式不正确。 您通常可以通过使用此选项来强制客户端使用系统字符集来修复此类问题。



https://dev.mysql.com/doc/refman/5.7/en/create-database.html

CREATE {DATABASE | SCHEMA} [IF NOT EXISTS] db_name

    [create_specification] ...

create_specification:

    [DEFAULT] CHARACTER SET [=] charset_name

  | [DEFAULT] COLLATE [=] collation_name

create_specification options specify database characteristics. Database characteristics are stored in the db.opt file in the database directory. The CHARACTER SET clause specifies the default database character set. The COLLATE clause specifies the default database collation.

create_specification选项指定数据库特征。 数据库特征存储在数据库目录的db.opt文件中。 CHARACTER SET子句指定默认数据库字符集。 COLLATE子句指定默认的数据库排序规则。

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30126024/viewspace-2216308/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/30126024/viewspace-2216308/

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值