大家都知道
SET NAMES x
相当于
SET character_set_client = x;
SET character_set_results = x;
SET character_set_connection = x;

以下从MySQL5.0官方文档上摘录了相关内容,并翻译,说明了相关系统变量的用处:

What character set is the statement in when it leaves the client?
statement离开客户端的时候是什么字符集?

The server takes the character_set_client system variable to be the character set in which statements are sent by the client.
客户端送过来的statement,服务器认为它的字符集是系统变量character_set_client的值。

What character set should the server translate a statement to after receiving it?
服务器收到一个statement后,会把它转换成什么字符集?

For this, the server uses the character_set_connection and collation_connection system variables. It converts statements sent by the client from character_set_client to character_set_connection (except for string literals that have an introducer such as _latin1 or _utf8). collation_connection is important for comparisons of literal strings. For comparisons of strings with column values, collation_connection does not matter because columns have their own collation, which has a higher collation precedence.
为了这一用途,服务器使用系统变量character_set_connection和collation_connection。它把客户端传来的statement,从character_set_client字符集转换成character_set_connection字符集(除非字符串中有类似_latin1或者_utf8的字符集声明)。collation_connection对于字符串的比较是非常重要的。对于字符类型的字段值的比较,collation_connection是不起作用的。因为字段有自己的collation,有更高的优先级。

What character set should the server translate to before shipping result sets or error messages back to the client?
在结果集由服务器传递给客户端之前,需要转换成什么字符集?

The character_set_results system variable indicates the character set in which the server returns query results to the client. This includes result data such as column values, and result metadata such as column names.
character_set_results系统变量表明了服务器返回查询结果时使用的字符集。返回的数据,有比如字段的值和元数据(例如字段名)。

If you are using the mysql client with auto-reconnect enabled (which is not recommended), it is preferable to use the charset command rather than SET NAMES. For example:
如果你使用mysql客户端的自动重连(不推荐使用),最好用charset命令,而不是SET NAMES。例如:

mysql> charset utf8
Charset changed

The charset command issues a SET NAMES statement, and also changes the default character set that is used if mysql reconnects after the connection has dropped.
charset命令发出了一个SET NAMES语句,并且连接断开后自动重连时使用的缺省字符集也被修改了。

The database character set and collation are used as default values if the table character set and collation are not specified in CREATE TABLE statements. They have no other purpose.
如果CREATE TABLE中没有明确指出字符集和collation,那么database字符集和collation将做为缺省值,它们没有其他的用处。

The character set and collation for the default database can be determined from the values of the character_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.
缺省database的字符集和collation可以通过系统变量character_set_database和collation_database查看。服务器当缺省database改变时设置这些变量的值。如果没有缺省的database,这些变量的将与对应的服务器级的系统变量-character_set_server和collation_server的值相同。

The table character set and collation are used as default values 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.
表的字符集和collation会作为缺省值,如果列的定义中没有指明字符集和collation的话。

Every “character” column (that is, a column of type CHAR, VARCHAR, or TEXT) has a column character set and a column collation.
每个“字符”字段(即CHAR、VARCHAR或者TEXT类型的字段)都有一个字段字符集和字段校验(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:
每个字符串有一个可选的字符集introducer和COLLATE子句:

[_charset_name]'string' [COLLATE collation_name]

Examples:

SELECT 'string';
SELECT _latin1'string';
SELECT _latin1'string' COLLATE latin1_danish_ci;

MySQL为什么要set names相关推荐

  1. Linux下MySQL的字符集乱码问题总结

    前言: 字符集就是一套文字符号及其编码比较规则的集合.MySQL数据库字符集包括字符集(CHARACTER)和校对规则(COLLATION)两个概念.其中,字符集用来定义MySQL数据字符串的存储方式 ...

  2. php mysql pdo use_PHP连接到mysql的方法--mysqli和PDO

    php连接到mysql数据库,经典的方式就是使用mysql_connect(),具体代码如下: mysql_connect($db_host, $db_user, $db_pass) or die(m ...

  3. Mysql中经常出现的乱码问题

    Mysql中执行SET NAMES utf8这条SQl的作用 1)首先,Mysql服务器的编码和数据库的编码在配置文件my.ini中设置: 用记事本打开配置文件,修改代码:default-charac ...

  4. mysql 批量drop_MySql批量drop table

    今天发现数据库中很多没用的表,想清理掉. 发现mysql好像不支持类似这样的写法:drop table like "%r" 在oracle中,查了资料,是可以通过其他方式来实现:看 ...

  5. 【教程】Linux下MySQL 8.0安装配置

    1.编译安装MySQL8.0 版本信息 #cat /etc/redhat-release CentOS Linux release 7.5.1804 (Core) 安装依赖包 yum -y insta ...

  6. mysql中json_merge函数的使用?

    需求描述: 通过mysql中的json_merge函数,可以将多个json对象合并成一个对象 操作过程: 1.查看一张包含json列的表 mysql> select * from tab_jso ...

  7. Mysql一些维护命令

    --------------------------------------------------------- -------------------使用mysql客户端程序----------- ...

  8. 数据库 Linux下的MySQL数据库管理

    数据库就是数据的集合. 关系数据库是一种特殊的数据库,他将数据组织城标,并表示为表之间的关系. 数据库系统往往是大型项目的核心数据内容,如银行的用户账户信息,腾讯QQ的用户账户信息.股市的各种交易信息 ...

  9. 10分钟学会理解和解决MySQL乱码问题

    在阅读本文之前,强烈建议对字符集编码概念还比较模糊的同学 阅读下博主之前对相关概念的一篇科普:十分钟搞清字符集和字符编码 本博客已经迁移至: http://cenalulu.github.io/ 为了 ...

最新文章

  1. PHP实时生成并下载超大数据量的EXCEL文件
  2. 第四章 Controller接口控制器详解(5)——跟着开涛学SpringMVC
  3. php中rbac三张表的关系,thinkphp中RBAC数据表分析
  4. Python 存储模型
  5. ExtJS 动态增加与删除items,动态设置textField可见与否
  6. [转贴]计算机学术期刊排名
  7. Redis与RabbitMQ作为消息队列的比较
  8. Seaborn初学指南
  9. 排查一个触摸屏驱动问题
  10. java 格式化字符串_Java入门 - 语言基础 - 14.String类
  11. 职高学计算机走单招是,职高学生不用愁了,还有机会上本科,走“单招”或是最佳途径!...
  12. 几种欧姆龙PLC型号的辨识
  13. paly 获取数据库的第一条数据
  14. 给你一个整数数组 nums 。 如果一组数字 (i,j) 满足 nums[i] == nums[j] 且 i < j ,就可以认为这是一组 好数对 。
  15. Domino M-Series 设置说明
  16. 2019年总结-2020年计划
  17. ping请求超时的解决方法?
  18. MATLAB 声音文件处理
  19. 菲尔博士给我们提供提升交际气场的36招
  20. 入手评测 ROG冰刃5 Plus怎么样

热门文章

  1. blob字段乱码怎么处理_下载的附件名总乱码?你该去读一下 RFC 文档了!
  2. qt通过代码创建滚动区域,添加滚动区域到窗口
  3. 如何在linux安装mysql7版本的,如何在linux下用tar方式安装Mysql5.7版本
  4. python xgboost用法_XGBoost使用教程(纯xgboost方法)一
  5. mysql数据应用从入门_MYSQL数据库应用从入门到精通----读书笔记
  6. fi sap 凭证冲销 稅_SAP 成本收集器与重复制造配置手册
  7. linux不显示无线网卡驱动安装失败,Linux_Ubuntu无线网卡驱动安装方法,安装了Ubuntu后发现没有无线网 - phpStudy...
  8. mysql报错级别_MySQL启动出现几个警告级别错误
  9. 用c语言程序算自己的年龄,新手编的小程序:计算年龄和出生后经历的天数的小程序...
  10. Kettle之数据抽取、转换、装载